cancel
Showing results for 
Search instead for 
Did you mean: 

how HAL_UARTEx_RxEventCallback is executed?

SGonz.2
Associate II

Hello! Im receiving data by UART with DMA and now I wonder, how is HAL_UARTEx_RxEventCallback is ejecuted?

Because I'm receiving and saving chunks of data in a buffer, then adding a header and a tail in this buffer to send it by USB, but I realized that I'm overwritting the next chunk of data data, even when debugging.

Are callback events ocurring simultaneously in background?

How can I set a buffer that only keep the current chunk of data? Maybe a static buffer?

This is my code if you want to check, this is my UserDataTreatment called in the callback function

void UserDataTreatment(UART_HandleTypeDef *huart, uint8_t* pData, uint16_t Size){
	uint8_t Tail[] = ",END#";
	uint8_t Head[] = "@HEAD=ACEL1";
 
        if(huart->Instance == USART1){
	  uint8_t* pBuff = pData;
	  uint32_t  i;
	  uint8_t DataArray[USB_BUFFER_SIZE];
	  for (i = 0; i < HEAD_LENGTH; i++){
		  DataArray[i] = Head[i];
	  }
	  for (i = HEAD_LENGTH; i < HEAD_LENGTH+Size; i++){
		 DataArray[i] = *pBuff;
		 pBuff++;
	  }
	  for (i = 0; i < TAIL_LENGTH; i++){
		  DataArray[HEAD_LENGTH+Size+i] = Tail[i];
	  }
	  CDC_Transmit_FS(DataArray, HEAD_LENGTH+Size+TAIL_LENGTH);
  }
}

2 REPLIES 2
Pavel A.
Evangelist III

In line 17 you pass array on the stack to a process that runs after the function returns.

Basic C language exam failed.

Also you cannot push the data received from the UART directly to USB,

because USB may be not ready to accept it yet. Need to buffer the incoming data somewhere,

maybe just have a ring buffer large enough,

My bad! Should I swap the second and third for loop?