I2C 驱动
结构体
I2C 描述结构体
typedef struct {
uint32_t base;
uint8_t id;
uint32_t speed;
gpio_mux_t gpio_scl;
gpio_mux_t gpio_sda;
bool status;
} sunxi_i2c_t;
I2C 总线枚举
enum {
SUNXI_I2C0 = 0,
SUNXI_I2C1,
SUNXI_I2C2,
SUNXI_I2C3,
SUNXI_I2C4,
SUNXI_I2C5,
SUNXI_R_I2C0,
SUNXI_R_I2C1,
SUNXI_I2C_BUS_MAX,
};
I2C 外设寄存器结构体
struct sunxi_twi_reg {
volatile uint32_t addr; /* slave address */
volatile uint32_t xaddr; /* extend address */
volatile uint32_t data; /* data */
volatile uint32_t ctl; /* control */
volatile uint32_t status; /* status */
volatile uint32_t clk; /* clock */
volatile uint32_t srst; /* soft reset */
volatile uint32_t eft; /* enhanced future */
volatile uint32_t lcr; /* line control */
volatile uint32_t dvfs; /* dvfs control */
};
接口 API
sunxi_i2c_init
void sunxi_i2c_init(sunxi_i2c_t *i2c_dev);
初始化sunxi_i2c控制器设备结构。
sunxi_i2c_write
int sunxi_i2c_write(sunxi_i2c_t *i2c_dev, uint8_t addr, uint32_t reg, uint8_t data);
向设备写入数据。
i2c_dev
:指向sunxi_i2c控制器设备结构的指针。addr
:设备地址。reg
:要在设备中读取/写入的寄存器。data
:要写入/读取的数据。- 返回值:状态的数量。