2024-03-28 03:34 PM
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?
2024-03-28 03:54 PM
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