Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /* mbed Microcontroller Library
shoaib_ahmed 0:791a779d6220 2 * Copyright (c) 2006-2012 ARM Limited
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
shoaib_ahmed 0:791a779d6220 5 * of this software and associated documentation files (the "Software"), to deal
shoaib_ahmed 0:791a779d6220 6 * in the Software without restriction, including without limitation the rights
shoaib_ahmed 0:791a779d6220 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
shoaib_ahmed 0:791a779d6220 8 * copies of the Software, and to permit persons to whom the Software is
shoaib_ahmed 0:791a779d6220 9 * furnished to do so, subject to the following conditions:
shoaib_ahmed 0:791a779d6220 10 *
shoaib_ahmed 0:791a779d6220 11 * The above copyright notice and this permission notice shall be included in
shoaib_ahmed 0:791a779d6220 12 * all copies or substantial portions of the Software.
shoaib_ahmed 0:791a779d6220 13 *
shoaib_ahmed 0:791a779d6220 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
shoaib_ahmed 0:791a779d6220 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
shoaib_ahmed 0:791a779d6220 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
shoaib_ahmed 0:791a779d6220 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
shoaib_ahmed 0:791a779d6220 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
shoaib_ahmed 0:791a779d6220 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
shoaib_ahmed 0:791a779d6220 20 * SOFTWARE.
shoaib_ahmed 0:791a779d6220 21 */
shoaib_ahmed 0:791a779d6220 22 #include "Thread.h"
shoaib_ahmed 0:791a779d6220 23
shoaib_ahmed 0:791a779d6220 24 #include "mbed_error.h"
shoaib_ahmed 0:791a779d6220 25 #include "rtos_idle.h"
shoaib_ahmed 0:791a779d6220 26
shoaib_ahmed 0:791a779d6220 27 // rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
shoaib_ahmed 0:791a779d6220 28 #undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
shoaib_ahmed 0:791a779d6220 29 #include "rt_TypeDef.h"
shoaib_ahmed 0:791a779d6220 30
shoaib_ahmed 0:791a779d6220 31 extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
shoaib_ahmed 0:791a779d6220 32
shoaib_ahmed 0:791a779d6220 33 namespace rtos {
shoaib_ahmed 0:791a779d6220 34
shoaib_ahmed 0:791a779d6220 35 Thread::Thread(void (*task)(void const *argument), void *argument,
shoaib_ahmed 0:791a779d6220 36 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
shoaib_ahmed 0:791a779d6220 37 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
shoaib_ahmed 0:791a779d6220 38 _thread_def.pthread = task;
shoaib_ahmed 0:791a779d6220 39 _thread_def.tpriority = priority;
shoaib_ahmed 0:791a779d6220 40 _thread_def.stacksize = stack_size;
shoaib_ahmed 0:791a779d6220 41 if (stack_pointer != NULL) {
shoaib_ahmed 0:791a779d6220 42 _thread_def.stack_pointer = (uint32_t*)stack_pointer;
shoaib_ahmed 0:791a779d6220 43 _dynamic_stack = false;
shoaib_ahmed 0:791a779d6220 44 } else {
shoaib_ahmed 0:791a779d6220 45 _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)];
shoaib_ahmed 0:791a779d6220 46 if (_thread_def.stack_pointer == NULL)
shoaib_ahmed 0:791a779d6220 47 error("Error allocating the stack memory\n");
shoaib_ahmed 0:791a779d6220 48 _dynamic_stack = true;
shoaib_ahmed 0:791a779d6220 49 }
shoaib_ahmed 0:791a779d6220 50
shoaib_ahmed 0:791a779d6220 51 //Fill the stack with a magic word for maximum usage checking
shoaib_ahmed 0:791a779d6220 52 for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) {
shoaib_ahmed 0:791a779d6220 53 _thread_def.stack_pointer[i] = 0xE25A2EA5;
shoaib_ahmed 0:791a779d6220 54 }
shoaib_ahmed 0:791a779d6220 55 #endif
shoaib_ahmed 0:791a779d6220 56 _tid = osThreadCreate(&_thread_def, argument);
shoaib_ahmed 0:791a779d6220 57 }
shoaib_ahmed 0:791a779d6220 58
shoaib_ahmed 0:791a779d6220 59 osStatus Thread::terminate() {
shoaib_ahmed 0:791a779d6220 60 return osThreadTerminate(_tid);
shoaib_ahmed 0:791a779d6220 61 }
shoaib_ahmed 0:791a779d6220 62
shoaib_ahmed 0:791a779d6220 63 osStatus Thread::set_priority(osPriority priority) {
shoaib_ahmed 0:791a779d6220 64 return osThreadSetPriority(_tid, priority);
shoaib_ahmed 0:791a779d6220 65 }
shoaib_ahmed 0:791a779d6220 66
shoaib_ahmed 0:791a779d6220 67 osPriority Thread::get_priority() {
shoaib_ahmed 0:791a779d6220 68 return osThreadGetPriority(_tid);
shoaib_ahmed 0:791a779d6220 69 }
shoaib_ahmed 0:791a779d6220 70
shoaib_ahmed 0:791a779d6220 71 int32_t Thread::signal_set(int32_t signals) {
shoaib_ahmed 0:791a779d6220 72 return osSignalSet(_tid, signals);
shoaib_ahmed 0:791a779d6220 73 }
shoaib_ahmed 0:791a779d6220 74
shoaib_ahmed 0:791a779d6220 75 int32_t Thread::signal_clr(int32_t signals) {
shoaib_ahmed 0:791a779d6220 76 return osSignalClear(_tid, signals);
shoaib_ahmed 0:791a779d6220 77 }
shoaib_ahmed 0:791a779d6220 78
shoaib_ahmed 0:791a779d6220 79 Thread::State Thread::get_state() {
shoaib_ahmed 0:791a779d6220 80 #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
shoaib_ahmed 0:791a779d6220 81 #ifdef CMSIS_OS_RTX
shoaib_ahmed 0:791a779d6220 82 return ((State)_thread_def.tcb.state);
shoaib_ahmed 0:791a779d6220 83 #endif
shoaib_ahmed 0:791a779d6220 84 #else
shoaib_ahmed 0:791a779d6220 85 uint8_t status;
shoaib_ahmed 0:791a779d6220 86 status = osThreadGetState(_tid);
shoaib_ahmed 0:791a779d6220 87 return ((State)status);
shoaib_ahmed 0:791a779d6220 88 #endif
shoaib_ahmed 0:791a779d6220 89 }
shoaib_ahmed 0:791a779d6220 90
shoaib_ahmed 0:791a779d6220 91 uint32_t Thread::stack_size() {
shoaib_ahmed 0:791a779d6220 92 #ifndef __MBED_CMSIS_RTOS_CA9
shoaib_ahmed 0:791a779d6220 93 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
shoaib_ahmed 0:791a779d6220 94 return _thread_def.tcb.priv_stack;
shoaib_ahmed 0:791a779d6220 95 #else
shoaib_ahmed 0:791a779d6220 96 P_TCB tcb = rt_tid2ptcb(_tid);
shoaib_ahmed 0:791a779d6220 97 return tcb->priv_stack;
shoaib_ahmed 0:791a779d6220 98 #endif
shoaib_ahmed 0:791a779d6220 99 #else
shoaib_ahmed 0:791a779d6220 100 return 0;
shoaib_ahmed 0:791a779d6220 101 #endif
shoaib_ahmed 0:791a779d6220 102 }
shoaib_ahmed 0:791a779d6220 103
shoaib_ahmed 0:791a779d6220 104 uint32_t Thread::free_stack() {
shoaib_ahmed 0:791a779d6220 105 #ifndef __MBED_CMSIS_RTOS_CA9
shoaib_ahmed 0:791a779d6220 106 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
shoaib_ahmed 0:791a779d6220 107 uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
shoaib_ahmed 0:791a779d6220 108 return _thread_def.tcb.tsk_stack - bottom;
shoaib_ahmed 0:791a779d6220 109 #else
shoaib_ahmed 0:791a779d6220 110 P_TCB tcb = rt_tid2ptcb(_tid);
shoaib_ahmed 0:791a779d6220 111 uint32_t bottom = (uint32_t)tcb->stack;
shoaib_ahmed 0:791a779d6220 112 return tcb->tsk_stack - bottom;
shoaib_ahmed 0:791a779d6220 113 #endif
shoaib_ahmed 0:791a779d6220 114 #else
shoaib_ahmed 0:791a779d6220 115 return 0;
shoaib_ahmed 0:791a779d6220 116 #endif
shoaib_ahmed 0:791a779d6220 117 }
shoaib_ahmed 0:791a779d6220 118
shoaib_ahmed 0:791a779d6220 119 uint32_t Thread::used_stack() {
shoaib_ahmed 0:791a779d6220 120 #ifndef __MBED_CMSIS_RTOS_CA9
shoaib_ahmed 0:791a779d6220 121 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
shoaib_ahmed 0:791a779d6220 122 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
shoaib_ahmed 0:791a779d6220 123 return top - _thread_def.tcb.tsk_stack;
shoaib_ahmed 0:791a779d6220 124 #else
shoaib_ahmed 0:791a779d6220 125 P_TCB tcb = rt_tid2ptcb(_tid);
shoaib_ahmed 0:791a779d6220 126 uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
shoaib_ahmed 0:791a779d6220 127 return top - tcb->tsk_stack;
shoaib_ahmed 0:791a779d6220 128 #endif
shoaib_ahmed 0:791a779d6220 129 #else
shoaib_ahmed 0:791a779d6220 130 return 0;
shoaib_ahmed 0:791a779d6220 131 #endif
shoaib_ahmed 0:791a779d6220 132 }
shoaib_ahmed 0:791a779d6220 133
shoaib_ahmed 0:791a779d6220 134 uint32_t Thread::max_stack() {
shoaib_ahmed 0:791a779d6220 135 #ifndef __MBED_CMSIS_RTOS_CA9
shoaib_ahmed 0:791a779d6220 136 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
shoaib_ahmed 0:791a779d6220 137 uint32_t high_mark = 0;
shoaib_ahmed 0:791a779d6220 138 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
shoaib_ahmed 0:791a779d6220 139 high_mark++;
shoaib_ahmed 0:791a779d6220 140 return _thread_def.tcb.priv_stack - (high_mark * 4);
shoaib_ahmed 0:791a779d6220 141 #else
shoaib_ahmed 0:791a779d6220 142 P_TCB tcb = rt_tid2ptcb(_tid);
shoaib_ahmed 0:791a779d6220 143 uint32_t high_mark = 0;
shoaib_ahmed 0:791a779d6220 144 while (tcb->stack[high_mark] == 0xE25A2EA5)
shoaib_ahmed 0:791a779d6220 145 high_mark++;
shoaib_ahmed 0:791a779d6220 146 return tcb->priv_stack - (high_mark * 4);
shoaib_ahmed 0:791a779d6220 147 #endif
shoaib_ahmed 0:791a779d6220 148 #else
shoaib_ahmed 0:791a779d6220 149 return 0;
shoaib_ahmed 0:791a779d6220 150 #endif
shoaib_ahmed 0:791a779d6220 151 }
shoaib_ahmed 0:791a779d6220 152
shoaib_ahmed 0:791a779d6220 153 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
shoaib_ahmed 0:791a779d6220 154 return osSignalWait(signals, millisec);
shoaib_ahmed 0:791a779d6220 155 }
shoaib_ahmed 0:791a779d6220 156
shoaib_ahmed 0:791a779d6220 157 osStatus Thread::wait(uint32_t millisec) {
shoaib_ahmed 0:791a779d6220 158 return osDelay(millisec);
shoaib_ahmed 0:791a779d6220 159 }
shoaib_ahmed 0:791a779d6220 160
shoaib_ahmed 0:791a779d6220 161 osStatus Thread::yield() {
shoaib_ahmed 0:791a779d6220 162 return osThreadYield();
shoaib_ahmed 0:791a779d6220 163 }
shoaib_ahmed 0:791a779d6220 164
shoaib_ahmed 0:791a779d6220 165 osThreadId Thread::gettid() {
shoaib_ahmed 0:791a779d6220 166 return osThreadGetId();
shoaib_ahmed 0:791a779d6220 167 }
shoaib_ahmed 0:791a779d6220 168
shoaib_ahmed 0:791a779d6220 169 void Thread::attach_idle_hook(void (*fptr)(void)) {
shoaib_ahmed 0:791a779d6220 170 rtos_attach_idle_hook(fptr);
shoaib_ahmed 0:791a779d6220 171 }
shoaib_ahmed 0:791a779d6220 172
shoaib_ahmed 0:791a779d6220 173 Thread::~Thread() {
shoaib_ahmed 0:791a779d6220 174 terminate();
shoaib_ahmed 0:791a779d6220 175 #ifdef __MBED_CMSIS_RTOS_CM
shoaib_ahmed 0:791a779d6220 176 if (_dynamic_stack) {
shoaib_ahmed 0:791a779d6220 177 delete[] (_thread_def.stack_pointer);
shoaib_ahmed 0:791a779d6220 178 }
shoaib_ahmed 0:791a779d6220 179 #endif
shoaib_ahmed 0:791a779d6220 180 }
shoaib_ahmed 0:791a779d6220 181
shoaib_ahmed 0:791a779d6220 182 }