Interview question - How to continuosly read a buffer without losing data

Viewed 232

I am an embedded engineer, with not much experience. I was at an interview and I was asked a question, which I guess is not new and already has answers:

You have a peripheral which is constantly receiving input from external world, the peripheral has a buffer. The rate of the input changes every now and then. The peripheral needs to read the input buffer and process it. It checks the input message and sees if it matches the filter configured. If it matches it is forwarded else it is discarded.

Questions were like:

  1. How do you manage this?
  2. What if the buffer gets full? You only have a limited size buffer? The peripheral has only a certain speed of processing the buffer messages.
  3. How do you adapt the buffer to different input speeds?

Appreciate your help. Thanks.

1 Answers

Basically they are checking if you have ever coded a program for some serial peripheral, so it's a pretty good interview question to check if someone is a complete beginner.

The normal way to do this in the "old school" way would be to set up an rx interrupt. If data can arrive irregularly and we aren't allowed to lose data, then an interrupt is pretty much the only sensible way. Especially if there are plans to support different baudrates as well.

The filter check can then be placed inside the ISR (except in CAN, where such checks are actually done by hardware). And from there on, valid data is passed on to a software ring buffer. This minimizes execution time inside the ISR, while solving the problem with limited hardware buffers at the same time. Data re-entrancy protection preferably needs to be carried out by the ring buffer code.

The modern way would rather be to avoid interrupts and instead use DMA, if the microcontroller supports it. This offloads the CPU from having to deal with the frequent rx interrupts. Then we can just swap the DMA buffer target address each time the buffer is full, and work with the raw buffer from there. Or alternatively just crudely hardcopy the whole DMA buffer elsewhere, if we for some reason can't swap DMA buffer address.

Related