Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Thu Oct 11 02:27:46 2018 +0000
Revision:
3:f3764f852aa8
Parent:
0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 /* mbed Microcontroller Library
kadonotakashi 0:8fdf9a60065b 2 * Copyright (c) 2006-2012 ARM Limited
kadonotakashi 0:8fdf9a60065b 3 *
kadonotakashi 0:8fdf9a60065b 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
kadonotakashi 0:8fdf9a60065b 5 * of this software and associated documentation files (the "Software"), to deal
kadonotakashi 0:8fdf9a60065b 6 * in the Software without restriction, including without limitation the rights
kadonotakashi 0:8fdf9a60065b 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kadonotakashi 0:8fdf9a60065b 8 * copies of the Software, and to permit persons to whom the Software is
kadonotakashi 0:8fdf9a60065b 9 * furnished to do so, subject to the following conditions:
kadonotakashi 0:8fdf9a60065b 10 *
kadonotakashi 0:8fdf9a60065b 11 * The above copyright notice and this permission notice shall be included in
kadonotakashi 0:8fdf9a60065b 12 * all copies or substantial portions of the Software.
kadonotakashi 0:8fdf9a60065b 13 *
kadonotakashi 0:8fdf9a60065b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kadonotakashi 0:8fdf9a60065b 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kadonotakashi 0:8fdf9a60065b 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kadonotakashi 0:8fdf9a60065b 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kadonotakashi 0:8fdf9a60065b 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kadonotakashi 0:8fdf9a60065b 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
kadonotakashi 0:8fdf9a60065b 20 * SOFTWARE.
kadonotakashi 0:8fdf9a60065b 21 */
kadonotakashi 0:8fdf9a60065b 22 #include "rtos/Thread.h"
kadonotakashi 0:8fdf9a60065b 23 #include "rtos/ThisThread.h"
kadonotakashi 0:8fdf9a60065b 24
kadonotakashi 0:8fdf9a60065b 25 #include "mbed.h"
kadonotakashi 0:8fdf9a60065b 26 #include "rtos/rtos_idle.h"
kadonotakashi 0:8fdf9a60065b 27 #include "rtos/rtos_handlers.h"
kadonotakashi 0:8fdf9a60065b 28 #include "mbed_assert.h"
kadonotakashi 0:8fdf9a60065b 29
kadonotakashi 0:8fdf9a60065b 30 #define ALIGN_UP(pos, align) ((pos) % (align) ? (pos) + ((align) - (pos) % (align)) : (pos))
kadonotakashi 0:8fdf9a60065b 31 MBED_STATIC_ASSERT(ALIGN_UP(0, 8) == 0, "ALIGN_UP macro error");
kadonotakashi 0:8fdf9a60065b 32 MBED_STATIC_ASSERT(ALIGN_UP(1, 8) == 8, "ALIGN_UP macro error");
kadonotakashi 0:8fdf9a60065b 33
kadonotakashi 0:8fdf9a60065b 34 #define ALIGN_DOWN(pos, align) ((pos) - ((pos) % (align)))
kadonotakashi 0:8fdf9a60065b 35 MBED_STATIC_ASSERT(ALIGN_DOWN(7, 8) == 0, "ALIGN_DOWN macro error");
kadonotakashi 0:8fdf9a60065b 36 MBED_STATIC_ASSERT(ALIGN_DOWN(8, 8) == 8, "ALIGN_DOWN macro error");
kadonotakashi 0:8fdf9a60065b 37
kadonotakashi 0:8fdf9a60065b 38 namespace rtos {
kadonotakashi 0:8fdf9a60065b 39
kadonotakashi 0:8fdf9a60065b 40 #ifndef MBED_TZ_DEFAULT_ACCESS
kadonotakashi 0:8fdf9a60065b 41 #define MBED_TZ_DEFAULT_ACCESS 0
kadonotakashi 0:8fdf9a60065b 42 #endif
kadonotakashi 0:8fdf9a60065b 43
kadonotakashi 0:8fdf9a60065b 44 void Thread::constructor(uint32_t tz_module, osPriority priority,
kadonotakashi 0:8fdf9a60065b 45 uint32_t stack_size, unsigned char *stack_mem, const char *name)
kadonotakashi 0:8fdf9a60065b 46 {
kadonotakashi 0:8fdf9a60065b 47
kadonotakashi 0:8fdf9a60065b 48 const uintptr_t unaligned_mem = reinterpret_cast<uintptr_t>(stack_mem);
kadonotakashi 0:8fdf9a60065b 49 const uintptr_t aligned_mem = ALIGN_UP(unaligned_mem, 8);
kadonotakashi 0:8fdf9a60065b 50 const uint32_t offset = aligned_mem - unaligned_mem;
kadonotakashi 0:8fdf9a60065b 51 const uint32_t aligned_size = ALIGN_DOWN(stack_size - offset, 8);
kadonotakashi 0:8fdf9a60065b 52
kadonotakashi 0:8fdf9a60065b 53 _tid = 0;
kadonotakashi 0:8fdf9a60065b 54 _dynamic_stack = (stack_mem == NULL);
kadonotakashi 0:8fdf9a60065b 55 _finished = false;
kadonotakashi 0:8fdf9a60065b 56 memset(&_obj_mem, 0, sizeof(_obj_mem));
kadonotakashi 0:8fdf9a60065b 57 memset(&_attr, 0, sizeof(_attr));
kadonotakashi 0:8fdf9a60065b 58 _attr.priority = priority;
kadonotakashi 0:8fdf9a60065b 59 _attr.stack_size = aligned_size;
kadonotakashi 0:8fdf9a60065b 60 _attr.name = name ? name : "application_unnamed_thread";
kadonotakashi 0:8fdf9a60065b 61 _attr.stack_mem = reinterpret_cast<uint32_t *>(aligned_mem);
kadonotakashi 0:8fdf9a60065b 62 _attr.tz_module = tz_module;
kadonotakashi 0:8fdf9a60065b 63 }
kadonotakashi 0:8fdf9a60065b 64
kadonotakashi 0:8fdf9a60065b 65 void Thread::constructor(osPriority priority,
kadonotakashi 0:8fdf9a60065b 66 uint32_t stack_size, unsigned char *stack_mem, const char *name)
kadonotakashi 0:8fdf9a60065b 67 {
kadonotakashi 0:8fdf9a60065b 68 constructor(MBED_TZ_DEFAULT_ACCESS, priority, stack_size, stack_mem, name);
kadonotakashi 0:8fdf9a60065b 69 }
kadonotakashi 0:8fdf9a60065b 70
kadonotakashi 0:8fdf9a60065b 71 void Thread::constructor(Callback<void()> task,
kadonotakashi 0:8fdf9a60065b 72 osPriority priority, uint32_t stack_size, unsigned char *stack_mem, const char *name)
kadonotakashi 0:8fdf9a60065b 73 {
kadonotakashi 0:8fdf9a60065b 74 constructor(MBED_TZ_DEFAULT_ACCESS, priority, stack_size, stack_mem, name);
kadonotakashi 0:8fdf9a60065b 75
kadonotakashi 0:8fdf9a60065b 76 switch (start(task)) {
kadonotakashi 0:8fdf9a60065b 77 case osErrorResource:
kadonotakashi 0:8fdf9a60065b 78 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_RESOURCES), "OS ran out of threads!\n", task);
kadonotakashi 0:8fdf9a60065b 79 break;
kadonotakashi 0:8fdf9a60065b 80 case osErrorParameter:
kadonotakashi 0:8fdf9a60065b 81 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_ALREADY_IN_USE), "Thread already running!\n", task);
kadonotakashi 0:8fdf9a60065b 82 break;
kadonotakashi 0:8fdf9a60065b 83 case osErrorNoMemory:
kadonotakashi 0:8fdf9a60065b 84 MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_MEMORY), "Error allocating the stack memory\n", task);
kadonotakashi 0:8fdf9a60065b 85 default:
kadonotakashi 0:8fdf9a60065b 86 break;
kadonotakashi 0:8fdf9a60065b 87 }
kadonotakashi 0:8fdf9a60065b 88 }
kadonotakashi 0:8fdf9a60065b 89
kadonotakashi 0:8fdf9a60065b 90 osStatus Thread::start(Callback<void()> task)
kadonotakashi 0:8fdf9a60065b 91 {
kadonotakashi 0:8fdf9a60065b 92 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 93
kadonotakashi 0:8fdf9a60065b 94 if ((_tid != 0) || _finished) {
kadonotakashi 0:8fdf9a60065b 95 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 96 return osErrorParameter;
kadonotakashi 0:8fdf9a60065b 97 }
kadonotakashi 0:8fdf9a60065b 98
kadonotakashi 0:8fdf9a60065b 99 if (_attr.stack_mem == NULL) {
kadonotakashi 0:8fdf9a60065b 100 _attr.stack_mem = new uint32_t[_attr.stack_size / sizeof(uint32_t)];
kadonotakashi 0:8fdf9a60065b 101 MBED_ASSERT(_attr.stack_mem != NULL);
kadonotakashi 0:8fdf9a60065b 102 }
kadonotakashi 0:8fdf9a60065b 103
kadonotakashi 0:8fdf9a60065b 104 //Fill the stack with a magic word for maximum usage checking
kadonotakashi 0:8fdf9a60065b 105 for (uint32_t i = 0; i < (_attr.stack_size / sizeof(uint32_t)); i++) {
kadonotakashi 0:8fdf9a60065b 106 ((uint32_t *)_attr.stack_mem)[i] = osRtxStackMagicWord;
kadonotakashi 0:8fdf9a60065b 107 }
kadonotakashi 0:8fdf9a60065b 108
kadonotakashi 0:8fdf9a60065b 109 memset(&_obj_mem, 0, sizeof(_obj_mem));
kadonotakashi 0:8fdf9a60065b 110 _attr.cb_size = sizeof(_obj_mem);
kadonotakashi 0:8fdf9a60065b 111 _attr.cb_mem = &_obj_mem;
kadonotakashi 0:8fdf9a60065b 112 _task = task;
kadonotakashi 0:8fdf9a60065b 113 _tid = osThreadNew(Thread::_thunk, this, &_attr);
kadonotakashi 0:8fdf9a60065b 114 if (_tid == NULL) {
kadonotakashi 0:8fdf9a60065b 115 if (_dynamic_stack) {
kadonotakashi 0:8fdf9a60065b 116 delete[](uint32_t *)(_attr.stack_mem);
kadonotakashi 0:8fdf9a60065b 117 _attr.stack_mem = (uint32_t *)NULL;
kadonotakashi 0:8fdf9a60065b 118 }
kadonotakashi 0:8fdf9a60065b 119 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 120 _join_sem.release();
kadonotakashi 0:8fdf9a60065b 121 return osErrorResource;
kadonotakashi 0:8fdf9a60065b 122 }
kadonotakashi 0:8fdf9a60065b 123
kadonotakashi 0:8fdf9a60065b 124 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 125 return osOK;
kadonotakashi 0:8fdf9a60065b 126 }
kadonotakashi 0:8fdf9a60065b 127
kadonotakashi 0:8fdf9a60065b 128 osStatus Thread::terminate()
kadonotakashi 0:8fdf9a60065b 129 {
kadonotakashi 0:8fdf9a60065b 130 osStatus_t ret = osOK;
kadonotakashi 0:8fdf9a60065b 131 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 132
kadonotakashi 0:8fdf9a60065b 133 // Set the Thread's tid to NULL and
kadonotakashi 0:8fdf9a60065b 134 // release the semaphore before terminating
kadonotakashi 0:8fdf9a60065b 135 // since this thread could be terminating itself
kadonotakashi 0:8fdf9a60065b 136 osThreadId_t local_id = _tid;
kadonotakashi 0:8fdf9a60065b 137 _join_sem.release();
kadonotakashi 0:8fdf9a60065b 138 _tid = (osThreadId_t)NULL;
kadonotakashi 0:8fdf9a60065b 139 if (!_finished) {
kadonotakashi 0:8fdf9a60065b 140 _finished = true;
kadonotakashi 0:8fdf9a60065b 141 // if local_id == 0 Thread was not started in first place
kadonotakashi 0:8fdf9a60065b 142 // and does not have to be terminated
kadonotakashi 0:8fdf9a60065b 143 if (local_id != 0) {
kadonotakashi 0:8fdf9a60065b 144 ret = osThreadTerminate(local_id);
kadonotakashi 0:8fdf9a60065b 145 }
kadonotakashi 0:8fdf9a60065b 146 }
kadonotakashi 0:8fdf9a60065b 147 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 148 return ret;
kadonotakashi 0:8fdf9a60065b 149 }
kadonotakashi 0:8fdf9a60065b 150
kadonotakashi 0:8fdf9a60065b 151 osStatus Thread::join()
kadonotakashi 0:8fdf9a60065b 152 {
kadonotakashi 0:8fdf9a60065b 153 int32_t ret = _join_sem.wait();
kadonotakashi 0:8fdf9a60065b 154 if (ret < 0) {
kadonotakashi 0:8fdf9a60065b 155 return osError;
kadonotakashi 0:8fdf9a60065b 156 }
kadonotakashi 0:8fdf9a60065b 157
kadonotakashi 0:8fdf9a60065b 158 // The semaphore has been released so this thread is being
kadonotakashi 0:8fdf9a60065b 159 // terminated or has been terminated. Once the mutex has
kadonotakashi 0:8fdf9a60065b 160 // been locked it is ensured that the thread is deleted.
kadonotakashi 0:8fdf9a60065b 161 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 162 MBED_ASSERT(NULL == _tid);
kadonotakashi 0:8fdf9a60065b 163 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 164
kadonotakashi 0:8fdf9a60065b 165 // Release sem so any other threads joining this thread wake up
kadonotakashi 0:8fdf9a60065b 166 _join_sem.release();
kadonotakashi 0:8fdf9a60065b 167 return osOK;
kadonotakashi 0:8fdf9a60065b 168 }
kadonotakashi 0:8fdf9a60065b 169
kadonotakashi 0:8fdf9a60065b 170 osStatus Thread::set_priority(osPriority priority)
kadonotakashi 0:8fdf9a60065b 171 {
kadonotakashi 0:8fdf9a60065b 172 osStatus_t ret;
kadonotakashi 0:8fdf9a60065b 173 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 174
kadonotakashi 0:8fdf9a60065b 175 ret = osThreadSetPriority(_tid, priority);
kadonotakashi 0:8fdf9a60065b 176
kadonotakashi 0:8fdf9a60065b 177 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 178 return ret;
kadonotakashi 0:8fdf9a60065b 179 }
kadonotakashi 0:8fdf9a60065b 180
kadonotakashi 0:8fdf9a60065b 181 osPriority Thread::get_priority() const
kadonotakashi 0:8fdf9a60065b 182 {
kadonotakashi 0:8fdf9a60065b 183 osPriority_t ret;
kadonotakashi 0:8fdf9a60065b 184 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 185
kadonotakashi 0:8fdf9a60065b 186 ret = osThreadGetPriority(_tid);
kadonotakashi 0:8fdf9a60065b 187
kadonotakashi 0:8fdf9a60065b 188 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 189 return ret;
kadonotakashi 0:8fdf9a60065b 190 }
kadonotakashi 0:8fdf9a60065b 191
kadonotakashi 0:8fdf9a60065b 192 uint32_t Thread::flags_set(uint32_t flags)
kadonotakashi 0:8fdf9a60065b 193 {
kadonotakashi 0:8fdf9a60065b 194 flags = osThreadFlagsSet(_tid, flags);
kadonotakashi 0:8fdf9a60065b 195 MBED_ASSERT(!(flags & osFlagsError));
kadonotakashi 0:8fdf9a60065b 196 return flags;
kadonotakashi 0:8fdf9a60065b 197 }
kadonotakashi 0:8fdf9a60065b 198
kadonotakashi 0:8fdf9a60065b 199 int32_t Thread::signal_set(int32_t flags)
kadonotakashi 0:8fdf9a60065b 200 {
kadonotakashi 0:8fdf9a60065b 201 return osThreadFlagsSet(_tid, flags);
kadonotakashi 0:8fdf9a60065b 202 }
kadonotakashi 0:8fdf9a60065b 203
kadonotakashi 0:8fdf9a60065b 204 Thread::State Thread::get_state() const
kadonotakashi 0:8fdf9a60065b 205 {
kadonotakashi 0:8fdf9a60065b 206 uint8_t state = osThreadTerminated;
kadonotakashi 0:8fdf9a60065b 207
kadonotakashi 0:8fdf9a60065b 208 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 209
kadonotakashi 0:8fdf9a60065b 210 if (_tid != NULL) {
kadonotakashi 0:8fdf9a60065b 211 #if defined(MBED_OS_BACKEND_RTX5)
kadonotakashi 0:8fdf9a60065b 212 state = _obj_mem.state;
kadonotakashi 0:8fdf9a60065b 213 #else
kadonotakashi 0:8fdf9a60065b 214 state = osThreadGetState(_tid);
kadonotakashi 0:8fdf9a60065b 215 #endif
kadonotakashi 0:8fdf9a60065b 216 }
kadonotakashi 0:8fdf9a60065b 217
kadonotakashi 0:8fdf9a60065b 218 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 219
kadonotakashi 0:8fdf9a60065b 220 State user_state;
kadonotakashi 0:8fdf9a60065b 221
kadonotakashi 0:8fdf9a60065b 222 switch (state) {
kadonotakashi 0:8fdf9a60065b 223 case osThreadInactive:
kadonotakashi 0:8fdf9a60065b 224 user_state = Inactive;
kadonotakashi 0:8fdf9a60065b 225 break;
kadonotakashi 0:8fdf9a60065b 226 case osThreadReady:
kadonotakashi 0:8fdf9a60065b 227 user_state = Ready;
kadonotakashi 0:8fdf9a60065b 228 break;
kadonotakashi 0:8fdf9a60065b 229 case osThreadRunning:
kadonotakashi 0:8fdf9a60065b 230 user_state = Running;
kadonotakashi 0:8fdf9a60065b 231 break;
kadonotakashi 0:8fdf9a60065b 232 #if defined(MBED_OS_BACKEND_RTX5)
kadonotakashi 0:8fdf9a60065b 233 case osRtxThreadWaitingDelay:
kadonotakashi 0:8fdf9a60065b 234 user_state = WaitingDelay;
kadonotakashi 0:8fdf9a60065b 235 break;
kadonotakashi 0:8fdf9a60065b 236 case osRtxThreadWaitingJoin:
kadonotakashi 0:8fdf9a60065b 237 user_state = WaitingJoin;
kadonotakashi 0:8fdf9a60065b 238 break;
kadonotakashi 0:8fdf9a60065b 239 case osRtxThreadWaitingThreadFlags:
kadonotakashi 0:8fdf9a60065b 240 user_state = WaitingThreadFlag;
kadonotakashi 0:8fdf9a60065b 241 break;
kadonotakashi 0:8fdf9a60065b 242 case osRtxThreadWaitingEventFlags:
kadonotakashi 0:8fdf9a60065b 243 user_state = WaitingEventFlag;
kadonotakashi 0:8fdf9a60065b 244 break;
kadonotakashi 0:8fdf9a60065b 245 case osRtxThreadWaitingMutex:
kadonotakashi 0:8fdf9a60065b 246 user_state = WaitingMutex;
kadonotakashi 0:8fdf9a60065b 247 break;
kadonotakashi 0:8fdf9a60065b 248 case osRtxThreadWaitingSemaphore:
kadonotakashi 0:8fdf9a60065b 249 user_state = WaitingSemaphore;
kadonotakashi 0:8fdf9a60065b 250 break;
kadonotakashi 0:8fdf9a60065b 251 case osRtxThreadWaitingMemoryPool:
kadonotakashi 0:8fdf9a60065b 252 user_state = WaitingMemoryPool;
kadonotakashi 0:8fdf9a60065b 253 break;
kadonotakashi 0:8fdf9a60065b 254 case osRtxThreadWaitingMessageGet:
kadonotakashi 0:8fdf9a60065b 255 user_state = WaitingMessageGet;
kadonotakashi 0:8fdf9a60065b 256 break;
kadonotakashi 0:8fdf9a60065b 257 case osRtxThreadWaitingMessagePut:
kadonotakashi 0:8fdf9a60065b 258 user_state = WaitingMessagePut;
kadonotakashi 0:8fdf9a60065b 259 break;
kadonotakashi 0:8fdf9a60065b 260 #endif
kadonotakashi 0:8fdf9a60065b 261 case osThreadTerminated:
kadonotakashi 0:8fdf9a60065b 262 default:
kadonotakashi 0:8fdf9a60065b 263 user_state = Deleted;
kadonotakashi 0:8fdf9a60065b 264 break;
kadonotakashi 0:8fdf9a60065b 265 }
kadonotakashi 0:8fdf9a60065b 266
kadonotakashi 0:8fdf9a60065b 267 return user_state;
kadonotakashi 0:8fdf9a60065b 268 }
kadonotakashi 0:8fdf9a60065b 269
kadonotakashi 0:8fdf9a60065b 270 uint32_t Thread::stack_size() const
kadonotakashi 0:8fdf9a60065b 271 {
kadonotakashi 0:8fdf9a60065b 272 uint32_t size = 0;
kadonotakashi 0:8fdf9a60065b 273 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 274
kadonotakashi 0:8fdf9a60065b 275 if (_tid != NULL) {
kadonotakashi 0:8fdf9a60065b 276 size = osThreadGetStackSize(_tid);
kadonotakashi 0:8fdf9a60065b 277 }
kadonotakashi 0:8fdf9a60065b 278
kadonotakashi 0:8fdf9a60065b 279 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 280 return size;
kadonotakashi 0:8fdf9a60065b 281 }
kadonotakashi 0:8fdf9a60065b 282
kadonotakashi 0:8fdf9a60065b 283 uint32_t Thread::free_stack() const
kadonotakashi 0:8fdf9a60065b 284 {
kadonotakashi 0:8fdf9a60065b 285 uint32_t size = 0;
kadonotakashi 0:8fdf9a60065b 286 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 287
kadonotakashi 0:8fdf9a60065b 288 #if defined(MBED_OS_BACKEND_RTX5)
kadonotakashi 0:8fdf9a60065b 289 if (_tid != NULL) {
kadonotakashi 0:8fdf9a60065b 290 mbed_rtos_storage_thread_t *thread = (mbed_rtos_storage_thread_t *)_tid;
kadonotakashi 0:8fdf9a60065b 291 size = (uint32_t)thread->sp - (uint32_t)thread->stack_mem;
kadonotakashi 0:8fdf9a60065b 292 }
kadonotakashi 0:8fdf9a60065b 293 #endif
kadonotakashi 0:8fdf9a60065b 294
kadonotakashi 0:8fdf9a60065b 295 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 296 return size;
kadonotakashi 0:8fdf9a60065b 297 }
kadonotakashi 0:8fdf9a60065b 298
kadonotakashi 0:8fdf9a60065b 299 uint32_t Thread::used_stack() const
kadonotakashi 0:8fdf9a60065b 300 {
kadonotakashi 0:8fdf9a60065b 301 uint32_t size = 0;
kadonotakashi 0:8fdf9a60065b 302 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 303
kadonotakashi 0:8fdf9a60065b 304 #if defined(MBED_OS_BACKEND_RTX5)
kadonotakashi 0:8fdf9a60065b 305 if (_tid != NULL) {
kadonotakashi 0:8fdf9a60065b 306 mbed_rtos_storage_thread_t *thread = (mbed_rtos_storage_thread_t *)_tid;
kadonotakashi 0:8fdf9a60065b 307 size = ((uint32_t)thread->stack_mem + thread->stack_size) - thread->sp;
kadonotakashi 0:8fdf9a60065b 308 }
kadonotakashi 0:8fdf9a60065b 309 #endif
kadonotakashi 0:8fdf9a60065b 310
kadonotakashi 0:8fdf9a60065b 311 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 312 return size;
kadonotakashi 0:8fdf9a60065b 313 }
kadonotakashi 0:8fdf9a60065b 314
kadonotakashi 0:8fdf9a60065b 315 uint32_t Thread::max_stack() const
kadonotakashi 0:8fdf9a60065b 316 {
kadonotakashi 0:8fdf9a60065b 317 uint32_t size = 0;
kadonotakashi 0:8fdf9a60065b 318 _mutex.lock();
kadonotakashi 0:8fdf9a60065b 319
kadonotakashi 0:8fdf9a60065b 320 if (_tid != NULL) {
kadonotakashi 0:8fdf9a60065b 321 #if defined(MBED_OS_BACKEND_RTX5)
kadonotakashi 0:8fdf9a60065b 322 mbed_rtos_storage_thread_t *thread = (mbed_rtos_storage_thread_t *)_tid;
kadonotakashi 0:8fdf9a60065b 323 uint32_t high_mark = 0;
kadonotakashi 0:8fdf9a60065b 324 while ((((uint32_t *)(thread->stack_mem))[high_mark] == osRtxStackMagicWord) || (((uint32_t *)(thread->stack_mem))[high_mark] == osRtxStackFillPattern)) {
kadonotakashi 0:8fdf9a60065b 325 high_mark++;
kadonotakashi 0:8fdf9a60065b 326 }
kadonotakashi 0:8fdf9a60065b 327 size = thread->stack_size - (high_mark * sizeof(uint32_t));
kadonotakashi 0:8fdf9a60065b 328 #else
kadonotakashi 0:8fdf9a60065b 329 size = osThreadGetStackSize(_tid) - osThreadGetStackSpace(_tid);
kadonotakashi 0:8fdf9a60065b 330 #endif
kadonotakashi 0:8fdf9a60065b 331 }
kadonotakashi 0:8fdf9a60065b 332
kadonotakashi 0:8fdf9a60065b 333 _mutex.unlock();
kadonotakashi 0:8fdf9a60065b 334 return size;
kadonotakashi 0:8fdf9a60065b 335 }
kadonotakashi 0:8fdf9a60065b 336
kadonotakashi 0:8fdf9a60065b 337 const char *Thread::get_name() const
kadonotakashi 0:8fdf9a60065b 338 {
kadonotakashi 0:8fdf9a60065b 339 return _attr.name;
kadonotakashi 0:8fdf9a60065b 340 }
kadonotakashi 0:8fdf9a60065b 341
kadonotakashi 0:8fdf9a60065b 342 osThreadId_t Thread::get_id() const
kadonotakashi 0:8fdf9a60065b 343 {
kadonotakashi 0:8fdf9a60065b 344 return _tid;
kadonotakashi 0:8fdf9a60065b 345 }
kadonotakashi 0:8fdf9a60065b 346
kadonotakashi 0:8fdf9a60065b 347 int32_t Thread::signal_clr(int32_t flags)
kadonotakashi 0:8fdf9a60065b 348 {
kadonotakashi 0:8fdf9a60065b 349 return osThreadFlagsClear(flags);
kadonotakashi 0:8fdf9a60065b 350 }
kadonotakashi 0:8fdf9a60065b 351
kadonotakashi 0:8fdf9a60065b 352 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec)
kadonotakashi 0:8fdf9a60065b 353 {
kadonotakashi 0:8fdf9a60065b 354 uint32_t res;
kadonotakashi 0:8fdf9a60065b 355 osEvent evt;
kadonotakashi 0:8fdf9a60065b 356 uint32_t options = osFlagsWaitAll;
kadonotakashi 0:8fdf9a60065b 357 if (signals == 0) {
kadonotakashi 0:8fdf9a60065b 358 options = osFlagsWaitAny;
kadonotakashi 0:8fdf9a60065b 359 signals = 0x7FFFFFFF;
kadonotakashi 0:8fdf9a60065b 360 }
kadonotakashi 0:8fdf9a60065b 361 res = osThreadFlagsWait(signals, options, millisec);
kadonotakashi 0:8fdf9a60065b 362 if (res & osFlagsError) {
kadonotakashi 0:8fdf9a60065b 363 switch (res) {
kadonotakashi 0:8fdf9a60065b 364 case osFlagsErrorISR:
kadonotakashi 0:8fdf9a60065b 365 evt.status = osErrorISR;
kadonotakashi 0:8fdf9a60065b 366 break;
kadonotakashi 0:8fdf9a60065b 367 case osFlagsErrorResource:
kadonotakashi 0:8fdf9a60065b 368 evt.status = osOK;
kadonotakashi 0:8fdf9a60065b 369 break;
kadonotakashi 0:8fdf9a60065b 370 case osFlagsErrorTimeout:
kadonotakashi 0:8fdf9a60065b 371 evt.status = (osStatus)osEventTimeout;
kadonotakashi 0:8fdf9a60065b 372 break;
kadonotakashi 0:8fdf9a60065b 373 case osFlagsErrorParameter:
kadonotakashi 0:8fdf9a60065b 374 default:
kadonotakashi 0:8fdf9a60065b 375 evt.status = (osStatus)osErrorValue;
kadonotakashi 0:8fdf9a60065b 376 break;
kadonotakashi 0:8fdf9a60065b 377 }
kadonotakashi 0:8fdf9a60065b 378 } else {
kadonotakashi 0:8fdf9a60065b 379 evt.status = (osStatus)osEventSignal;
kadonotakashi 0:8fdf9a60065b 380 evt.value.signals = res;
kadonotakashi 0:8fdf9a60065b 381 }
kadonotakashi 0:8fdf9a60065b 382
kadonotakashi 0:8fdf9a60065b 383 return evt;
kadonotakashi 0:8fdf9a60065b 384 }
kadonotakashi 0:8fdf9a60065b 385
kadonotakashi 0:8fdf9a60065b 386 osStatus Thread::wait(uint32_t millisec)
kadonotakashi 0:8fdf9a60065b 387 {
kadonotakashi 0:8fdf9a60065b 388 ThisThread::sleep_for(millisec);
kadonotakashi 0:8fdf9a60065b 389 return osOK;
kadonotakashi 0:8fdf9a60065b 390 }
kadonotakashi 0:8fdf9a60065b 391
kadonotakashi 0:8fdf9a60065b 392 osStatus Thread::wait_until(uint64_t millisec)
kadonotakashi 0:8fdf9a60065b 393 {
kadonotakashi 0:8fdf9a60065b 394 ThisThread::sleep_until(millisec);
kadonotakashi 0:8fdf9a60065b 395 return osOK;
kadonotakashi 0:8fdf9a60065b 396 }
kadonotakashi 0:8fdf9a60065b 397
kadonotakashi 0:8fdf9a60065b 398 osStatus Thread::yield()
kadonotakashi 0:8fdf9a60065b 399 {
kadonotakashi 0:8fdf9a60065b 400 return osThreadYield();
kadonotakashi 0:8fdf9a60065b 401 }
kadonotakashi 0:8fdf9a60065b 402
kadonotakashi 0:8fdf9a60065b 403 osThreadId Thread::gettid()
kadonotakashi 0:8fdf9a60065b 404 {
kadonotakashi 0:8fdf9a60065b 405 return osThreadGetId();
kadonotakashi 0:8fdf9a60065b 406 }
kadonotakashi 0:8fdf9a60065b 407
kadonotakashi 0:8fdf9a60065b 408 void Thread::attach_idle_hook(void (*fptr)(void))
kadonotakashi 0:8fdf9a60065b 409 {
kadonotakashi 0:8fdf9a60065b 410 rtos_attach_idle_hook(fptr);
kadonotakashi 0:8fdf9a60065b 411 }
kadonotakashi 0:8fdf9a60065b 412
kadonotakashi 0:8fdf9a60065b 413 void Thread::attach_terminate_hook(void (*fptr)(osThreadId_t id))
kadonotakashi 0:8fdf9a60065b 414 {
kadonotakashi 0:8fdf9a60065b 415 rtos_attach_thread_terminate_hook(fptr);
kadonotakashi 0:8fdf9a60065b 416 }
kadonotakashi 0:8fdf9a60065b 417
kadonotakashi 0:8fdf9a60065b 418 Thread::~Thread()
kadonotakashi 0:8fdf9a60065b 419 {
kadonotakashi 0:8fdf9a60065b 420 // terminate is thread safe
kadonotakashi 0:8fdf9a60065b 421 terminate();
kadonotakashi 0:8fdf9a60065b 422 if (_dynamic_stack) {
kadonotakashi 0:8fdf9a60065b 423 delete[](uint32_t *)(_attr.stack_mem);
kadonotakashi 0:8fdf9a60065b 424 _attr.stack_mem = (uint32_t *)NULL;
kadonotakashi 0:8fdf9a60065b 425 }
kadonotakashi 0:8fdf9a60065b 426 }
kadonotakashi 0:8fdf9a60065b 427
kadonotakashi 0:8fdf9a60065b 428 void Thread::_thunk(void *thread_ptr)
kadonotakashi 0:8fdf9a60065b 429 {
kadonotakashi 0:8fdf9a60065b 430 Thread *t = (Thread *)thread_ptr;
kadonotakashi 0:8fdf9a60065b 431 t->_task();
kadonotakashi 0:8fdf9a60065b 432 t->_mutex.lock();
kadonotakashi 0:8fdf9a60065b 433 t->_tid = (osThreadId)NULL;
kadonotakashi 0:8fdf9a60065b 434 t->_finished = true;
kadonotakashi 0:8fdf9a60065b 435 t->_join_sem.release();
kadonotakashi 0:8fdf9a60065b 436 // rtos will release the mutex automatically
kadonotakashi 0:8fdf9a60065b 437 }
kadonotakashi 0:8fdf9a60065b 438
kadonotakashi 0:8fdf9a60065b 439 }