I2C - Inter-Integrated Circuit#
Introduction#
This module contains functions to control the I2C peripheral of Silicon Labs 32-bit MCUs and SoCs. The I2C interface allows communication on I2C buses with the lowest energy consumption possible.
Example#
I2C TX example:
{
// I2C init.
sl_hal_i2c_init(i2c_peripheral, SL_I2C_LEADER_MODE);
// Set the clock frequency.
sl_hal_i2c_set_clock_frequency(i2c_peripheral, i2c_clk, freq_scl, clhr);
// Enable i2c.
sl_hal_i2c_enable(i2c_peripheral);
follower_addr &= 0xfe;
// Initiate start command.
sl_hal_i2c_start_cmd(i2c_peripheral);
// Transmit follower address with write mode.
sl_hal_i2c_tx(i2c_peripheral, follower_addr);
}
Functions#
This function initializes the I2C module in Leader/Follower Mode.
This function is used to send the data.
This function is used to receive the data.
The bus frequency is only relevant when acting as Leader.
Get I2C clock frequency.
Configure I2C to its reset state.
This function will send a start command.
This function will send a stop command.
This function clears the Tx , RX Buffer , Shift registers and Pending Commands.
Wait for disabling to finish.
Wait for all synchronizations to finish.
This function Enables I2C Peripheral.
This function Disables I2C Peripheral.
This function is used to send the ACK on the I2C Bus.
This function is used to enable/disable the Auto ACK feature on the I2C Bus.
This function is used to send the NACK on the I2C Bus.
This function is used to get the I2C STATUS register.
Enables one or more I2C interrupts.
Disables one or more I2C interrupts.
Sets one or more pending I2C interrupts from Software.
Clears one or more pending I2C interrupts from Software.
Get pending I2C interrupt flags.
Get enabled I2C interrupt flags.
Get enabled and Pending I2C interrupt flags.
Set Follower address to use for I2C peripheral (when operating in Follower mode).
Get Follower address used for I2C peripheral (when operating in Follower mode).
Set Follower address to use for I2C peripheral (when operating in Follower mode).
Get Follower address mask used for I2C peripheral (when operating in Follower mode).
Macros#
I2C instance validation.
I2C IF Errors flags.
I2C IEN Errors flags.
Standard mode max frequency assuming using 4:4 ratio for Nlow:Nhigh.
Fast mode max frequency assuming using 6:3 ratio for Nlow:Nhigh.
Fast mode+ max frequency assuming using 11:6 ratio for Nlow:Nhigh.
Function Documentation#
sl_hal_i2c_init#
void sl_hal_i2c_init (I2C_TypeDef * i2c, sl_i2c_operating_mode_t mode)
This function initializes the I2C module in Leader/Follower Mode.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
sl_i2c_operating_mode_t | [in] | mode | Leader or Follower mode. |
sl_hal_i2c_tx#
void sl_hal_i2c_tx (I2C_TypeDef * i2c, uint8_t data)
This function is used to send the data.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint8_t | [in] | data | Data to transmit. |
sl_hal_i2c_rx#
uint8_t sl_hal_i2c_rx (I2C_TypeDef * i2c)
This function is used to receive the data.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
Data received.
sl_hal_i2c_set_clock_frequency#
void sl_hal_i2c_set_clock_frequency (I2C_TypeDef * i2c, uint32_t i2c_clk, uint32_t freq_scl, sl_i2c_clock_hlr_t clhr)
The bus frequency is only relevant when acting as Leader.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint32_t | [in] | i2c_clk | An I2C Peripheral clock frequency in Hz that will be used. Setting it to a higher than actual configured value has the consequence of reducing the real I2C frequency. |
uint32_t | [in] | freq_scl | A bus frequency to set (bus speed may be lower due to integer prescaling). Safe (according to the I2C specification) maximum frequencies for standard fast and fast+ modes are available using SL_I2C_FREQ_ defines.(Using SL_I2C_FREQ_ defines requires corresponding setting of |
sl_i2c_clock_hlr_t | [in] | clhr | A clock low-to-high ratio type to use. If not using SL_I2C_CLK_HLR_STANDARD, make sure all devices on the bus support the specified mode. Using a non-standard ratio is useful to achieve a higher bus clock in fast and fast+ modes. |
The bus frequency should not be set higher than the maximum frequency accepted by the slowest device on the bus.
Notice that, due to asymmetric requirements on low and high I2C clock cycles in the I2C specification, the maximum frequency allowed to comply with the specification may be somewhat lower than expected.
See the reference manual, details on I2C clock generation, for maximum allowed theoretical frequencies for different modes.
sl_hal_i2c_get_clock_frequency#
uint32_t sl_hal_i2c_get_clock_frequency (I2C_TypeDef * i2c, uint32_t i2c_clk)
Get I2C clock frequency.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint32_t | [in] | i2c_clk | An I2C Peripheral clock frequency in Hz that will be used. Setting it to a higher than actual configured value has the consequence of reducing the real I2C frequency.Make sure to give i2c_clk as given in sl_hal_i2c_set_clock_frequency. |
Returns
Clock frequency value.
sl_hal_i2c_reset#
void sl_hal_i2c_reset (I2C_TypeDef * i2c)
Configure I2C to its reset state.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
sl_hal_i2c_start_cmd#
sl_status_t sl_hal_i2c_start_cmd (I2C_TypeDef * i2c)
This function will send a start command.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
On Success returns SL_STATUS_OK. On Failure returns SL_STATUS_NOT_INITIALIZED.
sl_hal_i2c_stop_cmd#
sl_status_t sl_hal_i2c_stop_cmd (I2C_TypeDef * i2c)
This function will send a stop command.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
On Success returns SL_STATUS_OK. On Failure returns SL_STATUS_NOT_INITIALIZED.
sl_hal_i2c_flush_buffers#
void sl_hal_i2c_flush_buffers (I2C_TypeDef * i2c)
This function clears the Tx , RX Buffer , Shift registers and Pending Commands.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
sl_hal_i2c_wait_ready#
void sl_hal_i2c_wait_ready (I2C_TypeDef * i2c)
Wait for disabling to finish.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
sl_hal_i2c_wait_sync#
void sl_hal_i2c_wait_sync (I2C_TypeDef * i2c)
Wait for all synchronizations to finish.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
sl_hal_i2c_enable#
void sl_hal_i2c_enable (I2C_TypeDef * i2c)
This function Enables I2C Peripheral.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. * |
sl_hal_i2c_disable#
void sl_hal_i2c_disable (I2C_TypeDef * i2c)
This function Disables I2C Peripheral.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
sl_hal_i2c_send_ack#
sl_status_t sl_hal_i2c_send_ack (I2C_TypeDef * i2c)
This function is used to send the ACK on the I2C Bus.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
On Success returns SL_STATUS_OK. On Failure returns SL_STATUS_NOT_INITIALIZED.
sl_hal_i2c_auto_ack#
void sl_hal_i2c_auto_ack (I2C_TypeDef * i2c, uint8_t enable)
This function is used to enable/disable the Auto ACK feature on the I2C Bus.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint8_t | [in] | enable | Enable or Disable the Auto ack. |
It is used only in Leader/Follower receiver mode.
sl_hal_i2c_send_nack#
sl_status_t sl_hal_i2c_send_nack (I2C_TypeDef * i2c)
This function is used to send the NACK on the I2C Bus.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
On Success returns SL_STATUS_OK. On Failure returns SL_STATUS_NOT_INITIALIZED.
sl_hal_i2c_get_status#
uint32_t sl_hal_i2c_get_status (I2C_TypeDef * i2c)
This function is used to get the I2C STATUS register.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
STATUS register value.
sl_hal_i2c_enable_interrupts#
void sl_hal_i2c_enable_interrupts (I2C_TypeDef * i2c, uint32_t flags)
Enables one or more I2C interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint32_t | [in] | flags | I2C interrupt sources to enable. Use a set of interrupt flags OR-ed together to set multiple interrupt sources. |
sl_hal_i2c_disable_interrupts#
void sl_hal_i2c_disable_interrupts (I2C_TypeDef * i2c, uint32_t flags)
Disables one or more I2C interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint32_t | [in] | flags | I2C interrupt sources to disable. Use a set of interrupt flags OR-ed together to set multiple interrupt sources. |
sl_hal_i2c_set_interrupts#
void sl_hal_i2c_set_interrupts (I2C_TypeDef * i2c, uint32_t flags)
Sets one or more pending I2C interrupts from Software.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint32_t | [in] | flags | I2C interrupt sources to set to pending. Use a set of interrupt flags OR-ed together to set multiple interrupt sources. |
sl_hal_i2c_clear_interrupts#
void sl_hal_i2c_clear_interrupts (I2C_TypeDef * i2c, uint32_t flags)
Clears one or more pending I2C interrupts from Software.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint32_t | [in] | flags | I2C interrupt sources to clear. Use a set of interrupt flags OR-ed together to clear multiple interrupt sources. |
sl_hal_i2c_get_pending_interrupts#
uint32_t sl_hal_i2c_get_pending_interrupts (I2C_TypeDef * i2c)
Get pending I2C interrupt flags.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
Pending I2C interrupt sources.
sl_hal_i2c_get_enabled_interrupts#
uint32_t sl_hal_i2c_get_enabled_interrupts (I2C_TypeDef * i2c)
Get enabled I2C interrupt flags.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Useful for handling more interrupt sources in the same interrupt handler.
Returns
Enabled I2C interrupt sources.
sl_hal_i2c_get_enabled_pending_interrupts#
uint32_t sl_hal_i2c_get_enabled_pending_interrupts (I2C_TypeDef * i2c)
Get enabled and Pending I2C interrupt flags.
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Useful for handling more interrupt sources in the same interrupt handler.
Returns
Pending and enabled I2C interrupt sources.
sl_hal_i2c_set_follower_address#
void sl_hal_i2c_set_follower_address (I2C_TypeDef * i2c, uint16_t addr, bool is_10bit_addr)
Set Follower address to use for I2C peripheral (when operating in Follower mode).
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint16_t | [in] | addr | I2C Follower address to use. The 7 most significant bits define the actual address, the least significant bit is reserved and always set to 0. |
bool | [in] | is_10bit_addr | 7bit or 10bit address configuration. |
sl_hal_i2c_get_follower_address#
uint8_t sl_hal_i2c_get_follower_address (I2C_TypeDef * i2c)
Get Follower address used for I2C peripheral (when operating in Follower mode).
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
I2C Follower address in use. The 7 most significant bits define the actual address, the least significant bit is reserved and always returned as 0.
sl_hal_i2c_set_follower_mask_address#
void sl_hal_i2c_set_follower_mask_address (I2C_TypeDef * i2c, uint8_t mask)
Set Follower address to use for I2C peripheral (when operating in Follower mode).
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
uint8_t | [in] | mask | Specifies the significant bits of the follower address. Setting the mask to 0x00 will match all addresses, while setting it to 0x7F will only match the exact address specified by ADDR. |
sl_hal_i2c_get_follower_mask_address#
uint8_t sl_hal_i2c_get_follower_mask_address (I2C_TypeDef * i2c)
Get Follower address mask used for I2C peripheral (when operating in Follower mode).
Type | Direction | Argument Name | Description |
---|---|---|---|
I2C_TypeDef * | [in] | i2c | A pointer to the I2C Peripheral register block. |
Returns
I2C Target address mask in use. The 7 most significant bits define the actual address mask, the least significant bit is reserved and always returned as 0.