Ultrasound timeout demo
Dependencies: F7_Ethernet HCSR04 MQTT mbed
Fork of Nucleo_F746ZG_Ethernet by
Thread.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #include "Thread.h" 00023 00024 #include "mbed_error.h" 00025 #include "rtos_idle.h" 00026 00027 // rt_tid2ptcb is an internal function which we exposed to get TCB for thread id 00028 #undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h 00029 #include "rt_TypeDef.h" 00030 00031 extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id); 00032 00033 namespace rtos { 00034 00035 Thread::Thread(void (*task)(void const *argument), void *argument, 00036 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) { 00037 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM) 00038 _thread_def.pthread = task; 00039 _thread_def.tpriority = priority; 00040 _thread_def.stacksize = stack_size; 00041 if (stack_pointer != NULL) { 00042 _thread_def.stack_pointer = (uint32_t*)stack_pointer; 00043 _dynamic_stack = false; 00044 } else { 00045 _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)]; 00046 if (_thread_def.stack_pointer == NULL) 00047 error("Error allocating the stack memory\n"); 00048 _dynamic_stack = true; 00049 } 00050 00051 //Fill the stack with a magic word for maximum usage checking 00052 for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) { 00053 _thread_def.stack_pointer[i] = 0xE25A2EA5; 00054 } 00055 #endif 00056 _tid = osThreadCreate(&_thread_def, argument); 00057 } 00058 00059 osStatus Thread::terminate() { 00060 return osThreadTerminate(_tid); 00061 } 00062 00063 osStatus Thread::set_priority(osPriority priority) { 00064 return osThreadSetPriority(_tid, priority); 00065 } 00066 00067 osPriority Thread::get_priority() { 00068 return osThreadGetPriority(_tid); 00069 } 00070 00071 int32_t Thread::signal_set(int32_t signals) { 00072 return osSignalSet(_tid, signals); 00073 } 00074 00075 int32_t Thread::signal_clr(int32_t signals) { 00076 return osSignalClear(_tid, signals); 00077 } 00078 00079 Thread::State Thread::get_state() { 00080 #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM) 00081 #ifdef CMSIS_OS_RTX 00082 return ((State)_thread_def.tcb.state); 00083 #endif 00084 #else 00085 uint8_t status; 00086 status = osThreadGetState(_tid); 00087 return ((State)status); 00088 #endif 00089 } 00090 00091 uint32_t Thread::stack_size() { 00092 #ifndef __MBED_CMSIS_RTOS_CA9 00093 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) 00094 return _thread_def.tcb.priv_stack; 00095 #else 00096 P_TCB tcb = rt_tid2ptcb(_tid); 00097 return tcb->priv_stack; 00098 #endif 00099 #else 00100 return 0; 00101 #endif 00102 } 00103 00104 uint32_t Thread::free_stack() { 00105 #ifndef __MBED_CMSIS_RTOS_CA9 00106 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) 00107 uint32_t bottom = (uint32_t)_thread_def.tcb.stack; 00108 return _thread_def.tcb.tsk_stack - bottom; 00109 #else 00110 P_TCB tcb = rt_tid2ptcb(_tid); 00111 uint32_t bottom = (uint32_t)tcb->stack; 00112 return tcb->tsk_stack - bottom; 00113 #endif 00114 #else 00115 return 0; 00116 #endif 00117 } 00118 00119 uint32_t Thread::used_stack() { 00120 #ifndef __MBED_CMSIS_RTOS_CA9 00121 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) 00122 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack; 00123 return top - _thread_def.tcb.tsk_stack; 00124 #else 00125 P_TCB tcb = rt_tid2ptcb(_tid); 00126 uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack; 00127 return top - tcb->tsk_stack; 00128 #endif 00129 #else 00130 return 0; 00131 #endif 00132 } 00133 00134 uint32_t Thread::max_stack() { 00135 #ifndef __MBED_CMSIS_RTOS_CA9 00136 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM) 00137 uint32_t high_mark = 0; 00138 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5) 00139 high_mark++; 00140 return _thread_def.tcb.priv_stack - (high_mark * 4); 00141 #else 00142 P_TCB tcb = rt_tid2ptcb(_tid); 00143 uint32_t high_mark = 0; 00144 while (tcb->stack[high_mark] == 0xE25A2EA5) 00145 high_mark++; 00146 return tcb->priv_stack - (high_mark * 4); 00147 #endif 00148 #else 00149 return 0; 00150 #endif 00151 } 00152 00153 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) { 00154 return osSignalWait(signals, millisec); 00155 } 00156 00157 osStatus Thread::wait(uint32_t millisec) { 00158 return osDelay(millisec); 00159 } 00160 00161 osStatus Thread::yield() { 00162 return osThreadYield(); 00163 } 00164 00165 osThreadId Thread::gettid() { 00166 return osThreadGetId(); 00167 } 00168 00169 void Thread::attach_idle_hook(void (*fptr)(void)) { 00170 rtos_attach_idle_hook(fptr); 00171 } 00172 00173 Thread::~Thread() { 00174 terminate(); 00175 #ifdef __MBED_CMSIS_RTOS_CM 00176 if (_dynamic_stack) { 00177 delete[] (_thread_def.stack_pointer); 00178 } 00179 #endif 00180 } 00181 00182 }
Generated on Tue Aug 2 2022 04:13:39 by 1.7.2