Skip to content

Commit 624bd48

Browse files
peterhinchdpgeorge
authored andcommitted
docs/reference/isr_rules: Describe issue with hard ISRs and globals.
Signed-off-by: Damien George <[email protected]>
1 parent b79ceec commit 624bd48

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Diff for: docs/reference/isr_rules.rst

+11-5
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,17 @@ is partially updated. When the ISR tries to read the object, a crash results. Be
170170
on rare, random occasions they can be hard to diagnose. There are ways to circumvent this issue, described in
171171
:ref:`Critical Sections <Critical>` below.
172172

173-
It is important to be clear about what constitutes the modification of an object. An alteration to a built-in type
174-
such as a dictionary is problematic. Altering the contents of an array or bytearray is not. This is because bytes
175-
or words are written as a single machine code instruction which is not interruptible: in the parlance of real time
176-
programming the write is atomic. A user defined object might instantiate an integer, array or bytearray. It is valid
177-
for both the main loop and the ISR to alter the contents of these.
173+
It is important to be clear about what constitutes the modification of an object. Altering the contents of an array
174+
or bytearray is safe. This is because bytes or words are written as a single machine code instruction which is not
175+
interruptible: in the parlance of real time programming the write is atomic. The same is true of updating a
176+
dictionary item because items are machine words, being integers or pointers to objects. A user defined object might
177+
instantiate an array or bytearray. It is valid for both the main loop and the ISR to alter the contents of these.
178+
179+
The hazard arises when the structure of an object is altered, notably in the case of dictionaries. Adding or deleting
180+
keys can trigger a rehash. If a hard ISR runs while a rehash is in progress and attempts to access an item, a crash
181+
may occur. Internally globals are implemented as a dictionary. Consequently the main program should create all
182+
necessary globals before starting a process that generates hard interrupts. Application code should also avoid
183+
deleting globals.
178184

179185
MicroPython supports integers of arbitrary precision. Values between 2**30 -1 and -2**30 will be stored in
180186
a single machine word. Larger values are stored as Python objects. Consequently changes to long integers cannot

0 commit comments

Comments
 (0)