Skip to content

Commit aa6f0b8

Browse files
committed
RTOS - update for RTX v4.79 for Cortex-M
Thread - stack methods are not available for now, as tcb pointer was removed from internal structure. To obtain it, we could get it from the kernel, but this should be reconsidered. Either RTOS should provide it, or these methods will become deprecated.
1 parent 9a68561 commit aa6f0b8

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

libraries/rtos/rtos/Mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Mutex {
5757
osMutexId _osMutexId;
5858
osMutexDef_t _osMutexDef;
5959
#ifdef CMSIS_OS_RTX
60-
#ifdef __MBED_CMSIS_RTOS_CA9
60+
#if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
6161
int32_t _mutex_data[4];
6262
#else
6363
int32_t _mutex_data[3];

libraries/rtos/rtos/RtosTimer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ class RtosTimer {
6161
private:
6262
osTimerId _timer_id;
6363
osTimerDef_t _timer;
64-
#ifdef CMSIS_OS_RTX
64+
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
6565
uint32_t _timer_data[5];
66+
#else
67+
uint32_t _timer_data[6];
6668
#endif
6769
};
6870

libraries/rtos/rtos/Thread.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace rtos {
2828

2929
Thread::Thread(void (*task)(void const *argument), void *argument,
3030
osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
31-
#ifdef CMSIS_OS_RTX
31+
#ifdef __MBED_CMSIS_RTOS_CM
3232
_thread_def.pthread = task;
3333
_thread_def.tpriority = priority;
3434
_thread_def.stacksize = stack_size;
@@ -71,8 +71,10 @@ int32_t Thread::signal_clr(int32_t signals) {
7171
}
7272

7373
Thread::State Thread::get_state() {
74-
#ifndef __MBED_CMSIS_RTOS_CA9
74+
#if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
75+
#ifdef CMSIS_OS_RTX
7576
return ((State)_thread_def.tcb.state);
77+
#endif
7678
#else
7779
uint8_t status;
7880
status = osThreadGetState(_tid);
@@ -82,39 +84,55 @@ Thread::State Thread::get_state() {
8284

8385
uint32_t Thread::stack_size() {
8486
#ifndef __MBED_CMSIS_RTOS_CA9
87+
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
8588
return _thread_def.tcb.priv_stack;
8689
#else
8790
return 0;
8891
#endif
92+
#else
93+
return 0;
94+
#endif
8995
}
9096

9197
uint32_t Thread::free_stack() {
9298
#ifndef __MBED_CMSIS_RTOS_CA9
99+
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
93100
uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
94101
return _thread_def.tcb.tsk_stack - bottom;
95102
#else
96103
return 0;
97104
#endif
105+
#else
106+
return 0;
107+
#endif
98108
}
99109

100110
uint32_t Thread::used_stack() {
101111
#ifndef __MBED_CMSIS_RTOS_CA9
112+
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
102113
uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
103114
return top - _thread_def.tcb.tsk_stack;
104115
#else
105116
return 0;
106117
#endif
118+
#else
119+
return 0;
120+
#endif
107121
}
108122

109123
uint32_t Thread::max_stack() {
110124
#ifndef __MBED_CMSIS_RTOS_CA9
125+
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
111126
uint32_t high_mark = 0;
112127
while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
113128
high_mark++;
114129
return _thread_def.tcb.priv_stack - (high_mark * 4);
115130
#else
116131
return 0;
117132
#endif
133+
#else
134+
return 0;
135+
#endif
118136
}
119137

120138
osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
@@ -139,9 +157,11 @@ void Thread::attach_idle_hook(void (*fptr)(void)) {
139157

140158
Thread::~Thread() {
141159
terminate();
160+
#ifdef __MBED_CMSIS_RTOS_CM
142161
if (_dynamic_stack) {
143162
delete[] (_thread_def.stack_pointer);
144163
}
164+
#endif
145165
}
146166

147167
}

libraries/rtos/rtx/TARGET_CORTEX_M/HAL_CM.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
9090
/* Task entry point. */
9191
p_TCB->ptask = task_body;
9292

93+
94+
#ifdef __MBED_CMSIS_RTOS_CM
95+
/* Set a magic word for checking of stack overflow.
96+
For the main thread (ID: 0x02) the stack is in a memory area shared with the
97+
heap, therefore the last word of the stack is a moving target.
98+
We want to do stack/heap collision detection instead.
99+
Similar applies to stack filling for the magic pattern.
100+
*/
101+
if (p_TCB->task_id != 0x02) {
102+
p_TCB->stack[0] = MAGIC_WORD;
103+
104+
/* Initialize stack with magic pattern. */
105+
if (os_stackinfo & 0x10000000U) {
106+
if (size > (16U+1U)) {
107+
for (i = ((size - 16U)/2U) - 1U; i; i--) {
108+
stk -= 2U;
109+
stk[1] = MAGIC_PATTERN;
110+
stk[0] = MAGIC_PATTERN;
111+
}
112+
if (--stk > p_TCB->stack) {
113+
*stk = MAGIC_PATTERN;
114+
}
115+
}
116+
}
117+
}
118+
#else
93119
/* Initialize stack with magic pattern. */
94120
if (os_stackinfo & 0x10000000U) {
95121
if (size > (16U+1U)) {
@@ -104,15 +130,6 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
104130
}
105131
}
106132

107-
#ifdef __MBED_CMSIS_RTOS_CM
108-
/* Set a magic word for checking of stack overflow.
109-
For the main thread (ID: 0x02) the stack is in a memory area shared with the
110-
heap, therefore the last word of the stack is a moving target.
111-
We want to do stack/heap collision detection instead.
112-
*/
113-
if (p_TCB->task_id != 0x02)
114-
p_TCB->stack[0] = MAGIC_WORD;
115-
#else
116133
/* Set a magic word for checking of stack overflow. */
117134
p_TCB->stack[0] = MAGIC_WORD;
118135
#endif

libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ __attribute__((naked)) void software_init_hook (void) {
571571
"mov r0,r4\n"
572572
"mov r1,r5\n"
573573
"bl osKernelInitialize\n"
574-
#ifdef
574+
#ifdef __MBED_CMSIS_RTOS_CM
575575
"bl set_main_stack\n"
576576
#endif
577577
"ldr r0,=os_thread_def_main\n"

0 commit comments

Comments
 (0)