I2C
I2C API Status
API |
BK7258_AP |
|---|---|
Y |
|
Y |
|
Y |
|
Y |
|
Y |
|
Y |
|
Y |
|
Y |
|
Y |
|
Y |
I2C UNIT Number
Capability |
BK7258 |
BK7258_cp1 |
|---|---|---|
UNIT Number |
2 |
2 |
I2C Pin and GPIO Map
SPI Pin |
BK7258 |
BK7258_cp1 |
|---|---|---|
I2C0 SCL |
20 |
20 |
I2C0 SDA |
21 |
21 |
I2C1 SCL |
42 |
42 |
I2C1 SDA |
43 |
43 |
I2C1 SCL |
0 |
0 |
I2C1 SDA |
1 |
1 |
I2C API Reference
Header File
Functions
-
bk_err_t bk_i2c_driver_init(void)
Init the I2C driver.
This API init the resoure common:
Init I2C driver control memory
- Attention
1. This API should be called before any other I2C APIs.
- 返回
BK_OK: succeed
others: other errors.
-
bk_err_t bk_i2c_driver_deinit(void)
Deinit the I2C driver.
This API free all resource related to I2C and disable I2C.
- 返回
BK_OK: succeed
others: other errors.
-
bk_err_t bk_i2c_init(i2c_id_t id, const i2c_config_t *cfg)
Init the I2C id.
This API init the I2C id:
Power up the I2C id
Configure the I2C id clock
Map the I2C id to dedicated GPIO port (hardware I2C) or configure GPIO pins (simulated I2C)
Set the I2C parameters
Start the I2C id
- Attention
Multifunction GPIO initialization affects other functions
- 参数
id – I2C id When CONFIG_SIM_I2C is enabled:
Hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1), e.g., id = 0, 1 for BK7258
Simulated I2C: id >= SIM_I2C_START_ID (typically SOC_I2C_UNIT_NUM), e.g., id >= 2 for BK7258 When CONFIG_SIM_I2C is disabled:
Only hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1) The unified API layer automatically routes to hardware or simulated I2C based on id value.
cfg – I2C parameter settings (pointer to i2c_config_t structure)
- 返回
BK_OK (0): succeed
BK_ERR_NULL_PARAM: cfg is NULL
BK_ERR_I2C_NOT_INIT (BK_ERR_I2C_BASE - 1): I2C driver not initialized, call bk_i2c_driver_init() first
BK_ERR_I2C_INVALID_ID (BK_ERR_I2C_BASE - 7): I2C id number is invalid
When CONFIG_SIM_I2C enabled: id must be < SOC_I2C_UNIT_NUM (hardware) or >= SIM_I2C_START_ID (simulated)
When CONFIG_SIM_I2C disabled: id must be < SOC_I2C_UNIT_NUM (hardware only)
others: other errors.
-
bk_err_t bk_i2c_deinit(i2c_id_t id)
Deinit the I2C id.
This API deinit the I2C id:
Stop the I2C id
Disable the I2C id interrupt (hardware I2C only)
Power down the I2C id (hardware I2C only)
Release GPIO resources (simulated I2C)
- 参数
id – I2C id When CONFIG_SIM_I2C is enabled:
Hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1), e.g., id = 0, 1 for BK7258
Simulated I2C: id >= SIM_I2C_START_ID (typically SOC_I2C_UNIT_NUM), e.g., id >= 2 for BK7258 When CONFIG_SIM_I2C is disabled:
Only hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1) The unified API layer automatically routes to hardware or simulated I2C based on id value.
- 返回
BK_OK (0): succeed
BK_ERR_I2C_NOT_INIT (BK_ERR_I2C_BASE - 1): I2C driver not initialized, call bk_i2c_driver_init() first
BK_ERR_I2C_INVALID_ID (BK_ERR_I2C_BASE - 7): I2C id number is invalid
When CONFIG_SIM_I2C enabled: id must be < SOC_I2C_UNIT_NUM (hardware) or >= SIM_I2C_START_ID (simulated)
When CONFIG_SIM_I2C disabled: id must be < SOC_I2C_UNIT_NUM (hardware only)
BK_ERR_I2C_ID_NOT_INIT (BK_ERR_I2C_BASE - 2): I2C id not initialized, call bk_i2c_init() first for this id
others: other errors.
-
bk_err_t bk_i2c_master_write(i2c_id_t id, uint32_t dev_addr, const uint8_t *data, uint32_t size, uint32_t timeout_ms)
Write data to the I2C port from a given buffer and length, It shall only be called in I2C master mode.
- 参数
id – I2C id
dev_addr – slave device address
data – pointer to the buffer
size – data length to write
timeout_ms – timeout ms
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_master_write_noaddr(i2c_id_t id, const uint8_t *data, uint32_t size, uint32_t timeout_ms)
Write data to the I2C port from a given buffer and length without slave address, It shall only be called in I2C master mode.
- 参数
id – I2C id
data – pointer to the buffer
size – data length to write
timeout_ms – timeout ms
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_master_read(i2c_id_t id, uint32_t dev_addr, uint8_t *data, uint32_t size, uint32_t timeout_ms)
I2C read data from I2C buffer, It shall only be called in I2C master mode.
- 参数
id – I2C id
dev_addr – slave device address
data – pointer to the buffer
size – data length to read
timeout_ms – timeout ms
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_master_read_noaddr(i2c_id_t id, uint8_t *data, uint32_t size, uint32_t timeout_ms)
I2C read data from I2C buffer without slave address, It shall only be called in I2C master mode.
- 参数
id – I2C id
data – pointer to the buffer
size – data length to read
timeout_ms – timeout ms
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_slave_write(i2c_id_t id, const uint8_t *data, uint32_t size, uint32_t timeout_ms)
Write data to the I2C port from a given buffer and length, It shall only be called in I2C slave mode.
- 参数
id – I2C id
data – pointer to the buffer
size – data length to write
timeout_ms – timeout ms
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_slave_read(i2c_id_t id, uint8_t *data, uint32_t size, uint32_t timeout_ms)
I2C read data from I2C buffer, It shall only be called in I2C slave mode.
- 参数
id – I2C id
dev_addr – slave device address
data – pointer to the buffer
size – data length to read
timeout_ms – timeout ms
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_memory_write(i2c_id_t id, const i2c_mem_param_t *mem_param)
Write data to the specific memory address from a given buffer and length, It shall only be called in I2C master mode.
- 参数
id – I2C id When CONFIG_SIM_I2C is enabled:
Hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1), e.g., id = 0, 1 for BK7258
Simulated I2C: id >= SIM_I2C_START_ID (typically SOC_I2C_UNIT_NUM), e.g., id >= 2 for BK7258 When CONFIG_SIM_I2C is disabled:
Only hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1) The unified API layer automatically routes to hardware or simulated I2C based on id value.
mem_param – memory parameter
- 返回
BK_OK (0): succeed
BK_ERR_NULL_PARAM: mem_param is NULL
BK_ERR_I2C_NOT_INIT (BK_ERR_I2C_BASE - 1): I2C driver not initialized, call bk_i2c_driver_init() first
BK_ERR_I2C_INVALID_ID (BK_ERR_I2C_BASE - 7): I2C id number is invalid
When CONFIG_SIM_I2C enabled: id must be < SOC_I2C_UNIT_NUM (hardware) or >= SIM_I2C_START_ID (simulated)
When CONFIG_SIM_I2C disabled: id must be < SOC_I2C_UNIT_NUM (hardware only)
BK_ERR_I2C_ID_NOT_INIT (BK_ERR_I2C_BASE - 2): I2C id not initialized, call bk_i2c_init() first for this id
BK_ERR_I2C_SM_BUS_BUSY (BK_ERR_I2C_BASE - 3): I2C bus is busy
BK_ERR_I2C_ACK_TIMEOUT (BK_ERR_I2C_BASE - 4): I2C receive ACK timeout
BK_ERR_I2C_SCL_TIMEOUT (BK_ERR_I2C_BASE - 6): I2C SCL line timeout
others: other errors.
-
bk_err_t bk_i2c_memory_read(i2c_id_t id, const i2c_mem_param_t *mem_param)
I2C read data from I2C specific memory address, It shall only be called in I2C master mode.
- 参数
id – I2C id When CONFIG_SIM_I2C is enabled:
Hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1), e.g., id = 0, 1 for BK7258
Simulated I2C: id >= SIM_I2C_START_ID (typically SOC_I2C_UNIT_NUM), e.g., id >= 2 for BK7258 When CONFIG_SIM_I2C is disabled:
Only hardware I2C: id = 0 to (SOC_I2C_UNIT_NUM - 1) The unified API layer automatically routes to hardware or simulated I2C based on id value.
mem_param – memory parameter
- 返回
BK_OK (0): succeed
BK_ERR_NULL_PARAM: mem_param is NULL
BK_ERR_I2C_NOT_INIT (BK_ERR_I2C_BASE - 1): I2C driver not initialized, call bk_i2c_driver_init() first
BK_ERR_I2C_INVALID_ID (BK_ERR_I2C_BASE - 7): I2C id number is invalid
When CONFIG_SIM_I2C enabled: id must be < SOC_I2C_UNIT_NUM (hardware) or >= SIM_I2C_START_ID (simulated)
When CONFIG_SIM_I2C disabled: id must be < SOC_I2C_UNIT_NUM (hardware only)
BK_ERR_I2C_ID_NOT_INIT (BK_ERR_I2C_BASE - 2): I2C id not initialized, call bk_i2c_init() first for this id
BK_ERR_I2C_SM_BUS_BUSY (BK_ERR_I2C_BASE - 3): I2C bus is busy
BK_ERR_I2C_ACK_TIMEOUT (BK_ERR_I2C_BASE - 4): I2C receive ACK timeout
BK_ERR_I2C_SCL_TIMEOUT (BK_ERR_I2C_BASE - 6): I2C SCL line timeout
others: other errors.
-
bk_err_t bk_i2c_set_baud_rate(i2c_id_t id, uint32_t baud_rate)
I2C set baud rate.
- 参数
id – I2C id
baud_rate – baud rate
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_set_slave_address(i2c_id_t id, uint16_t slave_addr)
Set slave address when current role is i2c slave.
- 参数
id – I2C id
slave_addr – slave address
- 返回
BK_OK: succeed
BK_ERR_I2C_NOT_INIT: I2C driver not init
others: other errors.
-
bk_err_t bk_i2c_enable_interrupt(i2c_id_t id)
Enable I2C interrupt.
- 参数
id – I2C id
- 返回
BK_OK: succeed
others: other errors.
-
bk_err_t bk_i2c_disable_interrupt(i2c_id_t id)
Disable I2C interrupt.
- 参数
id – I2C id
- 返回
BK_OK: succeed
others: other errors.
-
bool bk_i2c_is_bus_busy(i2c_id_t id)
Check if I2C is busy.
- 参数
id – I2C id
- 返回
true: busy, false: not busy
-
uint32_t bk_i2c_get_cur_action(i2c_id_t id)
Get i2c current action, such as start,stop,send write_addr.
- 参数
id – I2C id
- 返回
i2c action
-
void bk_i2c_timer_callback(int id, void *myTimer)
bk_i2c_timer_callback
This API set timer call back
- 返回
-
uint8_t bk_i2c_get_busstate(int id)
bk_i2c_get_busstate
This API get bus status idle or busy
- 返回
1: idle
0: busy.
-
uint8_t bk_i2c_get_transstate(int id)
bk_i2c_get_transstate
This API get bus status idle or busy
- 返回
1: ok
0: fail.
-
bk_err_t bk_i2c_init_v2(i2c_id_t id, const i2c_config_t *cfg)
Init the SIM I2C id (Deprecated)
- Deprecated:
This function is deprecated. Please use bk_i2c_init() instead. The unified API layer (i2c_unified.c) automatically routes to the appropriate implementation (hardware or simulated) based on I2C ID.
This API init the I2C id:
Power up the I2C id
Configure the I2C id clock
Map the I2C id to dedicated GPIO port
Set the I2C parameters
Start the I2C id
- Attention
Multifunction GPIO initialization affects other functions
- 参数
id – I2C id
config – I2C parameter settings
- 返回
BK_OK: succeed
BK_ERR_NULL_PARAM: I2C config parameter is NULL
BK_ERR_I2C_NOT_INIT: I2C driver not init
others: other errors.
-
bk_err_t bk_i2c_deinit_v2(i2c_id_t id)
Deinit the SIM I2C driver (Deprecated)
- Deprecated:
This function is deprecated. Please use bk_i2c_deinit() instead. The unified API layer automatically routes to the appropriate implementation.
This API free all resource related to I2C and disable I2C.
- Attention
This is an internal API used by i2c_unified.c. Applications should use bk_i2c_deinit().
- 参数
id – I2C id (should be >= SIM_I2C_START_ID for simulated I2C)
- 返回
BK_OK: succeed
others: other errors.
-
bk_err_t bk_i2c_memory_write_v2(i2c_id_t id, const i2c_mem_param_t *mem_param)
Write data to the specific memory address from a given buffer and length (Deprecated) It shall only be called in SIM I2C master mode.
- Deprecated:
This function is deprecated. Please use bk_i2c_memory_write() instead. The unified API layer automatically routes to the appropriate implementation.
- Attention
This is an internal API used by i2c_unified.c. Applications should use bk_i2c_memory_write().
- 参数
id – I2C id (should be >= SIM_I2C_START_ID for simulated I2C)
mem_param – memory parameter
- 返回
BK_OK: succeed
BK_ERR_NULL_PARAM: I2C mem_param is NULL
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
-
bk_err_t bk_i2c_memory_read_v2(i2c_id_t id, const i2c_mem_param_t *mem_param)
SIM I2C read data from I2C specific memory address (Deprecated) It shall only be called in SIM I2C master mode.
- Deprecated:
This function is deprecated. Please use bk_i2c_memory_read() instead. The unified API layer automatically routes to the appropriate implementation.
- Attention
This is an internal API used by i2c_unified.c. Applications should use bk_i2c_memory_read().
- 参数
id – I2C id (should be >= SIM_I2C_START_ID for simulated I2C)
mem_param – memory parameter
- 返回
BK_OK: succeed
BK_ERR_NULL_PARAM: I2C mem_param is NULL
BK_ERR_I2C_NOT_INIT: I2C driver not init
BK_ERR_I2C_INVALID_ID: I2C id number is invalid
BK_ERR_I2C_ID_NOT_INIT: I2C id not init
others: other errors.
I2C API Typedefs
Header File
Structures
-
struct i2c_mem_param_t
Macros
-
BK_ERR_I2C_NOT_INIT
I2C driver not init
-
BK_ERR_I2C_ID_NOT_INIT
I2C id not init
-
BK_ERR_I2C_SM_BUS_BUSY
I2C bus busy
-
BK_ERR_I2C_ACK_TIMEOUT
I2C receive ack timeout
-
BK_ERR_I2C_CHECK_DEFCONFIG
I2C please check defconfig
-
BK_ERR_I2C_SCL_TIMEOUT
I2C SCL timeout
-
BK_ERR_I2C_INVALID_ID
I2C id number is invalid
Type Definitions
Enumerations
Header File
Structures
-
struct i2c_config_t
Public Members
-
uint32_t baud_rate
i2c clock rate
-
i2c_addr_mode_t addr_mode
i2c address mode
-
uint16_t slave_addr
i2c address for slave mode
-
uint32_t baud_rate
Macros
-
I2C_BAUD_RATE_50KHZ
-
I2C_BAUD_RATE_100KHZ
-
I2C_BAUD_RATE_200KHZ
-
I2C_BAUD_RATE_400KHZ
-
I2C_DEFAULT_BAUD_RATE
Type Definitions
-
typedef uint8_t i2c_unit_t
i2c uint id
Enumerations
-
enum i2c_addr_mode_t
Values:
-
enumerator I2C_ADDR_MODE_7BIT
i2c address mode 7bit
-
enumerator I2C_ADDR_MODE_10BIT
i2c address mode 10bit
-
enumerator I2C_ADDR_MODE_7BIT