This section describes the programming interface of the I2C master mode Peripheral driver. The I2C master Peripheral driver provides functions for a master device to send and receive data.
I2C Initialization
To initialize the I2C driver, define an i2c_master_state_t type variable and pass it in the i2c_init. The variable does not need to have a specific value because the I2C driver only needs the memory associated with the variable. Because all I2C master API functions need the run-time structure, it should be maintained as long as the driver is used.
Because I2C drivers use the OSA_Delay form OSA layer, OSA_Init must be called before calling I2C transaction API functions. Otherwise, the API functions do not work.
I2C Data Transactions
I2C master driver provides these APIs for data transactions:
I2C_DRV_MasterSendDataBlocking \n
I2C_DRV_MasterReceiveDataBlocking \n
Both of these functions perform a blocking transaction, which means that the function does not return until all data is sent/received OR a time out occurs.
Before calling the API functions, the application should define the slave device with "i2c_device_t" type. Note that, if the slave is a 10-bit address, the first 6 bits must be 011110 in binary.
typedef struct I2CDevice
{
uint16_t address;
uint32_t baudRate_kbps;
If the buadRate_kbps inside the i2c_device_t object is changed, it is automatically applied to the next send/receive transaction.
|
LPI2C_Type *const | g_lpi2cBase [LPI2C_INSTANCE_COUNT] |
| Table of base addresses for LPI2C instances. More...
|
|
|
lpi2c_status_t | LPI2C_DRV_MasterInit (uint32_t instance, lpi2c_master_state_t *master) |
| Initializes the LPI2C master mode driver. More...
|
|
void | LPI2C_DRV_MasterDeinit (uint32_t instance) |
| Shuts down the driver. More...
|
|
void | LPI2C_DRV_MasterSetBaudRate (uint32_t instance, const lpi2c_device_t *device) |
| Configures the LPI2C bus to access a device. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterSendDataBlocking (uint32_t instance, const lpi2c_device_t *device, const uint8_t *cmdBuff, uint32_t cmdSize, const uint8_t *txBuff, uint32_t txSize, uint32_t timeout_ms) |
| Performs a blocking send transaction on the LPI2C bus. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterSendData (uint32_t instance, const lpi2c_device_t *device, const uint8_t *cmdBuff, uint32_t cmdSize, const uint8_t *txBuff, uint32_t txSize) |
| Performs a non-blocking send transaction on the LPI2C bus. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterGetSendStatus (uint32_t instance, uint32_t *bytesRemaining) |
| Gets the current status of the LPI2C master transmission. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterAbortSendData (uint32_t instance) |
| Terminates a non-blocking LPI2C master transmission early. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterReceiveDataBlocking (uint32_t instance, const lpi2c_device_t *device, const uint8_t *cmdBuff, uint32_t cmdSize, uint8_t *rxBuff, uint32_t rxSize, uint32_t timeout_ms) |
| Performs a blocking receive transaction on the LPI2C bus. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterReceiveData (uint32_t instance, const lpi2c_device_t *device, const uint8_t *cmdBuff, uint32_t cmdSize, uint8_t *rxBuff, uint32_t rxSize) |
| Performs a non-blocking receive transaction on the LPI2C bus. More...
|
|
lpi2c_status_t | LPI2C_DRV_MasterGetReceiveStatus (uint32_t instance, uint32_t *bytesRemaining) |
| Gets the current status of the LPI2C master receive. More...
|
|
static lpi2c_status_t | LPI2C_DRV_MasterReceiveDataPolling (uint32_t instance, uint16_t slaveAddr, const uint8_t *cmdBuff, uint32_t cmdSize, uint8_t *rxBuff, uint32_t rxSize) |
| Performs a polling receive transaction on the LPI2C bus. More...
|
|
static lpi2c_status_t | LPI2C_DRV_MasterSendDataPolling (uint32_t instance, uint16_t slaveAddr, const uint8_t *cmdBuff, uint32_t cmdSize, const uint8_t *txBuff, uint32_t txSize) |
| Performs a polling send transaction on the LPI2C bus. More...
|
|
Data Fields |
uint16_t | address |
| Slave's 7-bit or 10-bit address. More...
|
|
uint32_t | baudRate_kbps |
| The baud rate in kbps to use by current slave device.
|
|
uint16_t lpi2c_device_t::address |
If 10-bit address, the first 6 bits must be 011110 in binary.
struct lpi2c_master_state_t |
- Note
- The contents of this structure are internal to the driver and should not be modified by users. Contents of the structure are subject to change in future releases.
- Parameters
-
instance | The LPI2C peripheral instance number. |
master | The pointer to the LPI2C master driver state structure. |
- Returns
- Error or success status returned by API.
void LPI2C_DRV_MasterDeinit |
( |
uint32_t |
instance | ) |
|
- Parameters
-
instance | The LPI2C peripheral instance number. |
void LPI2C_DRV_MasterSetBaudRate |
( |
uint32_t |
instance, |
|
|
const lpi2c_device_t * |
device |
|
) |
| |
- Parameters
-
instance | The LPI2C peripheral instance number. |
device | The pointer to the LPI2C device information structure. |
lpi2c_status_t LPI2C_DRV_MasterSendDataBlocking |
( |
uint32_t |
instance, |
|
|
const lpi2c_device_t * |
device, |
|
|
const uint8_t * |
cmdBuff, |
|
|
uint32_t |
cmdSize, |
|
|
const uint8_t * |
txBuff, |
|
|
uint32_t |
txSize, |
|
|
uint32_t |
timeout_ms |
|
) |
| |
Both cmdBuff and txBuff are byte-aligned. If slave devices' data is not byte-aligned, prepare these buffers according to the related protocol.
- Parameters
-
instance | The LPI2C peripheral instance number. |
device | The pointer to the LPI2C device information structure. |
cmdBuff | The pointer to the commands to be transferred, could be NULL. |
cmdSize | The length in bytes of the commands to be transferred, could be 0. |
txBuff | The pointer to the data to be transferred, cannot be NULL. |
txSize | The length in bytes of the data to be transferred, cannot be 0. |
timeout_ms | A timeout for the transfer in microseconds. |
- Returns
- Error or success status returned by API.
lpi2c_status_t LPI2C_DRV_MasterSendData |
( |
uint32_t |
instance, |
|
|
const lpi2c_device_t * |
device, |
|
|
const uint8_t * |
cmdBuff, |
|
|
uint32_t |
cmdSize, |
|
|
const uint8_t * |
txBuff, |
|
|
uint32_t |
txSize |
|
) |
| |
This function returns immediately when setting the buffer pointer and length to transfer buffer and transfer size. Check the status of LPI2C to find out whether the transmission is complete or not. Both cmdBuff and txBuff are byte aligned. If slave devices' data is not byte-aligned, prepare these buffers according to the related protocol.
- Parameters
-
instance | The LPI2C peripheral instance number. |
device | The pointer to the LPI2C device information structure. |
cmdBuff | The pointer to the commands to be transferred,could be NULL. |
cmdSize | The length in bytes of the commands to be transferred, could be 0. |
txBuff | The pointer to the data to be transferred, cannot be NULL. |
txSize | The length in bytes of the data to be transferred, cannot be 0. |
- Returns
- Error or success status returned by API.
lpi2c_status_t LPI2C_DRV_MasterGetSendStatus |
( |
uint32_t |
instance, |
|
|
uint32_t * |
bytesRemaining |
|
) |
| |
This function gets the current LPI2C status of the non-blocking transmission.
- Parameters
-
instance | Instance number of the LPI2C module. |
bytesRemaining | The number of remaining bytes in the active LPI2C transmits. |
- Returns
- Current status of LPI2C transmission: in progress (busy) or complete (success).
- Parameters
-
instance | Instance number of the LPI2C module. |
- Returns
- Whether the aborting is success or not.
lpi2c_status_t LPI2C_DRV_MasterReceiveDataBlocking |
( |
uint32_t |
instance, |
|
|
const lpi2c_device_t * |
device, |
|
|
const uint8_t * |
cmdBuff, |
|
|
uint32_t |
cmdSize, |
|
|
uint8_t * |
rxBuff, |
|
|
uint32_t |
rxSize, |
|
|
uint32_t |
timeout_ms |
|
) |
| |
Both cmdBuff and rxBuff are byte aligned. If slave devices are not byte-aligned, prepare these buffers according to the related protocol.
- Parameters
-
instance | The LPI2C peripheral instance number. |
device | The pointer to the LPI2C device information structure. |
cmdBuff | The pointer to the commands to be transferred, could be NULL. |
cmdSize | The length in bytes of the commands to be transferred, could be 0. |
rxBuff | The pointer to the data to be transferred, cannot be NULL. |
rxSize | The length in bytes of the data to be transferred, cannot be 0. |
timeout_ms | A timeout for the transfer in microseconds. |
- Returns
- Error or success status returned by API.
lpi2c_status_t LPI2C_DRV_MasterReceiveData |
( |
uint32_t |
instance, |
|
|
const lpi2c_device_t * |
device, |
|
|
const uint8_t * |
cmdBuff, |
|
|
uint32_t |
cmdSize, |
|
|
uint8_t * |
rxBuff, |
|
|
uint32_t |
rxSize |
|
) |
| |
This function returns immediately after setting the buffer pointer and length to the receive buffer and the receive size. Check the status of LPI2C to find out whether the receive is complete or not. Both cmdBuff and rxBuff are byte aligned. If slave devices' data is not byte-aligned, prepare these buffers according to the related protocol.
- Parameters
-
instance | The LPI2C peripheral instance number. |
device | The pointer to the LPI2C device information structure. |
cmdBuff | The pointer to the commands to be transferred, could be NULL. |
cmdSize | The length in bytes of the commands to be transferred, could be 0. |
rxBuff | The pointer to the data to be transferred, cannot be NULL. |
rxSize | The length in bytes of the data to be transferred, cannot be 0. |
- Returns
- Error or success status returned by API.
lpi2c_status_t LPI2C_DRV_MasterGetReceiveStatus |
( |
uint32_t |
instance, |
|
|
uint32_t * |
bytesRemaining |
|
) |
| |
This function is designed to get the current LPI2C status of a non-blocking receive.
- Parameters
-
instance | Instance number of the LPI2C module. |
bytesRemaining | The number of remaining bytes in the active LPI2C transmits. |
- Returns
- Current status of LPI2C receive: in progress (busy) or complete (success).
static lpi2c_status_t LPI2C_DRV_MasterReceiveDataPolling |
( |
uint32_t |
instance, |
|
|
uint16_t |
slaveAddr, |
|
|
const uint8_t * |
cmdBuff, |
|
|
uint32_t |
cmdSize, |
|
|
uint8_t * |
rxBuff, |
|
|
uint32_t |
rxSize |
|
) |
| |
|
inlinestatic |
Both cmdBuff and rxBuff are byte aligned. If the slave device data is not byte-aligned, prepare these buffers according to the related protocol.
- Parameters
-
instance | Instance number of the LPI2C module. |
slaveAddr | The slave address to communicate. |
cmdBuff | The pointer to the commands to be transferred, could be NULL. |
cmdSize | The length in bytes of the commands to be transferred, could be 0. |
rxBuff | The pointer to the data to be transferred, cannot be NULL. |
rxSize | The length in bytes of the data to be transferred, cannot be 0. |
- Returns
- Error or success status returned by API.
static lpi2c_status_t LPI2C_DRV_MasterSendDataPolling |
( |
uint32_t |
instance, |
|
|
uint16_t |
slaveAddr, |
|
|
const uint8_t * |
cmdBuff, |
|
|
uint32_t |
cmdSize, |
|
|
const uint8_t * |
txBuff, |
|
|
uint32_t |
txSize |
|
) |
| |
|
inlinestatic |
Both cmdBuff and txBuff are byte aligned. If the slave device data is not byte-aligned, prepare these buffers according to the related protocol .
- Parameters
-
instance | Instance number of the LPI2C module. |
slaveAddr | The slave address to communicate. |
cmdBuff | The pointer to the commands to be transferred, could be NULL. |
cmdSize | The length in bytes of the commands to be transferred, could be 0. |
txBuff | The pointer to the data to be transferred, cannot be NULL. |
txSize | The length in bytes of the data to be transferred, cannot be 0. |
- Returns
- Error or success status returned by API.
LPI2C_Type* const g_lpi2cBase[LPI2C_INSTANCE_COUNT] |