From eb080e420071fb2a8a4dc292109d456be5e4cd0b Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Tue, 21 Apr 2026 15:18:19 +0100 Subject: [PATCH 1/6] Start examples using low power API --- CMakeLists.txt | 1 + low_power/CMakeLists.txt | 18 ++++++ .../low_power_dormant_timer/CMakeLists.txt | 18 ++++++ .../low_power_dormant_timer.c | 58 +++++++++++++++++++ .../low_power_pstate_timer/CMakeLists.txt | 18 ++++++ .../low_power_pstate_timer.c | 48 +++++++++++++++ .../low_power_sleep_timer/CMakeLists.txt | 18 ++++++ .../low_power_sleep_timer.c | 58 +++++++++++++++++++ 8 files changed, 237 insertions(+) create mode 100644 low_power/CMakeLists.txt create mode 100644 low_power/low_power_dormant_timer/CMakeLists.txt create mode 100644 low_power/low_power_dormant_timer/low_power_dormant_timer.c create mode 100644 low_power/low_power_pstate_timer/CMakeLists.txt create mode 100644 low_power/low_power_pstate_timer/low_power_pstate_timer.c create mode 100644 low_power/low_power_sleep_timer/CMakeLists.txt create mode 100644 low_power/low_power_sleep_timer/low_power_sleep_timer.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a76866c0d..202a09fcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ add_subdirectory(gpio) add_subdirectory(hstx) add_subdirectory(i2c) add_subdirectory(interp) +add_subdirectory(low_power) add_subdirectory(multicore) add_subdirectory(otp) add_subdirectory(picoboard) diff --git a/low_power/CMakeLists.txt b/low_power/CMakeLists.txt new file mode 100644 index 000000000..3b151fa79 --- /dev/null +++ b/low_power/CMakeLists.txt @@ -0,0 +1,18 @@ +if (0) + add_library(powman_example INTERFACE) + target_sources(powman_example INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/powman_example.c + ) + target_include_directories(powman_example INTERFACE + ${CMAKE_CURRENT_LIST_DIR} + ) + target_link_libraries(powman_example INTERFACE + hardware_powman + hardware_gpio + ) +endif() + +add_subdirectory(low_power_sleep_timer) +add_subdirectory(low_power_dormant_timer) +add_subdirectory(low_power_pstate_timer) +#add_subdirectory(powman_gpio) diff --git a/low_power/low_power_dormant_timer/CMakeLists.txt b/low_power/low_power_dormant_timer/CMakeLists.txt new file mode 100644 index 000000000..0880e93d0 --- /dev/null +++ b/low_power/low_power_dormant_timer/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(low_power_dormant_timer + low_power_dormant_timer.c + ) +target_link_libraries(low_power_dormant_timer PRIVATE + pico_stdlib + pico_low_power + pico_status_led + hardware_adc +) + +# create map/bin/hex file etc. +pico_add_extra_outputs(low_power_dormant_timer) + +# add url via pico_set_program_url +example_auto_set_url(low_power_dormant_timer) + +pico_enable_stdio_usb(low_power_dormant_timer 0) +pico_enable_stdio_uart(low_power_dormant_timer 1) diff --git a/low_power/low_power_dormant_timer/low_power_dormant_timer.c b/low_power/low_power_dormant_timer/low_power_dormant_timer.c new file mode 100644 index 000000000..7a7afd126 --- /dev/null +++ b/low_power/low_power_dormant_timer/low_power_dormant_timer.c @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "hardware/powman.h" +#include "pico/low_power.h" +#include "pico/aon_timer.h" +#include "pico/status_led.h" + +// How long to wait +#define AWAKE_TIME_MS 10000 +#define SLEEP_TIME_MS 10000 + +#define RTC_GPIO 22 + +// Got to sleep and wakeup after 5 seconds +// The example will repeatedly wait 10 seconds then switch off for 10 seconds +// The debugger will appear to be unresponsive while the device is off +int main() { + + stdio_init_all(); + hard_assert(status_led_init()); + + struct timespec ts = { .tv_sec = 1723124088, .tv_nsec = 0 }; + aon_timer_start(&ts); + //powman_timer_set_1khz_tick_source_xosc(); + + uint32_t count = 1; + while(true) { + status_led_set_state(true); + + // Scratch register survives power down + printf("Wake up, test run: %u\n", count++); + + // Stay awake for a few seconds + printf("Awake for %dms\n", AWAKE_TIME_MS); + sleep_ms(AWAKE_TIME_MS); + + // power off + printf("Sleep for %dms\n", SLEEP_TIME_MS); + status_led_set_state(false); + absolute_time_t start_time = aon_timer_get_absolute_time(); + absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); + + /*clock_dest_bitset_t keep_enabled = clock_dest_bitset_none(); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_REF_TICKS); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_SYS_TIMER0);*/ + + int rc = low_power_dormant_until_aon_timer(wakeup_time, DORMANT_CLOCK_SOURCE_LPOSC, XOSC_HZ, RTC_GPIO, NULL); + status_led_set_state(true); + printf("low_power_dormant_until_aon_timer returned error %d\n", rc); + } + return 0; +} diff --git a/low_power/low_power_pstate_timer/CMakeLists.txt b/low_power/low_power_pstate_timer/CMakeLists.txt new file mode 100644 index 000000000..b79f2f09d --- /dev/null +++ b/low_power/low_power_pstate_timer/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(low_power_pstate_timer + low_power_pstate_timer.c + ) +target_link_libraries(low_power_pstate_timer PRIVATE + pico_stdlib + pico_low_power + pico_status_led + hardware_adc +) + +# create map/bin/hex file etc. +pico_add_extra_outputs(low_power_pstate_timer) + +# add url via pico_set_program_url +example_auto_set_url(low_power_pstate_timer) + +pico_enable_stdio_usb(low_power_pstate_timer 0) +pico_enable_stdio_uart(low_power_pstate_timer 1) diff --git a/low_power/low_power_pstate_timer/low_power_pstate_timer.c b/low_power/low_power_pstate_timer/low_power_pstate_timer.c new file mode 100644 index 000000000..ec57cb4e1 --- /dev/null +++ b/low_power/low_power_pstate_timer/low_power_pstate_timer.c @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "hardware/powman.h" +#include "pico/low_power.h" +#include "pico/aon_timer.h" +#include "pico/status_led.h" + +// How long to wait +#define AWAKE_TIME_MS 10000 +#define SLEEP_TIME_MS 10000 + +// Got to sleep and wakeup after 5 seconds +// The example will repeatedly wait 10 seconds then switch off for 10 seconds +// The debugger will appear to be unresponsive while the device is off +int main() { + + stdio_init_all(); + hard_assert(status_led_init()); + status_led_set_state(true); + + // Scratch register survives power down + printf("Wake up, test run: %u\n", powman_hw->scratch[0]++); + printf("Current power state: 0x%x\n", powman_get_power_state()); + + // Stay awake for a few seconds + printf("Awake for %dms\n", AWAKE_TIME_MS); + sleep_ms(AWAKE_TIME_MS); + + powman_set_debug_power_request_ignored(true); + powman_timer_start(); + + // power off + printf("Sleep for %dms\n", SLEEP_TIME_MS); + status_led_set_state(false); + absolute_time_t start_time = aon_timer_get_absolute_time(); + absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); + int rc = low_power_pstate_until_aon_timer(wakeup_time, NULL, NULL); + status_led_set_state(true); + printf("low_power_pstate_until_aon_timer returned error %d\n", rc); + hard_assert(false); // should never get here! + return 0; +} diff --git a/low_power/low_power_sleep_timer/CMakeLists.txt b/low_power/low_power_sleep_timer/CMakeLists.txt new file mode 100644 index 000000000..d878231be --- /dev/null +++ b/low_power/low_power_sleep_timer/CMakeLists.txt @@ -0,0 +1,18 @@ +add_executable(low_power_sleep_timer + low_power_sleep_timer.c + ) +target_link_libraries(low_power_sleep_timer PRIVATE + pico_stdlib + pico_low_power + pico_status_led + hardware_adc +) + +# create map/bin/hex file etc. +pico_add_extra_outputs(low_power_sleep_timer) + +# add url via pico_set_program_url +example_auto_set_url(low_power_sleep_timer) + +pico_enable_stdio_usb(low_power_sleep_timer 0) +pico_enable_stdio_uart(low_power_sleep_timer 1) diff --git a/low_power/low_power_sleep_timer/low_power_sleep_timer.c b/low_power/low_power_sleep_timer/low_power_sleep_timer.c new file mode 100644 index 000000000..4f41866e2 --- /dev/null +++ b/low_power/low_power_sleep_timer/low_power_sleep_timer.c @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "hardware/powman.h" +#include "pico/low_power.h" +#include "pico/aon_timer.h" +#include "pico/status_led.h" + +// How long to wait +#define AWAKE_TIME_MS 10000 +#define SLEEP_TIME_MS 10000 + +#define RTC_GPIO 22 + +// Got to sleep and wakeup after 5 seconds +// The example will repeatedly wait 10 seconds then switch off for 10 seconds +// The debugger will appear to be unresponsive while the device is off +int main() { + + stdio_init_all(); + hard_assert(status_led_init()); + + //struct timespec ts = { .tv_sec = 1723124088, .tv_nsec = 0 }; + //aon_timer_start(&ts); + //powman_timer_set_1khz_tick_source_xosc(); + + uint32_t count = 1; + while(true) { + status_led_set_state(true); + + // Scratch register survives power down + printf("Wake up, test run: %u\n", count++); + + // Stay awake for a few seconds + printf("Awake for %dms\n", AWAKE_TIME_MS); + sleep_ms(AWAKE_TIME_MS); + + // power off + printf("Sleep for %dms\n", SLEEP_TIME_MS); + status_led_set_state(false); + absolute_time_t start_time = get_absolute_time(); + absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); + + /*clock_dest_bitset_t keep_enabled = clock_dest_bitset_none(); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_REF_TICKS); + clock_dest_bitset_add(&keep_enabled, CLK_DEST_SYS_TIMER0);*/ + + int rc = low_power_sleep_until_default_timer(wakeup_time, NULL, true); + status_led_set_state(true); + printf("low_power_sleep_until_default_timer returned error %d\n", rc); + } + return 0; +} From d05851e0d90efddb322f040b243f5abbe9655793 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Wed, 22 Apr 2026 12:24:15 +0100 Subject: [PATCH 2/6] Dormant test changes --- .../low_power_dormant_timer/CMakeLists.txt | 25 +++++++++---- .../low_power_clksrc.c | 35 +++++++++++++++++++ .../low_power_dormant_timer.c | 34 ++++++++++++------ 3 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 low_power/low_power_dormant_timer/low_power_clksrc.c diff --git a/low_power/low_power_dormant_timer/CMakeLists.txt b/low_power/low_power_dormant_timer/CMakeLists.txt index 0880e93d0..c62fc1ce5 100644 --- a/low_power/low_power_dormant_timer/CMakeLists.txt +++ b/low_power/low_power_dormant_timer/CMakeLists.txt @@ -1,11 +1,11 @@ add_executable(low_power_dormant_timer - low_power_dormant_timer.c - ) + low_power_dormant_timer.c +) target_link_libraries(low_power_dormant_timer PRIVATE - pico_stdlib - pico_low_power - pico_status_led - hardware_adc + pico_stdlib + pico_low_power + pico_status_led + hardware_adc ) # create map/bin/hex file etc. @@ -16,3 +16,16 @@ example_auto_set_url(low_power_dormant_timer) pico_enable_stdio_usb(low_power_dormant_timer 0) pico_enable_stdio_uart(low_power_dormant_timer 1) + +# firmware for generating a clock on gp21 for running the dormant test on rp2040 +# connect the clock out from gp21 to gp20 (or gp22) on the rp2040 running the dormant test +add_executable(low_power_clksrc + low_power_clksrc.c +) +target_link_libraries(low_power_clksrc PRIVATE + pico_stdlib + pico_status_led +) +pico_add_extra_outputs(low_power_clksrc) +pico_enable_stdio_usb(low_power_clksrc 0) +pico_enable_stdio_uart(low_power_clksrc 1) diff --git a/low_power/low_power_dormant_timer/low_power_clksrc.c b/low_power/low_power_dormant_timer/low_power_clksrc.c new file mode 100644 index 000000000..4a849e861 --- /dev/null +++ b/low_power/low_power_dormant_timer/low_power_clksrc.c @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2026 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "pico/status_led.h" +#include "pico/sync.h" +#include "hardware/clocks.h" + +#ifndef LOW_POWER_CLKSRC_GPIO_OUT +#define LOW_POWER_CLKSRC_GPIO_OUT 21 +#endif + +bool repeater(repeating_timer_t *timer) { + printf(" Repeating timer at %dms\n", to_ms_since_boot(get_absolute_time())); + status_led_set_state(!status_led_get_state()); + return true; +} + +int main() { + stdio_init_all(); + status_led_init(); + + clock_gpio_init(LOW_POWER_CLKSRC_GPIO_OUT, CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLK_USB, 1024); // 48kHz + + repeating_timer_t repeat; + add_repeating_timer_ms(500, repeater, NULL, &repeat); + + while (true) __wfi(); + + return 0; +} \ No newline at end of file diff --git a/low_power/low_power_dormant_timer/low_power_dormant_timer.c b/low_power/low_power_dormant_timer/low_power_dormant_timer.c index 7a7afd126..ca2302d9e 100644 --- a/low_power/low_power_dormant_timer/low_power_dormant_timer.c +++ b/low_power/low_power_dormant_timer/low_power_dormant_timer.c @@ -6,28 +6,37 @@ #include #include "pico/stdlib.h" -#include "hardware/powman.h" #include "pico/low_power.h" #include "pico/aon_timer.h" #include "pico/status_led.h" +#ifdef PICO_RP2350 +#include "hardware/powman.h" +#endif + // How long to wait #define AWAKE_TIME_MS 10000 #define SLEEP_TIME_MS 10000 -#define RTC_GPIO 22 +#ifndef LOW_POWER_CLKSRC_GPIO_IN +#define LOW_POWER_CLKSRC_GPIO_IN 20 +#endif // Got to sleep and wakeup after 5 seconds // The example will repeatedly wait 10 seconds then switch off for 10 seconds // The debugger will appear to be unresponsive while the device is off int main() { - stdio_init_all(); +#if AWAKE_TIME_MS < 10000 + // pause for at least 10s to allow the debugger to attach on power up to allow the device to be re-programmed + printf("Waiting a bit to allow debugger to attach\n"); + sleep_ms(10000 - AWAKE_TIME_MS); +#endif + hard_assert(status_led_init()); struct timespec ts = { .tv_sec = 1723124088, .tv_nsec = 0 }; aon_timer_start(&ts); - //powman_timer_set_1khz_tick_source_xosc(); uint32_t count = 1; while(true) { @@ -46,13 +55,18 @@ int main() { absolute_time_t start_time = aon_timer_get_absolute_time(); absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); - /*clock_dest_bitset_t keep_enabled = clock_dest_bitset_none(); - clock_dest_bitset_add(&keep_enabled, CLK_DEST_REF_TICKS); - clock_dest_bitset_add(&keep_enabled, CLK_DEST_SYS_TIMER0);*/ - - int rc = low_power_dormant_until_aon_timer(wakeup_time, DORMANT_CLOCK_SOURCE_LPOSC, XOSC_HZ, RTC_GPIO, NULL); + int rc = low_power_dormant_until_aon_timer(wakeup_time, +#if PICO_RP2040 + DORMANT_CLOCK_SOURCE_XOSC, RTC_CLOCK_FREQ_HZ, +#else + DORMANT_CLOCK_SOURCE_LPOSC, XOSC_HZ, +#endif + LOW_POWER_CLKSRC_GPIO_IN, NULL); status_led_set_state(true); - printf("low_power_dormant_until_aon_timer returned error %d\n", rc); + if (rc != PICO_OK) { + printf("low_power_dormant_until_aon_timer returned error %d\n", rc); + hard_assert(false); + } } return 0; } From bdd6ce1a5aaa15f6881ef41339ed68b570e5496b Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Wed, 22 Apr 2026 12:24:54 +0100 Subject: [PATCH 3/6] Sleep test changes --- .../low_power_sleep_timer.c | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/low_power/low_power_sleep_timer/low_power_sleep_timer.c b/low_power/low_power_sleep_timer/low_power_sleep_timer.c index 4f41866e2..8dcf67347 100644 --- a/low_power/low_power_sleep_timer/low_power_sleep_timer.c +++ b/low_power/low_power_sleep_timer/low_power_sleep_timer.c @@ -6,29 +6,19 @@ #include #include "pico/stdlib.h" -#include "hardware/powman.h" #include "pico/low_power.h" -#include "pico/aon_timer.h" #include "pico/status_led.h" // How long to wait #define AWAKE_TIME_MS 10000 #define SLEEP_TIME_MS 10000 -#define RTC_GPIO 22 - -// Got to sleep and wakeup after 5 seconds -// The example will repeatedly wait 10 seconds then switch off for 10 seconds -// The debugger will appear to be unresponsive while the device is off +// Got to sleep and wakeup after 10 seconds int main() { stdio_init_all(); hard_assert(status_led_init()); - //struct timespec ts = { .tv_sec = 1723124088, .tv_nsec = 0 }; - //aon_timer_start(&ts); - //powman_timer_set_1khz_tick_source_xosc(); - uint32_t count = 1; while(true) { status_led_set_state(true); @@ -45,14 +35,13 @@ int main() { status_led_set_state(false); absolute_time_t start_time = get_absolute_time(); absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); - - /*clock_dest_bitset_t keep_enabled = clock_dest_bitset_none(); - clock_dest_bitset_add(&keep_enabled, CLK_DEST_REF_TICKS); - clock_dest_bitset_add(&keep_enabled, CLK_DEST_SYS_TIMER0);*/ int rc = low_power_sleep_until_default_timer(wakeup_time, NULL, true); status_led_set_state(true); - printf("low_power_sleep_until_default_timer returned error %d\n", rc); + if (rc != PICO_OK) { + printf("low_power_sleep_until_default_timer returned error %d\n", rc); + hard_assert(false); + } } return 0; } From 86638a9844245fea3da70aa59fc5a6f0e6ec3bc6 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Wed, 22 Apr 2026 14:45:02 +0100 Subject: [PATCH 4/6] Rename low power folders and implement gpio --- low_power/CMakeLists.txt | 6 +- .../CMakeLists.txt | 19 +++++-- .../low_power_clksrc.c | 0 .../low_power_dormant_gpio.c | 56 +++++++++++++++++++ .../low_power_dormant_timer.c | 18 +++--- low_power/low_power_pstate/CMakeLists.txt | 25 +++++++++ .../low_power_pstate/low_power_pstate_gpio.c | 46 +++++++++++++++ .../low_power_pstate_timer.c | 21 ++++--- .../low_power_pstate_timer/CMakeLists.txt | 18 ------ .../CMakeLists.txt | 19 +++++-- .../low_power_sleep/low_power_sleep_gpio.c | 50 +++++++++++++++++ .../low_power_sleep_timer.c | 9 +-- 12 files changed, 228 insertions(+), 59 deletions(-) rename low_power/{low_power_dormant_timer => low_power_dormant}/CMakeLists.txt (68%) rename low_power/{low_power_dormant_timer => low_power_dormant}/low_power_clksrc.c (100%) create mode 100644 low_power/low_power_dormant/low_power_dormant_gpio.c rename low_power/{low_power_dormant_timer => low_power_dormant}/low_power_dormant_timer.c (81%) create mode 100644 low_power/low_power_pstate/CMakeLists.txt create mode 100644 low_power/low_power_pstate/low_power_pstate_gpio.c rename low_power/{low_power_pstate_timer => low_power_pstate}/low_power_pstate_timer.c (63%) delete mode 100644 low_power/low_power_pstate_timer/CMakeLists.txt rename low_power/{low_power_sleep_timer => low_power_sleep}/CMakeLists.txt (50%) create mode 100644 low_power/low_power_sleep/low_power_sleep_gpio.c rename low_power/{low_power_sleep_timer => low_power_sleep}/low_power_sleep_timer.c (78%) diff --git a/low_power/CMakeLists.txt b/low_power/CMakeLists.txt index 3b151fa79..60be39993 100644 --- a/low_power/CMakeLists.txt +++ b/low_power/CMakeLists.txt @@ -12,7 +12,7 @@ if (0) ) endif() -add_subdirectory(low_power_sleep_timer) -add_subdirectory(low_power_dormant_timer) -add_subdirectory(low_power_pstate_timer) +add_subdirectory(low_power_sleep) +add_subdirectory(low_power_dormant) +add_subdirectory(low_power_pstate) #add_subdirectory(powman_gpio) diff --git a/low_power/low_power_dormant_timer/CMakeLists.txt b/low_power/low_power_dormant/CMakeLists.txt similarity index 68% rename from low_power/low_power_dormant_timer/CMakeLists.txt rename to low_power/low_power_dormant/CMakeLists.txt index c62fc1ce5..8932b5ac2 100644 --- a/low_power/low_power_dormant_timer/CMakeLists.txt +++ b/low_power/low_power_dormant/CMakeLists.txt @@ -5,18 +5,25 @@ target_link_libraries(low_power_dormant_timer PRIVATE pico_stdlib pico_low_power pico_status_led - hardware_adc ) - -# create map/bin/hex file etc. pico_add_extra_outputs(low_power_dormant_timer) - -# add url via pico_set_program_url example_auto_set_url(low_power_dormant_timer) - pico_enable_stdio_usb(low_power_dormant_timer 0) pico_enable_stdio_uart(low_power_dormant_timer 1) +add_executable(low_power_dormant_gpio + low_power_dormant_gpio.c +) +target_link_libraries(low_power_dormant_gpio PRIVATE + pico_stdlib + pico_low_power + pico_status_led +) +pico_add_extra_outputs(low_power_dormant_gpio) +example_auto_set_url(low_power_dormant_gpio) +pico_enable_stdio_usb(low_power_dormant_gpio 0) +pico_enable_stdio_uart(low_power_dormant_gpio 1) + # firmware for generating a clock on gp21 for running the dormant test on rp2040 # connect the clock out from gp21 to gp20 (or gp22) on the rp2040 running the dormant test add_executable(low_power_clksrc diff --git a/low_power/low_power_dormant_timer/low_power_clksrc.c b/low_power/low_power_dormant/low_power_clksrc.c similarity index 100% rename from low_power/low_power_dormant_timer/low_power_clksrc.c rename to low_power/low_power_dormant/low_power_clksrc.c diff --git a/low_power/low_power_dormant/low_power_dormant_gpio.c b/low_power/low_power_dormant/low_power_dormant_gpio.c new file mode 100644 index 000000000..cb00e937d --- /dev/null +++ b/low_power/low_power_dormant/low_power_dormant_gpio.c @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "pico/low_power.h" +#include "pico/status_led.h" + +// GPIO to wait to go high, connect the other end to 3V3 OUT +#ifndef LOW_POWER_WAKE_GPIO +#define LOW_POWER_WAKE_GPIO 15 +#endif + +// Got to sleep and wakeup when gpio goes high +int main() { + + stdio_init_all(); + hard_assert(status_led_init()); + gpio_init(LOW_POWER_WAKE_GPIO); + + uint32_t count = 1; + while(true) { + status_led_set_state(true); + + printf("Wake up, test run: %u\n", count++); + + // Wait for gpio to go low + if (gpio_get(LOW_POWER_WAKE_GPIO)) { + printf("Awake until gpio %u goes low\n", LOW_POWER_WAKE_GPIO); + while(gpio_get(LOW_POWER_WAKE_GPIO)) { + tight_loop_contents(); + } + } + + // go dormant + printf("Dormant until gpio %u goes high\n", LOW_POWER_WAKE_GPIO); + status_led_set_state(false); + + int rc = low_power_dormant_until_gpio_pin_state(LOW_POWER_WAKE_GPIO, true, true, +#if PICO_RP2040 + DORMANT_CLOCK_SOURCE_XOSC, +#else + DORMANT_CLOCK_SOURCE_LPOSC, +#endif + NULL); + status_led_set_state(true); + if (rc != PICO_OK) { + printf("low_power_dormant_until_aon_timer returned error %d\n", rc); + hard_assert(false); + } + } + return 0; +} diff --git a/low_power/low_power_dormant_timer/low_power_dormant_timer.c b/low_power/low_power_dormant/low_power_dormant_timer.c similarity index 81% rename from low_power/low_power_dormant_timer/low_power_dormant_timer.c rename to low_power/low_power_dormant/low_power_dormant_timer.c index ca2302d9e..087136ecc 100644 --- a/low_power/low_power_dormant_timer/low_power_dormant_timer.c +++ b/low_power/low_power_dormant/low_power_dormant_timer.c @@ -27,39 +27,35 @@ // The debugger will appear to be unresponsive while the device is off int main() { stdio_init_all(); + // Must start aon timer + struct timespec ts = { .tv_sec = 1776858754, .tv_nsec = 0 }; + aon_timer_start(&ts); #if AWAKE_TIME_MS < 10000 // pause for at least 10s to allow the debugger to attach on power up to allow the device to be re-programmed printf("Waiting a bit to allow debugger to attach\n"); sleep_ms(10000 - AWAKE_TIME_MS); #endif - hard_assert(status_led_init()); - - struct timespec ts = { .tv_sec = 1723124088, .tv_nsec = 0 }; - aon_timer_start(&ts); - uint32_t count = 1; while(true) { status_led_set_state(true); - // Scratch register survives power down printf("Wake up, test run: %u\n", count++); // Stay awake for a few seconds printf("Awake for %dms\n", AWAKE_TIME_MS); sleep_ms(AWAKE_TIME_MS); - // power off - printf("Sleep for %dms\n", SLEEP_TIME_MS); + // go dormant + printf("Dormant for %dms\n", SLEEP_TIME_MS); status_led_set_state(false); - absolute_time_t start_time = aon_timer_get_absolute_time(); - absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); + absolute_time_t wakeup_time = delayed_by_ms(aon_timer_get_absolute_time(), SLEEP_TIME_MS); // note MUST use aon_timer_get_absolute_time int rc = low_power_dormant_until_aon_timer(wakeup_time, #if PICO_RP2040 DORMANT_CLOCK_SOURCE_XOSC, RTC_CLOCK_FREQ_HZ, #else - DORMANT_CLOCK_SOURCE_LPOSC, XOSC_HZ, + DORMANT_CLOCK_SOURCE_LPOSC, 0, #endif LOW_POWER_CLKSRC_GPIO_IN, NULL); status_led_set_state(true); diff --git a/low_power/low_power_pstate/CMakeLists.txt b/low_power/low_power_pstate/CMakeLists.txt new file mode 100644 index 000000000..e35d61614 --- /dev/null +++ b/low_power/low_power_pstate/CMakeLists.txt @@ -0,0 +1,25 @@ +add_executable(low_power_pstate_timer + low_power_pstate_timer.c + ) +target_link_libraries(low_power_pstate_timer PRIVATE + pico_stdlib + pico_low_power + pico_status_led +) +pico_add_extra_outputs(low_power_pstate_timer) +example_auto_set_url(low_power_pstate_timer) +pico_enable_stdio_usb(low_power_pstate_timer 0) +pico_enable_stdio_uart(low_power_pstate_timer 1) + +add_executable(low_power_pstate_gpio + low_power_pstate_gpio.c + ) +target_link_libraries(low_power_pstate_gpio PRIVATE + pico_stdlib + pico_low_power + pico_status_led +) +pico_add_extra_outputs(low_power_pstate_gpio) +example_auto_set_url(low_power_pstate_gpio) +pico_enable_stdio_usb(low_power_pstate_gpio 0) +pico_enable_stdio_uart(low_power_pstate_gpio 1) diff --git a/low_power/low_power_pstate/low_power_pstate_gpio.c b/low_power/low_power_pstate/low_power_pstate_gpio.c new file mode 100644 index 000000000..eec1712de --- /dev/null +++ b/low_power/low_power_pstate/low_power_pstate_gpio.c @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "hardware/powman.h" +#include "pico/low_power.h" +#include "pico/aon_timer.h" +#include "pico/status_led.h" + +// GPIO to wait to go high, connect the other end to 3V3 OUT +#ifndef LOW_POWER_WAKE_GPIO +#define LOW_POWER_WAKE_GPIO 15 +#endif + +// The example will repeatedly wait 10 seconds then switch off for 10 seconds +// The debugger will appear to be unresponsive while the device is off +int main() { + stdio_init_all(); + + hard_assert(status_led_init()); + status_led_set_state(true); + + // Scratch register survives power down + printf("Wake up, test run: %u\n", powman_hw->scratch[0]++); + + // Wait for gpio to go low + if (gpio_get(LOW_POWER_WAKE_GPIO)) { + printf("Awake until gpio %u goes low\n", LOW_POWER_WAKE_GPIO); + while(gpio_get(LOW_POWER_WAKE_GPIO)) { + tight_loop_contents(); + } + } + + // power off + printf("Low power until gpio %u goes high\n", LOW_POWER_WAKE_GPIO); + status_led_set_state(false); + int rc = low_power_pstate_until_gpio_pin_state(LOW_POWER_WAKE_GPIO, true, true, NULL, NULL); + status_led_set_state(true); + printf("low_power_pstate_until_aon_timer returned error %d\n", rc); + hard_assert(false); // should never get here! + return 0; +} diff --git a/low_power/low_power_pstate_timer/low_power_pstate_timer.c b/low_power/low_power_pstate/low_power_pstate_timer.c similarity index 63% rename from low_power/low_power_pstate_timer/low_power_pstate_timer.c rename to low_power/low_power_pstate/low_power_pstate_timer.c index ec57cb4e1..9c0881406 100644 --- a/low_power/low_power_pstate_timer/low_power_pstate_timer.c +++ b/low_power/low_power_pstate/low_power_pstate_timer.c @@ -15,31 +15,34 @@ #define AWAKE_TIME_MS 10000 #define SLEEP_TIME_MS 10000 -// Got to sleep and wakeup after 5 seconds // The example will repeatedly wait 10 seconds then switch off for 10 seconds // The debugger will appear to be unresponsive while the device is off int main() { - stdio_init_all(); + // Must start the aon timer if needed + if (!aon_timer_is_running()) { + struct timespec ts = { .tv_sec = 1776858754, .tv_nsec = 0 }; + hard_assert(aon_timer_start(&ts)); + } +#if AWAKE_TIME_MS < 10000 + // pause for at least 10s to allow the debugger to attach on power up to allow the device to be re-programmed + printf("Waiting a bit to allow debugger to attach\n"); + sleep_ms(10000 - AWAKE_TIME_MS); +#endif hard_assert(status_led_init()); status_led_set_state(true); // Scratch register survives power down printf("Wake up, test run: %u\n", powman_hw->scratch[0]++); - printf("Current power state: 0x%x\n", powman_get_power_state()); // Stay awake for a few seconds printf("Awake for %dms\n", AWAKE_TIME_MS); sleep_ms(AWAKE_TIME_MS); - powman_set_debug_power_request_ignored(true); - powman_timer_start(); - // power off - printf("Sleep for %dms\n", SLEEP_TIME_MS); + printf("Low power for %dms\n", SLEEP_TIME_MS); status_led_set_state(false); - absolute_time_t start_time = aon_timer_get_absolute_time(); - absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); + absolute_time_t wakeup_time = delayed_by_ms(aon_timer_get_absolute_time(), SLEEP_TIME_MS); // note: MUST use aon_timer_get_absolute_time int rc = low_power_pstate_until_aon_timer(wakeup_time, NULL, NULL); status_led_set_state(true); printf("low_power_pstate_until_aon_timer returned error %d\n", rc); diff --git a/low_power/low_power_pstate_timer/CMakeLists.txt b/low_power/low_power_pstate_timer/CMakeLists.txt deleted file mode 100644 index b79f2f09d..000000000 --- a/low_power/low_power_pstate_timer/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -add_executable(low_power_pstate_timer - low_power_pstate_timer.c - ) -target_link_libraries(low_power_pstate_timer PRIVATE - pico_stdlib - pico_low_power - pico_status_led - hardware_adc -) - -# create map/bin/hex file etc. -pico_add_extra_outputs(low_power_pstate_timer) - -# add url via pico_set_program_url -example_auto_set_url(low_power_pstate_timer) - -pico_enable_stdio_usb(low_power_pstate_timer 0) -pico_enable_stdio_uart(low_power_pstate_timer 1) diff --git a/low_power/low_power_sleep_timer/CMakeLists.txt b/low_power/low_power_sleep/CMakeLists.txt similarity index 50% rename from low_power/low_power_sleep_timer/CMakeLists.txt rename to low_power/low_power_sleep/CMakeLists.txt index d878231be..b143d5364 100644 --- a/low_power/low_power_sleep_timer/CMakeLists.txt +++ b/low_power/low_power_sleep/CMakeLists.txt @@ -5,14 +5,21 @@ target_link_libraries(low_power_sleep_timer PRIVATE pico_stdlib pico_low_power pico_status_led - hardware_adc ) - -# create map/bin/hex file etc. pico_add_extra_outputs(low_power_sleep_timer) - -# add url via pico_set_program_url example_auto_set_url(low_power_sleep_timer) - pico_enable_stdio_usb(low_power_sleep_timer 0) pico_enable_stdio_uart(low_power_sleep_timer 1) + +add_executable(low_power_sleep_gpio + low_power_sleep_gpio.c + ) +target_link_libraries(low_power_sleep_gpio PRIVATE + pico_stdlib + pico_low_power + pico_status_led +) +pico_add_extra_outputs(low_power_sleep_gpio) +example_auto_set_url(low_power_sleep_gpio) +pico_enable_stdio_usb(low_power_sleep_gpio 0) +pico_enable_stdio_uart(low_power_sleep_gpio 1) diff --git a/low_power/low_power_sleep/low_power_sleep_gpio.c b/low_power/low_power_sleep/low_power_sleep_gpio.c new file mode 100644 index 000000000..450c0cf79 --- /dev/null +++ b/low_power/low_power_sleep/low_power_sleep_gpio.c @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2024 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "pico/low_power.h" +#include "pico/status_led.h" + +// GPIO to wait to go high, connect the other end to 3V3 OUT +#ifndef LOW_POWER_WAKE_GPIO +#define LOW_POWER_WAKE_GPIO 15 +#endif + +// Got to sleep and wakeup after 10 seconds +int main() { + + stdio_init_all(); + hard_assert(status_led_init()); + gpio_init(LOW_POWER_WAKE_GPIO); + + uint32_t count = 1; + while(true) { + status_led_set_state(true); + + printf("Wake up, test run: %u\n", count++); + + // Wait for gpio to go low + if (gpio_get(LOW_POWER_WAKE_GPIO)) { + printf("Awake until gpio %u goes low\n", LOW_POWER_WAKE_GPIO); + while(gpio_get(LOW_POWER_WAKE_GPIO)) { + tight_loop_contents(); + } + } + + // go to sleep + printf("Sleeping until gpio %u goes high\n", LOW_POWER_WAKE_GPIO); + status_led_set_state(false); + + int rc = low_power_sleep_until_gpio_pin_state(LOW_POWER_WAKE_GPIO, true, true, NULL, true); + status_led_set_state(true); + if (rc != PICO_OK) { + printf("low_power_sleep_until_gpio_pin_state returned error %d\n", rc); + hard_assert(false); + } + } + return 0; +} diff --git a/low_power/low_power_sleep_timer/low_power_sleep_timer.c b/low_power/low_power_sleep/low_power_sleep_timer.c similarity index 78% rename from low_power/low_power_sleep_timer/low_power_sleep_timer.c rename to low_power/low_power_sleep/low_power_sleep_timer.c index 8dcf67347..7d119eba4 100644 --- a/low_power/low_power_sleep_timer/low_power_sleep_timer.c +++ b/low_power/low_power_sleep/low_power_sleep_timer.c @@ -23,19 +23,16 @@ int main() { while(true) { status_led_set_state(true); - // Scratch register survives power down printf("Wake up, test run: %u\n", count++); // Stay awake for a few seconds printf("Awake for %dms\n", AWAKE_TIME_MS); sleep_ms(AWAKE_TIME_MS); - // power off - printf("Sleep for %dms\n", SLEEP_TIME_MS); + // go to sleep + printf("Sleeping for %dms\n", SLEEP_TIME_MS); status_led_set_state(false); - absolute_time_t start_time = get_absolute_time(); - absolute_time_t wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS); - + absolute_time_t wakeup_time = delayed_by_ms(get_absolute_time(), SLEEP_TIME_MS); int rc = low_power_sleep_until_default_timer(wakeup_time, NULL, true); status_led_set_state(true); if (rc != PICO_OK) { From 368095b975533523557b33101d83f29d10874732 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Wed, 22 Apr 2026 14:59:43 +0100 Subject: [PATCH 5/6] Use persistent data --- low_power/low_power_pstate/CMakeLists.txt | 2 ++ low_power/low_power_pstate/low_power_pstate_gpio.c | 4 +++- low_power/low_power_pstate/low_power_pstate_timer.c | 4 +++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/low_power/low_power_pstate/CMakeLists.txt b/low_power/low_power_pstate/CMakeLists.txt index e35d61614..5889d7b6a 100644 --- a/low_power/low_power_pstate/CMakeLists.txt +++ b/low_power/low_power_pstate/CMakeLists.txt @@ -6,6 +6,7 @@ target_link_libraries(low_power_pstate_timer PRIVATE pico_low_power pico_status_led ) +pico_set_persistent_data_loc(low_power_pstate_timer 0x20040000) # SRAM1 power domain pico_add_extra_outputs(low_power_pstate_timer) example_auto_set_url(low_power_pstate_timer) pico_enable_stdio_usb(low_power_pstate_timer 0) @@ -19,6 +20,7 @@ target_link_libraries(low_power_pstate_gpio PRIVATE pico_low_power pico_status_led ) +pico_set_persistent_data_loc(low_power_pstate_gpio 0x20040000) # SRAM1 power domain pico_add_extra_outputs(low_power_pstate_gpio) example_auto_set_url(low_power_pstate_gpio) pico_enable_stdio_usb(low_power_pstate_gpio 0) diff --git a/low_power/low_power_pstate/low_power_pstate_gpio.c b/low_power/low_power_pstate/low_power_pstate_gpio.c index eec1712de..be1e14666 100644 --- a/low_power/low_power_pstate/low_power_pstate_gpio.c +++ b/low_power/low_power_pstate/low_power_pstate_gpio.c @@ -16,6 +16,8 @@ #define LOW_POWER_WAKE_GPIO 15 #endif +uint32_t __persistent_data(run_count); + // The example will repeatedly wait 10 seconds then switch off for 10 seconds // The debugger will appear to be unresponsive while the device is off int main() { @@ -25,7 +27,7 @@ int main() { status_led_set_state(true); // Scratch register survives power down - printf("Wake up, test run: %u\n", powman_hw->scratch[0]++); + printf("Wake up, test run: %u\n", run_count++); // Wait for gpio to go low if (gpio_get(LOW_POWER_WAKE_GPIO)) { diff --git a/low_power/low_power_pstate/low_power_pstate_timer.c b/low_power/low_power_pstate/low_power_pstate_timer.c index 9c0881406..09a1fdbb4 100644 --- a/low_power/low_power_pstate/low_power_pstate_timer.c +++ b/low_power/low_power_pstate/low_power_pstate_timer.c @@ -15,6 +15,8 @@ #define AWAKE_TIME_MS 10000 #define SLEEP_TIME_MS 10000 +uint32_t __persistent_data(run_count); + // The example will repeatedly wait 10 seconds then switch off for 10 seconds // The debugger will appear to be unresponsive while the device is off int main() { @@ -33,7 +35,7 @@ int main() { status_led_set_state(true); // Scratch register survives power down - printf("Wake up, test run: %u\n", powman_hw->scratch[0]++); + printf("Wake up, test run: %u\n", run_count++); // Stay awake for a few seconds printf("Awake for %dms\n", AWAKE_TIME_MS); From 55b07b6bc049bdfeb4e334d28cf2656b6055ba25 Mon Sep 17 00:00:00 2001 From: Peter Harper Date: Fri, 24 Apr 2026 12:40:41 +0100 Subject: [PATCH 6/6] Update examples for latest version of low power API --- low_power/CMakeLists.txt | 19 ++---------- .../low_power_dormant/low_power_clksrc.c | 6 ++-- .../low_power_dormant_gpio.c | 16 +++++----- .../low_power_dormant_timer.c | 31 ++++++++++++------- .../low_power_pstate/low_power_pstate_gpio.c | 1 - .../low_power_pstate/low_power_pstate_timer.c | 14 +++++---- .../low_power_sleep/low_power_sleep_timer.c | 9 ++++-- 7 files changed, 49 insertions(+), 47 deletions(-) diff --git a/low_power/CMakeLists.txt b/low_power/CMakeLists.txt index 60be39993..e53891a6c 100644 --- a/low_power/CMakeLists.txt +++ b/low_power/CMakeLists.txt @@ -1,18 +1,5 @@ -if (0) - add_library(powman_example INTERFACE) - target_sources(powman_example INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/powman_example.c - ) - target_include_directories(powman_example INTERFACE - ${CMAKE_CURRENT_LIST_DIR} - ) - target_link_libraries(powman_example INTERFACE - hardware_powman - hardware_gpio - ) -endif() - add_subdirectory(low_power_sleep) add_subdirectory(low_power_dormant) -add_subdirectory(low_power_pstate) -#add_subdirectory(powman_gpio) +if (NOT PICO_RP2040) + add_subdirectory(low_power_pstate) +endif() diff --git a/low_power/low_power_dormant/low_power_clksrc.c b/low_power/low_power_dormant/low_power_clksrc.c index 4a849e861..c76c248d8 100644 --- a/low_power/low_power_dormant/low_power_clksrc.c +++ b/low_power/low_power_dormant/low_power_clksrc.c @@ -10,8 +10,8 @@ #include "pico/sync.h" #include "hardware/clocks.h" -#ifndef LOW_POWER_CLKSRC_GPIO_OUT -#define LOW_POWER_CLKSRC_GPIO_OUT 21 +#ifndef RTC_CLOCK_SRC_GPIO_OUT +#define RTC_CLOCK_SRC_GPIO_OUT 21 #endif bool repeater(repeating_timer_t *timer) { @@ -24,7 +24,7 @@ int main() { stdio_init_all(); status_led_init(); - clock_gpio_init(LOW_POWER_CLKSRC_GPIO_OUT, CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLK_USB, 1024); // 48kHz + clock_gpio_init(RTC_CLOCK_SRC_GPIO_OUT, CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLK_USB, 1024); // 48kHz repeating_timer_t repeat; add_repeating_timer_ms(500, repeater, NULL, &repeat); diff --git a/low_power/low_power_dormant/low_power_dormant_gpio.c b/low_power/low_power_dormant/low_power_dormant_gpio.c index cb00e937d..f006847f0 100644 --- a/low_power/low_power_dormant/low_power_dormant_gpio.c +++ b/low_power/low_power_dormant/low_power_dormant_gpio.c @@ -14,6 +14,14 @@ #define LOW_POWER_WAKE_GPIO 15 #endif +#ifndef CLOCK_SOURCE +#if PICO_RP2040 +#define CLOCK_SOURCE DORMANT_CLOCK_SOURCE_XOSC +#else +#define CLOCK_SOURCE DORMANT_CLOCK_SOURCE_LPOSC +#endif +#endif + // Got to sleep and wakeup when gpio goes high int main() { @@ -39,13 +47,7 @@ int main() { printf("Dormant until gpio %u goes high\n", LOW_POWER_WAKE_GPIO); status_led_set_state(false); - int rc = low_power_dormant_until_gpio_pin_state(LOW_POWER_WAKE_GPIO, true, true, -#if PICO_RP2040 - DORMANT_CLOCK_SOURCE_XOSC, -#else - DORMANT_CLOCK_SOURCE_LPOSC, -#endif - NULL); + int rc = low_power_dormant_until_gpio_pin_state(LOW_POWER_WAKE_GPIO, true, true, CLOCK_SOURCE, NULL); status_led_set_state(true); if (rc != PICO_OK) { printf("low_power_dormant_until_aon_timer returned error %d\n", rc); diff --git a/low_power/low_power_dormant/low_power_dormant_timer.c b/low_power/low_power_dormant/low_power_dormant_timer.c index 087136ecc..823e2d162 100644 --- a/low_power/low_power_dormant/low_power_dormant_timer.c +++ b/low_power/low_power_dormant/low_power_dormant_timer.c @@ -15,11 +15,25 @@ #endif // How long to wait +#ifndef AWAKE_TIME_MS #define AWAKE_TIME_MS 10000 +#endif +#ifndef SLEEP_TIME_MS #define SLEEP_TIME_MS 10000 +#endif + +#if PICO_RP2040 +#ifndef RTC_CLOCK_SRC_GPIO_IN +#define RTC_CLOCK_SRC_GPIO_IN 20 +#endif +#endif -#ifndef LOW_POWER_CLKSRC_GPIO_IN -#define LOW_POWER_CLKSRC_GPIO_IN 20 +#ifndef CLOCK_SOURCE +#if PICO_RP2040 +#define CLOCK_SOURCE DORMANT_CLOCK_SOURCE_XOSC +#else +#define CLOCK_SOURCE DORMANT_CLOCK_SOURCE_LPOSC +#endif #endif // Got to sleep and wakeup after 5 seconds @@ -28,8 +42,7 @@ int main() { stdio_init_all(); // Must start aon timer - struct timespec ts = { .tv_sec = 1776858754, .tv_nsec = 0 }; - aon_timer_start(&ts); + low_power_start_aon_timer_at_time_ms(1776858754000); #if AWAKE_TIME_MS < 10000 // pause for at least 10s to allow the debugger to attach on power up to allow the device to be re-programmed printf("Waiting a bit to allow debugger to attach\n"); @@ -50,17 +63,13 @@ int main() { printf("Dormant for %dms\n", SLEEP_TIME_MS); status_led_set_state(false); - absolute_time_t wakeup_time = delayed_by_ms(aon_timer_get_absolute_time(), SLEEP_TIME_MS); // note MUST use aon_timer_get_absolute_time - int rc = low_power_dormant_until_aon_timer(wakeup_time, #if PICO_RP2040 - DORMANT_CLOCK_SOURCE_XOSC, RTC_CLOCK_FREQ_HZ, -#else - DORMANT_CLOCK_SOURCE_LPOSC, 0, + low_power_set_external_clock_source(RTC_CLOCK_FREQ_HZ, RTC_CLOCK_SRC_GPIO_IN); #endif - LOW_POWER_CLKSRC_GPIO_IN, NULL); + int rc = low_power_dormant_for_ms(SLEEP_TIME_MS, CLOCK_SOURCE, NULL); status_led_set_state(true); if (rc != PICO_OK) { - printf("low_power_dormant_until_aon_timer returned error %d\n", rc); + printf("low_power_dormant_for_ms returned error %d\n", rc); hard_assert(false); } } diff --git a/low_power/low_power_pstate/low_power_pstate_gpio.c b/low_power/low_power_pstate/low_power_pstate_gpio.c index be1e14666..9981f5813 100644 --- a/low_power/low_power_pstate/low_power_pstate_gpio.c +++ b/low_power/low_power_pstate/low_power_pstate_gpio.c @@ -6,7 +6,6 @@ #include #include "pico/stdlib.h" -#include "hardware/powman.h" #include "pico/low_power.h" #include "pico/aon_timer.h" #include "pico/status_led.h" diff --git a/low_power/low_power_pstate/low_power_pstate_timer.c b/low_power/low_power_pstate/low_power_pstate_timer.c index 09a1fdbb4..05cc905d3 100644 --- a/low_power/low_power_pstate/low_power_pstate_timer.c +++ b/low_power/low_power_pstate/low_power_pstate_timer.c @@ -6,14 +6,17 @@ #include #include "pico/stdlib.h" -#include "hardware/powman.h" #include "pico/low_power.h" #include "pico/aon_timer.h" #include "pico/status_led.h" // How long to wait +#ifndef AWAKE_TIME_MS #define AWAKE_TIME_MS 10000 +#endif +#ifndef SLEEP_TIME_MS #define SLEEP_TIME_MS 10000 +#endif uint32_t __persistent_data(run_count); @@ -22,9 +25,9 @@ uint32_t __persistent_data(run_count); int main() { stdio_init_all(); // Must start the aon timer if needed + printf("Current time: %llu\n", aon_timer_get_absolute_time()); if (!aon_timer_is_running()) { - struct timespec ts = { .tv_sec = 1776858754, .tv_nsec = 0 }; - hard_assert(aon_timer_start(&ts)); + low_power_start_aon_timer_at_time_ms(1776858754000); } #if AWAKE_TIME_MS < 10000 // pause for at least 10s to allow the debugger to attach on power up to allow the device to be re-programmed @@ -44,10 +47,9 @@ int main() { // power off printf("Low power for %dms\n", SLEEP_TIME_MS); status_led_set_state(false); - absolute_time_t wakeup_time = delayed_by_ms(aon_timer_get_absolute_time(), SLEEP_TIME_MS); // note: MUST use aon_timer_get_absolute_time - int rc = low_power_pstate_until_aon_timer(wakeup_time, NULL, NULL); + int rc = low_power_pstate_for_ms(SLEEP_TIME_MS, NULL, NULL); status_led_set_state(true); - printf("low_power_pstate_until_aon_timer returned error %d\n", rc); + printf("low_power_pstate_for_ms returned error %d\n", rc); hard_assert(false); // should never get here! return 0; } diff --git a/low_power/low_power_sleep/low_power_sleep_timer.c b/low_power/low_power_sleep/low_power_sleep_timer.c index 7d119eba4..1b55aa2eb 100644 --- a/low_power/low_power_sleep/low_power_sleep_timer.c +++ b/low_power/low_power_sleep/low_power_sleep_timer.c @@ -10,8 +10,12 @@ #include "pico/status_led.h" // How long to wait +#ifndef AWAKE_TIME_MS #define AWAKE_TIME_MS 10000 +#endif +#ifndef SLEEP_TIME_MS #define SLEEP_TIME_MS 10000 +#endif // Got to sleep and wakeup after 10 seconds int main() { @@ -32,11 +36,10 @@ int main() { // go to sleep printf("Sleeping for %dms\n", SLEEP_TIME_MS); status_led_set_state(false); - absolute_time_t wakeup_time = delayed_by_ms(get_absolute_time(), SLEEP_TIME_MS); - int rc = low_power_sleep_until_default_timer(wakeup_time, NULL, true); + int rc = low_power_sleep_for_ms(SLEEP_TIME_MS, NULL, true); status_led_set_state(true); if (rc != PICO_OK) { - printf("low_power_sleep_until_default_timer returned error %d\n", rc); + printf("low_power_sleep_for_ms returned error %d\n", rc); hard_assert(false); } }