High level interface for interacting with the I2C resource.
The I2C protocol is a synchronous serial interface protocol. This driver supports both controller and target mode of operation. The communication frequency and address (for target operation) can be configured.
Configure the behavior (controller/target) and the interface (bus frequency, target address) using the mtb_hal_i2c_configure function.
See Snippet 1: I2C Initialization and Configuration for example initialization as controller or target.
This snippet initializes an I2C resource as controller or target and assigns the sda and scl pins.
Initializing as I2C controller
Initializing as I2C target
This snippet shows how to enable and handle I2C events using mtb_hal_i2c_enable_event and mtb_hal_i2c_register_callback.
The callback parameter of mtb_hal_i2c_register_callback is used to pass the callback handler that will be invoked when an event occurs.
The event parameter of mtb_hal_i2c_enable_event is used to pass the bitmasks of events ( mtb_hal_i2c_event_t) to be enabled.
Peripheral Driver Library (PDL)
Code examples (Github)
API Reference | |
I2C HAL Results | |
I2C specific return codes. | |
Data Structures | |
struct | mtb_hal_i2c_cfg_t |
I2C configuration. More... | |
Macros | |
#define | MTB_HAL_I2C_MODE_TARGET (true) |
Named define for Target mode for use when initializing the mtb_hal_i2c_cfg_t structure. | |
#define | MTB_HAL_I2C_MODE_CONTROLLER (false) |
Named define for Controller mode for use when initializing the mtb_hal_i2c_cfg_t structure. | |
#define | MTB_HAL_I2C_DEFAULT_ADDR_MASK (0xFE) |
Named define for default address mask for use when initializing the mtb_hal_i2c_cfg_t structure. | |
Typedefs | |
typedef void(* | mtb_hal_i2c_event_callback_t) (void *callback_arg, mtb_hal_i2c_event_t event) |
Handler for I2C events. | |
typedef mtb_hal_i2c_command_rsp_t(* | mtb_hal_i2c_address_callback_t) (void *callback_arg, mtb_hal_i2c_addr_event_t event, uint8_t address) |
Handler for I2C address events. | |
Enumerations | |
enum | mtb_hal_i2c_event_t { MTB_HAL_I2C_EVENT_NONE = 0 , MTB_HAL_I2C_TARGET_READ_EVENT = (MTB_HAL_MAP_I2C_TARGET_READ_EVENT) , MTB_HAL_I2C_TARGET_WRITE_EVENT = (MTB_HAL_MAP_I2C_TARGET_WRITE_EVENT) , MTB_HAL_I2C_TARGET_RD_IN_FIFO_EVENT = (MTB_HAL_MAP_I2C_TARGET_RD_IN_FIFO_EVENT) , MTB_HAL_I2C_TARGET_RD_BUF_EMPTY_EVENT = (MTB_HAL_MAP_I2C_TARGET_RD_BUF_EMPTY_EVENT) , MTB_HAL_I2C_TARGET_RD_CMPLT_EVENT = (MTB_HAL_MAP_I2C_TARGET_RD_CMPLT_EVENT) , MTB_HAL_I2C_TARGET_WR_CMPLT_EVENT = (MTB_HAL_MAP_I2C_TARGET_WR_CMPLT_EVENT) , MTB_HAL_I2C_TARGET_ERR_EVENT = (MTB_HAL_MAP_I2C_TARGET_ERR_EVENT) , MTB_HAL_I2C_CONTROLLER_ERR_EVENT = (MTB_HAL_MAP_I2C_CONTROLLER_ERR_EVENT) } |
I2C enum to enable/disable/report interrupt cause flags. More... | |
enum | mtb_hal_i2c_addr_event_t { MTB_HAL_I2C_ADDR_EVENT_NONE = 0 , MTB_HAL_I2C_GENERAL_CALL_EVENT = (MTB_HAL_MAP_I2C_GENERAL_CALL_EVENT) , MTB_HAL_I2C_ADDR_MATCH_EVENT = (MTB_HAL_MAP_I2C_ADDR_IN_FIFO_EVENT) } |
I2C enum to enable/disable/report address interrupt cause flags. More... | |
enum | mtb_hal_i2c_fifo_type_t { MTB_HAL_I2C_FIFO_RX , MTB_HAL_I2C_FIFO_TX } |
I2C FIFO type. More... | |
enum | mtb_hal_i2c_command_rsp_t { MTB_HAL_I2C_CMD_ACK , MTB_HAL_I2C_CMD_NAK } |
I2C Command ACK / NAK. More... | |
enum | mtb_hal_i2c_output_t { MTB_HAL_I2C_OUTPUT_TRIGGER_RX_FIFO_LEVEL_REACHED , MTB_HAL_I2C_OUTPUT_TRIGGER_TX_FIFO_LEVEL_REACHED } |
Enum of possible output signals from an I2C. More... | |
Functions | |
cy_rslt_t | mtb_hal_i2c_configure (mtb_hal_i2c_t *obj, const mtb_hal_i2c_cfg_t *cfg) |
Configure the I2C block. More... | |
cy_rslt_t | mtb_hal_i2c_controller_write (mtb_hal_i2c_t *obj, uint16_t dev_addr, const uint8_t *data, uint16_t size, uint32_t timeout, bool send_stop) |
I2C controller blocking write. More... | |
cy_rslt_t | mtb_hal_i2c_controller_read (mtb_hal_i2c_t *obj, uint16_t dev_addr, uint8_t *data, uint16_t size, uint32_t timeout, bool send_stop) |
I2C controller blocking read. More... | |
cy_rslt_t | mtb_hal_i2c_target_config_read_buffer (mtb_hal_i2c_t *obj, const uint8_t *data, uint16_t size) |
The function configures the read buffer on an I2C Target. More... | |
cy_rslt_t | mtb_hal_i2c_target_config_write_buffer (mtb_hal_i2c_t *obj, uint8_t *data, uint16_t size) |
The function configures the write buffer on an I2C Target. More... | |
void | mtb_hal_i2c_register_callback (mtb_hal_i2c_t *obj, mtb_hal_i2c_event_callback_t callback, void *callback_arg) |
Register an I2C event callback handler More... | |
void | mtb_hal_i2c_register_address_callback (mtb_hal_i2c_t *obj, mtb_hal_i2c_address_callback_t callback, void *callback_arg) |
Register an I2C address callback handler More... | |
void | mtb_hal_i2c_enable_event (mtb_hal_i2c_t *obj, mtb_hal_i2c_event_t event, bool enable) |
Configure and Enable or Disable I2C Interrupt. More... | |
void | mtb_hal_i2c_enable_address_event (mtb_hal_i2c_t *obj, mtb_hal_i2c_addr_event_t event, bool enable) |
Configure and Enable or Disable I2C Address Interrupt. More... | |
uint32_t | mtb_hal_i2c_target_readable (mtb_hal_i2c_t *obj) |
Returns the number of bytes written by the I2C controller. More... | |
uint32_t | mtb_hal_i2c_target_writable (mtb_hal_i2c_t *obj) |
Returns the number of bytes can be read by the I2C controller. More... | |
cy_rslt_t | mtb_hal_i2c_target_read (mtb_hal_i2c_t *obj, uint8_t *dst_buff, uint16_t *size, uint32_t timeout) |
Wait for controller send data to RX buffer and store them to the user-defined buffer. More... | |
cy_rslt_t | mtb_hal_i2c_target_write (mtb_hal_i2c_t *obj, const uint8_t *src_buff, uint16_t *size, uint32_t timeout) |
Write data from the user-defined buffer to I2C TX buffer. More... | |
cy_rslt_t | mtb_hal_i2c_target_abort_read (mtb_hal_i2c_t *obj) |
The function aborts the configured target read buffer to be read by the controller. More... | |
cy_rslt_t | mtb_hal_i2c_clear (mtb_hal_i2c_t *obj) |
Clear the I2C buffers. More... | |
cy_rslt_t | mtb_hal_i2c_process_interrupt (mtb_hal_i2c_t *obj) |
Process interrupts related related to an I2C instance. More... | |
struct mtb_hal_i2c_cfg_t |
enum mtb_hal_i2c_event_t |
I2C enum to enable/disable/report interrupt cause flags.
enum mtb_hal_i2c_output_t |
Enum of possible output signals from an I2C.
cy_rslt_t mtb_hal_i2c_configure | ( | mtb_hal_i2c_t * | obj, |
const mtb_hal_i2c_cfg_t * | cfg | ||
) |
Configure the I2C block.
NOTE: Controller/Target specific functions only work when the block is configured to be in that mode.
See Snippet 1: I2C Initialization and Configuration
[in] | obj | The I2C object |
[in] | cfg | Configuration settings to apply |
cy_rslt_t mtb_hal_i2c_controller_write | ( | mtb_hal_i2c_t * | obj, |
uint16_t | dev_addr, | ||
const uint8_t * | data, | ||
uint16_t | size, | ||
uint32_t | timeout, | ||
bool | send_stop | ||
) |
I2C controller blocking write.
This will write size
bytes of data from the buffer pointed to by data
. It will not return until either all of the data has been written, or the timeout has elapsed.
[in] | obj | The I2C object |
[in] | dev_addr | device address (7-bit) |
[in] | data | I2C send data |
[in] | size | I2C send data size |
[in] | timeout | timeout in millisecond, set this value to 0 if you want to wait forever |
[in] | send_stop | whether the stop should be send, used to support repeat start conditions |
cy_rslt_t mtb_hal_i2c_controller_read | ( | mtb_hal_i2c_t * | obj, |
uint16_t | dev_addr, | ||
uint8_t * | data, | ||
uint16_t | size, | ||
uint32_t | timeout, | ||
bool | send_stop | ||
) |
I2C controller blocking read.
This will read size
bytes of data into the buffer pointed to by data
. It will not return until either all of the data has been read, or the timeout has elapsed.
[in] | obj | The I2C object |
[in] | dev_addr | device address (7-bit) |
[out] | data | I2C receive data |
[in] | size | I2C receive data size |
[in] | timeout | timeout in millisecond, set this value to 0 if you want to wait forever |
[in] | send_stop | whether the stop should be send, used to support repeat start conditions |
cy_rslt_t mtb_hal_i2c_target_config_read_buffer | ( | mtb_hal_i2c_t * | obj, |
const uint8_t * | data, | ||
uint16_t | size | ||
) |
The function configures the read buffer on an I2C Target.
This is the buffer that the target recieves data into. The user needs to setup a new buffer every time (i.e. call mtb_hal_i2c_target_config_write_buffer and mtb_hal_i2c_target_config_read_buffer every time the buffer has been used up)
See related code example: PSoC™ 6 MCU: I2C Controller
[in] | obj | The I2C object |
[in] | data | I2C target receive data buffer |
[in] | size | I2C target receive data buffer size |
cy_rslt_t mtb_hal_i2c_target_config_write_buffer | ( | mtb_hal_i2c_t * | obj, |
uint8_t * | data, | ||
uint16_t | size | ||
) |
The function configures the write buffer on an I2C Target.
This is the buffer that stores data to be written to the controller. The user needs to setup a new buffer every time (i.e. call mtb_hal_i2c_target_config_write_buffer and mtb_hal_i2c_target_config_read_buffer every time the buffer has been used up)
See related code example: PSoC™ 6 MCU: I2C Controller
[in] | obj | The I2C object |
[out] | data | I2C target write data buffer |
[in] | size | I2C target write data buffer size |
void mtb_hal_i2c_register_callback | ( | mtb_hal_i2c_t * | obj, |
mtb_hal_i2c_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register an I2C event callback handler
This function will be called when one of the events enabled by mtb_hal_i2c_enable_event occurs.
See Snippet 2: Handling events
[in] | obj | The I2C object |
[in] | callback | The callback handler which will be invoked when an event triggers |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void mtb_hal_i2c_register_address_callback | ( | mtb_hal_i2c_t * | obj, |
mtb_hal_i2c_address_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register an I2C address callback handler
This function will be called when one of the events enabled by mtb_hal_i2c_enable_address_event occurs. NOTE: This function will not have an effect if enable_address_callback parameter of mtb_hal_i2c_cfg_t structure was false when mtb_hal_i2c_configure was called.
See Snippet 2: Handling events
[in] | obj | The I2C object |
[in] | callback | The callback handler which will be invoked when an event triggers |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void mtb_hal_i2c_enable_event | ( | mtb_hal_i2c_t * | obj, |
mtb_hal_i2c_event_t | event, | ||
bool | enable | ||
) |
Configure and Enable or Disable I2C Interrupt.
When an enabled event occurs, the function specified by mtb_hal_i2c_register_callback will be called.
See Snippet 2: Handling events
[in] | obj | The I2C object |
[in] | event | The I2C event type |
[in] | enable | True to turn on interrupts, False to turn off |
void mtb_hal_i2c_enable_address_event | ( | mtb_hal_i2c_t * | obj, |
mtb_hal_i2c_addr_event_t | event, | ||
bool | enable | ||
) |
Configure and Enable or Disable I2C Address Interrupt.
When an enabled event occurs, the function specified by mtb_hal_i2c_register_address_callback will be called.
See Snippet 2: Handling events
[in] | obj | The I2C object |
[in] | event | The I2C address event type |
[in] | enable | True to turn on interrupts, False to turn off |
uint32_t mtb_hal_i2c_target_readable | ( | mtb_hal_i2c_t * | obj | ) |
Returns the number of bytes written by the I2C controller.
Calling the mtb_hal_i2c_target_config_write_buffer API will clear the counter of bytes sent by controller
[in] | obj | The I2C object |
uint32_t mtb_hal_i2c_target_writable | ( | mtb_hal_i2c_t * | obj | ) |
Returns the number of bytes can be read by the I2C controller.
Calling the mtb_hal_i2c_target_config_read_buffer API will clear the counter of bytes read by controller
[in] | obj | The I2C object |
cy_rslt_t mtb_hal_i2c_target_read | ( | mtb_hal_i2c_t * | obj, |
uint8_t * | dst_buff, | ||
uint16_t * | size, | ||
uint32_t | timeout | ||
) |
Wait for controller send data to RX buffer and store them to the user-defined buffer.
NOTE: If the read size requested is greater than the data available, the function only copies the available data.
[in] | obj | The I2C object |
[in] | dst_buff | Pointer on memory to store the data from the target RX buffer. |
[in,out] | size | [in] The number of bytes to read, [out] number actually read. |
[in] | timeout | Timeout in millisecond, set this value to 0 if you don't want to wait at all. |
cy_rslt_t mtb_hal_i2c_target_write | ( | mtb_hal_i2c_t * | obj, |
const uint8_t * | src_buff, | ||
uint16_t * | size, | ||
uint32_t | timeout | ||
) |
Write data from the user-defined buffer to I2C TX buffer.
NOTE: If the size of the data is greater than can fit the buffer, the function only copies data that can fit. The buffer size can be configured with mtb_hal_i2c_target_config_read_buffer
[in] | obj | The I2C object |
[in] | src_buff | Pointer on memory to copy the data to the target TX buffer. |
[in,out] | size | [in] The number of bytes to send, [out] number actually sent. |
[in] | timeout | Timeout in millisecond, set this value to 0 if you don't want to wait at all. |
cy_rslt_t mtb_hal_i2c_target_abort_read | ( | mtb_hal_i2c_t * | obj | ) |
The function aborts the configured target read buffer to be read by the controller.
If the controller reads and "abort operation" is requested, the MTB_HAL_I2C_TARGET_RD_BUF_EMPTY_EVENT event occurs.
[in] | obj | The I2C object |
cy_rslt_t mtb_hal_i2c_clear | ( | mtb_hal_i2c_t * | obj | ) |
Clear the I2C buffers.
[in] | obj | The I2C object |
cy_rslt_t mtb_hal_i2c_process_interrupt | ( | mtb_hal_i2c_t * | obj | ) |
Process interrupts related related to an I2C instance.
obj | HAL object for which the interrupt should be processed |