Skip to content

Commit 62cb6e9

Browse files
committed
Update to work with hwtests
Use external_sleep_timer to check sleeps are actually 2s, and use that instead of rtc_clksrc
1 parent 4f1b194 commit 62cb6e9

10 files changed

Lines changed: 248 additions & 112 deletions

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if (PICO_TEST_FILE_GENERATOR AND EXISTS ${PICO_TEST_FILE_GENERATOR})
3737
# - PICO_TEST_FAILURE_STRING: Failure string for the test (default is "FAILURE_STRING")
3838
# Can be used to force failure even when the test prints the success string
3939
# - PICO_TEST_BUDDY_FILE: File to run on buddy device for the test (default is NONE)
40-
# For example, low power dormant tests on RP2040 require rtc_clksrc running on the buddy device
40+
# For example, low power tests use external_sleep_timer running on the buddy device
4141
# This can either be a path relative to the test target, or an absolute path
4242
# - PICO_TEST_SKIP_IN_CI: Whether to skip the test in ci (default is FALSE)
4343
# If TRUE, the test is not included in the ci test suite
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1+
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
2+
load("@rules_cc//cc:cc_library.bzl", "cc_library")
13
load("//bazel:defs.bzl", "compatible_with_rp2")
24

35
package(default_visibility = ["//visibility:public"])
46

7+
cc_library(
8+
name = "low_power_test_common",
9+
hdrs = ["low_power_test_common.h"],
10+
includes = ["."],
11+
deps = [
12+
"//src/rp2_common/pico_stdlib",
13+
"//src/rp2_common/pico_low_power",
14+
"//src/rp2_common/pico_status_led",
15+
],
16+
)
17+
518
cc_binary(
619
name = "low_power_test_timers",
720
testonly = True,
821
srcs = ["low_power_test_timers.c"],
922
target_compatible_with = compatible_with_rp2(),
1023
deps = [
11-
"//src/rp2_common/pico_stdlib",
12-
"//src/rp2_common/pico_low_power",
13-
"//src/rp2_common/pico_status_led",
24+
"low_power_test_common",
1425
],
1526
)
1627

@@ -20,9 +31,7 @@ cc_binary(
2031
srcs = ["low_power_test_gpio.c"],
2132
target_compatible_with = compatible_with_rp2(),
2233
deps = [
23-
"//src/rp2_common/pico_stdlib",
24-
"//src/rp2_common/pico_low_power",
25-
"//src/rp2_common/pico_status_led",
34+
"low_power_test_common",
2635
],
2736
)
2837

@@ -32,19 +41,16 @@ cc_binary(
3241
srcs = ["low_power_test_simple.c"],
3342
target_compatible_with = compatible_with_rp2(),
3443
deps = [
35-
"//src/rp2_common/pico_stdlib",
36-
"//src/rp2_common/pico_low_power",
37-
"//src/rp2_common/pico_status_led",
44+
"low_power_test_common",
3845
],
3946
)
4047

4148
cc_binary(
42-
name = "rtc_clksrc",
49+
name = "external_sleep_timer",
4350
testonly = True,
44-
srcs = ["rtc_clksrc.c"],
45-
target_compatible_with = ["//bazel/constraint:rp2040"],
51+
srcs = ["external_sleep_timer.c"],
52+
target_compatible_with = compatible_with_rp2(),
4653
deps = [
47-
"//src/rp2_common/pico_stdlib",
48-
"//src/rp2_common/pico_status_led",
54+
"low_power_test_common",
4955
],
5056
)

