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