@@ -121,6 +121,9 @@ static void mp_machine_idle(void) {
121
121
MICROPY_INTERNAL_WFE (1 );
122
122
}
123
123
124
+ static void alarm_sleep_callback (uint alarm_id ) {
125
+ }
126
+
124
127
static void mp_machine_lightsleep (size_t n_args , const mp_obj_t * args ) {
125
128
mp_int_t delay_ms = 0 ;
126
129
bool use_timer_alarm = false;
@@ -190,22 +193,17 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
190
193
// Disable ROSC.
191
194
rosc_hw -> ctrl = ROSC_CTRL_ENABLE_VALUE_DISABLE << ROSC_CTRL_ENABLE_LSB ;
192
195
196
+ // pico-sdk alarm pool uses 3
197
+ // micropython soft timers use 2
198
+ int alarm_num = 1 ;
193
199
if (n_args == 0 ) {
194
200
#if MICROPY_PY_NETWORK_CYW43
195
201
gpio_set_dormant_irq_enabled (CYW43_PIN_WL_HOST_WAKE , GPIO_IRQ_LEVEL_HIGH , true);
196
202
#endif
197
203
xosc_dormant ();
198
204
} else {
199
- bool timer3_enabled = irq_is_enabled (3 );
200
-
201
- const uint32_t alarm_num = 3 ;
202
- const uint32_t irq_num = TIMER_ALARM_IRQ_NUM (timer_hw , alarm_num );
205
+ hardware_alarm_claim (alarm_num );
203
206
if (use_timer_alarm ) {
204
- // Make sure ALARM3/IRQ3 is enabled on _this_ core
205
- if (!timer3_enabled ) {
206
- irq_set_enabled (irq_num , true);
207
- }
208
- hw_set_bits (& timer_hw -> inte , 1u << alarm_num );
209
207
// Use timer alarm to wake.
210
208
clocks_hw -> sleep_en0 = 0x0 ;
211
209
#if PICO_RP2040
@@ -215,8 +213,12 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
215
213
#else
216
214
#error Unknown processor
217
215
#endif
218
- timer_hw -> intr = 1u << alarm_num ; // clear any IRQ
219
- timer_hw -> alarm [alarm_num ] = timer_hw -> timerawl + delay_ms * 1000 ;
216
+ hardware_alarm_set_callback (alarm_num , alarm_sleep_callback );
217
+ if (hardware_alarm_set_target (alarm_num , make_timeout_time_ms (delay_ms ))) {
218
+ hardware_alarm_set_callback (alarm_num , NULL );
219
+ hardware_alarm_unclaim (alarm_num );
220
+ alarm_num = -1 ;
221
+ }
220
222
} else {
221
223
// TODO: Use RTC alarm to wake.
222
224
clocks_hw -> sleep_en0 = 0x0 ;
@@ -246,11 +248,10 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
246
248
#endif
247
249
248
250
// Go into low-power mode.
249
- __wfi ();
250
-
251
- if (!timer3_enabled ) {
252
- irq_set_enabled (irq_num , false);
251
+ if (alarm_num >= 0 ) {
252
+ __wfi ();
253
253
}
254
+
254
255
clocks_hw -> sleep_en0 |= ~(0u );
255
256
clocks_hw -> sleep_en1 |= ~(0u );
256
257
}
@@ -264,6 +265,11 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
264
265
265
266
// Re-sync mp_hal_time_ns() counter with aon timer.
266
267
mp_hal_time_ns_set_from_rtc ();
268
+ if (alarm_num >= 0 ) {
269
+ hardware_alarm_cancel (alarm_num );
270
+ hardware_alarm_set_callback (alarm_num , NULL );
271
+ hardware_alarm_unclaim (alarm_num );
272
+ }
267
273
}
268
274
269
275
NORETURN static void mp_machine_deepsleep (size_t n_args , const mp_obj_t * args ) {
0 commit comments