Skip to content

Commit f74bd50

Browse files
committed
Update pico_sleep examples
Adds examples that sleep via an alarm or via an AON timer Adds dormant examples that wake on an AON timer or GPIO Fixed to work for RP2040 and RP2350
1 parent 9930bb0 commit f74bd50

9 files changed

+285
-127
lines changed

sleep/CMakeLists.txt

100644100755
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
if (NOT PICO_NO_HARDWARE)
2-
add_subdirectory_exclude_platforms(hello_dormant "rp2350.*")
3-
add_subdirectory_exclude_platforms(hello_sleep "rp2350.*")
2+
add_subdirectory(hello_dormant)
3+
add_subdirectory(hello_sleep)
44
endif ()

sleep/hello_dormant/CMakeLists.txt

100644100755
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
add_executable(hello_dormant
2-
hello_dormant.c
1+
add_executable(hello_dormant_gpio
2+
hello_dormant_gpio.c
33
)
4+
target_link_libraries(hello_dormant_gpio
5+
pico_stdlib
6+
hardware_sleep)
7+
pico_add_extra_outputs(hello_dormant_gpio)
48

5-
target_link_libraries(hello_dormant pico_stdlib hardware_sleep)
6-
7-
# create map/bin/hex file etc.
8-
pico_add_extra_outputs(hello_dormant)
9+
add_executable(hello_dormant_aon
10+
hello_dormant_aon.c
11+
)
12+
target_link_libraries(hello_dormant_aon
13+
pico_stdlib
14+
hardware_sleep
15+
)
16+
pico_add_extra_outputs(hello_dormant_aon)

sleep/hello_dormant/hello_dormant.c

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include "pico/stdlib.h"
9+
#include "pico/sleep.h"
10+
11+
// For clock_configure_gpin
12+
#ifdef PICO_RP2040
13+
#include "hardware/clocks.h"
14+
#endif
15+
16+
// For RP2040 this example needs an external clock fed into the GP20
17+
// Note: Only GP20 and GP22 can be used for clock input, See the GPIO function table in the datasheet.
18+
// You can use another Pico to generate this. See the clocks/hello_gpout example for more details.
19+
// rp2040: clock_gpio_init(21, CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLK_RTC, 1); // 46875Hz can only export a clock on gpios 21,23,24,25 and only 21 is exposed by Pico
20+
// RP2350 has an LPOSC it can use, so doesn't need this
21+
#define EXTERNAL_CLOCK_INPUT_PIN 20
22+
#define RTC_FREQ_HZ 46875
23+
24+
static void sleep_callback(void) {
25+
printf("AON timer woke us up\n");
26+
}
27+
28+
static void aon_sleep(void) {
29+
30+
// Get the time from the aon timer and set our alarm time
31+
struct timespec ts;
32+
aon_timer_get_time(&ts);
33+
ts.tv_sec += 10;
34+
35+
printf("Sleeping for 10 seconds\n");
36+
uart_default_tx_wait_blocking();
37+
38+
#if PICO_RP2040
39+
// The RTC must be run from an external source, since the dormant source will be inactive
40+
clock_configure_gpin(clk_rtc, EXTERNAL_CLOCK_INPUT_PIN, RTC_FREQ_HZ, 46875);
41+
#endif
42+
43+
// Go to sleep for 10 seconds, with RTC running off GP20
44+
// The external clock is the RTC of another pico being fed to GP20
45+
sleep_goto_aon_dormant_until(&ts, &sleep_callback);
46+
}
47+
48+
int main() {
49+
50+
stdio_init_all();
51+
printf("Hello Dormant AON Timer!\n");
52+
53+
struct timespec ts = { .tv_sec = 1723124088, .tv_nsec = 0 };
54+
aon_timer_start(&ts);
55+
56+
while(true) {
57+
printf("Awake for 10s\n");
58+
sleep_ms(10000);
59+
60+
uart_default_tx_wait_blocking();
61+
62+
// Set the crystal oscillator as the dormant clock source, UART will be reconfigured from here
63+
// This is necessary before sending the pico dormant
64+
#if PICO_RP2040
65+
printf("Switching to XOSC\n");
66+
sleep_run_from_xosc();
67+
#else
68+
printf("Switching to LPSC\n");
69+
sleep_run_from_lpsc();
70+
#endif
71+
72+
uart_default_tx_wait_blocking();
73+
74+
printf("Going dormant\n");
75+
uart_default_tx_wait_blocking();
76+
77+
// Go to sleep until the RTC interrupt is generated after 10 seconds
78+
aon_sleep();
79+
80+
// Re-enabling clock sources and generators.
81+
sleep_power_up();
82+
}
83+
return 0;
84+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include "pico/stdlib.h"
9+
#include "pico/sleep.h"
10+
11+
#define WAKE_GPIO 10
12+
13+
int main() {
14+
stdio_init_all();
15+
printf("Hello Dormant GPIO!\n");
16+
17+
printf("Test starting in 10s\n");
18+
sleep_ms(10000);
19+
20+
while(true) {
21+
printf("Switching to XOSC\n");
22+
uart_default_tx_wait_blocking();
23+
24+
// Set the crystal oscillator as the dormant clock source, UART will be reconfigured from here
25+
// This is necessary before sending the pico into dormancy
26+
sleep_run_from_xosc();
27+
28+
printf("Going dormant until GPIO %d goes edge high\n", WAKE_GPIO);
29+
uart_default_tx_wait_blocking();
30+
31+
// Go to sleep until we see a high edge on GPIO 10
32+
sleep_goto_dormant_until_edge_high(WAKE_GPIO);
33+
34+
// Re-enabling clock sources and generators.
35+
sleep_power_up();
36+
printf("Now awake for 10s\n");
37+
sleep_ms(1000 * 10);
38+
}
39+
40+
return 0;
41+
}

sleep/hello_sleep/CMakeLists.txt

100644100755
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
add_executable(hello_sleep
2-
hello_sleep.c
1+
add_executable(hello_sleep_alarm
2+
hello_sleep_alarm.c
33
)
4+
target_link_libraries(hello_sleep_alarm
5+
pico_stdlib
6+
hardware_sleep
7+
)
8+
pico_add_extra_outputs(hello_sleep_alarm)
49

5-
target_link_libraries(hello_sleep pico_stdlib hardware_sleep)
6-
7-
# create map/bin/hex file etc.
8-
pico_add_extra_outputs(hello_sleep)
10+
add_executable(hello_sleep_aon
11+
hello_sleep_aon.c
12+
)
13+
target_link_libraries(hello_sleep_aon
14+
pico_stdlib
15+
hardware_sleep
16+
pico_aon_timer
17+
)
18+
pico_add_extra_outputs(hello_sleep_aon)

sleep/hello_sleep/hello_sleep.c

Lines changed: 0 additions & 77 deletions
This file was deleted.

sleep/hello_sleep/hello_sleep_alarm.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include "pico/stdlib.h"
9+
#include "pico/sleep.h"
10+
11+
static bool awake;
12+
13+
static void alarm_sleep_callback(uint alarm_id) {
14+
printf("alarm woke us up\n");
15+
uart_default_tx_wait_blocking();
16+
awake = true;
17+
hardware_alarm_set_callback(alarm_id, NULL);
18+
hardware_alarm_unclaim(alarm_id);
19+
}
20+
21+
int main() {
22+
23+
stdio_init_all();
24+
printf("Hello Alarm Sleep!\n");
25+
26+
do {
27+
printf("Awake for 10 seconds\n");
28+
sleep_ms(1000 * 10);
29+
30+
printf("Switching to XOSC\n");
31+
32+
// Wait for the fifo to be drained so we get reliable output
33+
uart_default_tx_wait_blocking();
34+
35+
// Set the crystal oscillator as the dormant clock source, UART will be reconfigured from here
36+
// This is only really necessary before sending the pico dormant but running from xosc while asleep saves power
37+
sleep_run_from_xosc();
38+
awake = false;
39+
40+
// Go to sleep until the alarm interrupt is generated after 10 seconds
41+
printf("Sleeping for 10 seconds\n");
42+
uart_default_tx_wait_blocking();
43+
44+
int alarm_id = -1;
45+
sleep_goto_sleep_for(10000, &alarm_sleep_callback, &alarm_id);
46+
if (alarm_id >= 0) {
47+
// Make sure we don't wake
48+
while (!awake) {
49+
printf("Should be sleeping\n");
50+
}
51+
}
52+
53+
// Re-enabling clock sources and generators.
54+
sleep_power_up();
55+
} while(true);
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)