Question

I'm writing my own drivers for LPC2148 and a question came to mind. How do I receive a message of unspecified size in UART?

The only 2 things that come to mind are: 1 - Configure a watchdog and end the receiving when the time runs out. 2- make it so that whenever a meswsage is sent to it there must be an end of message character.

The first choice seems better in my opinion, but I'd like to know if anybody has a better answer, and I know there must be.

Thank you very much

Was it helpful?

Solution 2

It looks like a wrong use for a watchdog. I ended up with three solutions for this problem:

  1. Use fixed-size packets and DMA; so, you receive one packet per transaction. Apparently, it is not possible in your case.
  2. Receive message char-by-char until the end-of-message character is received. Kind of error-prone, since the EOM char may appear in the data, probably.
  3. Use a fixed-size header before every packet. In the header, store payload size and/or message type ID.

The third approach is probably the best one. You may combine it with the first one, i.e. use DMA to receive header and then data (in the second transaction, after the data size is known from the header). It is also one of the most flexible approaches.

One more thing to worry about is to keep bytestream in sync. There may be rubbish laying in the UART input buffers, which may get read as data, or you can get only a part of a packet after your MCU is powered (i.e. the beginning of the packet had already been sent by that time). To avoid that, you can add magic bytes in your packet header, and probably CRC.

EDIT

OK, one more option :) Just store everything you receive in a growing buffer for later use. That is basically what PC drivers do.

OTHER TIPS

Just give the caller whatever bytes you have received so far. The UART driver shouldn't try to implement the application protocol, the application should do that.

Real embedded uart drivers usually use a ring buffer. Bytes are stored in order and the clients promise to read from the buffer before it's full. A state machine can then process the message in multiple passes with no need for a watchdog to tell it reception is over

better to go for option 2) append end of transmission character to the transmission string.

but i suggest to add start of transmission also to validate that you are receiving actual transmission.

Watchdog timer is used to reset system when there is a unexpected behavior of device. I think it is better to use a buffer which can store size of data that your application requires.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top