Skip to content

Commit 757d340

Browse files
committed
RTOS - fix for main thread id might not be 0x02
Fixes #2059. As reported, if timer thread is not created, the main thread id is 0x01. We introduce MAIN_THREAD_ID macro to define the id. We shall consider, if we keep this in a variable. I placed MAIN_THREAD_ID in cmsis_os.h as that header is safe to include within RTX, not like RTX_Config.h or RTX_CM_Lib.h).
1 parent 467c8d5 commit 757d340

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

rtos/rtx/TARGET_CORTEX_M/HAL_CM.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "rt_TypeDef.h"
3636
#include "RTX_Config.h"
3737
#include "rt_HAL_CM.h"
38-
38+
#include "cmsis_os.h"
3939

4040
/*----------------------------------------------------------------------------
4141
* Global Variables
@@ -93,12 +93,12 @@ void rt_init_stack (P_TCB p_TCB, FUNCP task_body) {
9393

9494
#ifdef __MBED_CMSIS_RTOS_CM
9595
/* 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
96+
For the main thread (ID: MAIN_THREAD_ID) the stack is in a memory area shared with the
9797
heap, therefore the last word of the stack is a moving target.
9898
We want to do stack/heap collision detection instead.
9999
Similar applies to stack filling for the magic pattern.
100100
*/
101-
if (p_TCB->task_id != 0x02) {
101+
if (p_TCB->task_id != MAIN_THREAD_ID) {
102102
p_TCB->stack[0] = MAGIC_WORD;
103103

104104
/* Initialize stack with magic pattern. */

rtos/rtx/TARGET_CORTEX_M/RTX_Conf_CM.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
* RTX User configuration part BEGIN
4040
*---------------------------------------------------------------------------*/
4141

42-
#if defined(MBED_RTOS_SINGLE_THREAD)
43-
#define OS_TASKCNT 1
44-
#define OS_TIMERS 0
45-
#endif
46-
4742
//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
4843
//
4944
// <h>Thread Configuration

rtos/rtx/TARGET_CORTEX_M/cmsis_os.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@
7272
# define WORDS_STACK_SIZE 128
7373
#endif
7474

75+
#ifdef __MBED_CMSIS_RTOS_CM
76+
77+
/* Single thread - disable timers and set task count to one */
78+
#if defined(MBED_RTOS_SINGLE_THREAD)
79+
#define OS_TASKCNT 1
80+
#define OS_TIMERS 0
81+
#endif
82+
83+
/* If os timers macro is set to 0, there's no timer thread created, therefore
84+
* main thread has tid 0x01
85+
*/
86+
#if (OS_TIMERS != 0)
87+
#define MAIN_THREAD_ID 0x02
88+
#else
89+
#define MAIN_THREAD_ID 0x01
90+
#endif
91+
#endif
92+
7593
#define DEFAULT_STACK_SIZE (WORDS_STACK_SIZE*4)
7694

7795
#define osCMSIS 0x10002U ///< CMSIS-RTOS API version (main [31:16] .sub [15:0])

rtos/rtx/TARGET_CORTEX_M/rt_System.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ void rt_systick (void) {
315315
__weak void rt_stk_check (void) {
316316
#ifdef __MBED_CMSIS_RTOS_CM
317317
/* Check for stack overflow. */
318-
if (os_tsk.run->task_id == 0x02) {
318+
if (os_tsk.run->task_id == MAIN_THREAD_ID) {
319319
// TODO: For the main thread the check should be done against the main heap pointer
320320
} else {
321321
if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||

0 commit comments

Comments
 (0)