Skip to content

Commit 3236a0f

Browse files
authored
Merge pull request #10018 from dhalbert/restore-sleep-clocks
restore rp2040 sleep clocks after explicit light sleep
2 parents 5d39e46 + 8329861 commit 3236a0f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

ports/raspberrypi/common-hal/alarm/__init__.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
141141

142142
mp_obj_t wake_alarm = mp_const_none;
143143

144+
// Save current clocks.
145+
uint32_t saved_sleep_en0 = clocks_hw->sleep_en0;
146+
uint32_t saved_sleep_en1 = clocks_hw->sleep_en1;
147+
144148
while (!mp_hal_is_interrupted()) {
145149
RUN_BACKGROUND_TASKS;
146150
// Detect if interrupt was alarm or ctrl-C interrupt.
@@ -163,21 +167,25 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
163167
break;
164168
}
165169

166-
// Prune the clock for sleep
170+
// Prune the clocks for sleep.
167171
clocks_hw->sleep_en0 &= RP_LIGHTSLEEP_EN0_MASK;
168172
clocks_hw->sleep_en1 = RP_LIGHTSLEEP_EN1_MASK;
169173

170174
// Enable System Control Block (SCB) deep sleep
171-
uint save = scb_hw->scr;
172-
scb_hw->scr = save | M0PLUS_SCR_SLEEPDEEP_BITS;
175+
scb_hw->scr |= M0PLUS_SCR_SLEEPDEEP_BITS;
173176

174177
__wfi();
175178
}
176179

180+
// Restore clocks so other wfi() uses, like time.sleep(), won't use the light-sleep settings.
181+
clocks_hw->sleep_en0 = saved_sleep_en0;
182+
clocks_hw->sleep_en1 = saved_sleep_en1;
183+
177184
if (mp_hal_is_interrupted()) {
178185
return mp_const_none; // Shouldn't be given to python code because exception handling should kick in.
179186
}
180187

188+
181189
alarm_reset();
182190
return wake_alarm;
183191
}

0 commit comments

Comments
 (0)