@@ -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,15 @@ 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
+ int alarm_num = 2 ;
193
197
if (n_args == 0 ) {
194
198
#if MICROPY_PY_NETWORK_CYW43
195
199
gpio_set_dormant_irq_enabled (CYW43_PIN_WL_HOST_WAKE , GPIO_IRQ_LEVEL_HIGH , true);
196
200
#endif
197
201
xosc_dormant ();
198
202
} 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 );
203
+ hardware_alarm_claim (alarm_num );
203
204
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
205
// Use timer alarm to wake.
210
206
clocks_hw -> sleep_en0 = 0x0 ;
211
207
#if PICO_RP2040
@@ -215,8 +211,12 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
215
211
#else
216
212
#error Unknown processor
217
213
#endif
218
- timer_hw -> intr = 1u << alarm_num ; // clear any IRQ
219
- timer_hw -> alarm [alarm_num ] = timer_hw -> timerawl + delay_ms * 1000 ;
214
+ hardware_alarm_set_callback (alarm_num , alarm_sleep_callback );
215
+ if (hardware_alarm_set_target (alarm_num , make_timeout_time_ms (delay_ms ))) {
216
+ hardware_alarm_set_callback (alarm_num , NULL );
217
+ hardware_alarm_unclaim (alarm_num );
218
+ alarm_num = -1 ;
219
+ }
220
220
} else {
221
221
// TODO: Use RTC alarm to wake.
222
222
clocks_hw -> sleep_en0 = 0x0 ;
@@ -246,11 +246,10 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
246
246
#endif
247
247
248
248
// Go into low-power mode.
249
- __wfi ();
250
-
251
- if (!timer3_enabled ) {
252
- irq_set_enabled (irq_num , false);
249
+ if (alarm_num >= 0 ) {
250
+ __wfi ();
253
251
}
252
+
254
253
clocks_hw -> sleep_en0 |= ~(0u );
255
254
clocks_hw -> sleep_en1 |= ~(0u );
256
255
}
@@ -264,6 +263,11 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
264
263
265
264
// Re-sync mp_hal_time_ns() counter with aon timer.
266
265
mp_hal_time_ns_set_from_rtc ();
266
+ if (alarm_num >= 0 ) {
267
+ hardware_alarm_cancel (alarm_num );
268
+ hardware_alarm_set_callback (alarm_num , NULL );
269
+ hardware_alarm_unclaim (alarm_num );
270
+ }
267
271
}
268
272
269
273
NORETURN static void mp_machine_deepsleep (size_t n_args , const mp_obj_t * args ) {
0 commit comments