HAL_UART_Receive_IT - Receives Unknown Bytes
Hello:
Problems with the USART1 receive call on this device. Or is it me? I hope not.
I have a serial decode option on my scope. I can see what I send out, and what comes back, every byte. This is my call to receive:
rxChar[50];
HAL_UART_Receive_IT(&huart1, (uint8_t*)rxChar, 16);
I call it before main to set it up, then in the handler HAL_UART_RxCpltCallback, it is called at the end of the handler
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* uartHandle)
{
//...do stuff
HAL_UART_Receive_IT(&huart1, (uint8_t*)rxChar, 16);
}
After using HAL_UART_Transmit to send the byte string to the device, it responds back in about 10ms, with exactly 16 bytes. And the 16 bytes are correct, what I should get back, as confirmed on the scope serial decode. However, the HAL_UART_RxCpltCallback is behaving badly.
It is important to note that the device I am sending to may NOT respond to every request - it only responds if the device is active at that address. So, I have it set up that if I get no response in 100ms, I move on to the next query/transmit.
First, I set up the request to transmit at a known good address where it will respond. It receives back the 16 bytes, but the first 2 bytes are not correct, and not anything I see on the scope. It comes back with rxChar[0] = 0xFF, and rxChar[1] = 0x0.
Bytes 2 to 15 are correct, BUT: the last 2 bytes are missing! If I set my receive size to 18 in the call to HAL_UART_Receive_IT, run it again and then check bytes 16 and 17, the correct byes are there. So, even though my scope shows no trace of anything other than the exact correct byte data coming back, the UART handler feels otherwise.
After pushing reset on the development board, it will behave the same way and produce the same result every time. So it's consistently producing the same junk, not the random kind.
Now I try this.
I don't change the number of bytes on the call to HAL_UART_Receive_IT (leave at 18)
Instead of sending first to a known good address, I send to an address where I know I'll get no response. And I get back a string of "FF" and "0".
Next, I send to the know GOOD address, and this time it does not return good data, but rather, and bunch of random FF and 0. However - further up in the array - starting at around byte 16 - I see the correct preamble bytes. So odd. In summary, when the good address is sent first, the data is "semi-correct", and when the good address I sent second, the data is not correct. The devices never send back junk or bad bytes that I see on the scope.
I'm using IAR compiler, generating code with CubeMX (5.4). I thought I might try using LL instead of HAL. So, I set Cube up in advanced options to generate LL code for the UART, but when I try to compile, the copiler faults with: "Build error: Multiple tools write to the same file." A far and wide search with gewGul provided no solid answer on how to fix this odd error, and left me wondering if the nice folks at ST ever test their software produced by Cube for the target tool. Indeed, this is the kobayashi maru, a no-win scenario.
My questions:
Why is the UART (USART1) handler behaving badly? Why the 2 bytes of garbage that are definitely not seen on the scope?
How can I fix the compile error when using LL ? (no answer expected, this is really for ST)
Thanks - appreciate your help.
