Skip to content

Commit 81daba3

Browse files
committed
docs: Specify that machine.idle() returns at least every 1ms.
A lot of existing code (i.e. micropython-lib lps22h, lcd160cr sensor drivers, lora sync_modem driver, usb-device-hid) calls machine.idle() inside a tight loop that is polling some condition. This reduces the power usage compared to constantly looping, but can be faster than calling a sleep function. However on a tickless port there's not always an interrupt before the condition they are polling for, so it's difficult to restructure this code if machine.idle() doesn't have any upper limit on execution time. This commit specifies an upper limit of 1ms before machine.idle() resumes execution. This is already the case for all ports except rp2. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent e1ecc23 commit 81daba3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

docs/library/machine.rst

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,20 @@ Power related functions
127127

128128
.. function:: idle()
129129

130-
Gates the clock to the CPU, useful to reduce power consumption at any time during
131-
short or long periods. Peripherals continue working and execution resumes as soon
132-
as any interrupt is triggered (on many ports this includes system timer
133-
interrupt occurring at regular intervals on the order of millisecond).
130+
Gates the clock to the CPU, useful to reduce power consumption at any time
131+
during short or long periods. Peripherals continue working and execution
132+
resumes as soon as any interrupt is triggered, or at most one millisecond
133+
after the CPU was paused.
134+
135+
It is recommended to call this function inside any tight loop that is
136+
continuously checking for an external change (i.e. polling). This will reduce
137+
power consumption without significantly impacting performance. To reduce
138+
power consumption further then see the :func:`lightsleep`,
139+
:func:`time.sleep()` and :func:`time.sleep_ms()` functions.
134140

135141
.. function:: sleep()
136142

137-
.. note:: This function is deprecated, use `lightsleep()` instead with no arguments.
143+
.. note:: This function is deprecated, use :func:`lightsleep()` instead with no arguments.
138144

139145
.. function:: lightsleep([time_ms])
140146
deepsleep([time_ms])

0 commit comments

Comments
 (0)