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

Committer:
Kojto
Date:
Mon Jul 25 14:12:24 2016 +0100
Revision:
118:6635230e06ba
Parent:
115:11950e007d8a
Child:
119:19af2d39a542
RTOS rev118

Compatible with the mbed library v122

Changes:
- warnings about duplicated CM symbols fix
- init sequence update - allows init array to be run prior kernel start
- RTOS with OS_TIMERS=0 fix for thread id
- Thread ctor is deprecated, use start() method
- main stack fix for IAR (set via linker script)
- add TCB context pointer
- provide thread safety for toolchains (std lib locks)
- add MBED_RTOS_SINGLE_THREAD macro (sets TSKCNT to 1 and TIMERS to 0)
- nrf51, nucleo l423kc, nucleo f767zi, nucleo f446ze, efm32 support addition
- add OSObserver function

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
Kojto 118:6635230e06ba 24 #include "mbed.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
Kojto 118:6635230e06ba 35 void Thread::constructor(osPriority priority,
Kojto 118:6635230e06ba 36 uint32_t stack_size, unsigned char *stack_pointer) {
Kojto 118:6635230e06ba 37 _tid = 0;
Kojto 118:6635230e06ba 38 _dynamic_stack = (stack_pointer == NULL);
Kojto 118:6635230e06ba 39
mbed_official 115:11950e007d8a 40 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
emilmont 8:88a1a9c26ae3 41 _thread_def.tpriority = priority;
emilmont 8:88a1a9c26ae3 42 _thread_def.stacksize = stack_size;
Kojto 118:6635230e06ba 43 _thread_def.stack_pointer = (uint32_t*)stack_pointer;
Kojto 118:6635230e06ba 44 #endif
Kojto 118:6635230e06ba 45 }
Kojto 118:6635230e06ba 46
Kojto 118:6635230e06ba 47 void Thread::constructor(Callback<void()> task,
Kojto 118:6635230e06ba 48 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
Kojto 118:6635230e06ba 49 constructor(priority, stack_size, stack_pointer);
Kojto 118:6635230e06ba 50
Kojto 118:6635230e06ba 51 switch (start(task)) {
Kojto 118:6635230e06ba 52 case osErrorResource:
Kojto 118:6635230e06ba 53 error("OS ran out of threads!\n");
Kojto 118:6635230e06ba 54 break;
Kojto 118:6635230e06ba 55 case osErrorParameter:
Kojto 118:6635230e06ba 56 error("Thread already running!\n");
Kojto 118:6635230e06ba 57 break;
Kojto 118:6635230e06ba 58 case osErrorNoMemory:
Kojto 118:6635230e06ba 59 error("Error allocating the stack memory\n");
Kojto 118:6635230e06ba 60 default:
Kojto 118:6635230e06ba 61 break;
Kojto 118:6635230e06ba 62 }
Kojto 118:6635230e06ba 63 }
Kojto 118:6635230e06ba 64
Kojto 118:6635230e06ba 65 osStatus Thread::start(Callback<void()> task) {
Kojto 118:6635230e06ba 66 if (_tid != 0) {
Kojto 118:6635230e06ba 67 return osErrorParameter;
Kojto 118:6635230e06ba 68 }
Kojto 118:6635230e06ba 69
Kojto 118:6635230e06ba 70 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
Kojto 118:6635230e06ba 71 _thread_def.pthread = (void (*)(const void *))Callback<void()>::thunk;
Kojto 118:6635230e06ba 72 if (_thread_def.stack_pointer == NULL) {
Kojto 118:6635230e06ba 73 _thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
emilmont 8:88a1a9c26ae3 74 if (_thread_def.stack_pointer == NULL)
Kojto 118:6635230e06ba 75 return osErrorNoMemory;
emilmont 8:88a1a9c26ae3 76 }
Kojto 118:6635230e06ba 77
mbed_official 84:143955ffb790 78 //Fill the stack with a magic word for maximum usage checking
Kojto 118:6635230e06ba 79 for (uint32_t i = 0; i < (_thread_def.stacksize / sizeof(uint32_t)); i++) {
mbed_official 84:143955ffb790 80 _thread_def.stack_pointer[i] = 0xE25A2EA5;
mbed_official 84:143955ffb790 81 }
emilmont 8:88a1a9c26ae3 82 #endif
Kojto 118:6635230e06ba 83 _task = task;
Kojto 118:6635230e06ba 84 _tid = osThreadCreate(&_thread_def, &_task);
Kojto 118:6635230e06ba 85 if (_tid == NULL) {
Kojto 118:6635230e06ba 86 if (_dynamic_stack) delete[] (_thread_def.stack_pointer);
Kojto 118:6635230e06ba 87 return osErrorResource;
Kojto 118:6635230e06ba 88 }
Kojto 118:6635230e06ba 89 return osOK;
emilmont 8:88a1a9c26ae3 90 }
emilmont 8:88a1a9c26ae3 91
emilmont 8:88a1a9c26ae3 92 osStatus Thread::terminate() {
emilmont 8:88a1a9c26ae3 93 return osThreadTerminate(_tid);
emilmont 8:88a1a9c26ae3 94 }
emilmont 8:88a1a9c26ae3 95
Kojto 118:6635230e06ba 96 osStatus Thread::join() {
Kojto 118:6635230e06ba 97 while (true) {
Kojto 118:6635230e06ba 98 uint8_t state = get_state();
Kojto 118:6635230e06ba 99 if (state == Thread::Inactive || state == osErrorParameter) {
Kojto 118:6635230e06ba 100 return osOK;
Kojto 118:6635230e06ba 101 }
Kojto 118:6635230e06ba 102
Kojto 118:6635230e06ba 103 osStatus status = yield();
Kojto 118:6635230e06ba 104 if (status != osOK) {
Kojto 118:6635230e06ba 105 return status;
Kojto 118:6635230e06ba 106 }
Kojto 118:6635230e06ba 107 }
Kojto 118:6635230e06ba 108 }
Kojto 118:6635230e06ba 109
emilmont 8:88a1a9c26ae3 110 osStatus Thread::set_priority(osPriority priority) {
emilmont 8:88a1a9c26ae3 111 return osThreadSetPriority(_tid, priority);
emilmont 8:88a1a9c26ae3 112 }
emilmont 8:88a1a9c26ae3 113
emilmont 8:88a1a9c26ae3 114 osPriority Thread::get_priority() {
emilmont 8:88a1a9c26ae3 115 return osThreadGetPriority(_tid);
emilmont 8:88a1a9c26ae3 116 }
emilmont 8:88a1a9c26ae3 117
emilmont 8:88a1a9c26ae3 118 int32_t Thread::signal_set(int32_t signals) {
emilmont 8:88a1a9c26ae3 119 return osSignalSet(_tid, signals);
emilmont 8:88a1a9c26ae3 120 }
emilmont 8:88a1a9c26ae3 121
mbed_official 76:85a52b7ef44b 122 int32_t Thread::signal_clr(int32_t signals) {
mbed_official 76:85a52b7ef44b 123 return osSignalClear(_tid, signals);
mbed_official 76:85a52b7ef44b 124 }
mbed_official 76:85a52b7ef44b 125
emilmont 8:88a1a9c26ae3 126 Thread::State Thread::get_state() {
mbed_official 112:53ace74b190c 127 #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 112:53ace74b190c 128 #ifdef CMSIS_OS_RTX
emilmont 8:88a1a9c26ae3 129 return ((State)_thread_def.tcb.state);
mbed_official 112:53ace74b190c 130 #endif
mbed_official 48:e9a2c7cb57a4 131 #else
mbed_official 48:e9a2c7cb57a4 132 uint8_t status;
mbed_official 48:e9a2c7cb57a4 133 status = osThreadGetState(_tid);
mbed_official 48:e9a2c7cb57a4 134 return ((State)status);
mbed_official 48:e9a2c7cb57a4 135 #endif
emilmont 8:88a1a9c26ae3 136 }
emilmont 8:88a1a9c26ae3 137
mbed_official 84:143955ffb790 138 uint32_t Thread::stack_size() {
mbed_official 84:143955ffb790 139 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 140 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 141 return _thread_def.tcb.priv_stack;
mbed_official 84:143955ffb790 142 #else
mbed_official 112:53ace74b190c 143 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 144 return tcb->priv_stack;
mbed_official 112:53ace74b190c 145 #endif
mbed_official 112:53ace74b190c 146 #else
mbed_official 84:143955ffb790 147 return 0;
mbed_official 84:143955ffb790 148 #endif
mbed_official 84:143955ffb790 149 }
mbed_official 84:143955ffb790 150
mbed_official 84:143955ffb790 151 uint32_t Thread::free_stack() {
mbed_official 84:143955ffb790 152 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 153 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 154 uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
mbed_official 84:143955ffb790 155 return _thread_def.tcb.tsk_stack - bottom;
mbed_official 84:143955ffb790 156 #else
mbed_official 112:53ace74b190c 157 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 158 uint32_t bottom = (uint32_t)tcb->stack;
mbed_official 112:53ace74b190c 159 return tcb->tsk_stack - bottom;
mbed_official 112:53ace74b190c 160 #endif
mbed_official 112:53ace74b190c 161 #else
mbed_official 84:143955ffb790 162 return 0;
mbed_official 84:143955ffb790 163 #endif
mbed_official 84:143955ffb790 164 }
mbed_official 84:143955ffb790 165
mbed_official 84:143955ffb790 166 uint32_t Thread::used_stack() {
mbed_official 84:143955ffb790 167 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 168 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 169 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
mbed_official 84:143955ffb790 170 return top - _thread_def.tcb.tsk_stack;
mbed_official 84:143955ffb790 171 #else
mbed_official 112:53ace74b190c 172 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 173 uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
mbed_official 112:53ace74b190c 174 return top - tcb->tsk_stack;
mbed_official 112:53ace74b190c 175 #endif
mbed_official 112:53ace74b190c 176 #else
mbed_official 84:143955ffb790 177 return 0;
mbed_official 84:143955ffb790 178 #endif
mbed_official 84:143955ffb790 179 }
mbed_official 84:143955ffb790 180
mbed_official 84:143955ffb790 181 uint32_t Thread::max_stack() {
mbed_official 84:143955ffb790 182 #ifndef __MBED_CMSIS_RTOS_CA9
mbed_official 112:53ace74b190c 183 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
mbed_official 84:143955ffb790 184 uint32_t high_mark = 0;
mbed_official 84:143955ffb790 185 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
mbed_official 84:143955ffb790 186 high_mark++;
mbed_official 84:143955ffb790 187 return _thread_def.tcb.priv_stack - (high_mark * 4);
mbed_official 84:143955ffb790 188 #else
mbed_official 112:53ace74b190c 189 P_TCB tcb = rt_tid2ptcb(_tid);
mbed_official 112:53ace74b190c 190 uint32_t high_mark = 0;
mbed_official 112:53ace74b190c 191 while (tcb->stack[high_mark] == 0xE25A2EA5)
mbed_official 112:53ace74b190c 192 high_mark++;
mbed_official 112:53ace74b190c 193 return tcb->priv_stack - (high_mark * 4);
mbed_official 112:53ace74b190c 194 #endif
mbed_official 112:53ace74b190c 195 #else
mbed_official 84:143955ffb790 196 return 0;
mbed_official 84:143955ffb790 197 #endif
mbed_official 84:143955ffb790 198 }
mbed_official 84:143955ffb790 199
emilmont 8:88a1a9c26ae3 200 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
emilmont 8:88a1a9c26ae3 201 return osSignalWait(signals, millisec);
emilmont 8:88a1a9c26ae3 202 }
emilmont 8:88a1a9c26ae3 203
emilmont 8:88a1a9c26ae3 204 osStatus Thread::wait(uint32_t millisec) {
emilmont 8:88a1a9c26ae3 205 return osDelay(millisec);
emilmont 8:88a1a9c26ae3 206 }
emilmont 8:88a1a9c26ae3 207
emilmont 8:88a1a9c26ae3 208 osStatus Thread::yield() {
emilmont 8:88a1a9c26ae3 209 return osThreadYield();
emilmont 8:88a1a9c26ae3 210 }
emilmont 8:88a1a9c26ae3 211
emilmont 8:88a1a9c26ae3 212 osThreadId Thread::gettid() {
emilmont 8:88a1a9c26ae3 213 return osThreadGetId();
emilmont 8:88a1a9c26ae3 214 }
emilmont 8:88a1a9c26ae3 215
mbed_official 107:bdd541595fc5 216 void Thread::attach_idle_hook(void (*fptr)(void)) {
mbed_official 107:bdd541595fc5 217 rtos_attach_idle_hook(fptr);
mbed_official 107:bdd541595fc5 218 }
mbed_official 107:bdd541595fc5 219
emilmont 8:88a1a9c26ae3 220 Thread::~Thread() {
emilmont 8:88a1a9c26ae3 221 terminate();
mbed_official 112:53ace74b190c 222 #ifdef __MBED_CMSIS_RTOS_CM
emilmont 8:88a1a9c26ae3 223 if (_dynamic_stack) {
emilmont 8:88a1a9c26ae3 224 delete[] (_thread_def.stack_pointer);
emilmont 8:88a1a9c26ae3 225 }
mbed_official 112:53ace74b190c 226 #endif
emilmont 8:88a1a9c26ae3 227 }
emilmont 8:88a1a9c26ae3 228
emilmont 8:88a1a9c26ae3 229 }