26
26
#include "hal/ticker_api.h"
27
27
#include "hal/lp_ticker_api.h"
28
28
#include "cmsis_os2.h"
29
+ #include "rtx_lib.h"
29
30
#include <limits.h>
30
31
31
32
static ticker_event_t idle_loop_event ;
@@ -38,31 +39,30 @@ void idle_loop_handler(uint32_t id)
38
39
static void default_idle_hook (void )
39
40
{
40
41
#if DEVICE_LOWPOWERTIMER
41
- uint32_t tick_freq = osKernelGetTickFreq ();
42
42
timestamp_t time_in_sleep = 0UL ;
43
43
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 ();
45
47
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 ;
48
49
// 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 ;
52
52
}
53
53
54
54
const ticker_data_t * lp_ticker_data = get_lp_ticker_data ();
55
55
ticker_remove_event (lp_ticker_data , & idle_loop_event );
56
56
ticker_set_handler (lp_ticker_data , & idle_loop_handler );
57
57
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 );
59
59
60
60
sleep ();
61
- core_util_critical_section_exit ();
62
61
// calculate how long we slept
63
62
time_in_sleep = lp_ticker_read () - start_time ;
64
63
}
65
- osKernelResume (time_in_sleep / tick_freq );
64
+ svcRtxKernelResume ((uint64_t )time_in_sleep * tick_freq / 1000000 );
65
+ core_util_critical_section_exit ();
66
66
#else
67
67
sleep ();
68
68
#endif
0 commit comments