/
mbed_rtx_idle.cpp
178 lines (149 loc) · 5.06 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* mbed Microcontroller Library
* Copyright (c) 2006-2019 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/source/rtos_idle.h"
#include "platform/mbed_power_mgmt.h"
#include "platform/mbed_os_timer.h"
#include "TimerEvent.h"
#include "mbed_critical.h"
#include "mbed_assert.h"
#include <new>
#include "rtx_os.h"
/* Everything in rtx_lib.h, and provided by this file, has C linkage */
extern "C" {
#include "rtx_lib.h"
using mbed::internal::os_timer;
using mbed::internal::OsTimer;
#ifdef MBED_TICKLESS
#if MBED_CONF_TARGET_TICKLESS_FROM_US_TICKER && !DEVICE_USTICKER
#error Microsecond ticker required when MBED_CONF_TARGET_TICKLESS_FROM_US_TICKER is true
#endif
#if !MBED_CONF_TARGET_TICKLESS_FROM_US_TICKER && !DEVICE_LPTICKER
#error Low power ticker required when MBED_CONF_TARGET_TICKLESS_FROM_US_TICKER is false
#endif
// Setup OS Tick timer to generate periodic RTOS Kernel Ticks
int32_t OS_Tick_Setup(uint32_t freq, IRQHandler_t handler)
{
MBED_ASSERT(freq == 1000);
#ifdef TARGET_CORTEX_A
IRQn_ID_t irq = OsTimer::get_irq_number();
IRQ_SetPriority(irq, 0xFF);
IRQ_SetHandler(irq, handler);
IRQ_EnableIRQ(irq);
#else
IRQn_Type irq = OsTimer::get_irq_number();
NVIC_SetPriority(irq, 0xFF);
#ifdef NVIC_RAM_VECTOR_ADDRESS
NVIC_SetVector(irq, (uint32_t)handler);
#else
MBED_ASSERT(handler == (IRQHandler_t)NVIC_GetVector(irq));
#endif
if (irq >= 0) {
NVIC_EnableIRQ(irq);
}
#endif
return 0;
}
// Enable System Timer.
void OS_Tick_Enable(void)
{
// set to fire interrupt on next tick
mbed::internal::init_os_timer()->start_tick();
}
// Disable System Timer.
void OS_Tick_Disable(void)
{
os_timer->cancel_tick();
}
// Acknowledge System Timer IRQ.
void OS_Tick_AcknowledgeIRQ(void)
{
os_timer->acknowledge_tick();
}
// Get System Timer count.
uint32_t OS_Tick_GetCount(void)
{
return (uint32_t) os_timer->get_time_since_tick();
}
// Get OS Tick IRQ number.
int32_t OS_Tick_GetIRQn(void)
{
return os_timer->get_irq_number();
}
// Get OS Tick overflow status.
uint32_t OS_Tick_GetOverflow(void)
{
// No need to indicate overflow - we let OS_Tick_GetCount overflow above
// OS_Tick_GetInterval.
return 0;
}
// Get OS Tick timer clock frequency
uint32_t OS_Tick_GetClock(void)
{
return 1000000;
}
// Get OS Tick interval.
uint32_t OS_Tick_GetInterval(void)
{
return 1000;
}
static bool rtos_event_pending(void *)
{
return core_util_atomic_load_u8(&osRtxInfo.kernel.pendSV);
}
static void default_idle_hook(void)
{
uint32_t ticks_to_sleep = osKernelSuspend();
// osKernelSuspend will call OS_Tick_Disable, cancelling the tick, which frees
// up the os timer for the timed sleep
uint64_t ticks_slept = mbed::internal::do_timed_sleep_relative(ticks_to_sleep, rtos_event_pending);
MBED_ASSERT(ticks_slept < osWaitForever);
osKernelResume((uint32_t) ticks_slept);
}
#else // MBED_TICKLESS
static void default_idle_hook(void)
{
// critical section to complete sleep with locked deepsleep
core_util_critical_section_enter();
sleep_manager_lock_deep_sleep();
sleep();
sleep_manager_unlock_deep_sleep();
core_util_critical_section_exit();
}
#endif // MBED_TICKLESS
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 != nullptr) {
idle_hook_fptr = fptr;
} else {
idle_hook_fptr = default_idle_hook;
}
}
MBED_NORETURN void rtos_idle_loop(void)
{
//Continuously call the idle hook function pointer
while (1) {
idle_hook_fptr();
}
}
} // extern "C"