diff --git a/libraries/rtos/rtos/Thread.cpp b/libraries/rtos/rtos/Thread.cpp index ed8270a36a..3af476ce60 100644 --- a/libraries/rtos/rtos/Thread.cpp +++ b/libraries/rtos/rtos/Thread.cpp @@ -24,6 +24,12 @@ #include "mbed_error.h" #include "rtos_idle.h" +// rt_tid2ptcb is an internal function which we exposed to get TCB for thread id +#undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h +#include "rt_TypeDef.h" + +extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id); + namespace rtos { Thread::Thread(void (*task)(void const *argument), void *argument, @@ -87,7 +93,8 @@ uint32_t Thread::stack_size() { #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) return _thread_def.tcb.priv_stack; #else - return 0; + P_TCB tcb = rt_tid2ptcb(_tid); + return tcb->priv_stack; #endif #else return 0; @@ -100,7 +107,9 @@ uint32_t Thread::free_stack() { uint32_t bottom = (uint32_t)_thread_def.tcb.stack; return _thread_def.tcb.tsk_stack - bottom; #else - return 0; + P_TCB tcb = rt_tid2ptcb(_tid); + uint32_t bottom = (uint32_t)tcb->stack; + return tcb->tsk_stack - bottom; #endif #else return 0; @@ -113,7 +122,9 @@ uint32_t Thread::used_stack() { uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; return top - _thread_def.tcb.tsk_stack; #else - return 0; + P_TCB tcb = rt_tid2ptcb(_tid); + uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack; + return top - tcb->tsk_stack; #endif #else return 0; @@ -128,7 +139,11 @@ uint32_t Thread::max_stack() { high_mark++; return _thread_def.tcb.priv_stack - (high_mark * 4); #else - return 0; + P_TCB tcb = rt_tid2ptcb(_tid); + uint32_t high_mark = 0; + while (tcb->stack[high_mark] == 0xE25A2EA5) + high_mark++; + return tcb->priv_stack - (high_mark * 4); #endif #else return 0; diff --git a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c index d76eb89242..057fced7e3 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c +++ b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c @@ -407,7 +407,7 @@ static uint16_t rt_ms2tick (uint32_t millisec) { } /// Convert Thread ID to TCB pointer -static P_TCB rt_tid2ptcb (osThreadId thread_id) { +P_TCB rt_tid2ptcb (osThreadId thread_id) { P_TCB ptcb; if (thread_id == NULL) { return NULL; } @@ -506,8 +506,8 @@ osStatus svcKernelStart (void) { if (os_tsk.run->task_id == 0xFFU) { // Idle Thread __set_PSP(os_tsk.run->tsk_stack + (8U*4U)); // Setup PSP } - if (os_tsk.new == NULL) { // Force context switch - os_tsk.new = os_tsk.run; + if (os_tsk.new_tsk == NULL) { // Force context switch + os_tsk.new_tsk = os_tsk.run; os_tsk.run = NULL; } diff --git a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_HAL_CM.h b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_HAL_CM.h index 151eb2f771..d6bac9a0db 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_HAL_CM.h +++ b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_HAL_CM.h @@ -276,7 +276,7 @@ extern void dbg_task_switch (U32 task_id); #ifdef DBG_MSG #define DBG_INIT() dbg_init() #define DBG_TASK_NOTIFY(p_tcb,create) if (dbg_msg) dbg_task_notify(p_tcb,create) -#define DBG_TASK_SWITCH(task_id) if (dbg_msg && (os_tsk.new!=os_tsk.run)) \ +#define DBG_TASK_SWITCH(task_id) if (dbg_msg && (os_tsk.new_tsk!=os_tsk.run)) \ dbg_task_switch(task_id) #else #define DBG_INIT() diff --git a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_Task.c b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_Task.c index 73356b663d..17a3da04cf 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_Task.c +++ b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_Task.c @@ -99,7 +99,7 @@ static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) { void rt_switch_req (P_TCB p_new) { /* Switch to next task (identified by "p_new"). */ - os_tsk.new = p_new; + os_tsk.new_tsk = p_new; p_new->state = RUNNING; DBG_TASK_SWITCH(p_new->task_id); } diff --git a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_TypeDef.h b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_TypeDef.h index a3e7677239..86a84d2750 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_M/rt_TypeDef.h +++ b/libraries/rtos/rtx/TARGET_CORTEX_M/rt_TypeDef.h @@ -98,7 +98,7 @@ typedef struct OS_PSQ { /* Post Service Queue */ typedef struct OS_TSK { P_TCB run; /* Current running task */ - P_TCB new; /* Scheduled task to run */ + P_TCB new_tsk; /* Scheduled task to run */ } *P_TSK; typedef struct OS_ROBIN { /* Round Robin Control */