Skip to content

Commit cf8d808

Browse files
authored
Merge pull request #2609 from bulislaw/tickless_mode
RTOS: Add tickles sleep implementation
2 parents 9111aa4 + f1f325f commit cf8d808

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

rtos/rtos/rtos_idle.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,50 @@
2222

2323
#include "rtos_idle.h"
2424

25+
#if DEVICE_LOWPOWERTIMER && DEVICE_SLEEP
26+
#include "ticker_api.h"
27+
#include "critical.h"
28+
#include "lp_ticker_api.h"
29+
#include "cmsis_os.h"
30+
#include "sleep_api.h"
31+
32+
static ticker_event_t delay_event;
33+
static volatile bool sleep_ended;
34+
extern uint32_t os_clockrate;
35+
36+
void sleep_handler(uint32_t id)
37+
{
38+
sleep_ended = true;
39+
}
40+
#endif /* DEVICE_LOWPOWERTIMER && DEVICE_SLEEP */
41+
2542
static void default_idle_hook(void)
2643
{
44+
#if DEVICE_LOWPOWERTIMER && DEVICE_SLEEP
45+
const ticker_data_t *lp_ticker_data = get_lp_ticker_data();
46+
uint32_t delay_ticks = os_suspend();
47+
uint32_t delay_us;
48+
sleep_ended = false;
49+
50+
ticker_set_handler(lp_ticker_data, sleep_handler);
51+
52+
ticker_remove_event(lp_ticker_data, &delay_event);
53+
delay_us = lp_ticker_read() + delay_ticks * os_clockrate;
54+
ticker_insert_event(lp_ticker_data, &delay_event, delay_us, (uint32_t)&delay_event);
55+
56+
while(!sleep_ended) {
57+
sleep();
58+
}
59+
60+
os_resume(delay_ticks);
61+
62+
#else /* DEVICE_LOWPOWERTIMER && DEVICE_SLEEP */
2763
/* Sleep: ideally, we should put the chip to sleep.
2864
Unfortunately, this usually requires disconnecting the interface chip (debugger).
2965
This can be done, but it would break the local file system.
3066
*/
3167
// sleep();
68+
#endif /* DEVICE_LOWPOWERTIMER && DEVICE_SLEEP */
3269
}
3370
static void (*idle_hook_fptr)(void) = &default_idle_hook;
3471

0 commit comments

Comments
 (0)