test/pico_low_power_test/CMakeLists.txt

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1+
add_library(low_power_test_common INTERFACE)
2+
target_link_libraries(low_power_test_common INTERFACE pico_stdlib pico_low_power pico_status_led)
3+
target_include_directories(low_power_test_common INTERFACE ${CMAKE_CURRENT_LIST_DIR})
4+
15
add_executable(low_power_test_timers
26
low_power_test_timers.c
37
)
4-
target_link_libraries(low_power_test_timers pico_stdlib pico_low_power pico_status_led)
8+
target_link_libraries(low_power_test_timers low_power_test_common)
9+
set_target_properties(low_power_test_timers PROPERTIES PICO_TEST_TIMEOUT 35)
10+
set_target_properties(low_power_test_timers PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
11+
set_target_properties(low_power_test_timers PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
512
# create map/bin/hex file etc.
613
pico_add_extra_outputs(low_power_test_timers)
714

815

916
add_executable(low_power_test_timers_usb
1017
low_power_test_timers.c
1118
)
12-
target_link_libraries(low_power_test_timers_usb pico_stdlib pico_low_power pico_status_led)
19+
target_link_libraries(low_power_test_timers_usb low_power_test_common)
1320
pico_enable_stdio_usb(low_power_test_timers_usb 1)
21+
set_target_properties(low_power_test_timers_usb PROPERTIES PICO_TEST_TIMEOUT 35)
22+
set_target_properties(low_power_test_timers_usb PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
23+
set_target_properties(low_power_test_timers_usb PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
1424
# create map/bin/hex file etc.
1525
pico_add_extra_outputs(low_power_test_timers_usb)
1626

@@ -20,11 +30,14 @@ if (NOT PICO_RP2040)
2030
add_executable(low_power_test_timers_encrypted
2131
low_power_test_timers.c
2232
)
23-
target_link_libraries(low_power_test_timers_encrypted pico_stdlib pico_low_power pico_status_led)
33+
target_link_libraries(low_power_test_timers_encrypted low_power_test_common)
2434
pico_set_binary_type(low_power_test_timers_encrypted no_flash)
2535
pico_package_uf2_output(low_power_test_timers_encrypted)
2636
pico_sign_binary(low_power_test_timers_encrypted ${PICO_SDK_PATH}/tools/example_keys/private.pem)
2737
pico_encrypt_binary(low_power_test_timers_encrypted ${PICO_SDK_PATH}/tools/example_keys/privateaes.bin ${PICO_SDK_PATH}/tools/example_keys/ivsalt.bin EMBED NO_CLEAR)
38+
set_target_properties(low_power_test_timers_encrypted PROPERTIES PICO_TEST_TIMEOUT 35)
39+
set_target_properties(low_power_test_timers_encrypted PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
40+
set_target_properties(low_power_test_timers_encrypted PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
2841
# create map/bin/hex file etc.
2942
pico_add_extra_outputs(low_power_test_timers_encrypted)
3043
endif()
@@ -33,71 +46,94 @@ if (NOT PICO_RP2040)
3346
add_executable(low_power_test_timers_sram0
3447
low_power_test_timers.c
3548
)
36-
target_link_libraries(low_power_test_timers_sram0 pico_stdlib pico_low_power pico_status_led)
49+
target_link_libraries(low_power_test_timers_sram0 low_power_test_common)
3750
pico_set_persistent_data_loc(low_power_test_timers_sram0 0x20020000)
3851
# create map/bin/hex file etc.
52+
set_target_properties(low_power_test_timers_sram0 PROPERTIES PICO_TEST_TIMEOUT 35)
53+
set_target_properties(low_power_test_timers_sram0 PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
54+
set_target_properties(low_power_test_timers_sram0 PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
3955
pico_add_extra_outputs(low_power_test_timers_sram0)
4056

4157

4258
add_executable(low_power_test_timers_sram1
4359
low_power_test_timers.c
4460
)
45-
target_link_libraries(low_power_test_timers_sram1 pico_stdlib pico_low_power pico_status_led)
61+
target_link_libraries(low_power_test_timers_sram1 low_power_test_common)
4662
pico_set_persistent_data_loc(low_power_test_timers_sram1 0x20040000)
4763
# create map/bin/hex file etc.
64+
set_target_properties(low_power_test_timers_sram1 PROPERTIES PICO_TEST_TIMEOUT 35)
65+
set_target_properties(low_power_test_timers_sram1 PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
66+
set_target_properties(low_power_test_timers_sram1 PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
4867
pico_add_extra_outputs(low_power_test_timers_sram1)
4968

5069

5170
add_executable(low_power_test_timers_xip_sram
5271
low_power_test_timers.c
5372
)
54-
target_link_libraries(low_power_test_timers_xip_sram pico_stdlib pico_low_power pico_status_led)
73+
target_link_libraries(low_power_test_timers_xip_sram low_power_test_common)
5574
pico_set_persistent_data_loc(low_power_test_timers_xip_sram 0x13ffc000)
5675
# create map/bin/hex file etc.
76+
set_target_properties(low_power_test_timers_xip_sram PROPERTIES PICO_TEST_TIMEOUT 35)
77+
set_target_properties(low_power_test_timers_xip_sram PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
78+
set_target_properties(low_power_test_timers_xip_sram PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
5779
pico_add_extra_outputs(low_power_test_timers_xip_sram)
5880

5981

6082
add_executable(low_power_test_timers_xip_sram_no_flash
6183
low_power_test_timers.c
6284
)
63-
target_link_libraries(low_power_test_timers_xip_sram_no_flash pico_stdlib pico_low_power pico_status_led)
85+
target_link_libraries(low_power_test_timers_xip_sram_no_flash low_power_test_common)
6486
pico_set_persistent_data_loc(low_power_test_timers_xip_sram_no_flash 0x13ffc000)
6587
pico_set_binary_type(low_power_test_timers_xip_sram_no_flash no_flash)
6688
pico_package_uf2_output(low_power_test_timers_xip_sram_no_flash)
6789
# create map/bin/hex file etc.
90+
set_target_properties(low_power_test_timers_xip_sram_no_flash PROPERTIES PICO_TEST_TIMEOUT 35)
91+
set_target_properties(low_power_test_timers_xip_sram_no_flash PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
92+
set_target_properties(low_power_test_timers_xip_sram_no_flash PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
6893
pico_add_extra_outputs(low_power_test_timers_xip_sram_no_flash)
6994
endif()
7095

71-
if (PICO_RP2040)
72-
add_executable(rtc_clksrc
73-
rtc_clksrc.c
74-
)
75-
target_link_libraries(rtc_clksrc pico_stdlib pico_status_led)
76-
# create map/bin/hex file etc.
77-
pico_add_extra_outputs(rtc_clksrc)
78-
endif()
96+
97+
add_executable(external_sleep_timer
98+
external_sleep_timer.c
99+
)
100+
target_link_libraries(external_sleep_timer low_power_test_common)
101+
# This is a buddy program, not a test
102+
set_target_properties(external_sleep_timer PROPERTIES PICO_TEST_SKIP_IN_CI TRUE)
103+
# create map/bin/hex file etc.
104+
pico_add_extra_outputs(external_sleep_timer)
105+
79106

80107
add_executable(low_power_test_gpio
81108
low_power_test_gpio.c
82109
)
83-
target_link_libraries(low_power_test_gpio pico_stdlib pico_low_power pico_status_led)
110+
target_link_libraries(low_power_test_gpio low_power_test_common)
111+
set_target_properties(low_power_test_gpio PROPERTIES PICO_TEST_TIMEOUT 25)
112+
set_target_properties(low_power_test_gpio PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
113+
set_target_properties(low_power_test_gpio PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
84114
# create map/bin/hex file etc.
85115
pico_add_extra_outputs(low_power_test_gpio)
86116

87117

88118
add_executable(low_power_test_gpio_usb
89119
low_power_test_gpio.c
90120
)
91-
target_link_libraries(low_power_test_gpio_usb pico_stdlib pico_low_power pico_status_led)
121+
target_link_libraries(low_power_test_gpio_usb low_power_test_common)
92122
pico_enable_stdio_usb(low_power_test_gpio_usb 1)
123+
set_target_properties(low_power_test_gpio_usb PROPERTIES PICO_TEST_TIMEOUT 25)
124+
set_target_properties(low_power_test_gpio_usb PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
125+
set_target_properties(low_power_test_gpio_usb PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
93126
# create map/bin/hex file etc.
94127
pico_add_extra_outputs(low_power_test_gpio_usb)
95128

96129

97130
add_executable(low_power_test_simple
98131
low_power_test_simple.c
99132
)
100-
target_link_libraries(low_power_test_simple pico_stdlib pico_low_power pico_status_led)
133+
target_link_libraries(low_power_test_simple low_power_test_common)
134+
set_target_properties(low_power_test_simple PROPERTIES PICO_TEST_TIMEOUT 25)
135+
set_target_properties(low_power_test_simple PROPERTIES PICO_TEST_FAILURE_STRING "ERROR:")
136+
set_target_properties(low_power_test_simple PROPERTIES PICO_TEST_BUDDY_FILE external_sleep_timer.elf)
101137
# create map/bin/hex file etc.
102138
pico_add_extra_outputs(low_power_test_simple)
103139

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2026 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/sync.h"
10+
#include "hardware/clocks.h"
11+
12+
#include "low_power_test_common.h"
13+
14+
#if PICO_RP2040
15+
#define TOLERANCE_MS 1000 // The resolution of the AON timer is 1s on RP2040
16+
#else
17+
#define TOLERANCE_MS 50
18+
#endif
19+
#define MIN_SLEEP_TIME_MS SLEEP_TIME_MS - TOLERANCE_MS
20+
#define MAX_SLEEP_TIME_MS SLEEP_TIME_MS + TOLERANCE_MS
21+
22+
uint32_t wakeup_time_ms = 0;
23+
uint32_t sleep_time_ms = 0;
24+
bool good_sleep_done = false;
25+
26+
int64_t wake_up_gpio(__unused alarm_id_t id, __unused void *param) {
27+
gpio_put(WAKE_UP_PIN, 0);
28+
return 0;
29+
}
30+
31+
static alarm_id_t wake_up_alarm_id;
32+
33+
void gpio_callback(uint gpio, uint32_t events) {
34+
if (events & GPIO_IRQ_EDGE_RISE) {
35+
wakeup_time_ms = to_ms_since_boot(get_absolute_time());
36+
printf("Woke up at %dms\n", wakeup_time_ms);
37+
gpio_put(WAKE_UP_PIN, 1);
38+
} else if (events & GPIO_IRQ_EDGE_FALL) {
39+
sleep_time_ms = to_ms_since_boot(get_absolute_time());
40+
printf("Went to sleep at %dms\n", sleep_time_ms);
41+
wake_up_alarm_id = add_alarm_in_ms(SLEEP_TIME_MS, wake_up_gpio, NULL, false);
42+
}
43+
if (wakeup_time_ms > sleep_time_ms) {
44+
uint32_t diff = wakeup_time_ms - sleep_time_ms;
45+
if (good_sleep_done && (diff < MIN_SLEEP_TIME_MS || diff > MAX_SLEEP_TIME_MS)) {
46+
printf("ERROR: Was asleep for %dms, expected between %dms and %dms\n", diff, MIN_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS);
47+
} else if (diff >= MIN_SLEEP_TIME_MS && diff <= MAX_SLEEP_TIME_MS){
48+
printf("Was asleep for %dms\n", diff);
49+
good_sleep_done = true;
50+
}
51+
}
52+
}
53+
54+
55+
int main() {
56+
stdio_init_all();
57+
58+
#if PICO_RP2040
59+
printf("Outputting RTC clock to GPIO %d\n", RTC_GPIO_OUT);
60+
clock_gpio_init(RTC_GPIO_OUT, CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLK_RTC, 1);
61+
#endif
62+
63+
printf("Monitoring for sleep events on GPIO %d\n", SLEEP_MONITOR_PIN);
64+
gpio_init(SLEEP_MONITOR_PIN);
65+
gpio_pull_up(SLEEP_MONITOR_PIN);
66+
gpio_set_irq_enabled_with_callback(SLEEP_MONITOR_PIN, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
67+
68+
printf("Waking up device on GPIO %d\n", WAKE_UP_PIN);
69+
gpio_init(WAKE_UP_PIN);
70+
gpio_set_dir(WAKE_UP_PIN, GPIO_OUT);
71+
gpio_put(WAKE_UP_PIN, 1);
72+
73+
while (true) __wfi();
74+
75+
return 0;
76+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef LOW_POWER_TEST_COMMON_H
2+
#define LOW_POWER_TEST_COMMON_H
3+
4+
#include <stdio.h>
5+
#include <string.h>
6+
#include "pico/stdlib.h"
7+
#include "pico/low_power.h"
8+
#include "pico/aon_timer.h"
9+
#include "pico/status_led.h"
10+
#include "hardware/structs/xip_ctrl.h"
11+
12+
#define SLEEP_TIME_S 2
13+
#define SLEEP_TIME_MS SLEEP_TIME_S * 1000
14+
15+
// Use default I2C pins as that should be connected to the other board's I2C pins
16+
#define SLEEP_MONITOR_PIN PICO_DEFAULT_I2C_SDA_PIN
17+
#define WAKE_UP_PIN PICO_DEFAULT_I2C_SCL_PIN
18+
19+
// On RP2040 this must be a GPIO that supports clock input, see the GPIO function table in the datasheet.
20+
#define RTC_GPIO_IN 22
21+
22+
// On RP2040 this must be a GPIO that supports clock output, see the GPIO function table in the datasheet.
23+
#define RTC_GPIO_OUT 21
24+
25+
26+
static inline void init_external_gpios(void) {
27+
gpio_init(SLEEP_MONITOR_PIN);
28+
gpio_set_dir(SLEEP_MONITOR_PIN, GPIO_OUT);
29+
gpio_put(SLEEP_MONITOR_PIN, 1);
30+
gpio_init(WAKE_UP_PIN);
31+
gpio_pull_up(WAKE_UP_PIN);
32+
}
33+
34+
#if HAS_POWMAN_TIMER
35+
static inline void init_powman_ext_ctrl(void) {
36+
powman_hw->ext_ctrl[0] = POWMAN_EXT_CTRL0_LP_EXIT_STATE_BITS | POWMAN_EXT_CTRL0_INIT_STATE_BITS | SLEEP_MONITOR_PIN;
37+
hw_set_bits(&powman_hw->ext_ctrl[0], POWMAN_EXT_CTRL0_INIT_BITS);
38+
}
39+
#endif
40+
41+
#endif // LOW_POWER_TEST_COMMON_H

0 commit comments

Comments
 (0)