S32 SDK
FlexIO SPI Driver

Detailed Description

SPI communication over FlexIO module (FLEXIO_SPI)

The FLEXIO_SPI Driver allows communication on an SPI bus using the FlexIO module in the S32144K processor.

Features

Functionality

Before using any Flexio driver the device must first be initialized using function FLEXIO_DRV_InitDevice. Then the FLEXIO_SPI Driver must be initialized, using functions FLEXIO_SPI_DRV_MasterInit() or FLEXIO_SPI_DRV_SlaveInit(). It is possible to use more driver instances on the same FlexIO device, as long as sufficient resources are available. Different driver instances on the same FlexIO device can function independently of each other. When it is no longer needed, the driver can be de-initialized, using FLEXIO_SPI_DRV_MasterDeinit() or FLEXIO_SPI_DRV_SlaveDeinit(). This will release the hardware resources, allowing other driver instances to be initialized other.

Master Mode

Master Mode provides functions for transmitting or receiving data to/from an SPI slave. Baud rate is provided at initialization time through the master configuration structure, but can be changed at runtime by using FLEXIO_SPI_DRV_MasterSetBaudRate() function. Note that due to module limitation not any baud rate can be achieved. The driver will set a baud rate as close as possible to the requested baud rate, but there may still be substantial differences, for example if requesting a high baud rate while using a low-frequency FlexIO clock. The application should call FLEXIO_SPI_DRV_MasterGetBaudRate() after FLEXIO_SPI_DRV_MasterSetBaudRate() to check what baud rate was actually set.

To send or receive data, use function FLEXIO_SPI_DRV_MasterTransfer(). The transmit and receive buffers, together with parameters for the transfer are provided through the flexio_spi_transfer_t structure. If only transmit or receive is desired, any one of the Rx/Tx buffers can be set to NULL. This driver does not support continuous send/receive using a user callback function. The callback function is only used to signal the end of a transfer.

Blocking operations will return only when the transfer is completed, either successfully or with error. Non-blocking operations will initiate the transfer and return STATUS_SUCCESS, but the module is still busy with the transfer and another transfer can't be initiated until the current transfer is complete. The application will be notified through the user callback when the transfer completes, or it can check the status of the current transfer by calling FLEXIO_SPI_DRV_MasterGetStatus(). If the transfer is still ongoing this function will return STATUS_BUSY. If the transfer is completed, the function will return either STATUS_SUCCESS or an error code, depending on the outcome of the last transfer.

The driver supports interrupt, DMA and polling mode. In polling mode the function FLEXIO_SPI_DRV_MasterGetStatus() ensures the progress of the transfer by checking and handling transmit and receive events reported by the FlexIO module. The application should ensure that this function is called often enough (at least once per transferred byte)to avoid Tx underflows or Rx overflows. In DMA mode the DMA channels that will be used by the driver are received through the configuration structure. The channels must be initialized by the application before the flexio_spi driver is initialized. The flexio_spi driver will only set the DMA request source.

Slave Mode

Slave Mode is very similar to master mode, the main difference being that the FLEXIO_SPI_DRV_SlaveInit() function initializes the FlexIO module to use the clock signal received from the master instead of generating it. Consequently, there is no SetBaudRate function in slave mode. Other than that, the slave mode offers a similar interface to the master mode. FLEXIO_SPI_DRV_MasterTransfer() can be used to initiate transfers, and FLEXIO_SPI_DRV_SlaveGetStatus() is used to check the status of the transfer and advance the transfer in polling mode. All other specifications from the Master Mode description apply for Slave Mode too

Important Notes

Data Structures

struct  flexio_spi_master_user_config_t
 Master configuration structure. More...
 
struct  flexio_spi_slave_user_config_t
 Slave configuration structure. More...
 
struct  flexio_spi_master_state_t
 Master internal context structure. More...
 

Typedefs

typedef flexio_spi_master_state_t flexio_spi_slave_state_t
 Slave internal context structure. More...
 

Enumerations

enum  flexio_spi_transfer_bit_order_t { FLEXIO_SPI_TRANSFER_MSB_FIRST = 0U, FLEXIO_SPI_TRANSFER_LSB_FIRST = 1U }
 Order in which the data bits are transferred Implements : flexio_spi_transfer_bit_order_t_Class. More...
 
enum  flexio_spi_transfer_size_t { FLEXIO_SPI_TRANSFER_1BYTE = 1U, FLEXIO_SPI_TRANSFER_2BYTE = 2U, FLEXIO_SPI_TRANSFER_4BYTE = 4U }
 Size of transferred data in bytes Implements : flexio_spi_transfer_size_t_Class. More...
 

