Skip to main content
SGonz.2
Associate III
June 7, 2022
Question

how HAL_UARTEx_RxEventCallback is executed?

  • June 7, 2022
  • 2 replies
  • 1225 views

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);
 }
}

This topic has been closed for replies.

2 replies

Pavel A.
June 7, 2022

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,

SGonz.2
SGonz.2Author
Associate III
June 7, 2022

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