HT-TEAM / Mbed 2 deprecated Main_ntp_sd_nrf

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG Test_Mainboard SDFileSystem RF24

Committer:
leo44
Date:
Tue Jun 08 10:06:19 2021 +0000
Revision:
0:d984976f1f1c
proto

Who changed what in which revision?

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