FLEXIO_SPI Driver

status_t FLEXIO_SPI_DRV_MasterInit (uint32_t instance, const flexio_spi_master_user_config_t *userConfigPtr, flexio_spi_master_state_t *master)
 Initialize the FLEXIO_SPI master mode driver. More...
 
status_t FLEXIO_SPI_DRV_MasterDeinit (flexio_spi_master_state_t *master)
 De-initialize the FLEXIO_SPI master mode driver. More...
 
status_t FLEXIO_SPI_DRV_MasterSetBaudRate (flexio_spi_master_state_t *master, uint32_t baudRate)
 Set the baud rate for any subsequent SPI communication. More...
 
status_t FLEXIO_SPI_DRV_MasterGetBaudRate (flexio_spi_master_state_t *master, uint32_t *baudRate)
 Get the currently configured baud rate. More...
 
status_t FLEXIO_SPI_DRV_MasterTransfer (flexio_spi_master_state_t *master, const uint8_t *txData, uint8_t *rxData, uint32_t dataSize)
 Perform a non-blocking SPI master transaction. More...
 
status_t FLEXIO_SPI_DRV_MasterTransferBlocking (flexio_spi_master_state_t *master, const uint8_t *txData, uint8_t *rxData, uint32_t dataSize, uint32_t timeout)
 Perform a blocking SPI master transaction. More...
 
status_t FLEXIO_SPI_DRV_MasterTransferAbort (flexio_spi_master_state_t *master)
 Aborts a non-blocking SPI master transaction. More...
 
status_t FLEXIO_SPI_DRV_MasterGetStatus (flexio_spi_master_state_t *master, uint32_t *bytesRemaining)
 Get the status of the current non-blocking SPI master transaction. More...
 
status_t FLEXIO_SPI_DRV_SlaveInit (uint32_t instance, const flexio_spi_slave_user_config_t *userConfigPtr, flexio_spi_slave_state_t *slave)
 Initialize the FLEXIO_SPI slave mode driver. More...
 
static status_t FLEXIO_SPI_DRV_SlaveDeinit (flexio_spi_slave_state_t *slave)
 De-initialize the FLEXIO_SPI slave mode driver. More...
 
static status_t FLEXIO_SPI_DRV_SlaveTransfer (flexio_spi_slave_state_t *slave, const uint8_t *txData, uint8_t *rxData, uint32_t dataSize)
 Perform a non-blocking SPI slave transaction. More...
 
static status_t FLEXIO_SPI_DRV_SlaveTransferBlocking (flexio_spi_slave_state_t *slave, const uint8_t *txData, uint8_t *rxData, uint32_t dataSize, uint32_t timeout)
 Perform a blocking SPI slave transaction. More...
 
static status_t FLEXIO_SPI_DRV_SlaveTransferAbort (flexio_spi_slave_state_t *slave)
 Aborts a non-blocking SPI slave transaction. More...
 
static status_t FLEXIO_SPI_DRV_SlaveGetStatus (flexio_spi_slave_state_t *slave, uint32_t *bytesRemaining)
 Get the status of the current non-blocking SPI slave transaction. More...
 

Typedef Documentation

Slave internal context structure.

This structure is used by the slave-mode driver for its internal logic. It must be provided by the application through the FLEXIO_SPI_DRV_SlaveInit() function, then it cannot be freed until the driver is de-initialized using FLEXIO_SPI_DRV_SlaveDeinit(). The application should make no assumptions about the content of this structure.

Definition at line 158 of file flexio_spi_driver.h.

Enumeration Type Documentation

Order in which the data bits are transferred Implements : flexio_spi_transfer_bit_order_t_Class.

Enumerator
FLEXIO_SPI_TRANSFER_MSB_FIRST 

Transmit data starting with most significant bit

FLEXIO_SPI_TRANSFER_LSB_FIRST 

Transmit data starting with least significant bit

Definition at line 42 of file flexio_spi_driver.h.

Size of transferred data in bytes Implements : flexio_spi_transfer_size_t_Class.

Enumerator
FLEXIO_SPI_TRANSFER_1BYTE 

Data size is 1-byte

FLEXIO_SPI_TRANSFER_2BYTE 

Data size is 2-bytes

FLEXIO_SPI_TRANSFER_4BYTE 

Data size is 4-bytes

Definition at line 51 of file flexio_spi_driver.h.

Function Documentation

status_t FLEXIO_SPI_DRV_MasterDeinit ( flexio_spi_master_state_t master)

De-initialize the FLEXIO_SPI master mode driver.

