2025-02-05 07:54 PM
When initialising a UART using CubeMX generated code, either the receiver or transmitter or both are enabled. Typically that means that before receiving data with the UART for the first time, the RDR register should be cleared (or the RXFRQ function used), to clear out any junk that got read during boot.
But if using DMA RX mode I've discovered an extra consideration - if an error is received before calling HAL_UART_Receive_DMA() for the first time, the error handler will be called. The default HAL_UART_IRQHandler() (at least in stm32u5xx_hal_uart.c) aborts the DMA RX on error (regardless of the DDRE setting). So the user is required to restart if necessary.
This leads to an awkward situation where the error handler (which has to restart DMA RX) can be called before HAL_UART_Receive_DMA() is called for the first time. Rather that handling that strange edge case, it seems prudent to eliminate the undesirable order of operations.
Specifically, is there a sensible way to configure the UART with CubeMX, but then inhibit it (just the receiver part would do, I think) until we're ready to start receiving? I could enable transmit only in CubeMX, which would prevent RE being set. But then HAL_UART_Receive_DMA() doesn't set it, so doing so would be custom.
It seems to me that given the design of the HAL_UART_Receive* interface, that it would never be desirable to have the receiver enabled prior to them being called, so I wonder if there's a more logical way to go about this.