Skip to content

Commit

Permalink
Add hello_sleep_alarm
Browse files Browse the repository at this point in the history
Might get rid of all the RTC examples - why use RTC?
  • Loading branch information
peterharperuk committed May 24, 2024
1 parent 6e0db9d commit ff3ba53
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
21 changes: 15 additions & 6 deletions sleep/hello_sleep/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
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)
65 changes: 65 additions & 0 deletions sleep/hello_sleep/hello_sleep_alarm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <stdio.h>
#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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit ff3ba53

Please sign in to comment.