Reputation: 11
I have two USART lines, the first device sends a command, the second responds. I want it all to go through STM32. I'm tracking two UARTs via interrupts. I need the program to receive commands from the first device, send them to the second and return a response from it to the first device. But I ran into a problem, for some reason many messages do not reach, sometimes only commands are read in a row, and then a couple of responses are returned, although it is necessary that there be a command-response.
I am requesting to receive bytes in an infinite loop
while (1) { HAL_UART_Receive_IT(&huart2, (uint8_t*) masterData, 1); HAL_UART_Receive_IT(&huart3, (uint8_t*) peripheralData, 1); }
Upvotes: 0
Views: 391
Reputation: 706
Mutiple issues here.
HAL_UART_RxCpltCallback
is a callback function, which is called from UART's interrupt. Your code stalls, i.e. next reception is not started, untill everything is printed, . Never use printf
within an interrupt, since it is usualy a blocking call.
You are receiving and transmitting from the same buffers - for uart2
it is masterData
. Even if printfs are removed from the callback, HAL_UART_Transmit_IT
will start transfer from masterData
buffer, and main loop will start reception into the same buffer. You need to implement a message queue, or at least double-buffering.
sizeof(*masterData)
is not number of bytes received, it is a size of array element, which is probably 1. Actual size should be stored somewhere within huart2 handle.
You setting up reception for 1 byte, and printf-ing two bytes.
Perhaps there more, but that's what I see at a glance.
Upvotes: 1