![](/media/cache/profiles/debfdc81854eac26ec993b55a659c6e1.jpg.50x50_q85.jpg)
mbed client lightswitch demo
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-rtos/rtos/Thread.cpp@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* mbed Microcontroller Library |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbedAustin | 11:cada08fc8a70 | 3 | * |
mbedAustin | 11:cada08fc8a70 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbedAustin | 11:cada08fc8a70 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbedAustin | 11:cada08fc8a70 | 6 | * in the Software without restriction, including without limitation the rights |
mbedAustin | 11:cada08fc8a70 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbedAustin | 11:cada08fc8a70 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbedAustin | 11:cada08fc8a70 | 9 | * furnished to do so, subject to the following conditions: |
mbedAustin | 11:cada08fc8a70 | 10 | * |
mbedAustin | 11:cada08fc8a70 | 11 | * The above copyright notice and this permission notice shall be included in |
mbedAustin | 11:cada08fc8a70 | 12 | * all copies or substantial portions of the Software. |
mbedAustin | 11:cada08fc8a70 | 13 | * |
mbedAustin | 11:cada08fc8a70 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbedAustin | 11:cada08fc8a70 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbedAustin | 11:cada08fc8a70 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbedAustin | 11:cada08fc8a70 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbedAustin | 11:cada08fc8a70 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbedAustin | 11:cada08fc8a70 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbedAustin | 11:cada08fc8a70 | 20 | * SOFTWARE. |
mbedAustin | 11:cada08fc8a70 | 21 | */ |
mbedAustin | 11:cada08fc8a70 | 22 | #include "Thread.h" |
mbedAustin | 11:cada08fc8a70 | 23 | |
mbedAustin | 11:cada08fc8a70 | 24 | #include "mbed_error.h" |
mbedAustin | 11:cada08fc8a70 | 25 | |
mbedAustin | 11:cada08fc8a70 | 26 | namespace rtos { |
mbedAustin | 11:cada08fc8a70 | 27 | |
mbedAustin | 11:cada08fc8a70 | 28 | Thread::Thread(void (*task)(void const *argument), void *argument, |
mbedAustin | 11:cada08fc8a70 | 29 | osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { |
mbedAustin | 11:cada08fc8a70 | 30 | #ifdef CMSIS_OS_RTX |
mbedAustin | 11:cada08fc8a70 | 31 | _thread_def.pthread = task; |
mbedAustin | 11:cada08fc8a70 | 32 | _thread_def.tpriority = priority; |
mbedAustin | 11:cada08fc8a70 | 33 | _thread_def.stacksize = stack_size; |
mbedAustin | 11:cada08fc8a70 | 34 | if (stack_pointer != NULL) { |
mbedAustin | 11:cada08fc8a70 | 35 | _thread_def.stack_pointer = (uint32_t*)stack_pointer; |
mbedAustin | 11:cada08fc8a70 | 36 | _dynamic_stack = false; |
mbedAustin | 11:cada08fc8a70 | 37 | } else { |
mbedAustin | 11:cada08fc8a70 | 38 | _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)]; |
mbedAustin | 11:cada08fc8a70 | 39 | if (_thread_def.stack_pointer == NULL) |
mbedAustin | 11:cada08fc8a70 | 40 | error("Error allocating the stack memory\n"); |
mbedAustin | 11:cada08fc8a70 | 41 | _dynamic_stack = true; |
mbedAustin | 11:cada08fc8a70 | 42 | } |
mbedAustin | 11:cada08fc8a70 | 43 | |
mbedAustin | 11:cada08fc8a70 | 44 | //Fill the stack with a magic word for maximum usage checking |
mbedAustin | 11:cada08fc8a70 | 45 | for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) { |
mbedAustin | 11:cada08fc8a70 | 46 | _thread_def.stack_pointer[i] = 0xE25A2EA5; |
mbedAustin | 11:cada08fc8a70 | 47 | } |
mbedAustin | 11:cada08fc8a70 | 48 | #endif |
mbedAustin | 11:cada08fc8a70 | 49 | _tid = osThreadCreate(&_thread_def, argument); |
mbedAustin | 11:cada08fc8a70 | 50 | } |
mbedAustin | 11:cada08fc8a70 | 51 | |
mbedAustin | 11:cada08fc8a70 | 52 | osStatus Thread::terminate() { |
mbedAustin | 11:cada08fc8a70 | 53 | return osThreadTerminate(_tid); |
mbedAustin | 11:cada08fc8a70 | 54 | } |
mbedAustin | 11:cada08fc8a70 | 55 | |
mbedAustin | 11:cada08fc8a70 | 56 | osStatus Thread::set_priority(osPriority priority) { |
mbedAustin | 11:cada08fc8a70 | 57 | return osThreadSetPriority(_tid, priority); |
mbedAustin | 11:cada08fc8a70 | 58 | } |
mbedAustin | 11:cada08fc8a70 | 59 | |
mbedAustin | 11:cada08fc8a70 | 60 | osPriority Thread::get_priority() { |
mbedAustin | 11:cada08fc8a70 | 61 | return osThreadGetPriority(_tid); |
mbedAustin | 11:cada08fc8a70 | 62 | } |
mbedAustin | 11:cada08fc8a70 | 63 | |
mbedAustin | 11:cada08fc8a70 | 64 | int32_t Thread::signal_set(int32_t signals) { |
mbedAustin | 11:cada08fc8a70 | 65 | return osSignalSet(_tid, signals); |
mbedAustin | 11:cada08fc8a70 | 66 | } |
mbedAustin | 11:cada08fc8a70 | 67 | |
mbedAustin | 11:cada08fc8a70 | 68 | int32_t Thread::signal_clr(int32_t signals) { |
mbedAustin | 11:cada08fc8a70 | 69 | return osSignalClear(_tid, signals); |
mbedAustin | 11:cada08fc8a70 | 70 | } |
mbedAustin | 11:cada08fc8a70 | 71 | |
mbedAustin | 11:cada08fc8a70 | 72 | Thread::State Thread::get_state() { |
mbedAustin | 11:cada08fc8a70 | 73 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbedAustin | 11:cada08fc8a70 | 74 | return ((State)_thread_def.tcb.state); |
mbedAustin | 11:cada08fc8a70 | 75 | #else |
mbedAustin | 11:cada08fc8a70 | 76 | uint8_t status; |
mbedAustin | 11:cada08fc8a70 | 77 | status = osThreadGetState(_tid); |
mbedAustin | 11:cada08fc8a70 | 78 | return ((State)status); |
mbedAustin | 11:cada08fc8a70 | 79 | #endif |
mbedAustin | 11:cada08fc8a70 | 80 | } |
mbedAustin | 11:cada08fc8a70 | 81 | |
mbedAustin | 11:cada08fc8a70 | 82 | uint32_t Thread::stack_size() { |
mbedAustin | 11:cada08fc8a70 | 83 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbedAustin | 11:cada08fc8a70 | 84 | return _thread_def.tcb.priv_stack; |
mbedAustin | 11:cada08fc8a70 | 85 | #else |
mbedAustin | 11:cada08fc8a70 | 86 | return 0; |
mbedAustin | 11:cada08fc8a70 | 87 | #endif |
mbedAustin | 11:cada08fc8a70 | 88 | } |
mbedAustin | 11:cada08fc8a70 | 89 | |
mbedAustin | 11:cada08fc8a70 | 90 | uint32_t Thread::free_stack() { |
mbedAustin | 11:cada08fc8a70 | 91 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbedAustin | 11:cada08fc8a70 | 92 | uint32_t bottom = (uint32_t)_thread_def.tcb.stack; |
mbedAustin | 11:cada08fc8a70 | 93 | return _thread_def.tcb.tsk_stack - bottom; |
mbedAustin | 11:cada08fc8a70 | 94 | #else |
mbedAustin | 11:cada08fc8a70 | 95 | return 0; |
mbedAustin | 11:cada08fc8a70 | 96 | #endif |
mbedAustin | 11:cada08fc8a70 | 97 | } |
mbedAustin | 11:cada08fc8a70 | 98 | |
mbedAustin | 11:cada08fc8a70 | 99 | uint32_t Thread::used_stack() { |
mbedAustin | 11:cada08fc8a70 | 100 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbedAustin | 11:cada08fc8a70 | 101 | uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; |
mbedAustin | 11:cada08fc8a70 | 102 | return top - _thread_def.tcb.tsk_stack; |
mbedAustin | 11:cada08fc8a70 | 103 | #else |
mbedAustin | 11:cada08fc8a70 | 104 | return 0; |
mbedAustin | 11:cada08fc8a70 | 105 | #endif |
mbedAustin | 11:cada08fc8a70 | 106 | } |
mbedAustin | 11:cada08fc8a70 | 107 | |
mbedAustin | 11:cada08fc8a70 | 108 | uint32_t Thread::max_stack() { |
mbedAustin | 11:cada08fc8a70 | 109 | #ifndef __MBED_CMSIS_RTOS_CA9 |
mbedAustin | 11:cada08fc8a70 | 110 | uint32_t high_mark = 0; |
mbedAustin | 11:cada08fc8a70 | 111 | while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) |
mbedAustin | 11:cada08fc8a70 | 112 | high_mark++; |
mbedAustin | 11:cada08fc8a70 | 113 | return _thread_def.tcb.priv_stack - (high_mark * 4); |
mbedAustin | 11:cada08fc8a70 | 114 | #else |
mbedAustin | 11:cada08fc8a70 | 115 | return 0; |
mbedAustin | 11:cada08fc8a70 | 116 | #endif |
mbedAustin | 11:cada08fc8a70 | 117 | } |
mbedAustin | 11:cada08fc8a70 | 118 | |
mbedAustin | 11:cada08fc8a70 | 119 | osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { |
mbedAustin | 11:cada08fc8a70 | 120 | return osSignalWait(signals, millisec); |
mbedAustin | 11:cada08fc8a70 | 121 | } |
mbedAustin | 11:cada08fc8a70 | 122 | |
mbedAustin | 11:cada08fc8a70 | 123 | osStatus Thread::wait(uint32_t millisec) { |
mbedAustin | 11:cada08fc8a70 | 124 | return osDelay(millisec); |
mbedAustin | 11:cada08fc8a70 | 125 | } |
mbedAustin | 11:cada08fc8a70 | 126 | |
mbedAustin | 11:cada08fc8a70 | 127 | osStatus Thread::yield() { |
mbedAustin | 11:cada08fc8a70 | 128 | return osThreadYield(); |
mbedAustin | 11:cada08fc8a70 | 129 | } |
mbedAustin | 11:cada08fc8a70 | 130 | |
mbedAustin | 11:cada08fc8a70 | 131 | osThreadId Thread::gettid() { |
mbedAustin | 11:cada08fc8a70 | 132 | return osThreadGetId(); |
mbedAustin | 11:cada08fc8a70 | 133 | } |
mbedAustin | 11:cada08fc8a70 | 134 | |
mbedAustin | 11:cada08fc8a70 | 135 | Thread::~Thread() { |
mbedAustin | 11:cada08fc8a70 | 136 | terminate(); |
mbedAustin | 11:cada08fc8a70 | 137 | if (_dynamic_stack) { |
mbedAustin | 11:cada08fc8a70 | 138 | delete[] (_thread_def.stack_pointer); |
mbedAustin | 11:cada08fc8a70 | 139 | } |
mbedAustin | 11:cada08fc8a70 | 140 | } |
mbedAustin | 11:cada08fc8a70 | 141 | |
mbedAustin | 11:cada08fc8a70 | 142 | } |