36/317
2 - How does a typical microcontroller work?
2.5.2.5 Data desynchronization and atomicity
This paragraph addresses the precautions that must be taken, in the main program or any
service routine that may be interrupted.
In many cases, data is organized in blocks in memory, that is, several bytes, successive or
not, make up a piece of data. Some coherence rules must be followed when using these data.
Failure to observe these rules may produce unexpected results and, very likely, an application
crash.
When no interrupts are used, the main program can easily follow these rules by taking care to
perform all data changes in the appropriate order and respecting the predefined relationships
between each of the bytes that constitute a piece of data.
When interrupts are used, respecting the coherence rules may become more complex. Actu-
ally, an interrupt is an asynchronous action that can occur at any time. Let us assume that the
main program is currently altering one piece of data that is made of several bytes. It first writes
some bytes, then more bytes until it is finished with a new data in memory.
If an interrupt occurs in the middle of the process of altering the data, the following risk may
appear. If the interrupt service routine uses the data that the main program is writing, the
service routine may get data that fail to follow the coherence rules, since not all bytes have
been updated yet. The service routine may then be misled by an incorrect value that it cannot
handle properly, or just interpret that data differently from what it was expected to mean if it
had been fully modified. This may have very serious consequences on the working of the ap-
plication. This circumstance is called «data desynchronization».
To avoid this, some precautions may be taken in cases where interrupt service routine may
find incoherent data. They all ensure that all the data will be updated at once, and never used
unless completely updated. This condition is called «atomicity», from a Greek root meaning
«that cannot be cut». To properly handle multi-byte variables that are shared by a main pro-
gram and an interrupt service routine, or by two interrupt service routines of which one may in-
terrupt the other, the handling must be made “atomic”.
Содержание ST7 Series
Страница 1: ...ST7 8 BIT MCU FAMILY USER GUIDE JANUARY 1999 1 ...
Страница 238: ...238 317 8 C Language and the C Compiler 08 Burn bmp Then use the EPROMer programmer software as described in Chapter 7 ...
Страница 289: ...289 317 10 Second Application a Sailing Computer 10 befor Bs Rw Vw VMG AlphaR AlphaV Before the wind ...