on
2021-07-22
10:02 AM
- edited on
2024-06-04
05:29 AM
by
Laurids_PETERSE
The data processing is handled internally in a loop. A timeout (expressed in ms) is used to prevent process hanging.
The operation is considered complete when the function returns the HAL_OK status, otherwise an error status is returned.
The HAL function returns the process status after starting the data processing and enabling the appropriate interruption.
The end of the operation is indicated by a callback function: either transmit / receive complete or error.
The callback functions are declared as weak functions in the driver, with no code to execute. This means that the user can declare the Tx / Rx callback again in the application and customize it to be informed in real-time about the process completion and to execute some application code.
The principle is the same as in interrupt mode except that the DMA manages the data buffer transmission entirely. It continuously uses the data buffer to copy data to / from the USART data register until the DMA counter reaches 0.
When the DMA HT (half transfer) or TC (transfer complete) interrupt occurs, the UART half transfer callback or the transfer complete callback is called. Like in interrupt mode, the weak default callback executes no code. It can be declared and customized in the application.
In transmit direction, the DMA TC event is triggered when the last data has been written to the UART data register but not yet fully transmitted out of the UART (The TC event is triggered by the DMA).
In circular buffer mode, the HAL_UART_TxCpltCallback is called from the HAL_DMA_IRQHandler.
In normal buffer mode, the HAL_UART_TxCpltCallback is not called from the HAL_DMA_IRQHandler. Instead, the UART transmit complete (UART TC) interrupt is enabled.
In this case, the HAL_UART_TxCpltCallback will be called from the HAL_UART_IRQHandler when the last data is fully transmitted.
Stopping an on-going transfer is possible only in non-blocking modes, interrupt or DMA.
Either both transfers (TX and RX) are aborted or the transmit or receive channel can be selected.
If necessary, a callback can be called to execute some user application code.
Finally, specifically when a DMA is used, the transfer can be paused, resumed or stopped (aborted) using:
HAL_UART_DMAPause() / HAL_UART_DMAResume() / HAL_UART_DMAStop()
Thank you for sharing.