Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   Assignment_3__Embedded_Software

Fork of mbed-rtos by mbed official

Revision:
84:143955ffb790
Parent:
76:85a52b7ef44b
Child:
85:ef0a22cdf839
--- a/rtos/Thread.cpp	Tue Jun 16 07:45:29 2015 +0100
+++ b/rtos/Thread.cpp	Wed Jun 17 10:00:10 2015 +0100
@@ -41,6 +41,11 @@
             error("Error allocating the stack memory\n");
         _dynamic_stack = true;
     }
+    
+    //Fill the stack with a magic word for maximum usage checking
+    for (int i = 0; i < (stack_size / sizeof(uint32_t)); i++) {
+        _thread_def.stack_pointer[i] = 0xE25A2EA5;
+    }
 #endif
 #endif
     _tid = osThreadCreate(&_thread_def, argument);
@@ -76,6 +81,43 @@
 #endif
 }
 
+uint32_t Thread::stack_size() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    return _thread_def.tcb.priv_stack;
+#else
+    return 0;
+#endif
+}
+
+uint32_t Thread::free_stack() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
+    return _thread_def.tcb.tsk_stack - bottom;
+#else
+    return 0;
+#endif
+}
+
+uint32_t Thread::used_stack() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
+    return top - _thread_def.tcb.tsk_stack;
+#else
+    return 0;
+#endif
+}
+
+uint32_t Thread::max_stack() {
+#ifndef __MBED_CMSIS_RTOS_CA9
+    uint32_t high_mark = 0;
+    while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
+        high_mark++;
+    return _thread_def.tcb.priv_stack - (high_mark * 4);
+#else
+    return 0;
+#endif
+}
+
 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
     return osSignalWait(signals, millisec);
 }