Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /* mbed Microcontroller Library
switches 0:5c4d7b2438d3 2 * Copyright (c) 2006-2012 ARM Limited
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
switches 0:5c4d7b2438d3 5 * of this software and associated documentation files (the "Software"), to deal
switches 0:5c4d7b2438d3 6 * in the Software without restriction, including without limitation the rights
switches 0:5c4d7b2438d3 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
switches 0:5c4d7b2438d3 8 * copies of the Software, and to permit persons to whom the Software is
switches 0:5c4d7b2438d3 9 * furnished to do so, subject to the following conditions:
switches 0:5c4d7b2438d3 10 *
switches 0:5c4d7b2438d3 11 * The above copyright notice and this permission notice shall be included in
switches 0:5c4d7b2438d3 12 * all copies or substantial portions of the Software.
switches 0:5c4d7b2438d3 13 *
switches 0:5c4d7b2438d3 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
switches 0:5c4d7b2438d3 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
switches 0:5c4d7b2438d3 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
switches 0:5c4d7b2438d3 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
switches 0:5c4d7b2438d3 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
switches 0:5c4d7b2438d3 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
switches 0:5c4d7b2438d3 20 * SOFTWARE.
switches 0:5c4d7b2438d3 21 */
switches 0:5c4d7b2438d3 22 #include "rtos/Thread.h"
switches 0:5c4d7b2438d3 23
switches 0:5c4d7b2438d3 24 #include "mbed.h"
switches 0:5c4d7b2438d3 25 #include "rtos/rtos_idle.h"
switches 0:5c4d7b2438d3 26
switches 0:5c4d7b2438d3 27 // rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
switches 0:5c4d7b2438d3 28 #undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
switches 0:5c4d7b2438d3 29 #include "rt_TypeDef.h"
switches 0:5c4d7b2438d3 30
switches 0:5c4d7b2438d3 31 extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
switches 0:5c4d7b2438d3 32
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34 static void (*terminate_hook)(osThreadId id) = 0;
switches 0:5c4d7b2438d3 35 extern "C" void thread_terminate_hook(osThreadId id)
switches 0:5c4d7b2438d3 36 {
switches 0:5c4d7b2438d3 37 if (terminate_hook != (void (*)(osThreadId))NULL) {
switches 0:5c4d7b2438d3 38 terminate_hook(id);
switches 0:5c4d7b2438d3 39 }
switches 0:5c4d7b2438d3 40 }
switches 0:5c4d7b2438d3 41
switches 0:5c4d7b2438d3 42 namespace rtos {
switches 0:5c4d7b2438d3 43
switches 0:5c4d7b2438d3 44 void Thread::constructor(osPriority priority,
switches 0:5c4d7b2438d3 45 uint32_t stack_size, unsigned char *stack_pointer) {
switches 0:5c4d7b2438d3 46 _tid = 0;
switches 0:5c4d7b2438d3 47 _dynamic_stack = (stack_pointer == NULL);
switches 0:5c4d7b2438d3 48
switches 0:5c4d7b2438d3 49 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 50 _thread_def.tpriority = priority;
switches 0:5c4d7b2438d3 51 _thread_def.stacksize = stack_size;
switches 0:5c4d7b2438d3 52 _thread_def.stack_pointer = (uint32_t*)stack_pointer;
switches 0:5c4d7b2438d3 53 #endif
switches 0:5c4d7b2438d3 54 }
switches 0:5c4d7b2438d3 55
switches 0:5c4d7b2438d3 56 void Thread::constructor(Callback<void()> task,
switches 0:5c4d7b2438d3 57 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
switches 0:5c4d7b2438d3 58 constructor(priority, stack_size, stack_pointer);
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 switch (start(task)) {
switches 0:5c4d7b2438d3 61 case osErrorResource:
switches 0:5c4d7b2438d3 62 error("OS ran out of threads!\n");
switches 0:5c4d7b2438d3 63 break;
switches 0:5c4d7b2438d3 64 case osErrorParameter:
switches 0:5c4d7b2438d3 65 error("Thread already running!\n");
switches 0:5c4d7b2438d3 66 break;
switches 0:5c4d7b2438d3 67 case osErrorNoMemory:
switches 0:5c4d7b2438d3 68 error("Error allocating the stack memory\n");
switches 0:5c4d7b2438d3 69 default:
switches 0:5c4d7b2438d3 70 break;
switches 0:5c4d7b2438d3 71 }
switches 0:5c4d7b2438d3 72 }
switches 0:5c4d7b2438d3 73
switches 0:5c4d7b2438d3 74 osStatus Thread::start(Callback<void()> task) {
switches 0:5c4d7b2438d3 75 _mutex.lock();
switches 0:5c4d7b2438d3 76
switches 0:5c4d7b2438d3 77 if (_tid != 0) {
switches 0:5c4d7b2438d3 78 _mutex.unlock();
switches 0:5c4d7b2438d3 79 return osErrorParameter;
switches 0:5c4d7b2438d3 80 }
switches 0:5c4d7b2438d3 81
switches 0:5c4d7b2438d3 82 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 83 _thread_def.pthread = Thread::_thunk;
switches 0:5c4d7b2438d3 84 if (_thread_def.stack_pointer == NULL) {
switches 0:5c4d7b2438d3 85 _thread_def.stack_pointer = new uint32_t[_thread_def.stacksize/sizeof(uint32_t)];
switches 0:5c4d7b2438d3 86 MBED_ASSERT(_thread_def.stack_pointer != NULL);
switches 0:5c4d7b2438d3 87 }
switches 0:5c4d7b2438d3 88
switches 0:5c4d7b2438d3 89 //Fill the stack with a magic word for maximum usage checking
switches 0:5c4d7b2438d3 90 for (uint32_t i = 0; i < (_thread_def.stacksize / sizeof(uint32_t)); i++) {
switches 0:5c4d7b2438d3 91 _thread_def.stack_pointer[i] = 0xE25A2EA5;
switches 0:5c4d7b2438d3 92 }
switches 0:5c4d7b2438d3 93 #endif
switches 0:5c4d7b2438d3 94 _task = task;
switches 0:5c4d7b2438d3 95 _tid = osThreadCreate(&_thread_def, this);
switches 0:5c4d7b2438d3 96 if (_tid == NULL) {
switches 0:5c4d7b2438d3 97 if (_dynamic_stack) {
switches 0:5c4d7b2438d3 98 delete[] (_thread_def.stack_pointer);
switches 0:5c4d7b2438d3 99 _thread_def.stack_pointer = (uint32_t*)NULL;
switches 0:5c4d7b2438d3 100 }
switches 0:5c4d7b2438d3 101 _mutex.unlock();
switches 0:5c4d7b2438d3 102 _join_sem.release();
switches 0:5c4d7b2438d3 103 return osErrorResource;
switches 0:5c4d7b2438d3 104 }
switches 0:5c4d7b2438d3 105
switches 0:5c4d7b2438d3 106 _mutex.unlock();
switches 0:5c4d7b2438d3 107 return osOK;
switches 0:5c4d7b2438d3 108 }
switches 0:5c4d7b2438d3 109
switches 0:5c4d7b2438d3 110 osStatus Thread::terminate() {
switches 0:5c4d7b2438d3 111 osStatus ret;
switches 0:5c4d7b2438d3 112 _mutex.lock();
switches 0:5c4d7b2438d3 113
switches 0:5c4d7b2438d3 114 // Set the Thread's tid to NULL and
switches 0:5c4d7b2438d3 115 // release the semaphore before terminating
switches 0:5c4d7b2438d3 116 // since this thread could be terminating itself
switches 0:5c4d7b2438d3 117 osThreadId local_id = _tid;
switches 0:5c4d7b2438d3 118 _join_sem.release();
switches 0:5c4d7b2438d3 119 _tid = (osThreadId)NULL;
switches 0:5c4d7b2438d3 120
switches 0:5c4d7b2438d3 121 ret = osThreadTerminate(local_id);
switches 0:5c4d7b2438d3 122
switches 0:5c4d7b2438d3 123 _mutex.unlock();
switches 0:5c4d7b2438d3 124 return ret;
switches 0:5c4d7b2438d3 125 }
switches 0:5c4d7b2438d3 126
switches 0:5c4d7b2438d3 127 osStatus Thread::join() {
switches 0:5c4d7b2438d3 128 int32_t ret = _join_sem.wait();
switches 0:5c4d7b2438d3 129 if (ret < 0) {
switches 0:5c4d7b2438d3 130 return osErrorOS;
switches 0:5c4d7b2438d3 131 }
switches 0:5c4d7b2438d3 132
switches 0:5c4d7b2438d3 133 // The semaphore has been released so this thread is being
switches 0:5c4d7b2438d3 134 // terminated or has been terminated. Once the mutex has
switches 0:5c4d7b2438d3 135 // been locked it is ensured that the thread is deleted.
switches 0:5c4d7b2438d3 136 _mutex.lock();
switches 0:5c4d7b2438d3 137 MBED_ASSERT(NULL == _tid);
switches 0:5c4d7b2438d3 138 _mutex.unlock();
switches 0:5c4d7b2438d3 139
switches 0:5c4d7b2438d3 140 // Release sem so any other threads joining this thread wake up
switches 0:5c4d7b2438d3 141 _join_sem.release();
switches 0:5c4d7b2438d3 142 return osOK;
switches 0:5c4d7b2438d3 143 }
switches 0:5c4d7b2438d3 144
switches 0:5c4d7b2438d3 145 osStatus Thread::set_priority(osPriority priority) {
switches 0:5c4d7b2438d3 146 osStatus ret;
switches 0:5c4d7b2438d3 147 _mutex.lock();
switches 0:5c4d7b2438d3 148
switches 0:5c4d7b2438d3 149 ret = osThreadSetPriority(_tid, priority);
switches 0:5c4d7b2438d3 150
switches 0:5c4d7b2438d3 151 _mutex.unlock();
switches 0:5c4d7b2438d3 152 return ret;
switches 0:5c4d7b2438d3 153 }
switches 0:5c4d7b2438d3 154
switches 0:5c4d7b2438d3 155 osPriority Thread::get_priority() {
switches 0:5c4d7b2438d3 156 osPriority ret;
switches 0:5c4d7b2438d3 157 _mutex.lock();
switches 0:5c4d7b2438d3 158
switches 0:5c4d7b2438d3 159 ret = osThreadGetPriority(_tid);
switches 0:5c4d7b2438d3 160
switches 0:5c4d7b2438d3 161 _mutex.unlock();
switches 0:5c4d7b2438d3 162 return ret;
switches 0:5c4d7b2438d3 163 }
switches 0:5c4d7b2438d3 164
switches 0:5c4d7b2438d3 165 int32_t Thread::signal_set(int32_t signals) {
switches 0:5c4d7b2438d3 166 // osSignalSet is thread safe as long as the underlying
switches 0:5c4d7b2438d3 167 // thread does not get terminated or return from main
switches 0:5c4d7b2438d3 168 return osSignalSet(_tid, signals);
switches 0:5c4d7b2438d3 169 }
switches 0:5c4d7b2438d3 170
switches 0:5c4d7b2438d3 171 int32_t Thread::signal_clr(int32_t signals) {
switches 0:5c4d7b2438d3 172 // osSignalClear is thread safe as long as the underlying
switches 0:5c4d7b2438d3 173 // thread does not get terminated or return from main
switches 0:5c4d7b2438d3 174 return osSignalClear(_tid, signals);
switches 0:5c4d7b2438d3 175 }
switches 0:5c4d7b2438d3 176
switches 0:5c4d7b2438d3 177 Thread::State Thread::get_state() {
switches 0:5c4d7b2438d3 178 #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 179 #ifdef CMSIS_OS_RTX
switches 0:5c4d7b2438d3 180 State status = Deleted;
switches 0:5c4d7b2438d3 181 _mutex.lock();
switches 0:5c4d7b2438d3 182
switches 0:5c4d7b2438d3 183 if (_tid != NULL) {
switches 0:5c4d7b2438d3 184 status = (State)_thread_def.tcb.state;
switches 0:5c4d7b2438d3 185 }
switches 0:5c4d7b2438d3 186
switches 0:5c4d7b2438d3 187 _mutex.unlock();
switches 0:5c4d7b2438d3 188 return status;
switches 0:5c4d7b2438d3 189 #endif
switches 0:5c4d7b2438d3 190 #else
switches 0:5c4d7b2438d3 191 State status = Deleted;
switches 0:5c4d7b2438d3 192 _mutex.lock();
switches 0:5c4d7b2438d3 193
switches 0:5c4d7b2438d3 194 if (_tid != NULL) {
switches 0:5c4d7b2438d3 195 status = (State)osThreadGetState(_tid);
switches 0:5c4d7b2438d3 196 }
switches 0:5c4d7b2438d3 197
switches 0:5c4d7b2438d3 198 _mutex.unlock();
switches 0:5c4d7b2438d3 199 return status;
switches 0:5c4d7b2438d3 200 #endif
switches 0:5c4d7b2438d3 201 }
switches 0:5c4d7b2438d3 202
switches 0:5c4d7b2438d3 203 uint32_t Thread::stack_size() {
switches 0:5c4d7b2438d3 204 #ifndef __MBED_CMSIS_RTOS_CA9
switches 0:5c4d7b2438d3 205 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 206 uint32_t size = 0;
switches 0:5c4d7b2438d3 207 _mutex.lock();
switches 0:5c4d7b2438d3 208
switches 0:5c4d7b2438d3 209 if (_tid != NULL) {
switches 0:5c4d7b2438d3 210 size = _thread_def.tcb.priv_stack;
switches 0:5c4d7b2438d3 211 }
switches 0:5c4d7b2438d3 212
switches 0:5c4d7b2438d3 213 _mutex.unlock();
switches 0:5c4d7b2438d3 214 return size;
switches 0:5c4d7b2438d3 215 #else
switches 0:5c4d7b2438d3 216 uint32_t size = 0;
switches 0:5c4d7b2438d3 217 _mutex.lock();
switches 0:5c4d7b2438d3 218
switches 0:5c4d7b2438d3 219 if (_tid != NULL) {
switches 0:5c4d7b2438d3 220 P_TCB tcb = rt_tid2ptcb(_tid);
switches 0:5c4d7b2438d3 221 size = tcb->priv_stack;
switches 0:5c4d7b2438d3 222 }
switches 0:5c4d7b2438d3 223
switches 0:5c4d7b2438d3 224 _mutex.unlock();
switches 0:5c4d7b2438d3 225 return size;
switches 0:5c4d7b2438d3 226 #endif
switches 0:5c4d7b2438d3 227 #else
switches 0:5c4d7b2438d3 228 return 0;
switches 0:5c4d7b2438d3 229 #endif
switches 0:5c4d7b2438d3 230 }
switches 0:5c4d7b2438d3 231
switches 0:5c4d7b2438d3 232 uint32_t Thread::free_stack() {
switches 0:5c4d7b2438d3 233 #ifndef __MBED_CMSIS_RTOS_CA9
switches 0:5c4d7b2438d3 234 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 235 uint32_t size = 0;
switches 0:5c4d7b2438d3 236 _mutex.lock();
switches 0:5c4d7b2438d3 237
switches 0:5c4d7b2438d3 238 if (_tid != NULL) {
switches 0:5c4d7b2438d3 239 uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
switches 0:5c4d7b2438d3 240 size = _thread_def.tcb.tsk_stack - bottom;
switches 0:5c4d7b2438d3 241 }
switches 0:5c4d7b2438d3 242
switches 0:5c4d7b2438d3 243 _mutex.unlock();
switches 0:5c4d7b2438d3 244 return size;
switches 0:5c4d7b2438d3 245 #else
switches 0:5c4d7b2438d3 246 uint32_t size = 0;
switches 0:5c4d7b2438d3 247 _mutex.lock();
switches 0:5c4d7b2438d3 248
switches 0:5c4d7b2438d3 249 if (_tid != NULL) {
switches 0:5c4d7b2438d3 250 P_TCB tcb = rt_tid2ptcb(_tid);
switches 0:5c4d7b2438d3 251 uint32_t bottom = (uint32_t)tcb->stack;
switches 0:5c4d7b2438d3 252 size = tcb->tsk_stack - bottom;
switches 0:5c4d7b2438d3 253 }
switches 0:5c4d7b2438d3 254
switches 0:5c4d7b2438d3 255 _mutex.unlock();
switches 0:5c4d7b2438d3 256 return size;
switches 0:5c4d7b2438d3 257 #endif
switches 0:5c4d7b2438d3 258 #else
switches 0:5c4d7b2438d3 259 return 0;
switches 0:5c4d7b2438d3 260 #endif
switches 0:5c4d7b2438d3 261 }
switches 0:5c4d7b2438d3 262
switches 0:5c4d7b2438d3 263 uint32_t Thread::used_stack() {
switches 0:5c4d7b2438d3 264 #ifndef __MBED_CMSIS_RTOS_CA9
switches 0:5c4d7b2438d3 265 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 266 uint32_t size = 0;
switches 0:5c4d7b2438d3 267 _mutex.lock();
switches 0:5c4d7b2438d3 268
switches 0:5c4d7b2438d3 269 if (_tid != NULL) {
switches 0:5c4d7b2438d3 270 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
switches 0:5c4d7b2438d3 271 size = top - _thread_def.tcb.tsk_stack;
switches 0:5c4d7b2438d3 272 }
switches 0:5c4d7b2438d3 273
switches 0:5c4d7b2438d3 274 _mutex.unlock();
switches 0:5c4d7b2438d3 275 return size;
switches 0:5c4d7b2438d3 276 #else
switches 0:5c4d7b2438d3 277 uint32_t size = 0;
switches 0:5c4d7b2438d3 278 _mutex.lock();
switches 0:5c4d7b2438d3 279
switches 0:5c4d7b2438d3 280 if (_tid != NULL) {
switches 0:5c4d7b2438d3 281 P_TCB tcb = rt_tid2ptcb(_tid);
switches 0:5c4d7b2438d3 282 uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
switches 0:5c4d7b2438d3 283 size = top - tcb->tsk_stack;
switches 0:5c4d7b2438d3 284 }
switches 0:5c4d7b2438d3 285
switches 0:5c4d7b2438d3 286 _mutex.unlock();
switches 0:5c4d7b2438d3 287 return size;
switches 0:5c4d7b2438d3 288 #endif
switches 0:5c4d7b2438d3 289 #else
switches 0:5c4d7b2438d3 290 return 0;
switches 0:5c4d7b2438d3 291 #endif
switches 0:5c4d7b2438d3 292 }
switches 0:5c4d7b2438d3 293
switches 0:5c4d7b2438d3 294 uint32_t Thread::max_stack() {
switches 0:5c4d7b2438d3 295 #ifndef __MBED_CMSIS_RTOS_CA9
switches 0:5c4d7b2438d3 296 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
switches 0:5c4d7b2438d3 297 uint32_t size = 0;
switches 0:5c4d7b2438d3 298 _mutex.lock();
switches 0:5c4d7b2438d3 299
switches 0:5c4d7b2438d3 300 if (_tid != NULL) {
switches 0:5c4d7b2438d3 301 uint32_t high_mark = 0;
switches 0:5c4d7b2438d3 302 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
switches 0:5c4d7b2438d3 303 high_mark++;
switches 0:5c4d7b2438d3 304 size = _thread_def.tcb.priv_stack - (high_mark * 4);
switches 0:5c4d7b2438d3 305 }
switches 0:5c4d7b2438d3 306
switches 0:5c4d7b2438d3 307 _mutex.unlock();
switches 0:5c4d7b2438d3 308 return size;
switches 0:5c4d7b2438d3 309 #else
switches 0:5c4d7b2438d3 310 uint32_t size = 0;
switches 0:5c4d7b2438d3 311 _mutex.lock();
switches 0:5c4d7b2438d3 312
switches 0:5c4d7b2438d3 313 if (_tid != NULL) {
switches 0:5c4d7b2438d3 314 P_TCB tcb = rt_tid2ptcb(_tid);
switches 0:5c4d7b2438d3 315 uint32_t high_mark = 0;
switches 0:5c4d7b2438d3 316 while (tcb->stack[high_mark] == 0xE25A2EA5)
switches 0:5c4d7b2438d3 317 high_mark++;
switches 0:5c4d7b2438d3 318 size = tcb->priv_stack - (high_mark * 4);
switches 0:5c4d7b2438d3 319 }
switches 0:5c4d7b2438d3 320
switches 0:5c4d7b2438d3 321 _mutex.unlock();
switches 0:5c4d7b2438d3 322 return size;
switches 0:5c4d7b2438d3 323 #endif
switches 0:5c4d7b2438d3 324 #else
switches 0:5c4d7b2438d3 325 return 0;
switches 0:5c4d7b2438d3 326 #endif
switches 0:5c4d7b2438d3 327 }
switches 0:5c4d7b2438d3 328
switches 0:5c4d7b2438d3 329 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
switches 0:5c4d7b2438d3 330 return osSignalWait(signals, millisec);
switches 0:5c4d7b2438d3 331 }
switches 0:5c4d7b2438d3 332
switches 0:5c4d7b2438d3 333 osStatus Thread::wait(uint32_t millisec) {
switches 0:5c4d7b2438d3 334 return osDelay(millisec);
switches 0:5c4d7b2438d3 335 }
switches 0:5c4d7b2438d3 336
switches 0:5c4d7b2438d3 337 osStatus Thread::yield() {
switches 0:5c4d7b2438d3 338 return osThreadYield();
switches 0:5c4d7b2438d3 339 }
switches 0:5c4d7b2438d3 340
switches 0:5c4d7b2438d3 341 osThreadId Thread::gettid() {
switches 0:5c4d7b2438d3 342 return osThreadGetId();
switches 0:5c4d7b2438d3 343 }
switches 0:5c4d7b2438d3 344
switches 0:5c4d7b2438d3 345 void Thread::attach_idle_hook(void (*fptr)(void)) {
switches 0:5c4d7b2438d3 346 rtos_attach_idle_hook(fptr);
switches 0:5c4d7b2438d3 347 }
switches 0:5c4d7b2438d3 348
switches 0:5c4d7b2438d3 349 void Thread::attach_terminate_hook(void (*fptr)(osThreadId id)) {
switches 0:5c4d7b2438d3 350 terminate_hook = fptr;
switches 0:5c4d7b2438d3 351 }
switches 0:5c4d7b2438d3 352
switches 0:5c4d7b2438d3 353 Thread::~Thread() {
switches 0:5c4d7b2438d3 354 // terminate is thread safe
switches 0:5c4d7b2438d3 355 terminate();
switches 0:5c4d7b2438d3 356 #ifdef __MBED_CMSIS_RTOS_CM
switches 0:5c4d7b2438d3 357 if (_dynamic_stack) {
switches 0:5c4d7b2438d3 358 delete[] (_thread_def.stack_pointer);
switches 0:5c4d7b2438d3 359 _thread_def.stack_pointer = (uint32_t*)NULL;
switches 0:5c4d7b2438d3 360 }
switches 0:5c4d7b2438d3 361 #endif
switches 0:5c4d7b2438d3 362 }
switches 0:5c4d7b2438d3 363
switches 0:5c4d7b2438d3 364 void Thread::_thunk(const void * thread_ptr)
switches 0:5c4d7b2438d3 365 {
switches 0:5c4d7b2438d3 366 Thread *t = (Thread*)thread_ptr;
switches 0:5c4d7b2438d3 367 t->_task();
switches 0:5c4d7b2438d3 368 t->_mutex.lock();
switches 0:5c4d7b2438d3 369 t->_tid = (osThreadId)NULL;
switches 0:5c4d7b2438d3 370 t->_join_sem.release();
switches 0:5c4d7b2438d3 371 // rtos will release the mutex automatically
switches 0:5c4d7b2438d3 372 }
switches 0:5c4d7b2438d3 373
switches 0:5c4d7b2438d3 374 }