Skip to content

Commit 1fa489b

Browse files
committed
Merge pull request #1597 from neilt6/rtos-idle-patch
[RTOS] Added idle hook API
2 parents fec574a + c52e0dc commit 1fa489b

File tree

7 files changed

+110
-24
lines changed

7 files changed

+110
-24
lines changed

libraries/rtos/rtos/Thread.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "Thread.h"
2323

2424
#include "mbed_error.h"
25+
#include "rtos_idle.h"
2526

2627
namespace rtos {
2728

@@ -132,6 +133,10 @@ osThreadId Thread::gettid() {
132133
return osThreadGetId();
133134
}
134135

136+
void Thread::attach_idle_hook(void (*fptr)(void)) {
137+
rtos_attach_idle_hook(fptr);
138+
}
139+
135140
Thread::~Thread() {
136141
terminate();
137142
if (_dynamic_stack) {

libraries/rtos/rtos/Thread.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ class Thread {
131131
@return thread ID for reference by other functions or NULL in case of error.
132132
*/
133133
static osThreadId gettid();
134+
135+
/** Attach a function to be called by the RTOS idle task
136+
@param fptr pointer to the function to be called
137+
*/
138+
static void attach_idle_hook(void (*fptr)(void));
134139

135140
virtual ~Thread();
136141

libraries/rtos/rtos/rtos_idle.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2012 ARM Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
#include "rtos_idle.h"
24+
25+
static void default_idle_hook(void)
26+
{
27+
/* Sleep: ideally, we should put the chip to sleep.
28+
Unfortunately, this usually requires disconnecting the interface chip (debugger).
29+
This can be done, but it would break the local file system.
30+
*/
31+
// sleep();
32+
}
33+
static void (*idle_hook_fptr)(void) = &default_idle_hook;
34+
35+
void rtos_attach_idle_hook(void (*fptr)(void))
36+
{
37+
//Attach the specified idle hook, or the default idle hook in case of a NULL pointer
38+
if (fptr != NULL) {
39+
idle_hook_fptr = fptr;
40+
} else {
41+
idle_hook_fptr = default_idle_hook;
42+
}
43+
}
44+
45+
void rtos_idle_loop(void)
46+
{
47+
//Continuously call the idle hook function pointer
48+
while (1) {
49+
idle_hook_fptr();
50+
}
51+
}

libraries/rtos/rtos/rtos_idle.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2012 ARM Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
#ifndef RTOS_IDLE_H
23+
#define RTOS_IDLE_H
24+
25+
#include <stddef.h>
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
void rtos_attach_idle_hook(void (*fptr)(void));
32+
33+
#ifdef __cplusplus
34+
}
35+
#endif
36+
37+
#endif

libraries/rtos/rtx/TARGET_ARM7/RTX_Conf_CM.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,12 @@
204204
/*----------------------------------------------------------------------------
205205
* OS Idle daemon
206206
*---------------------------------------------------------------------------*/
207+
extern void rtos_idle_loop(void);
208+
207209
void os_idle_demon (void) {
208-
/* The idle demon is a system thread, running when no other thread is */
209-
/* ready to run. */
210-
211-
/* Sleep: ideally, we should put the chip to sleep.
212-
Unfortunately, this usually requires disconnecting the interface chip (debugger).
213-
This can be done, but it would break the local file system.
214-
*/
215-
for (;;) {
216-
// sleep();
217-
}
210+
/* The idle demon is a system thread, running when no other thread is */
211+
/* ready to run. */
212+
rtos_idle_loop();
218213
}
219214

220215
/*----------------------------------------------------------------------------

libraries/rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,12 @@
223223
*---------------------------------------------------------------------------*/
224224

225225
/*--------------------------- os_idle_demon ---------------------------------*/
226+
extern void rtos_idle_loop(void);
226227

227228
void os_idle_demon (void) {
228229
/* The idle demon is a system thread, running when no other thread is */
229230
/* ready to run. */
230-
231-
for (;;) {
232-
/* HERE: include optional user code to be executed when no thread runs.*/
233-
}
231+
rtos_idle_loop();
234232
}
235233

236234
#if (OS_SYSTICK == 0) // Functions for alternative timer as RTX kernel timer

libraries/rtos/rtx/TARGET_CORTEX_M/RTX_Conf_CM.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,12 @@
305305
/*----------------------------------------------------------------------------
306306
* OS Idle daemon
307307
*---------------------------------------------------------------------------*/
308+
extern void rtos_idle_loop(void);
309+
308310
void os_idle_demon (void) {
309-
/* The idle demon is a system thread, running when no other thread is */
310-
/* ready to run. */
311-
312-
/* Sleep: ideally, we should put the chip to sleep.
313-
Unfortunately, this usually requires disconnecting the interface chip (debugger).
314-
This can be done, but it would break the local file system.
315-
*/
316-
for (;;) {
317-
// sleep();
318-
}
311+
/* The idle demon is a system thread, running when no other thread is */
312+
/* ready to run. */
313+
rtos_idle_loop();
319314
}
320315

321316
/*----------------------------------------------------------------------------

0 commit comments

Comments
 (0)