sdf

Dependencies:   mbed-rtos

Fork of mbed-rtos by mbed official

Committer:
mbed_official
Date:
Thu May 05 20:45:13 2016 +0100
Revision:
112:53ace74b190c
Parent:
107:bdd541595fc5
Child:
115:11950e007d8a
Synchronized with git revision 860fdd282b0dc3631a6c46b39442d4ab5343e534

Full URL: https://github.com/mbedmicro/mbed/commit/860fdd282b0dc3631a6c46b39442d4ab5343e534/

rtx update to v4.79

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 8:88a1a9c26ae3 1 /* mbed Microcontroller Library
emilmont 8:88a1a9c26ae3 2 * Copyright (c) 2006-2012 ARM Limited
emilmont 8:88a1a9c26ae3 3 *
emilmont 8:88a1a9c26ae3 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
emilmont 8:88a1a9c26ae3 5 * of this software and associated documentation files (the "Software"), to deal
emilmont 8:88a1a9c26ae3 6 * in the Software without restriction, including without limitation the rights
emilmont 8:88a1a9c26ae3 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
emilmont 8:88a1a9c26ae3 8 * copies of the Software, and to permit persons to whom the Software is
emilmont 8:88a1a9c26ae3 9 * furnished to do so, subject to the following conditions:
emilmont 8:88a1a9c26ae3 10 *
emilmont 8:88a1a9c26ae3 11 * The above copyright notice and this permission notice shall be included in
emilmont 8:88a1a9c26ae3 12 * all copies or substantial portions of the Software.
emilmont 8:88a1a9c26ae3 13 *
emilmont 8:88a1a9c26ae3 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
emilmont 8:88a1a9c26ae3 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
emilmont 8:88a1a9c26ae3 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
emilmont 8:88a1a9c26ae3 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
emilmont 8:88a1a9c26ae3 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
emilmont 8:88a1a9c26ae3 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
emilmont 8:88a1a9c26ae3 20 * SOFTWARE.
emilmont 8:88a1a9c26ae3 21 */
emilmont 8:88a1a9c26ae3 22 #include "Thread.h"
emilmont 8:88a1a9c26ae3 23
mbed_official 40:bd07334df5b1 24 #include "mbed_error.h"
mbed_official 107:bdd541595fc5 25 #include "rtos_idle.h"
emilmont 8:88a1a9c26ae3 26
mbed_official 112:53ace74b190c 27 // rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
mbed_official 112:53ace74b190c 28 #undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
mbed_official 112:53ace74b190c 29 #include "rt_TypeDef.h"
mbed_official 112:53ace74b190c 30
mbed_official 112:53ace74b190c 31 extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
mbed_official 112:53ace74b190c 32
emilmont 8:88a1a9c26ae3 33 namespace rtos {
emilmont 8:88a1a9c26ae3 34
emilmont 8:88a1a9c26ae3 35 Thread::Thread(void (*task)(void const *argument), void *argument,
emilmont 8:88a1a9c26ae3 36 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
mbed_official 112:53ace74b190c 37 #ifdef __MBED_CMSIS_RTOS_CM
emilmont 8:88a1a9c26ae3 38 _thread_def.pthread = task;
emilmont 8:88a1a9c26ae3 39 _thread_def.tpriority = priority;
emilmont 8:88a1a9c26ae3 40 _thread_def.stacksize = stack_size;
emilmont 8:88a1a9c26ae3 41 if (stack_pointer != NULL) {
mbed_official 59:28712e303960 42 _thread_def.stack_pointer = (uint32_t*)stack_pointer;
emilmont 8:88a1a9c26ae3 43 _dynamic_stack = false;
emilmont 8:88a1a9c26ae3 44 } else {
mbed_official 59:28712e303960 45 _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)];
emilmont 8:88a1a9c26ae3 46 if (_thread_def.stack_pointer == NULL)
mbed_official 25:420a92812cd1 47 error("Error allocating the stack memory\n");
emilmont 8:88a1a9c26ae3 48 _dynamic_stack = true;
emilmont 8:88a1a9c26ae3 49 }
mbed_official 84:143955ffb790 50
mbed_official 84:143955ffb790 51 //Fill the stack with a magic word for maximum usage checking
mbed_official 90:21b438192b0f 52 for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) {
mbed_official 84:143955ffb790 53 _thread_def.stack_pointer[i] = 0xE25A2EA5;
mbed_official 84:143955ffb790 54 }
emilmont 8:88a1a9c26ae3 55 #endif
emilmont 8:88a1a9c26ae3 56 _tid = osThreadCreate(&_thread_def, argument);
emilmont 8:88a1a9c26ae3 57 }
emilmont 8:88a1a9c26ae3 58
emilmont 8:88a1a9c26ae3 59 osStatus Thread::terminate() {
emilmont 8:88a1a9c26ae3 60 return osThreadTerminate(_tid);
emilmont 8:88a1a9c26ae3 61 }
emilmont 8:88a1a9c26ae3 62
emilmont 8:88a1a9c26ae3 63 osStatus Thread::set_priority(osPriority priority) {
emilmont 8:88a1a9c26ae3 64 return osThreadSetPriority(_tid, priority);
emilmont 8:88a1a9c26ae3 65 }
emilmont 8:88a1a9c26ae3 66
emilmont 8:88a1a9c26ae3 67 osPriority Thread::get_priority() {
emilmont 8:88a1a9c26ae3 68 return osThreadGetPriority(_tid);
emilmont 8:88a1a9c26ae3 69 }
emilmont 8:88a1a9c26ae3 70
emilmont 8:88a1a9c26ae3 71 int32_t Thread::signal_set(int32_t signals) {
emilmont 8:88a1a9c26ae3 72 return osSignalSet(_tid, signals);
emilmont 8:88a1a9c26ae3 73 }
emilmont 8:88a1a9c26ae3 74
mbed_official 76:85a52b7ef44b 75 int32_t Thread::signal_clr(int32_t signals) {
mbed_official 76:85a52b7ef44b 76 return osSignalClear(_tid, signals);
mbed_official 76:85a52b7ef44b 77 }
mbed_official 76:85a52b7ef44b 78
emilmont 8:88a1a9c26ae3 79 Thread::State Thread::get_state() {
mbed_official 112:53ace74b190c 80 #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 112:53ace74b190c 81 #ifdef CMSIS_OS_RTX
emilmont 8:88a1a9c26ae3 82 return ((State)_thread_def.tcb.state);
mbed_official 112:53ace74b190c 83 #endif
mbed_official 48:e9a2c7cb57a4 84 #else
mbed_official 48:e9a2c7cb57a4 85 uint8_t status;
mbed_official 48:e9a2c7cb57a4 86 status = osThreadGetState(_tid);
mbed_official 48:e9a2c7cb57a4 87 return ((State)status);
mbed_official 48:e9a2c7cb57a4 88 #endif
emilmont 8:88a1a9c26ae3 89 }
emilmont 8:88a1a9c26ae3 90
mbed_official 84:143955ffb790 91 uint32_t Thread::stack_size() {
mbed_official 84:143955ffb790 92 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 93 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 94 return _thread_def.tcb.priv_stack;
mbed_official 84:143955ffb790 95 #else
mbed_official 112:53ace74b190c 96 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 97 return tcb->priv_stack;
mbed_official 112:53ace74b190c 98 #endif
mbed_official 112:53ace74b190c 99 #else
mbed_official 84:143955ffb790 100 return 0;
mbed_official 84:143955ffb790 101 #endif
mbed_official 84:143955ffb790 102 }
mbed_official 84:143955ffb790 103
mbed_official 84:143955ffb790 104 uint32_t Thread::free_stack() {
mbed_official 84:143955ffb790 105 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 106 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 107 uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
mbed_official 84:143955ffb790 108 return _thread_def.tcb.tsk_stack - bottom;
mbed_official 84:143955ffb790 109 #else
mbed_official 112:53ace74b190c 110 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 111 uint32_t bottom = (uint32_t)tcb->stack;
mbed_official 112:53ace74b190c 112 return tcb->tsk_stack - bottom;
mbed_official 112:53ace74b190c 113 #endif
mbed_official 112:53ace74b190c 114 #else
mbed_official 84:143955ffb790 115 return 0;
mbed_official 84:143955ffb790 116 #endif
mbed_official 84:143955ffb790 117 }
mbed_official 84:143955ffb790 118
mbed_official 84:143955ffb790 119 uint32_t Thread::used_stack() {
mbed_official 84:143955ffb790 120 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 121 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 122 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
mbed_official 84:143955ffb790 123 return top - _thread_def.tcb.tsk_stack;
mbed_official 84:143955ffb790 124 #else
mbed_official 112:53ace74b190c 125 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 126 uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
mbed_official 112:53ace74b190c 127 return top - tcb->tsk_stack;
mbed_official 112:53ace74b190c 128 #endif
mbed_official 112:53ace74b190c 129 #else
mbed_official 84:143955ffb790 130 return 0;
mbed_official 84:143955ffb790 131 #endif
mbed_official 84:143955ffb790 132 }
mbed_official 84:143955ffb790 133
mbed_official 84:143955ffb790 134 uint32_t Thread::max_stack() {
mbed_official 84:143955ffb790 135 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 136 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 137 uint32_t high_mark = 0;
mbed_official 84:143955ffb790 138 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
mbed_official 84:143955ffb790 139 high_mark++;
mbed_official 84:143955ffb790 140 return _thread_def.tcb.priv_stack - (high_mark * 4);
mbed_official 84:143955ffb790 141 #else
mbed_official 112:53ace74b190c 142 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 143 uint32_t high_mark = 0;
mbed_official 112:53ace74b190c 144 while (tcb->stack[high_mark] == 0xE25A2EA5)
mbed_official 112:53ace74b190c 145 high_mark++;
mbed_official 112:53ace74b190c 146 return tcb->priv_stack - (high_mark * 4);
mbed_official 112:53ace74b190c 147 #endif
mbed_official 112:53ace74b190c 148 #else
mbed_official 84:143955ffb790 149 return 0;
mbed_official 84:143955ffb790 150 #endif
mbed_official 84:143955ffb790 151 }
mbed_official 84:143955ffb790 152
emilmont 8:88a1a9c26ae3 153 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
emilmont 8:88a1a9c26ae3 154 return osSignalWait(signals, millisec);
emilmont 8:88a1a9c26ae3 155 }
emilmont 8:88a1a9c26ae3 156
emilmont 8:88a1a9c26ae3 157 osStatus Thread::wait(uint32_t millisec) {
emilmont 8:88a1a9c26ae3 158 return osDelay(millisec);
emilmont 8:88a1a9c26ae3 159 }
emilmont 8:88a1a9c26ae3 160
emilmont 8:88a1a9c26ae3 161 osStatus Thread::yield() {
emilmont 8:88a1a9c26ae3 162 return osThreadYield();
emilmont 8:88a1a9c26ae3 163 }
emilmont 8:88a1a9c26ae3 164
emilmont 8:88a1a9c26ae3 165 osThreadId Thread::gettid() {
emilmont 8:88a1a9c26ae3 166 return osThreadGetId();
emilmont 8:88a1a9c26ae3 167 }
emilmont 8:88a1a9c26ae3 168
mbed_official 107:bdd541595fc5 169 void Thread::attach_idle_hook(void (*fptr)(void)) {
mbed_official 107:bdd541595fc5 170 rtos_attach_idle_hook(fptr);
mbed_official 107:bdd541595fc5 171 }
mbed_official 107:bdd541595fc5 172
emilmont 8:88a1a9c26ae3 173 Thread::~Thread() {
emilmont 8:88a1a9c26ae3 174 terminate();
mbed_official 112:53ace74b190c 175 #ifdef __MBED_CMSIS_RTOS_CM
emilmont 8:88a1a9c26ae3 176 if (_dynamic_stack) {
emilmont 8:88a1a9c26ae3 177 delete[] (_thread_def.stack_pointer);
emilmont 8:88a1a9c26ae3 178 }
mbed_official 112:53ace74b190c 179 #endif
emilmont 8:88a1a9c26ae3 180 }
emilmont 8:88a1a9c26ae3 181
emilmont 8:88a1a9c26ae3 182 }