UART communication over FlexIO module (FLEXIO_UART)
The FLEXIO_UART Driver allows UART communication using the FlexIO module in the S32K1xx processors.
Before using any Flexio driver the device must first be initialized using function FLEXIO_DRV_InitDevice. Then the FLEXIO_UART Driver must be initialized, using function FLEXIO_UART_DRV_Init(). 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_UART_DRV_Deinit(). This will release the hardware resources, allowing other driver instances to be initialized.
To initialize the UART driver in transmit / receive mode the direction
field of the configuration structure must be set to FLEXIO_UART_DIRECTION_TX
/ FLEXIO_UART_DIRECTION_RX
when calling FLEXIO_UART_DRV_Init(). Once configured for one direction the driver must be used only for the chosen direction until it is de-initialized. One driver instance can only work in one direction at a time, but more driver instances can be created on the same device, up to the number of shifters present on the device (for example on S32K144 up to 4 driver instances can run in parallel on one device).
The baud rate and bit count are provided at initialization time through the master configuration structure, but they can be changed at runtime by using function FLEXIO_UART_DRV_SetConfig(). 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_UART_DRV_GetBaudRate() to check what baud rate was actually set.
To send or receive data to/from the currently configured slave address, use functions FLEXIO_UART_DRV_SendData() or FLEXIO_UART_DRV_ReceiveData() (or their blocking counterparts). Continuous send/receive can be realized by registering a user callback function. When the driver completes the transmission or reception of the current buffer, it will invoke the user callback with an appropriate event. The callback function can the use FLEXIO_UART_DRV_SetTxBuffer() orFLEXIO_UART_DRV_SetRxBuffer() to provide a new buffer.
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_UART_DRV_GetStatus(). 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_UART_DRV_GetStatus() 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 channel that will be used by the driver is received through the configuration structure. The channel must be initialized by the application before the flexio_uart driver is initialized. The flexio_uart driver will only set the DMA request source.
Data Structures | |
struct | flexio_uart_user_config_t |
Driver configuration structure. More... | |
struct | flexio_uart_state_t |
Driver internal context structure. More... | |
Enumerations | |
enum | flexio_uart_driver_direction_t { FLEXIO_UART_DIRECTION_TX = 0x01U, FLEXIO_UART_DIRECTION_RX = 0x00U } |
flexio_uart driver direction (tx or rx) More... | |
FLEXIO_UART Driver | |
status_t | FLEXIO_UART_DRV_Init (uint32_t instance, const flexio_uart_user_config_t *userConfigPtr, flexio_uart_state_t *state) |
Initialize the FLEXIO_UART driver. More... | |
status_t | FLEXIO_UART_DRV_Deinit (flexio_uart_state_t *state) |
De-initialize the FLEXIO_UART driver. More... | |
status_t | FLEXIO_UART_DRV_SetConfig (flexio_uart_state_t *state, uint32_t baudRate, uint8_t bitCount) |
Set the baud rate and bit width for any subsequent UART communication. More... | |
status_t | FLEXIO_UART_DRV_GetBaudRate (flexio_uart_state_t *state, uint32_t *baudRate) |
Get the currently configured baud rate. More... | |
status_t | FLEXIO_UART_DRV_SendDataBlocking (flexio_uart_state_t *state, const uint8_t *txBuff, uint32_t txSize, uint32_t timeout) |
Perform a blocking UART transmission. More... | |
status_t | FLEXIO_UART_DRV_SendData (flexio_uart_state_t *state, const uint8_t *txBuff, uint32_t txSize) |
Perform a non-blocking UART transmission. More... | |
status_t | FLEXIO_UART_DRV_ReceiveDataBlocking (flexio_uart_state_t *state, uint8_t *rxBuff, uint32_t rxSize, uint32_t timeout) |
Perform a blocking UART reception. More... | |
status_t | FLEXIO_UART_DRV_ReceiveData (flexio_uart_state_t *state, uint8_t *rxBuff, uint32_t rxSize) |
Perform a non-blocking UART reception. More... | |
status_t | FLEXIO_UART_DRV_GetStatus (flexio_uart_state_t *state, uint32_t *bytesRemaining) |
Get the status of the current non-blocking UART transfer. More... | |
status_t | FLEXIO_UART_DRV_TransferAbort (flexio_uart_state_t *state) |
Aborts a non-blocking UART transfer. More... | |
status_t | FLEXIO_UART_DRV_SetRxBuffer (flexio_uart_state_t *state, uint8_t *rxBuff, uint32_t rxSize) |
Provide a buffer for receiving data. More... | |
status_t | FLEXIO_UART_DRV_SetTxBuffer (flexio_uart_state_t *state, const uint8_t *txBuff, uint32_t txSize) |
Provide a buffer for transmitting data. More... | |
flexio_uart driver direction (tx or rx)
This structure describes the direction configuration options for the flexio_uart driver. Implements : flexio_uart_driver_direction_t_Class
Enumerator | |
---|---|
FLEXIO_UART_DIRECTION_TX |
Tx UART driver |
FLEXIO_UART_DIRECTION_RX |
Rx UART driver |
Definition at line 45 of file flexio_uart_driver.h.
status_t FLEXIO_UART_DRV_Deinit | ( | flexio_uart_state_t * | state | ) |
De-initialize the FLEXIO_UART driver.
This function de-initializes the FLEXIO_UART driver. 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.
state | Pointer to the FLEXIO_UART driver context structure. |
Definition at line 1044 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_GetBaudRate | ( | flexio_uart_state_t * | state, |
uint32_t * | baudRate | ||
) |
Get the currently configured baud rate.
This function returns the currently configured UART baud rate.
state | Pointer to the FLEXIO_UART driver context structure. |
baudRate | the current baud rate in hertz |
Definition at line 1121 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_GetStatus | ( | flexio_uart_state_t * | state, |
uint32_t * | bytesRemaining | ||
) |
Get the status of the current non-blocking UART transfer.
This function returns the current status of a non-blocking UART transfer. 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.
state | Pointer to the FLEXIO_UART driver context structure. |
bytesRemaining | the remaining number of bytes to be transferred |
Definition at line 1375 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_Init | ( | uint32_t | instance, |
const flexio_uart_user_config_t * | userConfigPtr, | ||
flexio_uart_state_t * | state | ||
) |
Initialize the FLEXIO_UART driver.
This function initializes the FLEXIO_UART driver.
instance | FLEXIO peripheral instance number |
userConfigPtr | Pointer to the FLEXIO_UART 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. |
state | Pointer to the FLEXIO_UART 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_UART_DRV_Deinit(). |
Definition at line 939 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_ReceiveData | ( | flexio_uart_state_t * | state, |
uint8_t * | rxBuff, | ||
uint32_t | rxSize | ||
) |
Perform a non-blocking UART reception.
This function receives a block of data and returns immediately. The rest of the transmission is handled by the interrupt service routine (if the driver is initialized in interrupt mode) or by the FLEXIO_UART_DRV_GetReceiveStatus() function (if the driver is initialized in polling mode).
state | Pointer to the FLEXIO_UART driver context structure. |
rxBuff | pointer to the receive buffer |
rxSize | length in bytes of the data to be received |
Definition at line 1257 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_ReceiveDataBlocking | ( | flexio_uart_state_t * | state, |
uint8_t * | rxBuff, | ||
uint32_t | rxSize, | ||
uint32_t | timeout | ||
) |
Perform a blocking UART reception.
This function receives a block of data and only returns when the transmission is complete.
state | Pointer to the FLEXIO_UART driver context structure. |
rxBuff | pointer to the receive buffer |
rxSize | length in bytes of the data to be received |
timeout | timeout for the transfer in milliseconds |
Definition at line 1318 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_SendData | ( | flexio_uart_state_t * | state, |
const uint8_t * | txBuff, | ||
uint32_t | txSize | ||
) |
Perform a non-blocking UART transmission.
This function sends a block of data and returns immediately. The rest of the transmission is handled by the interrupt service routine (if the driver is initialized in interrupt mode) or by the FLEXIO_UART_DRV_GetTransmitStatus() function (if the driver is initialized in polling mode).
state | Pointer to the FLEXIO_UART driver context structure. |
txBuff | pointer to the data to be transferred |
txSize | length in bytes of the data to be transferred |
Definition at line 1161 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_SendDataBlocking | ( | flexio_uart_state_t * | state, |
const uint8_t * | txBuff, | ||
uint32_t | txSize, | ||
uint32_t | timeout | ||
) |
Perform a blocking UART transmission.
This function sends a block of data and only returns when the transmission is complete.
state | Pointer to the FLEXIO_UART driver context structure. |
txBuff | pointer to the data to be transferred |
txSize | length in bytes of the data to be transferred |
timeout | timeout for the transfer in milliseconds |
Definition at line 1224 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_SetConfig | ( | flexio_uart_state_t * | state, |
uint32_t | baudRate, | ||
uint8_t | bitCount | ||
) |
Set the baud rate and bit width for any subsequent UART communication.
This function sets the baud rate and bit width for the UART driver. 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_UART_DRV_GetBaudRate() after FLEXIO_UART_DRV_SetConfig() to check what baud rate was actually set.
state | Pointer to the FLEXIO_UART driver context structure. |
baudRate | the desired baud rate in hertz |
bitCount | number of bits per word |
Definition at line 1071 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_SetRxBuffer | ( | flexio_uart_state_t * | state, |
uint8_t * | rxBuff, | ||
uint32_t | rxSize | ||
) |
Provide a buffer for receiving data.
This function can be used to provide a new buffer for receiving data to the driver. It can be called from the user callback when event STATUS_UART_RX_OVERRUN is reported. This way the reception will continue without interruption.
state | Pointer to the FLEXIO_UART driver context structure. |
rxBuff | pointer to the buffer where to store received data |
rxSize | length in bytes of the data to be transferred |
Definition at line 1428 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_SetTxBuffer | ( | flexio_uart_state_t * | state, |
const uint8_t * | txBuff, | ||
uint32_t | txSize | ||
) |
Provide a buffer for transmitting data.
This function can be used to provide a new buffer for transmitting data to the driver. It can be called from the user callback when event STATUS_UART_TX_UNDERRUN is reported. This way the transmission will continue without interruption.
state | Pointer to the FLEXIO_UART driver context structure. |
txBuff | pointer to the buffer containing transmit data |
txSize | length in bytes of the data to be transferred |
Definition at line 1450 of file flexio_uart_driver.c.
status_t FLEXIO_UART_DRV_TransferAbort | ( | flexio_uart_state_t * | state | ) |
Aborts a non-blocking UART transfer.
This function aborts a non-blocking UART transfer.
state | Pointer to the FLEXIO_UART driver context structure. |
Definition at line 1351 of file flexio_uart_driver.c.