The MCUXpresso SDK provides a peripheral driver for I2C master function using Flexible I/O module of MCUXpresso SDK devices.
The FlexIO I2C master driver includes functional APIs and transactional APIs.
Functional APIs target low level APIs. Functional APIs can be used for the FlexIO I2C master initialization/configuration/operation for the optimization/customization purpose. Using the functional APIs requires the knowledge of the FlexIO I2C master peripheral and how to organize functional APIs to meet the application requirements. The FlexIO I2C master functional operation groups provide the functional APIs set.
Transactional APIs target high-level APIs. The transactional APIs can be used to enable the peripheral quickly and also in the application if the code size and performance of transactional APIs satisfy the requirements. If the code size and performance are critical requirements, see the transactional API implementation and write custom code using the functional APIs or accessing the hardware registers.
Transactional APIs support an asynchronous transfer. This means that the functions FLEXIO_I2C_MasterTransferNonBlocking() set up the interrupt non-blocking transfer. When the transfer completes, the upper layer is notified through a callback function with the kStatus_Success status.
Typical use case
FlexIO I2C master transfer using an interrupt method
flexio_i2c_master_handle_t g_m_handle;
volatile bool completionFlag = false;
const uint8_t sendData[] = [......];
{
userData = userData;
if (kStatus_Success == status)
{
completionFlag = true;
}
}
void main(void)
{
masterXfer.
data = &who_am_i_value;
masterXfer.
flags = kI2C_TransferDefaultFlag;
while (!completionFlag)
{
}
}
|
#define | I2C_RETRY_TIMES 0U /* Define to zero means keep waiting until the flag is assert/deassert. */ |
| Retry times for waiting flag. More...
|
|
|
enum | {
kStatus_FLEXIO_I2C_Busy = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 0),
kStatus_FLEXIO_I2C_Idle = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 1),
kStatus_FLEXIO_I2C_Nak = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 2),
kStatus_FLEXIO_I2C_Timeout = MAKE_STATUS(kStatusGroup_FLEXIO_I2C, 3)
} |
| FlexIO I2C transfer status. More...
|
|
enum | _flexio_i2c_master_interrupt {
kFLEXIO_I2C_TxEmptyInterruptEnable = 0x1U,
kFLEXIO_I2C_RxFullInterruptEnable = 0x2U
} |
| Define FlexIO I2C master interrupt mask. More...
|
|
enum | _flexio_i2c_master_status_flags {
kFLEXIO_I2C_TxEmptyFlag = 0x1U,
kFLEXIO_I2C_RxFullFlag = 0x2U,
kFLEXIO_I2C_ReceiveNakFlag = 0x4U
} |
| Define FlexIO I2C master status mask. More...
|
|
enum | flexio_i2c_direction_t {
kFLEXIO_I2C_Write = 0x0U,
kFLEXIO_I2C_Read = 0x1U
} |
| Direction of master transfer. More...
|
|
|
void | FLEXIO_I2C_MasterSetBaudRate (FLEXIO_I2C_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz) |
| Sets the FlexIO I2C master transfer baudrate. More...
|
|
void | FLEXIO_I2C_MasterStart (FLEXIO_I2C_Type *base, uint8_t address, flexio_i2c_direction_t direction) |
| Sends START + 7-bit address to the bus. More...
|
|
void | FLEXIO_I2C_MasterStop (FLEXIO_I2C_Type *base) |
| Sends the stop signal on the bus. More...
|
|
void | FLEXIO_I2C_MasterRepeatedStart (FLEXIO_I2C_Type *base) |
| Sends the repeated start signal on the bus. More...
|
|
void | FLEXIO_I2C_MasterAbortStop (FLEXIO_I2C_Type *base) |
| Sends the stop signal when transfer is still on-going. More...
|
|
void | FLEXIO_I2C_MasterEnableAck (FLEXIO_I2C_Type *base, bool enable) |
| Configures the sent ACK/NAK for the following byte. More...
|
|
status_t | FLEXIO_I2C_MasterSetTransferCount (FLEXIO_I2C_Type *base, uint16_t count) |
| Sets the number of bytes to be transferred from a start signal to a stop signal. More...
|
|
static void | FLEXIO_I2C_MasterWriteByte (FLEXIO_I2C_Type *base, uint32_t data) |
| Writes one byte of data to the I2C bus. More...
|
|
static uint8_t | FLEXIO_I2C_MasterReadByte (FLEXIO_I2C_Type *base) |
| Reads one byte of data from the I2C bus. More...
|
|
status_t | FLEXIO_I2C_MasterWriteBlocking (FLEXIO_I2C_Type *base, const uint8_t *txBuff, uint8_t txSize) |
| Sends a buffer of data in bytes. More...
|
|
status_t | FLEXIO_I2C_MasterReadBlocking (FLEXIO_I2C_Type *base, uint8_t *rxBuff, uint8_t rxSize) |
| Receives a buffer of bytes. More...
|
|
status_t | FLEXIO_I2C_MasterTransferBlocking (FLEXIO_I2C_Type *base, flexio_i2c_master_transfer_t *xfer) |
| Performs a master polling transfer on the I2C bus. More...
|
|
FLEXIO_Type* FLEXIO_I2C_Type::flexioBase |
uint8_t FLEXIO_I2C_Type::SDAPinIndex |
uint8_t FLEXIO_I2C_Type::SCLPinIndex |
uint8_t FLEXIO_I2C_Type::shifterIndex[2] |
uint8_t FLEXIO_I2C_Type::timerIndex[3] |
struct flexio_i2c_master_config_t |
bool flexio_i2c_master_config_t::enableMaster |
bool flexio_i2c_master_config_t::enableInDoze |
bool flexio_i2c_master_config_t::enableInDebug |
bool flexio_i2c_master_config_t::enableFastAccess |
uint32_t flexio_i2c_master_config_t::baudRate_Bps |
struct flexio_i2c_master_transfer_t |
uint32_t flexio_i2c_master_transfer_t::flags |
uint8_t flexio_i2c_master_transfer_t::slaveAddress |
uint32_t flexio_i2c_master_transfer_t::subaddress |
uint8_t flexio_i2c_master_transfer_t::subaddressSize |
uint8_t volatile* flexio_i2c_master_transfer_t::data |
volatile size_t flexio_i2c_master_transfer_t::dataSize |
struct _flexio_i2c_master_handle |
FlexIO I2C master handle typedef.
size_t flexio_i2c_master_handle_t::transferSize |
uint8_t flexio_i2c_master_handle_t::state |
Callback function called at transfer event.
void* flexio_i2c_master_handle_t::userData |
bool flexio_i2c_master_handle_t::needRestart |
#define FSL_FLEXIO_I2C_MASTER_DRIVER_VERSION (MAKE_VERSION(2, 3, 0)) |
#define I2C_RETRY_TIMES 0U /* Define to zero means keep waiting until the flag is assert/deassert. */ |
typedef void(* flexio_i2c_master_transfer_callback_t)(FLEXIO_I2C_Type *base, flexio_i2c_master_handle_t *handle, status_t status, void *userData) |
Enumerator |
---|
kStatus_FLEXIO_I2C_Busy |
I2C is busy doing transfer.
|
kStatus_FLEXIO_I2C_Idle |
I2C is busy doing transfer.
|
kStatus_FLEXIO_I2C_Nak |
NAK received during transfer.
|
kStatus_FLEXIO_I2C_Timeout |
Timeout polling status flags.
|
Enumerator |
---|
kFLEXIO_I2C_TxEmptyInterruptEnable |
Tx buffer empty interrupt enable.
|
kFLEXIO_I2C_RxFullInterruptEnable |
Rx buffer full interrupt enable.
|
Enumerator |
---|
kFLEXIO_I2C_TxEmptyFlag |
Tx shifter empty flag.
|
kFLEXIO_I2C_RxFullFlag |
Rx shifter full/Transfer complete flag.
|
kFLEXIO_I2C_ReceiveNakFlag |
Receive NAK flag.
|
Enumerator |
---|
kFLEXIO_I2C_Write |
Master send to slave.
|
kFLEXIO_I2C_Read |
Master receive from slave.
|
Check the FLEXIO pin status to see whether either of SDA and SCL pin is pulled down.
- Parameters
-
- Return values
-
Example
.SDAPinIndex = 0,
.SCLPinIndex = 1,
.shifterIndex = {0,1},
.timerIndex = {0,1}
};
.enableInDebug = true,
.enableFastAccess = false,
.baudRate_Bps = 100000
};
- Parameters
-
- Return values
-
kStatus_Success | Initialization successful |
kStatus_InvalidArgument | The source clock exceed upper range limitation |
Calling this API Resets the FlexIO I2C master shifer and timer config, module can't work unless the FLEXIO_I2C_MasterInit is called.
- Parameters
-
static void FLEXIO_I2C_MasterEnable |
( |
FLEXIO_I2C_Type * |
base, |
|
|
bool |
enable |
|
) |
| |
|
inlinestatic |
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
enable | Pass true to enable module, false does not have any effect. |
void FLEXIO_I2C_MasterClearStatusFlags |
( |
FLEXIO_I2C_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
mask | Status flag. The parameter can be any combination of the following values:
- kFLEXIO_I2C_RxFullFlag
- kFLEXIO_I2C_ReceiveNakFlag
|
void FLEXIO_I2C_MasterEnableInterrupts |
( |
FLEXIO_I2C_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
mask | Interrupt source. Currently only one interrupt request source:
- kFLEXIO_I2C_TransferCompleteInterruptEnable
|
void FLEXIO_I2C_MasterDisableInterrupts |
( |
FLEXIO_I2C_Type * |
base, |
|
|
uint32_t |
mask |
|
) |
| |
void FLEXIO_I2C_MasterSetBaudRate |
( |
FLEXIO_I2C_Type * |
base, |
|
|
uint32_t |
baudRate_Bps, |
|
|
uint32_t |
srcClock_Hz |
|
) |
| |
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure |
baudRate_Bps | the baud rate value in HZ |
srcClock_Hz | source clock in HZ |
- Note
- This API should be called when the transfer configuration is ready to send a START signal and 7-bit address to the bus. This is a non-blocking API, which returns directly after the address is put into the data register but the address transfer is not finished on the bus. Ensure that the kFLEXIO_I2C_RxFullFlag status is asserted before calling this API.
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
address | 7-bit address. |
direction | transfer direction. This parameter is one of the values in flexio_i2c_direction_t:
- kFLEXIO_I2C_Write: Transmit
- kFLEXIO_I2C_Read: Receive
|
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
enable | True to configure send ACK, false configure to send NAK. |
- Note
- Call this API before a transfer begins because the timer generates a number of clocks according to the number of bytes that need to be transferred.
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
count | Number of bytes need to be transferred from a start signal to a re-start/stop signal |
- Return values
-
kStatus_Success | Successfully configured the count. |
kStatus_InvalidArgument | Input argument is invalid. |
static void FLEXIO_I2C_MasterWriteByte |
( |
FLEXIO_I2C_Type * |
base, |
|
|
uint32_t |
data |
|
) |
| |
|
inlinestatic |
- Note
- This is a non-blocking API, which returns directly after the data is put into the data register but the data transfer is not finished on the bus. Ensure that the TxEmptyFlag is asserted before calling this API.
- Parameters
-
- Note
- This is a non-blocking API, which returns directly after the data is read from the data register. Ensure that the data is ready in the register.
- Parameters
-
- Returns
- data byte read.
- Note
- This function blocks via polling until all bytes have been sent.
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
txBuff | The data bytes to send. |
txSize | The number of data bytes to send. |
- Return values
-
kStatus_Success | Successfully write data. |
kStatus_FLEXIO_I2C_Nak | Receive NAK during writing data. |
kStatus_FLEXIO_I2C_Timeout | Timeout polling status flags. |
- Note
- This function blocks via polling until all bytes have been received.
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
rxBuff | The buffer to store the received bytes. |
rxSize | The number of data bytes to be received. |
- Return values
-
kStatus_Success | Successfully read data. |
kStatus_FLEXIO_I2C_Timeout | Timeout polling status flags. |
- Note
- The API does not return until the transfer succeeds or fails due to receiving NAK.
- Parameters
-
- Returns
- status of status_t.
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
handle | Pointer to flexio_i2c_master_handle_t structure to store the transfer state. |
callback | Pointer to user callback function. |
userData | User param passed to the callback function. |
- Return values
-
kStatus_Success | Successfully create the handle. |
kStatus_OutOfRange | The FlexIO type/handle/isr table out of range. |
- Note
- The API returns immediately after the transfer initiates. Call FLEXIO_I2C_MasterGetTransferCount to poll the transfer status to check whether the transfer is finished. If the return status is not kStatus_FLEXIO_I2C_Busy, the transfer is finished.
- Parameters
-
- Return values
-
kStatus_Success | Successfully start a transfer. |
kStatus_FLEXIO_I2C_Busy | FlexIO I2C is not idle, is running another transfer. |
status_t FLEXIO_I2C_MasterTransferGetCount |
( |
FLEXIO_I2C_Type * |
base, |
|
|
flexio_i2c_master_handle_t * |
handle, |
|
|
size_t * |
count |
|
) |
| |
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure. |
handle | Pointer to flexio_i2c_master_handle_t structure which stores the transfer state. |
count | Number of bytes transferred so far by the non-blocking transaction. |
- Return values
-
kStatus_InvalidArgument | count is Invalid. |
kStatus_Success | Successfully return the count. |
void FLEXIO_I2C_MasterTransferAbort |
( |
FLEXIO_I2C_Type * |
base, |
|
|
flexio_i2c_master_handle_t * |
handle |
|
) |
| |
- Note
- This API can be called at any time when an interrupt non-blocking transfer initiates to abort the transfer early.
- Parameters
-
base | Pointer to FLEXIO_I2C_Type structure |
handle | Pointer to flexio_i2c_master_handle_t structure which stores the transfer state |
void FLEXIO_I2C_MasterTransferHandleIRQ |
( |
void * |
i2cType, |
|
|
void * |
i2cHandle |
|
) |
| |