cancel
Showing results for 
Search instead for 
Did you mean: 

How to deal with unknown length of the UART receive in Interrupt mode even after using Interrupt for each byte?

RSrin
Associate

I am working on a embedded board using STM32H743ZI MCU. My test App ABC on windows PC send commands to the MCU over USB. STM32 MCU after receiving this 

command as input, sends a part of it to the chip XYZ presnt on the same board using UART. Now chip XYZ is supposed to return some reply bytes which are unknown in length

and depend on the command being sent by the App ABC on the PC. I am using Interrupt mode for UART communication between STM32 MCU and chip XYZ, and since

the length of the response is unknown for UART so I have modified the HAL to use custom UART code where there comes an interrupt on each byte received and it is working fine.

My use case is: the response received from chip XYZ has to be sent back to the App ABC in PC by MCU as it is. So to be precise my question is how do I know

that chip XYZ has sent all the response bytes and now MCU can send those response bytes to the PC App , as the legth of the response is unknown and there is no fixed time limit also.

Is there any other way to do read write over UART using HAL (or otherwise) when length of the response is unknown.

6 REPLIES 6
Ozone
Lead

This is called a "protocol", and means you have a defined start and end signature for each message.

GPS (NMEA 183) sentences, for example, always start with a "$" character, and end with "\r\n".

OTOH, Modbus RTU uses only defined idle times to separate messages, and not specific characters.

If your are not bound to e specific protocol, pick your best matching favourite, or define your own.

Some consistency check (CRC, checksum) and perhaps a retry mechanism is recommended.

> Is there any other way to do read write over UART using HAL (or otherwise) when length of the response is unknown.

Check for the message termination character, and sync with the message start character.

Sending shouldn't be a problem.

I'm no Cube user.

S.Ma
Principal

Use LL for USART.

Some USART can generate interrupt on specific termination byte (like console /n)

Otherwise, you can also fill a rolling buffer with DMA and check every 1 msec what happened (time slice vs byte by byte), latency compromise

SF??r
Associate III

Or you could use the IDLE Interrupt or Receiver timeout function.... depense on your incomming datastream timing

S.Ma
Principal

The first step is to write down how the MCU "in between" knows when the reply of the XZY is complete. Until it is, MCU can't reply to host PC and this will trigger lots of "hang" situation. Define Timeout and protocol. If possible reuse one so you won't have to debug it all the way or toss it down the line once reaching practical tests.

There is an alternate to this which would be to send 2x8 bit when sending one byte, the first byte is the "destination device ID". Then it can be interleaved and decoded/reconstruted.

SF??r
Associate III

The keyword here is "framing"

If the google translater does a good job, maybe my blog artikel is a good startup:

http://translate.google.com/translate?hl=&sl=auto&tl=en&u=https%3A%2F%2Fsebastianfoerster86.wordpress.com%2F2017%2F02%2F01%2Fprotokolldesign-kommunikation%2F

MKucu
Associate

I haven't tested for STM32H7 but this might be useful.

https://github.com/mikucukyilmaz/STM32-Extended-UART