ca13734bf0
Add kernel (Cortex-M0/M3/M4, Tricore, S32K, RISC-V ports), drivers, middleware (CAN stack, diagnostics, safety), applications, board support, build/test tooling, and documentation.
102 lines
3.0 KiB
C
102 lines
3.0 KiB
C
/**
|
|
* @file i2c_driver.h
|
|
* @brief I2C driver interface
|
|
*/
|
|
|
|
#ifndef I2C_DRIVER_H
|
|
#define I2C_DRIVER_H
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "kernel.h"
|
|
|
|
/* I2C Configuration Constants */
|
|
#define I2C_MAX_INSTANCES 4
|
|
#define I2C_MAX_BUFFER_SIZE 1024
|
|
#define I2C_MAX_TRANSFER_SIZE 255
|
|
|
|
/* I2C Speeds */
|
|
typedef enum {
|
|
I2C_SPEED_STANDARD = 100000, /* 100 kHz */
|
|
I2C_SPEED_FAST = 400000, /* 400 kHz */
|
|
I2C_SPEED_FAST_PLUS = 1000000, /* 1 MHz */
|
|
I2C_SPEED_HIGH = 3400000 /* 3.4 MHz */
|
|
} I2cSpeed_t;
|
|
|
|
/* I2C Addressing Mode */
|
|
typedef enum {
|
|
I2C_ADDRESSING_7BIT = 0,
|
|
I2C_ADDRESSING_10BIT = 1
|
|
} I2cAddressingMode_t;
|
|
|
|
/* I2C Transfer Direction */
|
|
typedef enum {
|
|
I2C_DIRECTION_WRITE = 0,
|
|
I2C_DIRECTION_READ = 1
|
|
} I2cDirection_t;
|
|
|
|
/* I2C Transfer Status */
|
|
typedef enum {
|
|
I2C_TRANSFER_COMPLETE = 0,
|
|
I2C_TRANSFER_ERROR = 1,
|
|
I2C_TRANSFER_NACK = 2,
|
|
I2C_TRANSFER_TIMEOUT = 3,
|
|
I2C_TRANSFER_ARBITRATION_LOST = 4,
|
|
I2C_TRANSFER_BUS_ERROR = 5
|
|
} I2cTransferStatus_t;
|
|
|
|
/* I2C Callbacks */
|
|
typedef void (*I2cTransferCompleteCallback_t)(I2cTransferStatus_t status, void* user_data);
|
|
|
|
/* I2C Configuration */
|
|
typedef struct {
|
|
I2cSpeed_t speed;
|
|
I2cAddressingMode_t addressing_mode;
|
|
uint16_t own_address;
|
|
bool enable_general_call;
|
|
bool enable_clock_stretching;
|
|
bool use_dma;
|
|
I2cTransferCompleteCallback_t transfer_complete_callback;
|
|
} I2cConfig_t;
|
|
|
|
/* I2C Message */
|
|
typedef struct {
|
|
uint16_t slave_address;
|
|
I2cDirection_t direction;
|
|
uint8_t* data;
|
|
uint16_t length;
|
|
bool generate_stop;
|
|
bool generate_restart;
|
|
} I2cMessage_t;
|
|
|
|
/* I2C Statistics */
|
|
typedef struct {
|
|
uint32_t transfers_completed;
|
|
uint32_t transfers_failed;
|
|
uint32_t nack_errors;
|
|
uint32_t arbitration_lost;
|
|
uint32_t bus_errors;
|
|
uint32_t timeout_errors;
|
|
uint32_t bytes_transferred;
|
|
} I2cStatistics_t;
|
|
|
|
/* I2C Driver Interface */
|
|
KernelStatus_t i2c_init(uint8_t instance, I2cConfig_t* config);
|
|
KernelStatus_t i2c_deinit(uint8_t instance);
|
|
KernelStatus_t i2c_transfer(uint8_t instance, const I2cMessage_t* message,
|
|
uint32_t timeout_ms);
|
|
KernelStatus_t i2c_transfer_async(uint8_t instance, const I2cMessage_t* message);
|
|
KernelStatus_t i2c_write(uint8_t instance, uint16_t slave_address,
|
|
const uint8_t* data, uint16_t length, uint32_t timeout_ms);
|
|
KernelStatus_t i2c_read(uint8_t instance, uint16_t slave_address,
|
|
uint8_t* data, uint16_t length, uint32_t timeout_ms);
|
|
KernelStatus_t i2c_write_read(uint8_t instance, uint16_t slave_address,
|
|
const uint8_t* tx_data, uint16_t tx_length,
|
|
uint8_t* rx_data, uint16_t rx_length,
|
|
uint32_t timeout_ms);
|
|
KernelStatus_t i2c_get_statistics(uint8_t instance, I2cStatistics_t* stats);
|
|
bool i2c_is_device_ready(uint8_t instance, uint16_t slave_address);
|
|
void i2c_process_interrupt(uint8_t instance);
|
|
|
|
#endif /* I2C_DRIVER_H */
|