Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
FlexIO I2C Master peripheral driver

This section describes the programming interface of the FlexIO I2C master mode peripheral driver. The FlexIO I2C master peripheral driver provides functions for a master device to send and receive data.

FlexIO I2C Device structures

The driver uses an instantiation of the flexio_i2c_state_t structure to maintain the current state of a particular FlexIO-simulated I2C master driver. This structures holds data that is used by the FlexIO-simulated I2C master peripheral driver to communicate between the transmit and receive transfer functions and the interrupt handler. The interrupt handler also uses this information to keep track of its progress. Because the driver itself does not statically allocate memory, the caller provides memory for the driver state structure during initialization by providing a structure.

FlexIO I2C user configuration structures

The FlexIO-simulated I2C driver uses instances of the user configuration structure flexio_i2c_userconfig_t for the FlexIO-simulated I2C driver. The user settings include: I2C mode(master or slave), baud rate, and the associated FlexIO timers, shifters, and pins for SDA and SCL.

FlexIO I2C Master Initialization

  1. To initialize the FlexIO I2C Master driver, call the FLEXIO_I2C_DRV_MasterInit() function and pass the instance number of the FlexIO peripheral, memory for the run-time state structure, and a pointer to the user configuration structure. For example, to use FlexIO0 pass a value of 0 to the initialization function.
  2. Then, pass the memory for the run-time state structure.
  3. Finally, pass a user configuration structure of the type flexio_i2c_userconfig_t as shown here:
// FlexIO I2C Master configuration structure
typedef struct flexio_i2c_userconfig{
uint32_t baudRate;
flexio_i2c_hwconfig_t i2cHwConfig;
}flexio_i2c_userconfig_t;

The flexio_i2c_userconfig instantiation can be easily modified to configure the FlexIO I2C master peripheral driver either to a different baud rate or using different FlexIO hardware resource. This is an example code to set up a user FlexIO I2C master configuration instantiation:

flexio_i2c_userconif_t i2cMasterConfig;
i2cMasterConfig.baudRate = 100000;
i2cMasterConfig.i2cHwConfig.sdaPinIdx = 0;
i2cMasterConfig.i2cHwConfig.sclkPinIdx = 1;
i2cMasterConfig.i2cHwConfig.shifterIdx[0] = 0;
i2cMasterConfig.i2cHwConfig.shifterIdx[1] = 1;
i2cMasterConfig.i2cHwConfig.timerIdx[0] = 0;
i2cMasterConfig.i2cHwConfig.timerIdx[1] = 1;

This example shows how to call the FLEXIO_I2C_DRV_MasterInit() given the user configuration structure and the FlexIO instance 0.

uint32_t flexioInstance = 0;
flexio_i2c_state_t i2cState; // user provides memory for the driver state structure
FLEXIO_I2C_DRV_MasterInit(flexioInstance, &i2cState, &i2cMasterConfig);

FlexIO I2C Master Data Transactions

The driver implements transmit and receive functions to transfer buffers of data in blocking and non-blocking modes.

The non-blocking transmit and receive functions include the FLEXIO_I2C_DRV_MasterSendData() and FLEXIO_I2C_DRV_MasterReceiveData() functions.

The blocking (async) transmit and receive functions include the FLEXIO_I2C_DRV_MasterSendDataBlocking() and FLEXIO_I2C_DRV_MasterReceiveDataBlocking() functions.

In all cases mentioned here, the functions are interrupt-driven.

This code example show how to use the functions, assuming that the FlexIO-simulated I2C Master module has been initialized as described previously in the Initialization Section.

This is an example code for blocking transfer functions transmit and receive:

uint8_t sourceBuff[26] ={0}; // sourceBuff can be filled out with desired data
uint8_t readBuffer[10] = {0}; // readBuffer gets filled
uint32_t byteCount = sizeof(sourceBuff);
uint32_t rxRemainingSize = sizeof(readBuffer);
// For each use there, set timeout as "1"
// i2cState is the run-time state. Pass in memory for this
// declared previously in the initialization section
FLEXIO_I2C_DRV_MasterSendDataBlocking(i2cState, sourceBuff, byteCount, 1); // Function won't return until transmit is complete
FLEXIO_I2C_DRV_MasterReceiveDataBlocking(i2cState, readBuffer, 1, timeoutValue); // Function won't return until it receives all data

This is an example code for non-blocking (async) transfer functions transmit and receive:

uint8_t *pTxBuff;
uint8_t rxBuff[10];
uint32_t txBytesRemaining, rxBytesRemaining;
// Assume pTxBuff and txSize have been initialized
FLEXIO_I2C_DRV_MasterSendData(i2cState, pTxBuff, txSize);
// Check on status of transmit and wait until done; The code can do something else and
// check back later
while (FLEXIO_I2C_DRV_MasterGetTransmitStatus(i2cState, &txBytesRemaining) == kStatus_FlexIO_I2C_XBusy);
// To receive, assume rxBuff is set up to receive data and rxSize is initialized
FLEXIO_I2C_DRV_MasterReceiveData(i2cState, rxBuff, rxSize);
// Check on receive status and wait until done; The code can do something else and
// check back later
while (FLEXIO_I2C_DRV_MasterGetReceiveStatus(i2cState, &rxBytesRemaining) == kStatus_FlexIO_I2C_XBusy);