Showing results for 
Search instead for 
Did you mean: 

Serial UART design for various size transfers

David Pekin


I'm writing an application on the STM32F303 Nucleo board that uses the serial UART to receive commands and send responses. I believe example code from the standard peripherals library shows either single byte transfers or fixed size buffer transfers. That is, the UART receive interrupt is fired only after N bytes are received. But N is fixed... This doesn't work for the case where you may be receiving a variable number of characters in your input stream to make up a command.

I've got a system limping along asking for and receiving a single character interrupt at a time but this is less than a reasonable solution and it fails randomly with a HAL_UART_ErrorCallback as other things are going on in the Nucleo code. The error occurs much more frequently when I am simultaneously reading/writing the I2C bus.

Is there a way to peek into the RX UART handle and see how many characters are in the RX buffer and grab them? Polling the number of characters received at the top of the main loop and pulling them into my own circular buffer would probably work more reliably than single character interrupts.

Any thoughts on the best solution?




Ah... That sounds pretty straight forward then So setup a DMA RX buffer of appropriate size. Start DMA and it will fill the buffer character by character and will automatically wrap back to the start of the buffer when it reaches the end. No need to do anything with DMA interrupts or the usart handle after starting DMA. Just monitor the buffer for new characters. Is there any need to synchronize when I'm looking at the RX buffer? Good point of using a 16bit buffer and setting to 0xfff to know when it is updated.

Thanks again.