asd
Fork of mbed-rtos by
rtos/Thread.cpp@107:bdd541595fc5, 2016-03-15 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Mar 15 09:00:39 2016 +0000
- Revision:
- 107:bdd541595fc5
- Parent:
- 90:21b438192b0f
- Child:
- 112:53ace74b190c
Synchronized with git revision 3afb55095212c88fc1c17177348bc1e16259c1ca
Full URL: https://github.com/mbedmicro/mbed/commit/3afb55095212c88fc1c17177348bc1e16259c1ca/
[STM32F4] Add DEVICE_RTC_LSI=0 to all targets
Who changed what in which revision?
User | Revision | Line number | New 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 | |
emilmont | 8:88a1a9c26ae3 | 27 | namespace rtos { |
emilmont | 8:88a1a9c26ae3 | 28 | |
emilmont | 8:88a1a9c26ae3 | 29 | Thread::Thread(void (*task)(void const *argument), void *argument, |
emilmont | 8:88a1a9c26ae3 | 30 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
emilmont | 8:88a1a9c26ae3 | 31 | #ifdef CMSIS_OS_RTX |
emilmont | 8:88a1a9c26ae3 | 32 | _thread_def.pthread = task; |
emilmont | 8:88a1a9c26ae3 | 33 | _thread_def.tpriority = priority; |
emilmont | 8:88a1a9c26ae3 | 34 | _thread_def.stacksize = stack_size; |
emilmont | 8:88a1a9c26ae3 | 35 | if (stack_pointer != NULL) { |
mbed_official | 59:28712e303960 | 36 | _thread_def.stack_pointer = (uint32_t*)stack_pointer; |
emilmont | 8:88a1a9c26ae3 | 37 | _dynamic_stack = false; |
emilmont | 8:88a1a9c26ae3 | 38 | } else { |
mbed_official | 59:28712e303960 | 39 | _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)]; |
emilmont | 8:88a1a9c26ae3 | 40 | if (_thread_def.stack_pointer == NULL) |
mbed_official | 25:420a92812cd1 | 41 | error("Error allocating the stack memory\n"); |
emilmont | 8:88a1a9c26ae3 | 42 | _dynamic_stack = true; |
emilmont | 8:88a1a9c26ae3 | 43 | } |
mbed_official | 84:143955ffb790 | 44 | |
mbed_official | 84:143955ffb790 | 45 | //Fill the stack with a magic word for maximum usage checking |
mbed_official | 90:21b438192b0f | 46 | for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) { |
mbed_official | 84:143955ffb790 | 47 | _thread_def.stack_pointer[i] = 0xE25A2EA5; |
mbed_official | 84:143955ffb790 | 48 | } |
emilmont | 8:88a1a9c26ae3 | 49 | #endif |
emilmont | 8:88a1a9c26ae3 | 50 | _tid = osThreadCreate(&_thread_def, argument); |
emilmont | 8:88a1a9c26ae3 | 51 | } |
emilmont | 8:88a1a9c26ae3 | 52 | |
emilmont | 8:88a1a9c26ae3 | 53 | osStatus Thread::terminate() { |
emilmont | 8:88a1a9c26ae3 | 54 | return osThreadTerminate(_tid); |
emilmont | 8:88a1a9c26ae3 | 55 | } |
emilmont | 8:88a1a9c26ae3 | 56 | |
emilmont | 8:88a1a9c26ae3 | 57 | osStatus Thread::set_priority(osPriority priority) { |
emilmont | 8:88a1a9c26ae3 | 58 | return osThreadSetPriority(_tid, priority); |
emilmont | 8:88a1a9c26ae3 | 59 | } |
emilmont | 8:88a1a9c26ae3 | 60 | |
emilmont | 8:88a1a9c26ae3 | 61 | osPriority Thread::get_priority() { |
emilmont | 8:88a1a9c26ae3 | 62 | return osThreadGetPriority(_tid); |
emilmont | 8:88a1a9c26ae3 | 63 | } |
emilmont | 8:88a1a9c26ae3 | 64 | |
emilmont | 8:88a1a9c26ae3 | 65 | int32_t Thread::signal_set(int32_t signals) { |
emilmont | 8:88a1a9c26ae3 | 66 | return osSignalSet(_tid, signals); |
emilmont | 8:88a1a9c26ae3 | 67 | } |
emilmont | 8:88a1a9c26ae3 | 68 | |
mbed_official | 76:85a52b7ef44b | 69 | int32_t Thread::signal_clr(int32_t signals) { |
mbed_official | 76:85a52b7ef44b | 70 | return osSignalClear(_tid, signals); |
mbed_official | 76:85a52b7ef44b | 71 | } |
mbed_official | 76:85a52b7ef44b | 72 | |
emilmont | 8:88a1a9c26ae3 | 73 | Thread::State Thread::get_state() { |
mbed_official | 48:e9a2c7cb57a4 | 74 | #ifndef __MBED_CMSIS_RTOS_CA9 |
emilmont | 8:88a1a9c26ae3 | 75 | return ((State)_thread_def.tcb.state); |
mbed_official | 48:e9a2c7cb57a4 | 76 | #else |
mbed_official | 48:e9a2c7cb57a4 | 77 | uint8_t status; |
mbed_official | 48:e9a2c7cb57a4 | 78 | status = osThreadGetState(_tid); |
mbed_official | 48:e9a2c7cb57a4 | 79 | return ((State)status); |
mbed_official | 48:e9a2c7cb57a4 | 80 | #endif |
emilmont | 8:88a1a9c26ae3 | 81 | } |
emilmont | 8:88a1a9c26ae3 | 82 | |
mbed_official | 84:143955ffb790 | 83 | uint32_t Thread::stack_size() { |
mbed_official | 84:143955ffb790 | 84 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 84:143955ffb790 | 85 | return _thread_def.tcb.priv_stack; |
mbed_official | 84:143955ffb790 | 86 | #else |
mbed_official | 84:143955ffb790 | 87 | return 0; |
mbed_official | 84:143955ffb790 | 88 | #endif |
mbed_official | 84:143955ffb790 | 89 | } |
mbed_official | 84:143955ffb790 | 90 | |
mbed_official | 84:143955ffb790 | 91 | uint32_t Thread::free_stack() { |
mbed_official | 84:143955ffb790 | 92 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 84:143955ffb790 | 93 | uint32_t bottom = (uint32_t)_thread_def.tcb.stack; |
mbed_official | 84:143955ffb790 | 94 | return _thread_def.tcb.tsk_stack - bottom; |
mbed_official | 84:143955ffb790 | 95 | #else |
mbed_official | 84:143955ffb790 | 96 | return 0; |
mbed_official | 84:143955ffb790 | 97 | #endif |
mbed_official | 84:143955ffb790 | 98 | } |
mbed_official | 84:143955ffb790 | 99 | |
mbed_official | 84:143955ffb790 | 100 | uint32_t Thread::used_stack() { |
mbed_official | 84:143955ffb790 | 101 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 84:143955ffb790 | 102 | uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; |
mbed_official | 84:143955ffb790 | 103 | return top - _thread_def.tcb.tsk_stack; |
mbed_official | 84:143955ffb790 | 104 | #else |
mbed_official | 84:143955ffb790 | 105 | return 0; |
mbed_official | 84:143955ffb790 | 106 | #endif |
mbed_official | 84:143955ffb790 | 107 | } |
mbed_official | 84:143955ffb790 | 108 | |
mbed_official | 84:143955ffb790 | 109 | uint32_t Thread::max_stack() { |
mbed_official | 84:143955ffb790 | 110 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbed_official | 84:143955ffb790 | 111 | uint32_t high_mark = 0; |
mbed_official | 84:143955ffb790 | 112 | while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) |
mbed_official | 84:143955ffb790 | 113 | high_mark++; |
mbed_official | 84:143955ffb790 | 114 | return _thread_def.tcb.priv_stack - (high_mark * 4); |
mbed_official | 84:143955ffb790 | 115 | #else |
mbed_official | 84:143955ffb790 | 116 | return 0; |
mbed_official | 84:143955ffb790 | 117 | #endif |
mbed_official | 84:143955ffb790 | 118 | } |
mbed_official | 84:143955ffb790 | 119 | |
emilmont | 8:88a1a9c26ae3 | 120 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
emilmont | 8:88a1a9c26ae3 | 121 | return osSignalWait(signals, millisec); |
emilmont | 8:88a1a9c26ae3 | 122 | } |
emilmont | 8:88a1a9c26ae3 | 123 | |
emilmont | 8:88a1a9c26ae3 | 124 | osStatus Thread::wait(uint32_t millisec) { |
emilmont | 8:88a1a9c26ae3 | 125 | return osDelay(millisec); |
emilmont | 8:88a1a9c26ae3 | 126 | } |
emilmont | 8:88a1a9c26ae3 | 127 | |
emilmont | 8:88a1a9c26ae3 | 128 | osStatus Thread::yield() { |
emilmont | 8:88a1a9c26ae3 | 129 | return osThreadYield(); |
emilmont | 8:88a1a9c26ae3 | 130 | } |
emilmont | 8:88a1a9c26ae3 | 131 | |
emilmont | 8:88a1a9c26ae3 | 132 | osThreadId Thread::gettid() { |
emilmont | 8:88a1a9c26ae3 | 133 | return osThreadGetId(); |
emilmont | 8:88a1a9c26ae3 | 134 | } |
emilmont | 8:88a1a9c26ae3 | 135 | |
mbed_official | 107:bdd541595fc5 | 136 | void Thread::attach_idle_hook(void (*fptr)(void)) { |
mbed_official | 107:bdd541595fc5 | 137 | rtos_attach_idle_hook(fptr); |
mbed_official | 107:bdd541595fc5 | 138 | } |
mbed_official | 107:bdd541595fc5 | 139 | |
emilmont | 8:88a1a9c26ae3 | 140 | Thread::~Thread() { |
emilmont | 8:88a1a9c26ae3 | 141 | terminate(); |
emilmont | 8:88a1a9c26ae3 | 142 | if (_dynamic_stack) { |
emilmont | 8:88a1a9c26ae3 | 143 | delete[] (_thread_def.stack_pointer); |
emilmont | 8:88a1a9c26ae3 | 144 | } |
emilmont | 8:88a1a9c26ae3 | 145 | } |
emilmont | 8:88a1a9c26ae3 | 146 | |
emilmont | 8:88a1a9c26ae3 | 147 | } |