Question

Imagine the following scenario. A microcontroller is slave on a bus, say SPI. Once writing a byte on the bus, it wants to sleep until the byte is transferred. The code would look something like this:

write_byte_to_bus(byte);
wait_for_interrupt(); /* a single assembly instruction */

Now since the microcontroller is not the master, theoretically (and likely if the bus is really fast), the byte could be transferred immediately as the master requests and therefore something like this happens:

write_byte_to_bus(byte);
                                          interrupt arrives saying
                                          that operations is done
wait_for_interrupt();

which results in the microcontroller sleeping for interrupt after the interrupt has arrived. How can one protect itself from such cases?

Was it helpful?

Solution

For your specific microcontroller, you can disable interrupts first with a SIM, then execute your write_byte_to_bus and then WFI will re-enable interrupts appropriately.

Other microcontrollers may have different ways of managing this.

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