This function de-initializes the FLEXIO_SPI driver in master mode. The driver can't be used again until reinitialized. The context structure is no longer needed by the driver and can be freed after calling this function.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
Returns
Error or success status returned by API

Definition at line 985 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterGetBaudRate ( flexio_spi_master_state_t master,
uint32_t *  baudRate 
)

Get the currently configured baud rate.

This function returns the currently configured SPI baud rate.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
baudRatethe current baud rate in hertz
Returns
Error or success status returned by API

Definition at line 1051 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterGetStatus ( flexio_spi_master_state_t master,
uint32_t *  bytesRemaining 
)

Get the status of the current non-blocking SPI master transaction.

This function returns the current status of a non-blocking SPI master transaction. A return code of STATUS_BUSY means the transfer is still in progress. Otherwise the function returns a status reflecting the outcome of the last transfer. When the driver is initialized in polling mode this function also advances the transfer by checking and handling the transmit and receive events, so it must be called frequently to avoid overflows or underflows.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
bytesRemainingthe remaining number of bytes to be transferred
Returns
Error or success status returned by API

Definition at line 1204 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterInit ( uint32_t  instance,
const flexio_spi_master_user_config_t userConfigPtr,
flexio_spi_master_state_t master 
)

Initialize the FLEXIO_SPI master mode driver.

This function initializes the FLEXIO_SPI driver in master mode.

Parameters
instanceFLEXIO peripheral instance number
userConfigPtrPointer to the FLEXIO_SPI master user configuration structure. The function reads configuration data from this structure and initializes the driver accordingly. The application may free this structure after the function returns.
masterPointer to the FLEXIO_SPI master driver context structure. The driver uses this memory area for its internal logic. The application must make no assumptions about the content of this structure, and must not free this memory until the driver is de-initialized using FLEXIO_SPI_DRV_MasterDeinit().
Returns
Error or success status returned by API

Definition at line 896 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterSetBaudRate ( flexio_spi_master_state_t master,
uint32_t  baudRate 
)

Set the baud rate for any subsequent SPI communication.

This function sets the baud rate for the SPI master. Note that due to module limitation not any baud rate can be achieved. The driver will set a baud rate as close as possible to the requested baud rate, but there may still be substantial differences, for example if requesting a high baud rate while using a low-frequency FlexIO clock. The application should call FLEXIO_SPI_DRV_MasterGetBaudRate() after FLEXIO_SPI_DRV_MasterSetBaudRate() to check what baud rate was actually set.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
baudRatethe desired baud rate in hertz
Returns
Error or success status returned by API

Definition at line 1009 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterTransfer ( flexio_spi_master_state_t master,
const uint8_t *  txData,
uint8_t *  rxData,
uint32_t  dataSize 
)

Perform a non-blocking SPI master transaction.

This function performs an SPI full-duplex transaction, transmit and receive in parallel. If only transmit or receive are required, it is possible to provide NULL pointers for txData or rxData. The transfer is non-blocking, the function only initiates the transfer and then returns, leaving the transfer to complete asynchronously). FLEXIO_SPI_DRV_MasterGetStatus() can be called to check the status of the transfer.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
txDatapointer to the data to be transmitted
rxDatapointer to the buffer where to store received data
dataSizelength in bytes of the data to be transferred
Returns
Error or success status returned by API

Definition at line 1091 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterTransferAbort ( flexio_spi_master_state_t master)

Aborts a non-blocking SPI master transaction.

This function aborts a non-blocking SPI transfer.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
Returns
Error or success status returned by API

Definition at line 1186 of file flexio_spi_driver.c.

status_t FLEXIO_SPI_DRV_MasterTransferBlocking ( flexio_spi_master_state_t master,
const uint8_t *  txData,
uint8_t *  rxData,
uint32_t  dataSize,
uint32_t  timeout 
)

Perform a blocking SPI master transaction.

This function performs an SPI full-duplex transaction, transmit and receive in parallel. If only transmit or receive are required, it is possible to provide NULL pointers for txData or rxData. The transfer is blocking, the function only returns when the transfer is complete.

Parameters
masterPointer to the FLEXIO_SPI master driver context structure.
txDatapointer to the data to be transmitted
rxDatapointer to the buffer where to store received data
dataSizelength in bytes of the data to be transferred
timeouttimeout for the transfer in milliseconds
Returns
Error or success status returned by API

Definition at line 1153 of file flexio_spi_driver.c.

static status_t FLEXIO_SPI_DRV_SlaveDeinit ( flexio_spi_slave_state_t slave)
inlinestatic

De-initialize the FLEXIO_SPI slave mode driver.

