Hi dear folks, thank you very much!
To keep track of overflow, you need to maintain some sort of information, e.g. serial numbers in messages or such.
Overall, I would maintain separate buffers on sending and receiving ends, which would reduce overhead and minimize errors.
Yes, you're right.
By maintaining serial number of each message, I could check whether the serial number of the currently read message is bigger than the last serial number by 1.
If yes, no overflow.
If not, overflow occurred and the Message the uC reads is garbage.
I believe I could instead maintain a 16-bit (or 32-bit) SOP (Start Of Packet) for each Message, and if the Message turns garbage, I'd know it.
I do use 2 separate buffers as you said (but it of course doesn't protect against overflow).
When the head wraps around and crosses over the tail, then you know that you will overflow the buffer and ruin the first message if you attempt to write a new one. The solution would be to drop the new messages and wait for the receiving cpu to empty the buffer.
Also I wouldnt call the indexes head and tail, but rather something like lastWrittenAddress and lastReadAddress respectively. Receiver increments lastReadAddress, sender increments lastWrittenAddres, when lastWrittenAddress reaches lastReadAddress after it wraps around, then you know you filled the buffer completely and need to stop further writes.
That is also a good idea, thank you.
How would you easily tell that the head (lastWrittenAddress) wrapped around?
If at any point after the first written byte the head index equals the tail then you have filled the buffer.
BTW, how do the two micros communicate the head and tail variables between each other? I don't see how this can be done.
Mike.
yes, but you need to somehow know (as I told
kubeek) that the head wrapped around.
How would you tell that?
The 2 uCs don't send to each other the head and tail, but they do write the length of each message inside the message.
That way, each uC maintains its own head and tail.
How are they sharing the buffer? Is it an external memory that both access?
Exactly