8000 rp2/modmachine: Implement lightsleep for RP2350. · micropython/micropython@d1423ef · GitHub
[go: up one dir, main page]

Skip to content

Commit d1423ef

Browse files
peterharperukdpgeorge
authored andcommitted
rp2/modmachine: Implement lightsleep for RP2350.
This isn't fully working, the CPU often wakes up early. That will be fixed when a newer version of pico-sdk is released. Signed-off-by: Damien George <damien@micropython.org>
1 parent c90d996 commit d1423ef

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

ports/rp2/modmachine.c

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
162162
}
163163

164164
clock_stop(clk_adc);
165+
#if PICO_RP2350
166+
clock_stop(clk_hstx);
167+
#endif
165168

166169
// CLK_REF = XOSC
167170
clock_configure(clk_ref, CLOCKS_CLK_REF_CTRL_SRC_VALUE_XOSC_CLKSRC, 0, xosc_hz, xosc_hz);
@@ -170,7 +173,9 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
170173
clock_configure(clk_sys, CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLK_REF, 0, xosc_hz, xosc_hz);
171174

172175
// CLK_RTC = XOSC / 256
176+
#if PICO_RP2040
173177
clock_configure(clk_rtc, 0, CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_XOSC_CLKSRC, xosc_hz, xosc_hz / 256);
178+
#endif
174179

175180
// CLK_PERI = CLK_SYS
176181
clock_configure(clk_peri, 0, CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS, xosc_hz, xosc_hz);
@@ -190,38 +195,63 @@ static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) {
190195
#endif
191196
xosc_dormant();
192197
} else {
193-
uint32_t sleep_en0 = clocks_hw->sleep_en0;
194-
uint32_t sleep_en1 = clocks_hw->sleep_en1;
195198
bool timer3_enabled = irq_is_enabled(3);
196199

197-
clocks_hw->sleep_en0 = CLOCKS_SLEEP_EN0_CLK_RTC_RTC_BITS;
200+
const uint32_t alarm_num = 3;
201+
const uint32_t irq_num = TIMER_ALARM_IRQ_NUM(timer_hw, alarm_num);
198202
if (use_timer_alarm) {
199203
// Make sure ALARM3/IRQ3 is enabled on _this_ core
200-
timer_hw->inte |= 1 << 3;
201204
if (!timer3_enabled) {
202-
irq_set_enabled(3, true);
205+
irq_set_enabled(irq_num, true);
203206
}
207+
hw_set_bits(&timer_hw->inte, 1u << alarm_num);
204208
// Use timer alarm to wake.
209+
clocks_hw->sleep_en0 = 0x0;
210+
#if PICO_RP2040
205211
clocks_hw->sleep_en1 = CLOCKS_SLEEP_EN1_CLK_SYS_TIMER_BITS;
206-
timer_hw->alarm[3] = timer_hw->timerawl + delay_ms * 1000;
212+
#elif PICO_RP2350
213+
clocks_hw->sleep_en1 = CLOCKS_SLEEP_EN1_CLK_REF_TICKS_BITS | CLOCKS_SLEEP_EN1_CLK_SYS_TIMER0_BITS;
214+
#else
215+
#error Unknown processor
216+
#endif
217+
timer_hw->intr = 1u << alarm_num; // clear any IRQ
218+
timer_hw->alarm[alarm_num] = timer_hw->timerawl + delay_ms * 1000;
207219
} else {
208220
// TODO: Use RTC alarm to wake.
209-
clocks_hw->sleep_en1 = 0;
221+
clocks_hw->sleep_en0 = 0x0;
222+
clocks_hw->sleep_en1 = 0x0;
210223
}
211224

212225
if (!disable_usb) {
213226
clocks_hw->sleep_en0 |= CLOCKS_SLEEP_EN0_CLK_SYS_PLL_USB_BITS;
227+
#if PICO_RP2040
214228
clocks_hw->sleep_en1 |= CLOCKS_SLEEP_EN1_CLK_USB_USBCTRL_BITS;
229+
#elif PICO_RP2350
230+
clocks_hw->sleep_en1 |= CLOCKS_SLEEP_EN1_CLK_SYS_USBCTRL_BITS;
231+
#else
232+
#error Unknown processor
233+
#endif
215234
}
216235

236+
#if PICO_ARM
237+
// Configure SLEEPDEEP bits on Cortex-M CPUs.
238+
#if PICO_RP2040
217239
scb_hw->scr |= M0PLUS_SCR_SLEEPDEEP_BITS;
240+
#elif PICO_RP2350
241+
scb_hw->scr |= M33_SCR_SLEEPDEEP_BITS;
242+
#else
243+
#error Unknown processor
244+
#endif
245+
#endif
246+
247+
// Go into low-power mode.
218248
__wfi();
219-
scb_hw->scr &= ~M0PLUS_SCR_SLEEPDEEP_BITS;
249+
220250
if (!timer3_enabled) {
221-
irq_set_enabled(3, false);
251+
irq_set_enabled(irq_num, false);
222252
}
223-
clocks_hw->sleep_en0 = sleep_en0;
224-
clocks_hw->sleep_en1 = sleep_en1;
253+
clocks_hw->sleep_en0 |= ~(0u);
254+
clocks_hw->sleep_en1 |= ~(0u);
225255
}
226256

227257
// Enable ROSC.

0 commit comments

Comments
 (0)
0