cancel
Showing results for 
Search instead for 
Did you mean: 

Few packets are getting missed while reading byte by byte using HAL_UART API's

TH.16.336
Associate III

Hello All,

I have configured UARt for Rx interrupt and in the ISR I am reading the UART byte by byte and sending the same to my terminal using HAL_UART_Receive and Transfer API's.

But if I use HAL_UART API's, few bytes of my data is missed in middle and also if I send continuously the system get stuck after multiple iterations. Below is my code and packet sent,

Packet Sent from terminal: 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3A 0x0D

Packets received on terminal: 12345689:                                  

12345689: (here 7 is missed)

  

void DEBUG_IRQ_Handler(UART_HandleTypeDef *huart)

{

  uint32_t isrflags  = READ_REG(huart->Instance->SR);

  uint32_t cr1its   = READ_REG(huart->Instance->CR1);

  /* UART in mode Receiver -------------------------------------------------*/

  if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))

  {

#if 0

   dbg_byetRecvd = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);

   huart->Instance->DR = (dbg_byetRecvd & (uint8_t)0xFF);

#endif

HAL_UART_Receive(huart, &dbg_byetRecvd, 1, 100);

HAL_UART_Transmit(huart, &dbg_byetRecvd, 1, 100);

   return;

  }

  else

  {

   // Do nothing as we are not handling any interrupt other than RXNE

  }

}

May I know what is the issue with HAL_UART api's here? Because if I am using the code to directly read from the Data Register there is no issue .

Please help to know the issue here and also help how to code using the DR directly with all the flags.

2 REPLIES 2
Ozone
Lead

I'm no Cube user, don't know what HAL_UART_Transmit() actually does.

But do not ever call functions from within interrupt context that rely on interrupts, or do busy-waits.

berendi
Principal

THe HAL APIs are poorly documented, and their undocumented aspects can change without notice, so there is no way of knowing their proper usage except by using them exactly as in the shipped example projects, or reverse engineering their source. And even then there is no guarantee that it would work with the next HAL release.

The register interface is properly documented in the reference manual for your MCU, with lots of step-by-step instructions.