Skip to content

Commit bc270f1

Browse files
committed
RTX - expose rt_tid2ptcb() function to get TCB
This enables the stack info methods to be supported for Cortex-M targets. The rt_TypeDef required one small change - rename new structure member as this is a reserved keyword in C++. We need to ask for tid everytime we need to use tcb, do not expose internal RTX details, we keep it within Thread.cpp file.
1 parent 9d06547 commit bc270f1

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

libraries/rtos/rtos/Thread.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
#include "mbed_error.h"
2525
#include "rtos_idle.h"
2626

27+
// rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
28+
#undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
29+
#include "rt_TypeDef.h"
30+
31+
extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
32+
2733
namespace rtos {
2834

2935
Thread::Thread(void (*task)(void const *argument), void *argument,
@@ -87,7 +93,8 @@ uint32_t Thread::stack_size() {
8793
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
8894
return _thread_def.tcb.priv_stack;
8995
#else
90-
return 0;
96+
P_TCB tcb = rt_tid2ptcb(_tid);
97+
return tcb->priv_stack;
9198
#endif
9299
#else
93100
return 0;
@@ -100,7 +107,9 @@ uint32_t Thread::free_stack() {
100107
uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
101108
return _thread_def.tcb.tsk_stack - bottom;
102109
#else
103-
return 0;
110+
P_TCB tcb = rt_tid2ptcb(_tid);
111+
uint32_t bottom = (uint32_t)tcb->stack;
112+
return tcb->tsk_stack - bottom;
104113
#endif
105114
#else
106115
return 0;
@@ -113,7 +122,9 @@ uint32_t Thread::used_stack() {
113122
uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
114123
return top - _thread_def.tcb.tsk_stack;
115124
#else
116-
return 0;
125+
P_TCB tcb = rt_tid2ptcb(_tid);
126+
uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
127+
return top - tcb->tsk_stack;
117128
#endif
118129
#else
119130
return 0;
@@ -128,7 +139,11 @@ uint32_t Thread::max_stack() {
128139
high_mark++;
129140
return _thread_def.tcb.priv_stack - (high_mark * 4);
130141
#else
131-
return 0;
142+
P_TCB tcb = rt_tid2ptcb(_tid);
143+
uint32_t high_mark = 0;
144+
while (tcb->stack[high_mark] == 0xE25A2EA5)
145+
high_mark++;
146+
return tcb->priv_stack - (high_mark * 4);
132147
#endif
133148
#else
134149
return 0;

libraries/rtos/rtx/TARGET_CORTEX_M/rt_CMSIS.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static uint16_t rt_ms2tick (uint32_t millisec) {
407407
}
408408

409409
/// Convert Thread ID to TCB pointer
410-
static P_TCB rt_tid2ptcb (osThreadId thread_id) {
410+
P_TCB rt_tid2ptcb (osThreadId thread_id) {
411411
P_TCB ptcb;
412412

413413
if (thread_id == NULL) { return NULL; }
@@ -506,8 +506,8 @@ osStatus svcKernelStart (void) {
506506
if (os_tsk.run->task_id == 0xFFU) { // Idle Thread
507507
__set_PSP(os_tsk.run->tsk_stack + (8U*4U)); // Setup PSP
508508
}
509-
if (os_tsk.new == NULL) { // Force context switch
510-
os_tsk.new = os_tsk.run;
509+
if (os_tsk.new_tsk == NULL) { // Force context switch
510+
os_tsk.new_tsk = os_tsk.run;
511511
os_tsk.run = NULL;
512512
}
513513

libraries/rtos/rtx/TARGET_CORTEX_M/rt_HAL_CM.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ extern void dbg_task_switch (U32 task_id);
276276
#ifdef DBG_MSG
277277
#define DBG_INIT() dbg_init()
278278
#define DBG_TASK_NOTIFY(p_tcb,create) if (dbg_msg) dbg_task_notify(p_tcb,create)
279-
#define DBG_TASK_SWITCH(task_id) if (dbg_msg && (os_tsk.new!=os_tsk.run)) \
279+
#define DBG_TASK_SWITCH(task_id) if (dbg_msg && (os_tsk.new_tsk!=os_tsk.run)) \
280280
dbg_task_switch(task_id)
281281
#else
282282
#define DBG_INIT()

libraries/rtos/rtx/TARGET_CORTEX_M/rt_Task.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
9999

100100
void rt_switch_req (P_TCB p_new) {
101101
/* Switch to next task (identified by "p_new"). */
102-
os_tsk.new = p_new;
102+
os_tsk.new_tsk = p_new;
103103
p_new->state = RUNNING;
104104
DBG_TASK_SWITCH(p_new->task_id);
105105
}

libraries/rtos/rtx/TARGET_CORTEX_M/rt_TypeDef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ typedef struct OS_PSQ { /* Post Service Queue */
9898

9999
typedef struct OS_TSK {
100100
P_TCB run; /* Current running task */
101-
P_TCB new; /* Scheduled task to run */
101+
P_TCB new_tsk; /* Scheduled task to run */
102102
} *P_TSK;
103103

104104
typedef struct OS_ROBIN { /* Round Robin Control */

0 commit comments

Comments
 (0)