@@ -21,6 +21,7 @@ This summarises the points detailed below and lists the principal recommendation
21
21
22
22
* Keep the code as short and simple as possible.
23
23
* Avoid memory allocation: no appending to lists or insertion into dictionaries, no floating point.
24
+ * Consider using ``micropython.schedule `` to work around the above constraint.
24
25
* Where an ISR returns multiple bytes use a pre-allocated ``bytearray ``. If multiple integers are to be
25
26
shared between an ISR and the main program consider an array (``array.array ``).
26
27
* Where data is shared between the main program and an ISR, consider disabling interrupts prior to accessing
@@ -158,6 +159,26 @@ On platforms with hardware floating point (such as the Pyboard) the inline ARM T
158
159
round this limitation. This is because the processor stores float values in a machine word; values can be shared
159
160
between the ISR and main program code via an array of floats.
160
161
162
+ Using micropython.schedule
163
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
164
+
165
+ This function enables an ISR to schedule a callback for execution "very soon". The callback is queued for
166
+ execution which will take place at a time when the heap is not locked. Hence it can create Python objects
167
+ and use floats. The callback is also guaranteed to run at a time when the main program has completed any
168
+ update of Python objects, so the callback will not encounter partially updated objects.
169
+
170
+ Typical usage is to handle sensor hardware. The ISR acquires data from the hardware and enables it to
171
+ issue a further interrupt. It then schedules a callback to process the data.
172
+
173
+ Scheduled callbacks should comply with the principles of interrupt handler design outlined below. This is to
174
+ avoid problems resulting from I/O activity and the modification of shared data which can arise in any code
175
+ which pre-empts the main program loop.
176
+
177
+ Execution time needs to be considered in relation to the frequency with which interrupts can occur. If an
178
+ interrupt occurs while the previous callback is executing, a further instance of the callback will be queued
179
+ for execution; this will run after the current instance has completed. A sustained high interrupt repetition
180
+ rate therefore carries a risk of unconstrained queue growth and eventual failure with a ``RuntimeError ``.
181
+
161
182
Exceptions
162
183
----------
163
184
0 commit comments