8000 WIP: rp2,docs: Stop machine.idle() blocking indefinitely, update docs. · micropython/micropython@5edcfaa · GitHub
[go: up one dir, main page]

Skip to content

Commit 5edcfaa

Browse files
committed
WIP: rp2,docs: Stop machine.idle() blocking indefinitely, update docs.
When rp2 port went tickless the behaviour of machine.idle() changed as there is no longer a tick interrupt to wake it up every millisecond. On a quiet system it would now block indefinitely. No other port does this. A lot of existing code (i.e. micropython-lib lps22h, lcd160cr sensor drivers, lora sync_modem driver, usb-device-hid) call 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 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. Marked WIP because on rp2 PICO, USB consumption of "while True: machine.idle()" went from 18.9mA to 21.5mA. Consumption of "while True: pass" is 21.7mA. This increase stays even with a much longer timeout, so I suspect that softtimer is somehow keeping the CPU awake incorrectly. More investigation needed. Also changes the machine module documentation to explain the new behaviour and recommended usage. I believe all ports will pause for at most 1ms now, and suggest this is good behaviour to stick with. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent cfa55b4 commit 5edcfaa

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
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])

ports/rp2/modmachine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
103103
}
104104

105105
static void mp_machine_idle(void) {
106-
__wfe();
106+
MICROPY_INTERNAL_WFE(1);
107107
}
108108

109109
static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {

0 commit comments

Comments
 (0)
0