This function de-initializes the FLEXIO_SPI driver in slave mode. The driver can't be used again until reinitialized. The context structure is no longer needed by the driver and can be freed after calling this function.

Parameters
slavePointer to the FLEXIO_SPI slave driver context structure.
Returns
Error or success status returned by API Implements : FLEXIO_SPI_DRV_SlaveDeinit_Activity

Definition at line 341 of file flexio_spi_driver.h.

static status_t FLEXIO_SPI_DRV_SlaveGetStatus ( flexio_spi_slave_state_t slave,
uint32_t *  bytesRemaining 
)
inlinestatic

Get the status of the current non-blocking SPI slave transaction.

This function returns the current status of a non-blocking SPI slave transaction. A return code of STATUS_BUSY means the transfer is still in progress. Otherwise the function returns a status reflecting the outcome of the last transfer. When the driver is initialized in polling mode this function also advances the transfer by checking and handling the transmit and receive events, so it must be called frequently to avoid overflows or underflows.

Parameters
slavePointer to the FLEXIO_SPI slave driver context structure.
bytesRemainingthe remaining number of bytes to be transferred
Returns
Error or success status returned by API Implements : FLEXIO_SPI_DRV_SlaveGetStatus_Activity

Definition at line 428 of file flexio_spi_driver.h.

status_t FLEXIO_SPI_DRV_SlaveInit ( uint32_t  instance,
const flexio_spi_slave_user_config_t userConfigPtr,
flexio_spi_slave_state_t slave 
)

Initialize the FLEXIO_SPI slave mode driver.

This function initializes the FLEXIO_SPI driver in slave mode.

Parameters
instanceFLEXIO peripheral instance number
userConfigPtrPointer to the FLEXIO_SPI slave user configuration structure. The function reads configuration data from this structure and initializes the driver accordingly. The application may free this structure after the function returns.
slavePointer to the FLEXIO_SPI slave driver context structure. The driver uses this memory area for its internal logic. The application must make no assumptions about the content of this structure, and must not free this memory until the driver is de-initialized using FLEXIO_SPI_DRV_SlaveDeinit().
Returns
Error or success status returned by API

Definition at line 1231 of file flexio_spi_driver.c.

static status_t FLEXIO_SPI_DRV_SlaveTransfer ( flexio_spi_slave_state_t slave,
const uint8_t *  txData,
uint8_t *  rxData,
uint32_t  dataSize 
)
inlinestatic

Perform a non-blocking SPI slave transaction.

This function performs an SPI full-duplex transaction, transmit and receive in parallel. If only transmit or receive are required, it is possible to provide NULL pointers for txData or rxData. The transfer is non-blocking, the function only initiates the transfer and then returns, leaving the transfer to complete asynchronously). FLEXIO_SPI_DRV_SlaveGetStatus() can be called to check the status of the transfer.

Parameters
slavePointer to the FLEXIO_SPI slave driver context structure.
txDatapointer to the data to be transmitted
rxDatapointer to the buffer where to store received data
dataSizelength in bytes of the data to be transferred
Returns
Error or success status returned by API Implements : FLEXIO_SPI_DRV_SlaveTransfer_Activity

Definition at line 363 of file flexio_spi_driver.h.

static status_t FLEXIO_SPI_DRV_SlaveTransferAbort ( flexio_spi_slave_state_t slave)
inlinestatic

Aborts a non-blocking SPI slave transaction.

This function aborts a non-blocking SPI transfer.

Parameters
slavePointer to the FLEXIO_SPI slave driver context structure.
Returns
Error or success status returned by API Implements : FLEXIO_SPI_DRV_SlaveTransferAbort_Activity

Definition at line 407 of file flexio_spi_driver.h.

static status_t FLEXIO_SPI_DRV_SlaveTransferBlocking ( flexio_spi_slave_state_t slave,
const uint8_t *  txData,
uint8_t *  rxData,
uint32_t  dataSize,
uint32_t  timeout 
)
inlinestatic

Perform a blocking SPI slave transaction.

This function performs an SPI full-duplex transaction, transmit and receive in parallel. If only transmit or receive are required, it is possible to provide NULL pointers for txData or rxData. The transfer is blocking, the function only returns when the transfer is complete.

Parameters
slavePointer to the FLEXIO_SPI slave driver context structure.
txDatapointer to the data to be transmitted
rxDatapointer to the buffer where to store received data
dataSizelength in bytes of the data to be transferred
timeouttimeout for the transfer in milliseconds
Returns
Error or success status returned by API Implements : FLEXIO_SPI_DRV_SlaveTransferBlocking_Activity

Definition at line 388 of file flexio_spi_driver.h.