Skip to content

Commit b0bcf96

Browse files
committed
Use time 0 for low_power_start_aon_timer
Also use low_power_start_aon_timer_at_time_ms in low power tests
1 parent 355aea5 commit b0bcf96

3 files changed

Lines changed: 18 additions & 21 deletions

File tree

src/rp2_common/pico_low_power/include/pico/low_power.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ pstate_bitset_t *low_power_persistent_pstate_get(pstate_bitset_t *pstate);
293293
* \ingroup pico_low_power
294294
* See \ref aon_timer_start for more information.
295295
*
296+
* If the AON timer is already running, this function will restart it
297+
* from the specified time.
298+
*
296299
* \param ms The time in milliseconds to start the AON timer at.
297300
* \return true on success, false on failure.
298301
*/
@@ -304,14 +307,16 @@ static inline bool low_power_start_aon_timer_at_time_ms(uint64_t ms) {
304307

305308
/*! \brief Start the AON timer at the current system time
306309
* \ingroup pico_low_power
310+
*
307311
* See \ref aon_timer_start for more information.
308312
*
313+
* If the AON timer is already running, this function will not restart it.
314+
*
309315
* \return true on success, false on failure.
310316
*/
311317
static inline bool low_power_start_aon_timer(void) {
312-
struct timespec ts;
313-
ms_to_timespec(to_ms_64_since_boot(get_absolute_time()), &ts);
314-
return aon_timer_start(&ts);
318+
if (aon_timer_is_running()) return true;
319+
return low_power_start_aon_timer_at_time_ms(0);
315320
}
316321

317322
/*! \brief Sleep for a number of microseconds
@@ -351,7 +356,7 @@ static inline int low_power_sleep_for_ms(uint32_t ms, const clock_dest_bitset_t
351356
* \return 0 on success, non-zero on error.
352357
*/
353358
static inline int low_power_dormant_for_ms(uint32_t ms, dormant_clock_source_t dormant_clock_source, const clock_dest_bitset_t *keep_enabled) {
354-
if (!aon_timer_is_running()) low_power_start_aon_timer();
359+
low_power_start_aon_timer();
355360
return low_power_dormant_until_aon_timer(aon_timer_make_timeout_time_ms(ms), dormant_clock_source, keep_enabled);
356361
}
357362

@@ -366,7 +371,7 @@ static inline int low_power_dormant_for_ms(uint32_t ms, dormant_clock_source_t d
366371
* \return 0 on success, non-zero on error.
367372
*/
368373
static inline int low_power_pstate_for_ms(uint32_t ms, pstate_bitset_t *pstate, low_power_pstate_resume_func resume_func) {
369-
if (!aon_timer_is_running()) low_power_start_aon_timer();
374+
low_power_start_aon_timer();
370375
return low_power_pstate_until_aon_timer(aon_timer_make_timeout_time_ms(ms), pstate, resume_func);
371376
}
372377
#endif

test/pico_low_power_test/low_power_test_gpio.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ int main() {
8181
printf("Waiting %d seconds\n", SLEEP_TIME_S); // so we can see some repeat printfs
8282
busy_wait_ms(SLEEP_TIME_MS);
8383

84-
absolute_time_t start_time;
85-
struct timespec ts;
8684
int ret;
8785

8886
printf("Going to sleep until GPIO wakeup\n");
@@ -112,10 +110,7 @@ int main() {
112110
printf("Doing %d second pause to prove timer running\n", SLEEP_TIME_S);
113111
busy_wait_ms(SLEEP_TIME_MS);
114112

115-
// todo, ah; we should start the aon timer; still have to decide what to do about keeping them in sync
116-
start_time = get_absolute_time();
117-
us_to_timespec(to_us_since_boot(start_time), &ts);
118-
aon_timer_start(&ts);
113+
low_power_start_aon_timer_at_time_ms(0);
119114

120115
printf("Going DORMANT until GPIO wakeup\n");
121116

@@ -143,4 +138,4 @@ int main() {
143138
printf("PASSED\n");
144139

145140
return 0;
146-
}
141+
}

test/pico_low_power_test/low_power_test_timers.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ int main() {
138138
absolute_time_t start_time;
139139
static absolute_time_t __persistent_data(wakeup_time);
140140
int64_t diff;
141-
struct timespec ts;
142141
int ret;
143142

144143
low_power_set_external_clock_source(DORMANT_CLOCK_HZ_DEFAULT, RTC_GPIO);
@@ -188,10 +187,7 @@ int main() {
188187

189188

190189
// start the AON timer
191-
start_time = get_absolute_time();
192-
us_to_timespec(to_us_since_boot(start_time), &ts);
193-
aon_timer_start(&ts);
194-
190+
low_power_start_aon_timer_at_time_ms(0);
195191
printf("AON timer started @%dms\n", to_ms_since_boot(aon_timer_get_absolute_time()));
196192

197193

@@ -251,6 +247,7 @@ int main() {
251247
printf("Going DORMANT for %d seconds via AON TIMER\n", SLEEP_TIME_S);
252248

253249
start_time = aon_timer_get_absolute_time();
250+
absolute_time_t system_time_before = get_absolute_time();
254251
wakeup_time = delayed_by_ms(start_time, SLEEP_TIME_MS);
255252
ret = low_power_dormant_until_aon_timer(wakeup_time, DORMANT_CLOCK_SOURCE_DEFAULT, NULL);
256253
if (ret != PICO_OK) {
@@ -262,14 +259,14 @@ int main() {
262259
#endif
263260
EXIT_TEST;
264261
}
265-
// need to use the AON timer for checking time, since the other timer is unclocked
266-
diff = absolute_time_diff_us(wakeup_time, get_absolute_time());
267-
if (diff > -1000000
262+
// check the system timer was stopped while dormant
263+
diff = absolute_time_diff_us(system_time_before, get_absolute_time());
264+
if (diff > 50 * 1000 // 50ms
268265
#ifdef PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS
269266
+ (PICO_STDIO_USB_CONNECT_WAIT_TIMEOUT_MS * 1000)
270267
#endif
271268
) {
272-
printf("ERROR: doesn't seem like timer was stopped\n");
269+
printf("ERROR: doesn't seem like timer was stopped: diff %lldus\n", diff);
273270
return - 1;
274271
}
275272
diff = absolute_time_diff_us(wakeup_time, aon_timer_get_absolute_time());

0 commit comments

Comments
 (0)