Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Deprecated

This is the mbed 2 rtos library. mbed OS 5 integrates the mbed library with mbed-rtos. With this, we have provided thread safety for all mbed APIs. If you'd like to learn about using mbed OS 5, please see the docs.

Committer:
Kojto
Date:
Tue Jul 04 13:32:20 2017 +0100
Revision:
125:5713cbbdb706
Parent:
123:58563e6cba1e
replace mbed_rtx by mbed_rtx4

Not causing a conflict with mbed_rtx that is for newer rtos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
c1728p9 123:58563e6cba1e 1
c1728p9 123:58563e6cba1e 2 /** \addtogroup rtos */
c1728p9 123:58563e6cba1e 3 /** @{*/
mbed_official 49:77c8e4604045 4 /*----------------------------------------------------------------------------
mbed_official 112:53ace74b190c 5 * CMSIS-RTOS - RTX
mbed_official 49:77c8e4604045 6 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 7 * Name: RTX_CM_LIB.H
mbed_official 49:77c8e4604045 8 * Purpose: RTX Kernel System Configuration
mbed_official 112:53ace74b190c 9 * Rev.: V4.79
mbed_official 49:77c8e4604045 10 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 11 *
mbed_official 112:53ace74b190c 12 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbed_official 49:77c8e4604045 13 * All rights reserved.
mbed_official 49:77c8e4604045 14 * Redistribution and use in source and binary forms, with or without
mbed_official 49:77c8e4604045 15 * modification, are permitted provided that the following conditions are met:
mbed_official 49:77c8e4604045 16 * - Redistributions of source code must retain the above copyright
mbed_official 49:77c8e4604045 17 * notice, this list of conditions and the following disclaimer.
mbed_official 49:77c8e4604045 18 * - Redistributions in binary form must reproduce the above copyright
mbed_official 49:77c8e4604045 19 * notice, this list of conditions and the following disclaimer in the
mbed_official 49:77c8e4604045 20 * documentation and/or other materials provided with the distribution.
mbed_official 49:77c8e4604045 21 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 49:77c8e4604045 22 * to endorse or promote products derived from this software without
mbed_official 49:77c8e4604045 23 * specific prior written permission.
mbed_official 49:77c8e4604045 24 *
mbed_official 49:77c8e4604045 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 49:77c8e4604045 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 49:77c8e4604045 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbed_official 49:77c8e4604045 28 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbed_official 49:77c8e4604045 29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 49:77c8e4604045 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 49:77c8e4604045 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 49:77c8e4604045 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 49:77c8e4604045 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbed_official 49:77c8e4604045 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 49:77c8e4604045 35 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 49:77c8e4604045 36 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 37 #include "mbed_error.h"
mbed_official 49:77c8e4604045 38
mbed_official 49:77c8e4604045 39 #if defined (__CC_ARM)
Kojto 119:19af2d39a542 40 #include <rt_misc.h>
mbed_official 49:77c8e4604045 41 #pragma O3
mbed_official 49:77c8e4604045 42 #define __USED __attribute__((used))
mbed_official 49:77c8e4604045 43 #elif defined (__GNUC__)
mbed_official 49:77c8e4604045 44 #pragma GCC optimize ("O3")
mbed_official 49:77c8e4604045 45 #define __USED __attribute__((used))
mbed_official 49:77c8e4604045 46 #elif defined (__ICCARM__)
mbed_official 49:77c8e4604045 47 #define __USED __root
mbed_official 49:77c8e4604045 48 #endif
mbed_official 49:77c8e4604045 49
mbed_official 49:77c8e4604045 50
mbed_official 49:77c8e4604045 51 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 52 * Definitions
mbed_official 49:77c8e4604045 53 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 54
mbed_official 49:77c8e4604045 55 #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
mbed_official 49:77c8e4604045 56 #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
mbed_official 49:77c8e4604045 57
c1728p9 123:58563e6cba1e 58 #define OS_TCB_SIZE 64
mbed_official 49:77c8e4604045 59 #define OS_TMR_SIZE 8
mbed_official 49:77c8e4604045 60
mbed_official 49:77c8e4604045 61 typedef void *OS_ID;
mbed_official 49:77c8e4604045 62 typedef uint32_t OS_TID;
mbed_official 112:53ace74b190c 63 typedef uint32_t OS_MUT[4];
mbed_official 49:77c8e4604045 64 typedef uint32_t OS_RESULT;
mbed_official 49:77c8e4604045 65
Kojto 118:6635230e06ba 66 #if defined (__CC_ARM) && !defined (__MICROLIB)
Kojto 118:6635230e06ba 67
mbed_official 49:77c8e4604045 68 #define runtask_id() rt_tsk_self()
mbed_official 49:77c8e4604045 69 #define mutex_init(m) rt_mut_init(m)
mbed_official 112:53ace74b190c 70 #define mutex_wait(m) os_mut_wait(m,0xFFFFU)
mbed_official 49:77c8e4604045 71 #define mutex_rel(m) os_mut_release(m)
mbed_official 49:77c8e4604045 72
mbed_official 112:53ace74b190c 73 extern uint8_t os_running;
mbed_official 49:77c8e4604045 74 extern OS_TID rt_tsk_self (void);
mbed_official 49:77c8e4604045 75 extern void rt_mut_init (OS_ID mutex);
mbed_official 49:77c8e4604045 76 extern OS_RESULT rt_mut_release (OS_ID mutex);
mbed_official 49:77c8e4604045 77 extern OS_RESULT rt_mut_wait (OS_ID mutex, uint16_t timeout);
mbed_official 49:77c8e4604045 78
mbed_official 49:77c8e4604045 79 #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
mbed_official 49:77c8e4604045 80 #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex)
mbed_official 49:77c8e4604045 81
mbed_official 49:77c8e4604045 82 OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex) __svc_indirect(0);
mbed_official 49:77c8e4604045 83 OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0);
mbed_official 49:77c8e4604045 84
mbed_official 49:77c8e4604045 85 #endif
mbed_official 49:77c8e4604045 86
mbed_official 49:77c8e4604045 87
mbed_official 49:77c8e4604045 88 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 89 * Global Variables
mbed_official 49:77c8e4604045 90 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 91
mbed_official 112:53ace74b190c 92 #if (OS_TASKCNT == 0)
mbed_official 112:53ace74b190c 93 #error "Invalid number of concurrent running threads!"
mbed_official 112:53ace74b190c 94 #endif
mbed_official 112:53ace74b190c 95
mbed_official 112:53ace74b190c 96 #if (OS_PRIVCNT >= OS_TASKCNT)
mbed_official 112:53ace74b190c 97 #error "Too many threads with user-provided stack size!"
mbed_official 112:53ace74b190c 98 #endif
mbed_official 112:53ace74b190c 99
mbed_official 49:77c8e4604045 100 #if (OS_TIMERS != 0)
mbed_official 49:77c8e4604045 101 #define OS_TASK_CNT (OS_TASKCNT + 1)
mbed_official 112:53ace74b190c 102 #ifndef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 103 #define OS_PRIV_CNT (OS_PRIVCNT + 2)
mbed_official 112:53ace74b190c 104 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ))
mbed_official 112:53ace74b190c 105 #endif
mbed_official 49:77c8e4604045 106 #else
mbed_official 49:77c8e4604045 107 #define OS_TASK_CNT OS_TASKCNT
mbed_official 112:53ace74b190c 108 #ifndef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 109 #define OS_PRIV_CNT (OS_PRIVCNT + 1)
mbed_official 112:53ace74b190c 110 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE))
mbed_official 112:53ace74b190c 111 #endif
mbed_official 112:53ace74b190c 112 #endif
mbed_official 112:53ace74b190c 113
mbed_official 112:53ace74b190c 114 #ifndef OS_STKINIT
mbed_official 112:53ace74b190c 115 #define OS_STKINIT 0
mbed_official 49:77c8e4604045 116 #endif
mbed_official 49:77c8e4604045 117
mbed_official 49:77c8e4604045 118 uint16_t const os_maxtaskrun = OS_TASK_CNT;
mbed_official 112:53ace74b190c 119 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 120 uint32_t const os_stackinfo = (OS_STKINIT<<28) | (OS_STKCHECK<<24) | (OS_IDLESTKSIZE*4);
mbed_official 112:53ace74b190c 121 #else
mbed_official 112:53ace74b190c 122 uint32_t const os_stackinfo = (OS_STKINIT<<28) | (OS_STKCHECK<<24) | (OS_PRIV_CNT<<16) | (OS_STKSIZE*4);
mbed_official 112:53ace74b190c 123 #endif
mbed_official 49:77c8e4604045 124 uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT;
mbed_official 112:53ace74b190c 125 uint32_t const os_tickfreq = OS_CLOCK;
mbed_official 112:53ace74b190c 126 uint16_t const os_tickus_i = OS_CLOCK/1000000;
mbed_official 112:53ace74b190c 127 uint16_t const os_tickus_f = (((uint64_t)(OS_CLOCK-1000000*(OS_CLOCK/1000000)))<<16)/1000000;
mbed_official 49:77c8e4604045 128 uint32_t const os_trv = OS_TRV;
Kojto 118:6635230e06ba 129 #if defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)
Kojto 118:6635230e06ba 130 uint8_t const os_flags = 0;
Kojto 118:6635230e06ba 131 #else /* defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) */
mbed_official 49:77c8e4604045 132 uint8_t const os_flags = OS_RUNPRIV;
Kojto 118:6635230e06ba 133 #endif /* defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) */
mbed_official 49:77c8e4604045 134
mbed_official 49:77c8e4604045 135 /* Export following defines to uVision debugger. */
mbed_official 112:53ace74b190c 136 __USED uint32_t const CMSIS_RTOS_API_Version = osCMSIS;
mbed_official 112:53ace74b190c 137 __USED uint32_t const CMSIS_RTOS_RTX_Version = osCMSIS_RTX;
mbed_official 49:77c8e4604045 138 __USED uint32_t const os_clockrate = OS_TICK;
mbed_official 112:53ace74b190c 139 __USED uint32_t const os_timernum = 0U;
mbed_official 112:53ace74b190c 140
mbed_official 112:53ace74b190c 141 /* Memory pool for TCB allocation */
mbed_official 112:53ace74b190c 142 _declare_box (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT);
mbed_official 112:53ace74b190c 143 uint16_t const mp_tcb_size = sizeof(mp_tcb);
mbed_official 49:77c8e4604045 144
mbed_official 112:53ace74b190c 145 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 146 /* Memory pool for os_idle_demon stack allocation. */
mbed_official 112:53ace74b190c 147 _declare_box8 (mp_stk, OS_IDLESTKSIZE*4, 1);
mbed_official 112:53ace74b190c 148 uint32_t const mp_stk_size = sizeof(mp_stk);
mbed_official 112:53ace74b190c 149 #else
mbed_official 112:53ace74b190c 150 /* Memory pool for System stack allocation (+os_idle_demon). */
mbed_official 112:53ace74b190c 151 _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1);
mbed_official 112:53ace74b190c 152 uint32_t const mp_stk_size = sizeof(mp_stk);
mbed_official 112:53ace74b190c 153
mbed_official 112:53ace74b190c 154 /* Memory pool for user specified stack allocation (+main, +timer) */
mbed_official 112:53ace74b190c 155 uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
mbed_official 112:53ace74b190c 156 uint32_t const os_stack_sz = sizeof(os_stack_mem);
mbed_official 112:53ace74b190c 157 #endif
mbed_official 49:77c8e4604045 158
mbed_official 49:77c8e4604045 159 #ifndef OS_FIFOSZ
mbed_official 49:77c8e4604045 160 #define OS_FIFOSZ 16
mbed_official 49:77c8e4604045 161 #endif
mbed_official 49:77c8e4604045 162
mbed_official 49:77c8e4604045 163 /* Fifo Queue buffer for ISR requests.*/
mbed_official 49:77c8e4604045 164 uint32_t os_fifo[OS_FIFOSZ*2+1];
mbed_official 49:77c8e4604045 165 uint8_t const os_fifo_size = OS_FIFOSZ;
mbed_official 49:77c8e4604045 166
mbed_official 49:77c8e4604045 167 /* An array of Active task pointers. */
mbed_official 49:77c8e4604045 168 void *os_active_TCB[OS_TASK_CNT];
mbed_official 49:77c8e4604045 169
mbed_official 49:77c8e4604045 170 /* User Timers Resources */
mbed_official 49:77c8e4604045 171 #if (OS_TIMERS != 0)
mbed_official 49:77c8e4604045 172 extern void osTimerThread (void const *argument);
mbed_official 112:53ace74b190c 173 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 49:77c8e4604045 174 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ);
mbed_official 112:53ace74b190c 175 #else
mbed_official 112:53ace74b190c 176 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ);
mbed_official 112:53ace74b190c 177 #endif
mbed_official 49:77c8e4604045 178 osThreadId osThreadId_osTimerThread;
mbed_official 49:77c8e4604045 179 osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
mbed_official 49:77c8e4604045 180 osMessageQId osMessageQId_osTimerMessageQ;
mbed_official 49:77c8e4604045 181 #else
mbed_official 49:77c8e4604045 182 osThreadDef_t os_thread_def_osTimerThread = { NULL };
mbed_official 49:77c8e4604045 183 osThreadId osThreadId_osTimerThread;
mbed_official 112:53ace74b190c 184 osMessageQDef(osTimerMessageQ, 0U, void *);
mbed_official 49:77c8e4604045 185 osMessageQId osMessageQId_osTimerMessageQ;
mbed_official 49:77c8e4604045 186 #endif
mbed_official 49:77c8e4604045 187
mbed_official 112:53ace74b190c 188 /* Legacy RTX User Timers not used */
Kojto 118:6635230e06ba 189 uint32_t os_tmr = 0U;
mbed_official 112:53ace74b190c 190 uint32_t const *m_tmr = NULL;
mbed_official 112:53ace74b190c 191 uint16_t const mp_tmr_size = 0U;
mbed_official 112:53ace74b190c 192
Kojto 119:19af2d39a542 193 /* singleton mutex */
Kojto 119:19af2d39a542 194 osMutexId singleton_mutex_id;
Kojto 119:19af2d39a542 195 osMutexDef(singleton_mutex);
Kojto 119:19af2d39a542 196
mbed_official 112:53ace74b190c 197 #if defined (__CC_ARM) && !defined (__MICROLIB)
mbed_official 112:53ace74b190c 198 /* A memory space for arm standard library. */
mbed_official 112:53ace74b190c 199 static uint32_t std_libspace[OS_TASK_CNT][96/4];
mbed_official 112:53ace74b190c 200 static OS_MUT std_libmutex[OS_MUTEXCNT];
mbed_official 112:53ace74b190c 201 static uint32_t nr_mutex;
mbed_official 112:53ace74b190c 202 extern void *__libspace_start;
mbed_official 112:53ace74b190c 203 #endif
mbed_official 112:53ace74b190c 204
Kojto 118:6635230e06ba 205 #if defined (__ICCARM__)
Kojto 118:6635230e06ba 206 static osMutexId std_mutex_id_sys[_MAX_LOCK] = {0};
Kojto 118:6635230e06ba 207 static OS_MUT std_mutex_sys[_MAX_LOCK] = {0};
Kojto 118:6635230e06ba 208 #define _FOPEN_MAX 10
Kojto 118:6635230e06ba 209 static osMutexId std_mutex_id_file[_FOPEN_MAX] = {0};
Kojto 118:6635230e06ba 210 static OS_MUT std_mutex_file[_FOPEN_MAX] = {0};
Kojto 118:6635230e06ba 211 void __iar_system_Mtxinit(__iar_Rmtx *mutex) /* Initialize a system lock */
Kojto 118:6635230e06ba 212 {
Kojto 118:6635230e06ba 213 osMutexDef_t def;
Kojto 118:6635230e06ba 214 uint32_t index;
Kojto 118:6635230e06ba 215 for (index = 0; index < _MAX_LOCK; index++) {
Kojto 118:6635230e06ba 216 if (0 == std_mutex_id_sys[index]) {
Kojto 118:6635230e06ba 217 def.mutex = &std_mutex_sys[index];
Kojto 118:6635230e06ba 218 std_mutex_id_sys[index] = osMutexCreate(&def);
Kojto 118:6635230e06ba 219 *mutex = (__iar_Rmtx*)&std_mutex_id_sys[index];
Kojto 118:6635230e06ba 220 return;
Kojto 118:6635230e06ba 221 }
Kojto 118:6635230e06ba 222 }
Kojto 118:6635230e06ba 223 // This should never happen
Kojto 118:6635230e06ba 224 error("Not enough mutexes\n");
Kojto 118:6635230e06ba 225 }
Kojto 118:6635230e06ba 226
Kojto 118:6635230e06ba 227 void __iar_system_Mtxdst(__iar_Rmtx *mutex)/*Destroy a system lock */
Kojto 118:6635230e06ba 228 {
Kojto 118:6635230e06ba 229 osMutexDelete(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 230 *mutex = 0;
Kojto 118:6635230e06ba 231 }
Kojto 118:6635230e06ba 232
Kojto 118:6635230e06ba 233 void __iar_system_Mtxlock(__iar_Rmtx *mutex) /* Lock a system lock */
Kojto 118:6635230e06ba 234 {
Kojto 118:6635230e06ba 235 osMutexWait(*(osMutexId*)*mutex, osWaitForever);
Kojto 118:6635230e06ba 236 }
Kojto 118:6635230e06ba 237
Kojto 118:6635230e06ba 238 void __iar_system_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a system lock */
Kojto 118:6635230e06ba 239 {
Kojto 118:6635230e06ba 240 osMutexRelease(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 241 }
Kojto 118:6635230e06ba 242
Kojto 118:6635230e06ba 243 void __iar_file_Mtxinit(__iar_Rmtx *mutex)/*Initialize a file lock */
Kojto 118:6635230e06ba 244 {
Kojto 118:6635230e06ba 245 osMutexDef_t def;
Kojto 118:6635230e06ba 246 uint32_t index;
Kojto 118:6635230e06ba 247 for (index = 0; index < _FOPEN_MAX; index++) {
Kojto 118:6635230e06ba 248 if (0 == std_mutex_id_file[index]) {
Kojto 118:6635230e06ba 249 def.mutex = &std_mutex_file[index];
Kojto 118:6635230e06ba 250 std_mutex_id_file[index] = osMutexCreate(&def);
Kojto 118:6635230e06ba 251 *mutex = (__iar_Rmtx*)&std_mutex_id_file[index];
Kojto 118:6635230e06ba 252 return;
Kojto 118:6635230e06ba 253 }
Kojto 118:6635230e06ba 254 }
Kojto 118:6635230e06ba 255 // The variable _FOPEN_MAX needs to be increased
Kojto 118:6635230e06ba 256 error("Not enough mutexes\n");
Kojto 118:6635230e06ba 257 }
Kojto 118:6635230e06ba 258
Kojto 118:6635230e06ba 259 void __iar_file_Mtxdst(__iar_Rmtx *mutex) /* Destroy a file lock */
Kojto 118:6635230e06ba 260 {
Kojto 118:6635230e06ba 261 osMutexDelete(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 262 *mutex = 0;
Kojto 118:6635230e06ba 263 }
Kojto 118:6635230e06ba 264
Kojto 118:6635230e06ba 265 void __iar_file_Mtxlock(__iar_Rmtx *mutex) /* Lock a file lock */
Kojto 118:6635230e06ba 266 {
Kojto 118:6635230e06ba 267 osMutexWait(*(osMutexId*)*mutex, osWaitForever);
Kojto 118:6635230e06ba 268 }
Kojto 118:6635230e06ba 269
Kojto 118:6635230e06ba 270 void __iar_file_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a file lock */
Kojto 118:6635230e06ba 271 {
Kojto 118:6635230e06ba 272 osMutexRelease(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 273 }
Kojto 118:6635230e06ba 274
Kojto 118:6635230e06ba 275 #endif
mbed_official 49:77c8e4604045 276
mbed_official 49:77c8e4604045 277 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 278 * RTX Optimizations (empty functions)
mbed_official 49:77c8e4604045 279 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 280
mbed_official 49:77c8e4604045 281 #if OS_ROBIN == 0
mbed_official 49:77c8e4604045 282 void rt_init_robin (void) {;}
mbed_official 49:77c8e4604045 283 void rt_chk_robin (void) {;}
mbed_official 49:77c8e4604045 284 #endif
mbed_official 49:77c8e4604045 285
mbed_official 49:77c8e4604045 286 #if OS_STKCHECK == 0
mbed_official 49:77c8e4604045 287 void rt_stk_check (void) {;}
mbed_official 49:77c8e4604045 288 #endif
mbed_official 49:77c8e4604045 289
mbed_official 49:77c8e4604045 290
mbed_official 49:77c8e4604045 291 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 292 * Standard Library multithreading interface
mbed_official 49:77c8e4604045 293 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 294
mbed_official 49:77c8e4604045 295 #if defined (__CC_ARM) && !defined (__MICROLIB)
mbed_official 112:53ace74b190c 296
mbed_official 112:53ace74b190c 297 /*--------------------------- __user_perthread_libspace ---------------------*/
mbed_official 112:53ace74b190c 298
mbed_official 112:53ace74b190c 299 void *__user_perthread_libspace (void) {
mbed_official 112:53ace74b190c 300 /* Provide a separate libspace for each task. */
mbed_official 112:53ace74b190c 301 uint32_t idx;
mbed_official 49:77c8e4604045 302
mbed_official 112:53ace74b190c 303 idx = (os_running != 0U) ? runtask_id () : 0U;
mbed_official 112:53ace74b190c 304 if (idx == 0U) {
mbed_official 112:53ace74b190c 305 /* RTX not running yet. */
mbed_official 112:53ace74b190c 306 return (&__libspace_start);
mbed_official 112:53ace74b190c 307 }
mbed_official 112:53ace74b190c 308 return ((void *)&std_libspace[idx-1]);
mbed_official 112:53ace74b190c 309 }
mbed_official 112:53ace74b190c 310
mbed_official 112:53ace74b190c 311 /*--------------------------- _mutex_initialize -----------------------------*/
mbed_official 49:77c8e4604045 312
mbed_official 49:77c8e4604045 313 int _mutex_initialize (OS_ID *mutex) {
mbed_official 49:77c8e4604045 314 /* Allocate and initialize a system mutex. */
mbed_official 49:77c8e4604045 315
mbed_official 49:77c8e4604045 316 if (nr_mutex >= OS_MUTEXCNT) {
mbed_official 49:77c8e4604045 317 /* If you are here, you need to increase the number OS_MUTEXCNT. */
mbed_official 49:77c8e4604045 318 error("Not enough stdlib mutexes\n");
mbed_official 49:77c8e4604045 319 }
mbed_official 49:77c8e4604045 320 *mutex = &std_libmutex[nr_mutex++];
mbed_official 49:77c8e4604045 321 mutex_init (*mutex);
mbed_official 49:77c8e4604045 322 return (1);
mbed_official 49:77c8e4604045 323 }
mbed_official 49:77c8e4604045 324
mbed_official 49:77c8e4604045 325
mbed_official 49:77c8e4604045 326 /*--------------------------- _mutex_acquire --------------------------------*/
mbed_official 49:77c8e4604045 327
mbed_official 49:77c8e4604045 328 __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
mbed_official 49:77c8e4604045 329 /* Acquire a system mutex, lock stdlib resources. */
mbed_official 112:53ace74b190c 330 if (os_running) {
mbed_official 49:77c8e4604045 331 /* RTX running, acquire a mutex. */
mbed_official 49:77c8e4604045 332 mutex_wait (*mutex);
mbed_official 49:77c8e4604045 333 }
mbed_official 49:77c8e4604045 334 }
mbed_official 49:77c8e4604045 335
mbed_official 49:77c8e4604045 336
mbed_official 49:77c8e4604045 337 /*--------------------------- _mutex_release --------------------------------*/
mbed_official 49:77c8e4604045 338
mbed_official 49:77c8e4604045 339 __attribute__((used)) void _mutex_release (OS_ID *mutex) {
mbed_official 49:77c8e4604045 340 /* Release a system mutex, unlock stdlib resources. */
mbed_official 112:53ace74b190c 341 if (os_running) {
mbed_official 49:77c8e4604045 342 /* RTX running, release a mutex. */
mbed_official 49:77c8e4604045 343 mutex_rel (*mutex);
mbed_official 49:77c8e4604045 344 }
mbed_official 49:77c8e4604045 345 }
mbed_official 49:77c8e4604045 346
mbed_official 49:77c8e4604045 347 #endif
mbed_official 49:77c8e4604045 348
mbed_official 49:77c8e4604045 349
mbed_official 49:77c8e4604045 350 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 351 * RTX Startup
mbed_official 49:77c8e4604045 352 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 353
mbed_official 49:77c8e4604045 354 /* Main Thread definition */
mbed_official 116:0788b1a76461 355 extern void pre_main (void);
c1728p9 123:58563e6cba1e 356 osThreadDef_t os_thread_def_main = {(os_pthread)pre_main, osPriorityNormal, 1U, 0U, NULL};
Kojto 120:4dc938e301cc 357
c1728p9 123:58563e6cba1e 358 #ifdef __CC_ARM
c1728p9 123:58563e6cba1e 359 #if defined(TARGET_NUMAKER_PFM_NUC472)
c1728p9 123:58563e6cba1e 360 extern uint32_t Image$$ARM_LIB_HEAP$$Base[];
c1728p9 123:58563e6cba1e 361 #define HEAP_START ((uint32_t) Image$$ARM_LIB_HEAP$$Base)
c1728p9 123:58563e6cba1e 362 #else
c1728p9 123:58563e6cba1e 363 extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
c1728p9 123:58563e6cba1e 364 #define HEAP_START (Image$$RW_IRAM1$$ZI$$Limit)
c1728p9 123:58563e6cba1e 365 #endif
c1728p9 123:58563e6cba1e 366 #elif defined(__GNUC__)
c1728p9 123:58563e6cba1e 367 extern uint32_t __end__[];
c1728p9 123:58563e6cba1e 368 #define HEAP_START (__end__)
c1728p9 123:58563e6cba1e 369 #elif defined(__ICCARM__)
c1728p9 123:58563e6cba1e 370 #pragma section="HEAP"
c1728p9 123:58563e6cba1e 371 #define HEAP_END (void *)__section_end("HEAP")
Kojto 120:4dc938e301cc 372 #endif
Kojto 120:4dc938e301cc 373
c1728p9 123:58563e6cba1e 374 void set_main_stack(void) {
c1728p9 123:58563e6cba1e 375 #if defined(TARGET_NUMAKER_PFM_NUC472)
c1728p9 123:58563e6cba1e 376 // Scheduler stack: OS_MAINSTKSIZE words
c1728p9 123:58563e6cba1e 377 // Main thread stack: Reserved stack size - OS_MAINSTKSIZE words
c1728p9 123:58563e6cba1e 378 os_thread_def_main.stack_pointer = (uint32_t *) FINAL_SP;
c1728p9 123:58563e6cba1e 379 os_thread_def_main.stacksize = (uint32_t) INITIAL_SP - (uint32_t) FINAL_SP - OS_MAINSTKSIZE * 4;
c1728p9 123:58563e6cba1e 380 #else
c1728p9 123:58563e6cba1e 381 #if defined(__ICCARM__)
c1728p9 123:58563e6cba1e 382 /* For IAR heap is defined .icf file */
c1728p9 123:58563e6cba1e 383 uint32_t main_stack_size = ((uint32_t)INITIAL_SP - (uint32_t)HEAP_END) - interrupt_stack_size;
c1728p9 123:58563e6cba1e 384 #else
c1728p9 123:58563e6cba1e 385 /* For ARM , uARM, or GCC_ARM , heap can grow and reach main stack */
c1728p9 123:58563e6cba1e 386 #endif
c1728p9 123:58563e6cba1e 387 // That is the bottom of the main stack block: no collision detection
c1728p9 123:58563e6cba1e 388 os_thread_def_main.stack_pointer = HEAP_START;
Kojto 118:6635230e06ba 389
c1728p9 123:58563e6cba1e 390 // Leave OS_MAINSTKSIZE words for the scheduler and interrupts
c1728p9 123:58563e6cba1e 391 os_thread_def_main.stacksize = (INITIAL_SP - (unsigned int)HEAP_START) - (OS_MAINSTKSIZE * 4);
Kojto 119:19af2d39a542 392 #endif
mbed_official 49:77c8e4604045 393 }
mbed_official 49:77c8e4604045 394
mbed_official 49:77c8e4604045 395 #if defined (__CC_ARM)
mbed_official 112:53ace74b190c 396
mbed_official 49:77c8e4604045 397 #ifdef __MICROLIB
mbed_official 116:0788b1a76461 398
mbed_official 116:0788b1a76461 399 int main(void);
mbed_official 49:77c8e4604045 400 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
mbed_official 116:0788b1a76461 401 void $Super$$__cpp_initialize__aeabi_(void);
mbed_official 116:0788b1a76461 402
mbed_official 49:77c8e4604045 403 void _main_init (void) {
mbed_official 49:77c8e4604045 404 osKernelInitialize();
mbed_official 112:53ace74b190c 405 #ifdef __MBED_CMSIS_RTOS_CM
c1728p9 123:58563e6cba1e 406 set_main_stack();
mbed_official 112:53ace74b190c 407 #endif
mbed_official 49:77c8e4604045 408 osThreadCreate(&os_thread_def_main, NULL);
mbed_official 49:77c8e4604045 409 osKernelStart();
mbed_official 49:77c8e4604045 410 for (;;);
mbed_official 49:77c8e4604045 411 }
mbed_official 116:0788b1a76461 412
mbed_official 116:0788b1a76461 413 void $Sub$$__cpp_initialize__aeabi_(void)
mbed_official 116:0788b1a76461 414 {
mbed_official 116:0788b1a76461 415 // this should invoke C++ initializers prior _main_init, we keep this empty and
mbed_official 116:0788b1a76461 416 // invoke them after _main_init (=starts RTX kernel)
mbed_official 116:0788b1a76461 417 }
mbed_official 116:0788b1a76461 418
mbed_official 116:0788b1a76461 419 void pre_main()
mbed_official 116:0788b1a76461 420 {
Kojto 119:19af2d39a542 421 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
mbed_official 116:0788b1a76461 422 $Super$$__cpp_initialize__aeabi_();
mbed_official 116:0788b1a76461 423 main();
mbed_official 116:0788b1a76461 424 }
mbed_official 116:0788b1a76461 425
mbed_official 49:77c8e4604045 426 #else
mbed_official 49:77c8e4604045 427
c1728p9 123:58563e6cba1e 428 void * armcc_heap_base;
c1728p9 123:58563e6cba1e 429 void * armcc_heap_top;
c1728p9 123:58563e6cba1e 430
Kojto 119:19af2d39a542 431 int main(void);
mbed_official 116:0788b1a76461 432
Kojto 119:19af2d39a542 433 void pre_main (void)
Kojto 119:19af2d39a542 434 {
Kojto 119:19af2d39a542 435 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
c1728p9 123:58563e6cba1e 436 __rt_lib_init((unsigned)armcc_heap_base, (unsigned)armcc_heap_top);
Kojto 119:19af2d39a542 437 main();
mbed_official 116:0788b1a76461 438 }
mbed_official 116:0788b1a76461 439
mbed_official 49:77c8e4604045 440 /* The single memory model is checking for stack collision at run time, verifing
mbed_official 49:77c8e4604045 441 that the heap pointer is underneath the stack pointer.
mbed_official 49:77c8e4604045 442
mbed_official 49:77c8e4604045 443 With the RTOS there is not only one stack above the heap, there are multiple
mbed_official 49:77c8e4604045 444 stacks and some of them are underneath the heap pointer.
mbed_official 49:77c8e4604045 445 */
mbed_official 49:77c8e4604045 446 #pragma import(__use_two_region_memory)
mbed_official 49:77c8e4604045 447
mbed_official 49:77c8e4604045 448 __asm void __rt_entry (void) {
mbed_official 49:77c8e4604045 449
mbed_official 49:77c8e4604045 450 IMPORT __user_setup_stackheap
c1728p9 123:58563e6cba1e 451 IMPORT armcc_heap_base
c1728p9 123:58563e6cba1e 452 IMPORT armcc_heap_top
c1728p9 123:58563e6cba1e 453 IMPORT _platform_post_stackheap_init
mbed_official 49:77c8e4604045 454 IMPORT os_thread_def_main
mbed_official 49:77c8e4604045 455 IMPORT osKernelInitialize
mbed_official 112:53ace74b190c 456 #ifdef __MBED_CMSIS_RTOS_CM
c1728p9 123:58563e6cba1e 457 IMPORT set_main_stack
mbed_official 112:53ace74b190c 458 #endif
mbed_official 49:77c8e4604045 459 IMPORT osKernelStart
mbed_official 49:77c8e4604045 460 IMPORT osThreadCreate
mbed_official 49:77c8e4604045 461
mbed_official 116:0788b1a76461 462 /* __user_setup_stackheap returns:
mbed_official 116:0788b1a76461 463 * - Heap base in r0 (if the program uses the heap).
mbed_official 116:0788b1a76461 464 * - Stack base in sp.
mbed_official 116:0788b1a76461 465 * - Heap limit in r2 (if the program uses the heap and uses two-region memory).
mbed_official 116:0788b1a76461 466 *
mbed_official 116:0788b1a76461 467 * More info can be found in:
mbed_official 116:0788b1a76461 468 * ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
mbed_official 116:0788b1a76461 469 */
mbed_official 49:77c8e4604045 470 BL __user_setup_stackheap
c1728p9 123:58563e6cba1e 471 LDR R3,=armcc_heap_base
c1728p9 123:58563e6cba1e 472 LDR R4,=armcc_heap_top
c1728p9 123:58563e6cba1e 473 STR R0,[R3]
c1728p9 123:58563e6cba1e 474 STR R2,[R4]
c1728p9 123:58563e6cba1e 475 BL _platform_post_stackheap_init
mbed_official 49:77c8e4604045 476 BL osKernelInitialize
mbed_official 112:53ace74b190c 477 #ifdef __MBED_CMSIS_RTOS_CM
c1728p9 123:58563e6cba1e 478 BL set_main_stack
mbed_official 112:53ace74b190c 479 #endif
mbed_official 49:77c8e4604045 480 LDR R0,=os_thread_def_main
mbed_official 49:77c8e4604045 481 MOVS R1,#0
mbed_official 49:77c8e4604045 482 BL osThreadCreate
mbed_official 49:77c8e4604045 483 BL osKernelStart
mbed_official 116:0788b1a76461 484 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 485 B .
mbed_official 49:77c8e4604045 486
mbed_official 49:77c8e4604045 487 ALIGN
mbed_official 49:77c8e4604045 488 }
mbed_official 112:53ace74b190c 489
mbed_official 49:77c8e4604045 490 #endif
mbed_official 49:77c8e4604045 491
mbed_official 49:77c8e4604045 492 #elif defined (__GNUC__)
mbed_official 49:77c8e4604045 493
Kojto 118:6635230e06ba 494 osMutexDef(malloc_mutex);
Kojto 118:6635230e06ba 495 static osMutexId malloc_mutex_id;
Kojto 118:6635230e06ba 496 osMutexDef(env_mutex);
Kojto 118:6635230e06ba 497 static osMutexId env_mutex_id;
Kojto 118:6635230e06ba 498
<> 121:3da5f554d8bf 499 extern int atexit(void (*func)(void));
mbed_official 116:0788b1a76461 500 extern void __libc_fini_array(void);
mbed_official 49:77c8e4604045 501 extern void __libc_init_array (void);
mbed_official 116:0788b1a76461 502 extern int main(int argc, char **argv);
mbed_official 49:77c8e4604045 503
mbed_official 116:0788b1a76461 504 void pre_main(void) {
Kojto 119:19af2d39a542 505 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
Kojto 118:6635230e06ba 506 malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
Kojto 118:6635230e06ba 507 env_mutex_id = osMutexCreate(osMutex(env_mutex));
mbed_official 116:0788b1a76461 508 __libc_init_array();
mbed_official 116:0788b1a76461 509 main(0, NULL);
mbed_official 49:77c8e4604045 510 }
mbed_official 49:77c8e4604045 511
Kojto 118:6635230e06ba 512 __attribute__((naked)) void software_init_hook_rtos (void) {
mbed_official 49:77c8e4604045 513 __asm (
mbed_official 49:77c8e4604045 514 "bl osKernelInitialize\n"
mbed_official 112:53ace74b190c 515 #ifdef __MBED_CMSIS_RTOS_CM
c1728p9 123:58563e6cba1e 516 "bl set_main_stack\n"
mbed_official 112:53ace74b190c 517 #endif
mbed_official 49:77c8e4604045 518 "ldr r0,=os_thread_def_main\n"
mbed_official 49:77c8e4604045 519 "movs r1,#0\n"
mbed_official 49:77c8e4604045 520 "bl osThreadCreate\n"
mbed_official 49:77c8e4604045 521 "bl osKernelStart\n"
mbed_official 116:0788b1a76461 522 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 523 "B .\n"
mbed_official 49:77c8e4604045 524 );
mbed_official 49:77c8e4604045 525 }
mbed_official 49:77c8e4604045 526
Kojto 118:6635230e06ba 527 // Opaque declaration of _reent structure
Kojto 118:6635230e06ba 528 struct _reent;
Kojto 118:6635230e06ba 529
Kojto 118:6635230e06ba 530 void __rtos_malloc_lock( struct _reent *_r )
Kojto 118:6635230e06ba 531 {
Kojto 118:6635230e06ba 532 osMutexWait(malloc_mutex_id, osWaitForever);
Kojto 118:6635230e06ba 533 }
Kojto 118:6635230e06ba 534
Kojto 118:6635230e06ba 535 void __rtos_malloc_unlock( struct _reent *_r )
Kojto 118:6635230e06ba 536 {
Kojto 118:6635230e06ba 537 osMutexRelease(malloc_mutex_id);
Kojto 118:6635230e06ba 538 }
Kojto 118:6635230e06ba 539
Kojto 118:6635230e06ba 540 void __rtos_env_lock( struct _reent *_r )
Kojto 118:6635230e06ba 541 {
Kojto 118:6635230e06ba 542 osMutexWait(env_mutex_id, osWaitForever);
Kojto 118:6635230e06ba 543 }
Kojto 118:6635230e06ba 544
Kojto 118:6635230e06ba 545 void __rtos_env_unlock( struct _reent *_r )
Kojto 118:6635230e06ba 546 {
Kojto 118:6635230e06ba 547 osMutexRelease(env_mutex_id);
Kojto 118:6635230e06ba 548 }
Kojto 118:6635230e06ba 549
mbed_official 49:77c8e4604045 550 #elif defined (__ICCARM__)
mbed_official 49:77c8e4604045 551
mbed_official 56:2b2a7cc13e28 552 extern void* __vector_table;
mbed_official 49:77c8e4604045 553 extern int __low_level_init(void);
mbed_official 49:77c8e4604045 554 extern void __iar_data_init3(void);
mbed_official 56:2b2a7cc13e28 555 extern __weak void __iar_init_core( void );
mbed_official 56:2b2a7cc13e28 556 extern __weak void __iar_init_vfp( void );
mbed_official 56:2b2a7cc13e28 557 extern void __iar_dynamic_initialization(void);
mbed_official 56:2b2a7cc13e28 558 extern void mbed_sdk_init(void);
Kojto 118:6635230e06ba 559 extern void mbed_main(void);
Kojto 118:6635230e06ba 560 extern int main(void);
mbed_official 49:77c8e4604045 561 extern void exit(int arg);
mbed_official 49:77c8e4604045 562
mbed_official 116:0788b1a76461 563 static uint8_t low_level_init_needed;
mbed_official 116:0788b1a76461 564
mbed_official 116:0788b1a76461 565 void pre_main(void) {
Kojto 119:19af2d39a542 566 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
mbed_official 116:0788b1a76461 567 if (low_level_init_needed) {
mbed_official 116:0788b1a76461 568 __iar_dynamic_initialization();
mbed_official 116:0788b1a76461 569 }
Kojto 118:6635230e06ba 570 mbed_main();
mbed_official 116:0788b1a76461 571 main();
mbed_official 116:0788b1a76461 572 }
mbed_official 116:0788b1a76461 573
mbed_official 56:2b2a7cc13e28 574 #pragma required=__vector_table
mbed_official 56:2b2a7cc13e28 575 void __iar_program_start( void )
mbed_official 56:2b2a7cc13e28 576 {
mbed_official 112:53ace74b190c 577 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 56:2b2a7cc13e28 578 __iar_init_core();
mbed_official 56:2b2a7cc13e28 579 __iar_init_vfp();
mbed_official 56:2b2a7cc13e28 580
mbed_official 116:0788b1a76461 581 uint8_t low_level_init_needed_local;
mbed_official 49:77c8e4604045 582
mbed_official 116:0788b1a76461 583 low_level_init_needed_local = __low_level_init();
mbed_official 116:0788b1a76461 584 if (low_level_init_needed_local) {
mbed_official 49:77c8e4604045 585 __iar_data_init3();
mbed_official 56:2b2a7cc13e28 586 mbed_sdk_init();
mbed_official 68:d3d0e710b443 587 }
mbed_official 116:0788b1a76461 588 /* Store in a global variable after RAM has been initialized */
mbed_official 116:0788b1a76461 589 low_level_init_needed = low_level_init_needed_local;
mbed_official 112:53ace74b190c 590 #endif
mbed_official 49:77c8e4604045 591 osKernelInitialize();
mbed_official 112:53ace74b190c 592 #ifdef __MBED_CMSIS_RTOS_CM
c1728p9 123:58563e6cba1e 593 set_main_stack();
mbed_official 112:53ace74b190c 594 #endif
mbed_official 49:77c8e4604045 595 osThreadCreate(&os_thread_def_main, NULL);
mbed_official 116:0788b1a76461 596 osKernelStart();
mbed_official 116:0788b1a76461 597 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 598 while (1);
mbed_official 49:77c8e4604045 599 }
mbed_official 49:77c8e4604045 600
mbed_official 49:77c8e4604045 601 #endif
mbed_official 49:77c8e4604045 602
mbed_official 49:77c8e4604045 603
mbed_official 49:77c8e4604045 604 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 605 * end of file
mbed_official 49:77c8e4604045 606 *---------------------------------------------------------------------------*/
c1728p9 123:58563e6cba1e 607
c1728p9 123:58563e6cba1e 608 /** @}*/