diff --git a/sleep/hello_sleep/CMakeLists.txt b/sleep/hello_sleep/CMakeLists.txt index a45f6ac42..76a9a9257 100644 --- a/sleep/hello_sleep/CMakeLists.txt +++ b/sleep/hello_sleep/CMakeLists.txt @@ -1,8 +1,17 @@ -add_executable(hello_sleep - hello_sleep.c +add_executable(hello_sleep_rtc + hello_sleep_rtc.c ) +target_link_libraries(hello_sleep_rtc + pico_stdlib + hardware_sleep + ) +pico_add_extra_outputs(hello_sleep_rtc) -target_link_libraries(hello_sleep pico_stdlib hardware_sleep) - -# create map/bin/hex file etc. -pico_add_extra_outputs(hello_sleep) \ No newline at end of file +add_executable(hello_sleep_alarm + hello_sleep_alarm.c + ) +target_link_libraries(hello_sleep_alarm + pico_stdlib + hardware_sleep + ) +pico_add_extra_outputs(hello_sleep_alarm) \ No newline at end of file diff --git a/sleep/hello_sleep/hello_sleep_alarm.c b/sleep/hello_sleep/hello_sleep_alarm.c new file mode 100644 index 000000000..dff81f777 --- /dev/null +++ b/sleep/hello_sleep/hello_sleep_alarm.c @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include "pico/stdlib.h" +#include "pico/sleep.h" +#include "hardware/clocks.h" +#include "hardware/pll.h" + +static bool awake; + +static void alarm_sleep_callback(uint alarm_id) { + printf("timer woke us up\n"); + uart_default_tx_wait_blocking(); + awake = true; + + hardware_alarm_set_callback(alarm_id, NULL); + hardware_alarm_unclaim(alarm_id); +} + +int main() { + + stdio_init_all(); + + printf("Hello Alarm Sleep!\n"); + + do { + printf("Awake for 10 seconds\n"); + sleep_ms(1000 * 10); + + printf("Switching to XOSC\n"); + + // Wait for the fifo to be drained so we get reliable output + uart_default_tx_wait_blocking(); + + // Set the crystal oscillator as the dormant clock source, UART will be reconfigured from here + // This is necessary before sending the pico to sleep + sleep_run_from_xosc(); + + printf("Switched to XOSC\n"); + + awake = false; + + // Go to sleep until the alarm interrupt is generated after 10 seconds + printf("Sleeping for 10 seconds\n"); + uart_default_tx_wait_blocking(); + + int alarm_id = -1; + sleep_goto_sleep_for(10000, &alarm_sleep_callback, &alarm_id); + if (alarm_id >= 0) { + // Make sure we don't wake + while (!awake) { + printf("Should be sleeping\n"); + } + } + + //Re-enabling clock sources and generators. + sleep_power_up(); + } while(true); + + return 0; +} diff --git a/sleep/hello_sleep/hello_sleep.c b/sleep/hello_sleep/hello_sleep_rtc.c similarity index 98% rename from sleep/hello_sleep/hello_sleep.c rename to sleep/hello_sleep/hello_sleep_rtc.c index 4d133ae0a..920789bb4 100644 --- a/sleep/hello_sleep/hello_sleep.c +++ b/sleep/hello_sleep/hello_sleep_rtc.c @@ -56,7 +56,7 @@ int main() { stdio_init_all(); - printf("Hello Sleep!\n"); + printf("Hello RTC Sleep!\n"); do { printf("Awake for 10 seconds\n");