I2C
概述
I2C (Inter-Intergrated Circuit)是一种串行通讯总线,使用多主从架构,用来连接低速外围装置。 每个器件都有一个唯一的识别地址,并且都可以作为一个发送器或接收器。 如果有两个或多个主机同时初始化,数据传输可以通过冲突检测和仲裁防止数据被破坏。 BL602/BL604包含一个I2C控制器主机,可灵活配置slaveAddr、subAddr以及传输数据,方便与从设备通信,提供2个word深度的fifo,提供中断功能,可搭配DMA使用提高效率,可灵活调整时钟频率。
注解
i2c更多资料请参考 BL602参考手册 。
应用实例
i2c实例: customer_app/peripheral/demo_i2c
。
API参考
Header File
- components/platform/hosal/include/hosal_i2c.h
Functions
-
int
hosal_i2c_init
(hosal_i2c_dev_t *i2c) Initialises an I2C interface.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the device for which the i2c port should be initialised
-
int
hosal_i2c_master_send
(hosal_i2c_dev_t *i2c, uint16_t dev_addr, const uint8_t *data, uint16_t size, uint32_t timeout) I2c master send.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the i2c devicedev_addr
: device addressdata
: i2c send datasize
: i2c send data sizetimeout
: timeout in milisecond, set this value to HAL_WAIT_FOREVER if you want to wait forever
-
int
hosal_i2c_master_recv
(hosal_i2c_dev_t *i2c, uint16_t dev_addr, uint8_t *data, uint16_t size, uint32_t timeout) I2c master recv.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the i2c devicedev_addr
: device addressdata
: i2c receive datasize
: i2c receive data sizetimeout
: timeout in milisecond, set this value to HAL_WAIT_FOREVER if you want to wait forever
-
int
hosal_i2c_slave_send
(hosal_i2c_dev_t *i2c, const uint8_t *data, uint16_t size, uint32_t timeout) I2c slave send.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the i2c devicedata
: i2c slave send datasize
: i2c slave send data sizetimeout
: timeout in milisecond, set this value to HAL_WAIT_FOREVER if you want to wait forever
-
int
hosal_i2c_slave_recv
(hosal_i2c_dev_t *i2c, uint8_t *data, uint16_t size, uint32_t timeout) I2c slave receive.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: tthe i2c devicedata
: i2c slave receive datasize
: i2c slave receive data sizetimeout
: timeout in milisecond, set this value to HAL_WAIT_FOREVER if you want to wait forever
-
int
hosal_i2c_mem_write
(hosal_i2c_dev_t *i2c, uint16_t dev_addr, uint32_t mem_addr, uint16_t mem_addr_size, const uint8_t *data, uint16_t size, uint32_t timeout) I2c mem write.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the i2c devicedev_addr
: device addressmem_addr
: mem addressmem_addr_size
: mem addressdata
: i2c master send datasize
: i2c master send data sizetimeout
: timeout in milisecond, set this value to HAL_WAIT_FOREVER if you want to wait forever
-
int
hosal_i2c_mem_read
(hosal_i2c_dev_t *i2c, uint16_t dev_addr, uint32_t mem_addr, uint16_t mem_addr_size, uint8_t *data, uint16_t size, uint32_t timeout) I2c master mem read.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the i2c devicedev_addr
: device addressmem_addr
: mem addressmem_addr_size
: mem addressdata
: i2c master send datasize
: i2c master send data sizetimeout
: timeout in milisecond, set this value to HAL_WAIT_FOREVER if you want to wait forever
-
int
hosal_i2c_finalize
(hosal_i2c_dev_t *i2c) Deinitialises an I2C device.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
i2c
: the i2c device
Macros
-
HOSAL_WAIT_FOREVER
Define the wait forever timeout macro.
-
HOSAL_I2C_MODE_MASTER
i2c communication is master mode
-
HOSAL_I2C_MODE_SLAVE
i2c communication is slave mode
-
HOSAL_I2C_MEM_ADDR_SIZE_8BIT
i2c memory address size 8bit
-
HOSAL_I2C_MEM_ADDR_SIZE_16BIT
i2c memory address size 16bit
-
HOSAL_I2C_MEM_ADDR_SIZE_24BIT
i2c memory address size 24bit
-
HOSAL_I2C_MEM_ADDR_SIZE_32BIT
i2c memory address size 32bit
-
HOSAL_I2C_ADDRESS_WIDTH_7BIT
7 bit mode
-
HOSAL_I2C_ADDRESS_WIDTH_10BIT
10 bit mode