2021-02-26 03:11 AM
Hello!
I'm doing an application in which my control board is I2C slave of my PC and my control board has to communicate with another board (let's call it test board) via UART.
This test board uses custom UART text commands to perform tests on itself.
For example I send "toggle relay 1\n" and the test board responds "ok\n".
Or "vin\n" and the test board responds "Reading vin\n 4.98V\n"
I don't know the lenght of the response i'm going to recieve and the test board does not send any termination character either.
So I use HAL_UART_Transmit with my command and then I do a HAL_UART_Recieve with a buffer long enough to be able to contain the largest possible response. And it works good.
The problem is that my control board is being interrupted by the HAL_I2C_Slave_Receive_IT interruption when my PC sends a command, and if it happens that this interrupt is fired during the UART_Recieve then my UART data gets corrupted.
I was thinking on seting a 1 byte interrupt for my UART and add it to a recieve buffer, that will not be terrible because the test board only send data when I request for it and it is not very big either. But I dont know the length of the message received so I don't know when to say that my buffer is complete.
Is there any way to resolve this? Thank you!
2021-02-26 03:28 AM
> The problem is that my control board is being interrupted by the HAL_I2C_Slave_Receive_IT ...
Than you are doing too much stuff in interrupt context. Remember, that includes all the callbacks.
Cube/Hal is IMHO very careless in that regard.
> I was thinking on seting a 1 byte interrupt for my UART and add it to a recieve buffer, that will not be terrible because the test board only send data when I request for it and it is not very big either. But I dont know the length of the message received so I don't know when to say that my buffer is complete.
I almost always do it that way, i.e. single-byte reception interrupt.
You have a dedicated termination character ('\n'), so it's easy to detect when a message ended.
Still, character can get lost or corrupted. Check your buffer index in the interrupt handler, and if beyond max. size discard anything until re-synchronized.