Skip to main content
bakhti
Associate III
January 3, 2021
Question

STM32 UART Rx with unknown length

  • January 3, 2021
  • 2 replies
  • 1166 views

hi i have a problem with my uart program the thing is i have to determine the buffer size of the rx i'll call it rx_buff now i have multiple reception well different messages sizes some times 50bytes some times 2bytes but the problem is that when sending the 2 bytes the call back doesnt trigger until the full 50bytes are full until the rx_buff is full meaning i have to send the 2bytes message 25 times to get a callback is there a way to receive unknown messages with different length ?

using the stm32H750

thanks in advance

This topic has been closed for replies.

2 replies

Danish1
Lead III
January 3, 2021

If you can guarantee there will be a pause after your 2-byte short message, then you could consider using the "Receiver timeout interrupt" present in the USARTs/UARTs. No I don't know how to do that from ST's software layers; I access the registers directly.

What I tend to do is arrange a DMA-driven circular receive-buffer, and then poll that.

Hope this helps,

Danish

AKall.2
Visitor II
September 22, 2022

No DMA:

Have a buffer with size bigger than your incoming data may be. After init, enable UART RX Timeout by:

HAL_UART_ReceiverTimeout_Config(&huart1,35);// for me, 35=37ms for baud9600
HAL_UART_EnableReceiverTimeout(&huart1);

Then insert your UART ERROR callback and include inside it the RTO:

void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
 {
 if(UartHandle.ErrorCode & (HAL_UART_ERROR_RTO)){
 RXcount=UartHandle.RxXferSize-UartHandle.RxXferCount;
 /* Set transmission flag: transfer complete */
 UartReady = SET;
 HAL_UART_Abort_IT(UartHandle);//noise on the line or wrong bytes will cause further OVR ints to 
 //be called after the final RTO causing havoc. END transfer here.
 }
 else{//maybe ovr is set
 }
 }