Skip to content

Commit b0b8d1e

Browse files
c1728p90xc0170
authored andcommitted
Update tickless code
Change over os calls to use the svc equivalent. Also fixed time calculations.
1 parent bc235d6 commit b0b8d1e

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

rtos/rtos_idle.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "hal/ticker_api.h"
2727
#include "hal/lp_ticker_api.h"
2828
#include "cmsis_os2.h"
29+
#include "rtx_lib.h"
2930
#include <limits.h>
3031

3132
static ticker_event_t idle_loop_event;
@@ -38,31 +39,30 @@ void idle_loop_handler(uint32_t id)
3839
static void default_idle_hook(void)
3940
{
4041
#if DEVICE_LOWPOWERTIMER
41-
uint32_t tick_freq = osKernelGetTickFreq();
4242
timestamp_t time_in_sleep = 0UL;
4343

44-
uint32_t ticks_to_sleep = osKernelSuspend();
44+
core_util_critical_section_enter();
45+
uint32_t tick_freq = svcRtxKernelGetTickFreq();
46+
uint32_t ticks_to_sleep = svcRtxKernelSuspend();
4547
if (ticks_to_sleep) {
46-
core_util_critical_section_enter();
47-
uint64_t us_to_sleep = ticks_to_sleep * tick_freq;
48+
uint64_t us_to_sleep = 1000000 * (uint64_t)ticks_to_sleep / tick_freq;
4849
// Calculate the maximum period we can sleep and stay within
49-
uint64_t max_us_sleep = (UINT_MAX / tick_freq) * tick_freq;
50-
if (us_to_sleep > max_us_sleep) {
51-
us_to_sleep = max_us_sleep;
50+
if (us_to_sleep >= UINT32_MAX) {
51+
us_to_sleep = UINT32_MAX;
5252
}
5353

5454
const ticker_data_t *lp_ticker_data = get_lp_ticker_data();
5555
ticker_remove_event(lp_ticker_data, &idle_loop_event);
5656
ticker_set_handler(lp_ticker_data, &idle_loop_handler);
5757
timestamp_t start_time = lp_ticker_read();
58-
ticker_insert_event_us(lp_ticker_data, &idle_loop_event, us_to_sleep, (uint32_t)&idle_loop_event);
58+
ticker_insert_event_us(lp_ticker_data, &idle_loop_event, start_time + us_to_sleep, (uint32_t)&idle_loop_event);
5959

6060
sleep();
61-
core_util_critical_section_exit();
6261
// calculate how long we slept
6362
time_in_sleep = lp_ticker_read() - start_time;
6463
}
65-
osKernelResume(time_in_sleep / tick_freq);
64+
svcRtxKernelResume((uint64_t)time_in_sleep * tick_freq / 1000000);
65+
core_util_critical_section_exit();
6666
#else
6767
sleep();
6868
#endif

0 commit comments

Comments
 (0)