/
mbed_rtx_idle.cpp
163 lines (133 loc) · 4.02 KB
/
mbed_rtx_idle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* mbed Microcontroller Library
* Copyright (c) 2006-2012 ARM Limited
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "rtos/rtos_idle.h"
#include "platform/mbed_power_mgmt.h"
#include "TimerEvent.h"
#include "lp_ticker_api.h"
#include "mbed_critical.h"
#include "mbed_assert.h"
#include <new>
#include "rtx_os.h"
extern "C" {
#include "rtx_lib.h"
}
#include "mbed.h"
extern DigitalOut pin_run;
using namespace mbed;
#if (defined(MBED_TICKLESS) && defined(DEVICE_LPTICKER))
#include "rtos/TARGET_CORTEX/SysTimer.h"
static rtos::internal::SysTimer *os_timer;
static uint64_t os_timer_data[sizeof(rtos::internal::SysTimer) / 8];
/// Enable System Timer.
int32_t OS_Tick_Enable (void)
{
// Do not use SingletonPtr since this relies on the RTOS
if (NULL == os_timer) {
os_timer = new (os_timer_data) rtos::internal::SysTimer();
os_timer->setup_irq();
}
// set to fire interrupt on next tick
os_timer->schedule_tick();
return 0;
}
/// Disable System Timer.
int32_t OS_Tick_Disable (void)
{
os_timer->cancel_tick();
return 0;
}
/// Acknowledge System Timer IRQ.
int32_t OS_Tick_AcknowledgeIRQ (void)
{
os_timer->schedule_tick();
return 0;
}
/// Get System Timer count.
uint32_t OS_Tick_GetCount (void)
{
return os_timer->get_time() & 0xFFFFFFFF;
}
// Get OS Tick IRQ number.
int32_t OS_Tick_GetIRQn (void) {
return -1;
}
// Get OS Tick overflow status.
uint32_t OS_Tick_GetOverflow (void) {
return 0;
}
// Get OS Tick interval.
uint32_t OS_Tick_GetInterval (void) {
return 1000;
}
static void default_idle_hook(void)
{
uint32_t elapsed_ticks = 0;
core_util_critical_section_enter();
pin_run = 0;
uint32_t ticks_to_sleep = svcRtxKernelSuspend();
if (ticks_to_sleep) {
os_timer->schedule_tick(ticks_to_sleep);
sleep();
os_timer->cancel_tick();
// calculate how long we slept
elapsed_ticks = os_timer->update_tick();
}
svcRtxKernelResume(elapsed_ticks);
pin_run = 1;
core_util_critical_section_exit();
}
#elif defined(FEATURE_UVISOR)
static void default_idle_hook(void)
{
/* uVisor can't sleep. See <https://github.com/ARMmbed/uvisor/issues/420>
* for details. */
}
#else
static void default_idle_hook(void)
{
// critical section to complete sleep with locked deepsleep
core_util_critical_section_enter();
pin_run = 0;
sleep_manager_lock_deep_sleep();
sleep();
sleep_manager_unlock_deep_sleep();
pin_run = 1;
core_util_critical_section_exit();
}
#endif // (defined(MBED_TICKLESS) && defined(DEVICE_LPTICKER))
static void (*idle_hook_fptr)(void) = &default_idle_hook;
void rtos_attach_idle_hook(void (*fptr)(void))
{
//Attach the specified idle hook, or the default idle hook in case of a NULL pointer
if (fptr != NULL) {
idle_hook_fptr = fptr;
} else {
idle_hook_fptr = default_idle_hook;
}
}
extern "C" void rtos_idle_loop(void)
{
//Continuously call the idle hook function pointer
while (1) {
idle_hook_fptr();
}
}