Skip to main content
Visitor II
March 28, 2024
Question

HAL_UARTEx_ReceiveToIdle_DMA repeating same data

  • March 28, 2024
  • 1 reply
  • 1115 views

I am currently using HAL_UARTEx_ReceiveToIdle_DMA and getting results that don't make sense to me. My main function is very simple:

 

int main() {

 MX_DMA_Init();
 MX_UART4_Init();

 HAL_UARTEx_ReceiveToIdle_DMA(&huart4, rxDmaBuffer, sizeof(rxDmaBuffer));
 __HAL_DMA_DISABLE_IT(&hdma_uart4_rx, DMA_IT_HT);

 while (true) {
 if (tcpToSendQueue.size() > 0) {
 size_t length = tcpToSendQueue.front();
 tcpToSendQueue.dequeue();
 fwrite(tcpToSendBuffer, length, 1, stdout);
 }
 ThisThread::sleep_for(5ms);
 }
}

 

And my HAL_UARTEx_RxEventCallback is also minimal.

 

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t size) {
 if (huart->Instance == UART4) {

 std::memcpy(tcpToSendBuffer, rxDmaBuffer, size);
 tcpToSendQueue.enqueue(size); 

 HAL_UARTEx_ReceiveToIdle_DMA(&huart4, rxDmaBuffer, sizeof(rxDmaBuffer));
 __HAL_DMA_DISABLE_IT(&hdma_uart4_rx, DMA_IT_HT);
 }
}

 

I am receiving ASCII data at 5Hz to test, and the very first message is coming through fine. However, all subsequent messages are just duplicates of the first. The HAL_UARTEx_RxEventCallback appears to be called correctly each message and at 5Hz with the correct reported packet size.

Here is an example of what I am seeing from my serial monitor:

17:12:38.013 [RX] - -044.751,+000.385,-000.930
17:12:38.213 [RX] - -044.751,+000.385,-000.930
17:12:38.413 [RX] - -044.751,+000.385,-000.930
17:12:38.613 [RX] - -044.751,+000.385,-000.930
17:12:38.813 [RX] - -044.751,+000.385,-000.930

These numbers should all be different. Any ideas of what could be going wrong? 

This topic has been closed for replies.

1 reply

Karl Yamashita
Principal
March 28, 2024

Not sure how frequent you are receiving data? However, you should avoid calling a function from within an interrupt. Set a flag and poll it in main while loop then send the data. You can also use a ring buffer queue so that while you're sending data, you can receive new data in the next queue.

See this project https://github.com/karlyamashita/Nucleo-G431RB_Three_UART/wiki

 

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source