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:
Mon Jul 25 14:12:24 2016 +0100
Revision:
118:6635230e06ba
Parent:
116:0788b1a76461
Child:
119:19af2d39a542
RTOS rev118

Compatible with the mbed library v122

Changes:
- warnings about duplicated CM symbols fix
- init sequence update - allows init array to be run prior kernel start
- RTOS with OS_TIMERS=0 fix for thread id
- Thread ctor is deprecated, use start() method
- main stack fix for IAR (set via linker script)
- add TCB context pointer
- provide thread safety for toolchains (std lib locks)
- add MBED_RTOS_SINGLE_THREAD macro (sets TSKCNT to 1 and TIMERS to 0)
- nrf51, nucleo l423kc, nucleo f767zi, nucleo f446ze, efm32 support addition
- add OSObserver function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 49:77c8e4604045 1 /*----------------------------------------------------------------------------
mbed_official 112:53ace74b190c 2 * CMSIS-RTOS - RTX
mbed_official 49:77c8e4604045 3 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 4 * Name: RTX_CM_LIB.H
mbed_official 49:77c8e4604045 5 * Purpose: RTX Kernel System Configuration
mbed_official 112:53ace74b190c 6 * Rev.: V4.79
mbed_official 49:77c8e4604045 7 *----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 8 *
mbed_official 112:53ace74b190c 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbed_official 49:77c8e4604045 10 * All rights reserved.
mbed_official 49:77c8e4604045 11 * Redistribution and use in source and binary forms, with or without
mbed_official 49:77c8e4604045 12 * modification, are permitted provided that the following conditions are met:
mbed_official 49:77c8e4604045 13 * - Redistributions of source code must retain the above copyright
mbed_official 49:77c8e4604045 14 * notice, this list of conditions and the following disclaimer.
mbed_official 49:77c8e4604045 15 * - Redistributions in binary form must reproduce the above copyright
mbed_official 49:77c8e4604045 16 * notice, this list of conditions and the following disclaimer in the
mbed_official 49:77c8e4604045 17 * documentation and/or other materials provided with the distribution.
mbed_official 49:77c8e4604045 18 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 49:77c8e4604045 19 * to endorse or promote products derived from this software without
mbed_official 49:77c8e4604045 20 * specific prior written permission.
mbed_official 49:77c8e4604045 21 *
mbed_official 49:77c8e4604045 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 49:77c8e4604045 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 49:77c8e4604045 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbed_official 49:77c8e4604045 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbed_official 49:77c8e4604045 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 49:77c8e4604045 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 49:77c8e4604045 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 49:77c8e4604045 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 49:77c8e4604045 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbed_official 49:77c8e4604045 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 49:77c8e4604045 32 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 49:77c8e4604045 33 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 34 #include "mbed_error.h"
mbed_official 49:77c8e4604045 35
mbed_official 49:77c8e4604045 36 #if defined (__CC_ARM)
mbed_official 49:77c8e4604045 37 #pragma O3
mbed_official 49:77c8e4604045 38 #define __USED __attribute__((used))
mbed_official 49:77c8e4604045 39 #elif defined (__GNUC__)
mbed_official 49:77c8e4604045 40 #pragma GCC optimize ("O3")
mbed_official 49:77c8e4604045 41 #define __USED __attribute__((used))
mbed_official 49:77c8e4604045 42 #elif defined (__ICCARM__)
mbed_official 49:77c8e4604045 43 #define __USED __root
mbed_official 49:77c8e4604045 44 #endif
mbed_official 49:77c8e4604045 45
mbed_official 49:77c8e4604045 46
mbed_official 49:77c8e4604045 47 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 48 * Definitions
mbed_official 49:77c8e4604045 49 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 50
mbed_official 49:77c8e4604045 51 #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
mbed_official 49:77c8e4604045 52 #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
mbed_official 49:77c8e4604045 53
Kojto 118:6635230e06ba 54 #define OS_TCB_SIZE 60
mbed_official 49:77c8e4604045 55 #define OS_TMR_SIZE 8
mbed_official 49:77c8e4604045 56
mbed_official 49:77c8e4604045 57 typedef void *OS_ID;
mbed_official 49:77c8e4604045 58 typedef uint32_t OS_TID;
mbed_official 112:53ace74b190c 59 typedef uint32_t OS_MUT[4];
mbed_official 49:77c8e4604045 60 typedef uint32_t OS_RESULT;
mbed_official 49:77c8e4604045 61
Kojto 118:6635230e06ba 62 #if defined (__CC_ARM) && !defined (__MICROLIB)
Kojto 118:6635230e06ba 63
mbed_official 49:77c8e4604045 64 #define runtask_id() rt_tsk_self()
mbed_official 49:77c8e4604045 65 #define mutex_init(m) rt_mut_init(m)
mbed_official 112:53ace74b190c 66 #define mutex_wait(m) os_mut_wait(m,0xFFFFU)
mbed_official 49:77c8e4604045 67 #define mutex_rel(m) os_mut_release(m)
mbed_official 49:77c8e4604045 68
mbed_official 112:53ace74b190c 69 extern uint8_t os_running;
mbed_official 49:77c8e4604045 70 extern OS_TID rt_tsk_self (void);
mbed_official 49:77c8e4604045 71 extern void rt_mut_init (OS_ID mutex);
mbed_official 49:77c8e4604045 72 extern OS_RESULT rt_mut_release (OS_ID mutex);
mbed_official 49:77c8e4604045 73 extern OS_RESULT rt_mut_wait (OS_ID mutex, uint16_t timeout);
mbed_official 49:77c8e4604045 74
mbed_official 49:77c8e4604045 75 #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
mbed_official 49:77c8e4604045 76 #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex)
mbed_official 49:77c8e4604045 77
mbed_official 49:77c8e4604045 78 OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex) __svc_indirect(0);
mbed_official 49:77c8e4604045 79 OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0);
mbed_official 49:77c8e4604045 80
mbed_official 49:77c8e4604045 81 #endif
mbed_official 49:77c8e4604045 82
mbed_official 49:77c8e4604045 83
mbed_official 49:77c8e4604045 84 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 85 * Global Variables
mbed_official 49:77c8e4604045 86 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 87
mbed_official 112:53ace74b190c 88 #if (OS_TASKCNT == 0)
mbed_official 112:53ace74b190c 89 #error "Invalid number of concurrent running threads!"
mbed_official 112:53ace74b190c 90 #endif
mbed_official 112:53ace74b190c 91
mbed_official 112:53ace74b190c 92 #if (OS_PRIVCNT >= OS_TASKCNT)
mbed_official 112:53ace74b190c 93 #error "Too many threads with user-provided stack size!"
mbed_official 112:53ace74b190c 94 #endif
mbed_official 112:53ace74b190c 95
mbed_official 49:77c8e4604045 96 #if (OS_TIMERS != 0)
mbed_official 49:77c8e4604045 97 #define OS_TASK_CNT (OS_TASKCNT + 1)
mbed_official 112:53ace74b190c 98 #ifndef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 99 #define OS_PRIV_CNT (OS_PRIVCNT + 2)
mbed_official 112:53ace74b190c 100 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ))
mbed_official 112:53ace74b190c 101 #endif
mbed_official 49:77c8e4604045 102 #else
mbed_official 49:77c8e4604045 103 #define OS_TASK_CNT OS_TASKCNT
mbed_official 112:53ace74b190c 104 #ifndef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 105 #define OS_PRIV_CNT (OS_PRIVCNT + 1)
mbed_official 112:53ace74b190c 106 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE))
mbed_official 112:53ace74b190c 107 #endif
mbed_official 112:53ace74b190c 108 #endif
mbed_official 112:53ace74b190c 109
mbed_official 112:53ace74b190c 110 #ifndef OS_STKINIT
mbed_official 112:53ace74b190c 111 #define OS_STKINIT 0
mbed_official 49:77c8e4604045 112 #endif
mbed_official 49:77c8e4604045 113
mbed_official 49:77c8e4604045 114 uint16_t const os_maxtaskrun = OS_TASK_CNT;
mbed_official 112:53ace74b190c 115 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 116 uint32_t const os_stackinfo = (OS_STKINIT<<28) | (OS_STKCHECK<<24) | (OS_IDLESTKSIZE*4);
mbed_official 112:53ace74b190c 117 #else
mbed_official 112:53ace74b190c 118 uint32_t const os_stackinfo = (OS_STKINIT<<28) | (OS_STKCHECK<<24) | (OS_PRIV_CNT<<16) | (OS_STKSIZE*4);
mbed_official 112:53ace74b190c 119 #endif
mbed_official 49:77c8e4604045 120 uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT;
mbed_official 112:53ace74b190c 121 uint32_t const os_tickfreq = OS_CLOCK;
mbed_official 112:53ace74b190c 122 uint16_t const os_tickus_i = OS_CLOCK/1000000;
mbed_official 112:53ace74b190c 123 uint16_t const os_tickus_f = (((uint64_t)(OS_CLOCK-1000000*(OS_CLOCK/1000000)))<<16)/1000000;
mbed_official 49:77c8e4604045 124 uint32_t const os_trv = OS_TRV;
Kojto 118:6635230e06ba 125 #if defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)
Kojto 118:6635230e06ba 126 uint8_t const os_flags = 0;
Kojto 118:6635230e06ba 127 #else /* defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) */
mbed_official 49:77c8e4604045 128 uint8_t const os_flags = OS_RUNPRIV;
Kojto 118:6635230e06ba 129 #endif /* defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED) */
mbed_official 49:77c8e4604045 130
mbed_official 49:77c8e4604045 131 /* Export following defines to uVision debugger. */
mbed_official 112:53ace74b190c 132 __USED uint32_t const CMSIS_RTOS_API_Version = osCMSIS;
mbed_official 112:53ace74b190c 133 __USED uint32_t const CMSIS_RTOS_RTX_Version = osCMSIS_RTX;
mbed_official 49:77c8e4604045 134 __USED uint32_t const os_clockrate = OS_TICK;
mbed_official 112:53ace74b190c 135 __USED uint32_t const os_timernum = 0U;
mbed_official 112:53ace74b190c 136
mbed_official 112:53ace74b190c 137 /* Memory pool for TCB allocation */
mbed_official 112:53ace74b190c 138 _declare_box (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT);
mbed_official 112:53ace74b190c 139 uint16_t const mp_tcb_size = sizeof(mp_tcb);
mbed_official 49:77c8e4604045 140
mbed_official 112:53ace74b190c 141 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 112:53ace74b190c 142 /* Memory pool for os_idle_demon stack allocation. */
mbed_official 112:53ace74b190c 143 _declare_box8 (mp_stk, OS_IDLESTKSIZE*4, 1);
mbed_official 112:53ace74b190c 144 uint32_t const mp_stk_size = sizeof(mp_stk);
mbed_official 112:53ace74b190c 145 #else
mbed_official 112:53ace74b190c 146 /* Memory pool for System stack allocation (+os_idle_demon). */
mbed_official 112:53ace74b190c 147 _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1);
mbed_official 112:53ace74b190c 148 uint32_t const mp_stk_size = sizeof(mp_stk);
mbed_official 112:53ace74b190c 149
mbed_official 112:53ace74b190c 150 /* Memory pool for user specified stack allocation (+main, +timer) */
mbed_official 112:53ace74b190c 151 uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
mbed_official 112:53ace74b190c 152 uint32_t const os_stack_sz = sizeof(os_stack_mem);
mbed_official 112:53ace74b190c 153 #endif
mbed_official 49:77c8e4604045 154
mbed_official 49:77c8e4604045 155 #ifndef OS_FIFOSZ
mbed_official 49:77c8e4604045 156 #define OS_FIFOSZ 16
mbed_official 49:77c8e4604045 157 #endif
mbed_official 49:77c8e4604045 158
mbed_official 49:77c8e4604045 159 /* Fifo Queue buffer for ISR requests.*/
mbed_official 49:77c8e4604045 160 uint32_t os_fifo[OS_FIFOSZ*2+1];
mbed_official 49:77c8e4604045 161 uint8_t const os_fifo_size = OS_FIFOSZ;
mbed_official 49:77c8e4604045 162
mbed_official 49:77c8e4604045 163 /* An array of Active task pointers. */
mbed_official 49:77c8e4604045 164 void *os_active_TCB[OS_TASK_CNT];
mbed_official 49:77c8e4604045 165
mbed_official 49:77c8e4604045 166 /* User Timers Resources */
mbed_official 49:77c8e4604045 167 #if (OS_TIMERS != 0)
mbed_official 49:77c8e4604045 168 extern void osTimerThread (void const *argument);
mbed_official 112:53ace74b190c 169 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 49:77c8e4604045 170 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ);
mbed_official 112:53ace74b190c 171 #else
mbed_official 112:53ace74b190c 172 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ);
mbed_official 112:53ace74b190c 173 #endif
mbed_official 49:77c8e4604045 174 osThreadId osThreadId_osTimerThread;
mbed_official 49:77c8e4604045 175 osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
mbed_official 49:77c8e4604045 176 osMessageQId osMessageQId_osTimerMessageQ;
mbed_official 49:77c8e4604045 177 #else
mbed_official 49:77c8e4604045 178 osThreadDef_t os_thread_def_osTimerThread = { NULL };
mbed_official 49:77c8e4604045 179 osThreadId osThreadId_osTimerThread;
mbed_official 112:53ace74b190c 180 osMessageQDef(osTimerMessageQ, 0U, void *);
mbed_official 49:77c8e4604045 181 osMessageQId osMessageQId_osTimerMessageQ;
mbed_official 49:77c8e4604045 182 #endif
mbed_official 49:77c8e4604045 183
mbed_official 112:53ace74b190c 184 /* Legacy RTX User Timers not used */
Kojto 118:6635230e06ba 185 uint32_t os_tmr = 0U;
mbed_official 112:53ace74b190c 186 uint32_t const *m_tmr = NULL;
mbed_official 112:53ace74b190c 187 uint16_t const mp_tmr_size = 0U;
mbed_official 112:53ace74b190c 188
mbed_official 112:53ace74b190c 189 #if defined (__CC_ARM) && !defined (__MICROLIB)
mbed_official 112:53ace74b190c 190 /* A memory space for arm standard library. */
mbed_official 112:53ace74b190c 191 static uint32_t std_libspace[OS_TASK_CNT][96/4];
mbed_official 112:53ace74b190c 192 static OS_MUT std_libmutex[OS_MUTEXCNT];
mbed_official 112:53ace74b190c 193 static uint32_t nr_mutex;
mbed_official 112:53ace74b190c 194 extern void *__libspace_start;
mbed_official 112:53ace74b190c 195 #endif
mbed_official 112:53ace74b190c 196
Kojto 118:6635230e06ba 197 #if defined (__ICCARM__)
Kojto 118:6635230e06ba 198 static osMutexId std_mutex_id_sys[_MAX_LOCK] = {0};
Kojto 118:6635230e06ba 199 static OS_MUT std_mutex_sys[_MAX_LOCK] = {0};
Kojto 118:6635230e06ba 200 #define _FOPEN_MAX 10
Kojto 118:6635230e06ba 201 static osMutexId std_mutex_id_file[_FOPEN_MAX] = {0};
Kojto 118:6635230e06ba 202 static OS_MUT std_mutex_file[_FOPEN_MAX] = {0};
Kojto 118:6635230e06ba 203 void __iar_system_Mtxinit(__iar_Rmtx *mutex) /* Initialize a system lock */
Kojto 118:6635230e06ba 204 {
Kojto 118:6635230e06ba 205 osMutexDef_t def;
Kojto 118:6635230e06ba 206 uint32_t index;
Kojto 118:6635230e06ba 207 for (index = 0; index < _MAX_LOCK; index++) {
Kojto 118:6635230e06ba 208 if (0 == std_mutex_id_sys[index]) {
Kojto 118:6635230e06ba 209 def.mutex = &std_mutex_sys[index];
Kojto 118:6635230e06ba 210 std_mutex_id_sys[index] = osMutexCreate(&def);
Kojto 118:6635230e06ba 211 *mutex = (__iar_Rmtx*)&std_mutex_id_sys[index];
Kojto 118:6635230e06ba 212 return;
Kojto 118:6635230e06ba 213 }
Kojto 118:6635230e06ba 214 }
Kojto 118:6635230e06ba 215 // This should never happen
Kojto 118:6635230e06ba 216 error("Not enough mutexes\n");
Kojto 118:6635230e06ba 217 }
Kojto 118:6635230e06ba 218
Kojto 118:6635230e06ba 219 void __iar_system_Mtxdst(__iar_Rmtx *mutex)/*Destroy a system lock */
Kojto 118:6635230e06ba 220 {
Kojto 118:6635230e06ba 221 osMutexDelete(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 222 *mutex = 0;
Kojto 118:6635230e06ba 223 }
Kojto 118:6635230e06ba 224
Kojto 118:6635230e06ba 225 void __iar_system_Mtxlock(__iar_Rmtx *mutex) /* Lock a system lock */
Kojto 118:6635230e06ba 226 {
Kojto 118:6635230e06ba 227 osMutexWait(*(osMutexId*)*mutex, osWaitForever);
Kojto 118:6635230e06ba 228 }
Kojto 118:6635230e06ba 229
Kojto 118:6635230e06ba 230 void __iar_system_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a system lock */
Kojto 118:6635230e06ba 231 {
Kojto 118:6635230e06ba 232 osMutexRelease(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 233 }
Kojto 118:6635230e06ba 234
Kojto 118:6635230e06ba 235 void __iar_file_Mtxinit(__iar_Rmtx *mutex)/*Initialize a file lock */
Kojto 118:6635230e06ba 236 {
Kojto 118:6635230e06ba 237 osMutexDef_t def;
Kojto 118:6635230e06ba 238 uint32_t index;
Kojto 118:6635230e06ba 239 for (index = 0; index < _FOPEN_MAX; index++) {
Kojto 118:6635230e06ba 240 if (0 == std_mutex_id_file[index]) {
Kojto 118:6635230e06ba 241 def.mutex = &std_mutex_file[index];
Kojto 118:6635230e06ba 242 std_mutex_id_file[index] = osMutexCreate(&def);
Kojto 118:6635230e06ba 243 *mutex = (__iar_Rmtx*)&std_mutex_id_file[index];
Kojto 118:6635230e06ba 244 return;
Kojto 118:6635230e06ba 245 }
Kojto 118:6635230e06ba 246 }
Kojto 118:6635230e06ba 247 // The variable _FOPEN_MAX needs to be increased
Kojto 118:6635230e06ba 248 error("Not enough mutexes\n");
Kojto 118:6635230e06ba 249 }
Kojto 118:6635230e06ba 250
Kojto 118:6635230e06ba 251 void __iar_file_Mtxdst(__iar_Rmtx *mutex) /* Destroy a file lock */
Kojto 118:6635230e06ba 252 {
Kojto 118:6635230e06ba 253 osMutexDelete(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 254 *mutex = 0;
Kojto 118:6635230e06ba 255 }
Kojto 118:6635230e06ba 256
Kojto 118:6635230e06ba 257 void __iar_file_Mtxlock(__iar_Rmtx *mutex) /* Lock a file lock */
Kojto 118:6635230e06ba 258 {
Kojto 118:6635230e06ba 259 osMutexWait(*(osMutexId*)*mutex, osWaitForever);
Kojto 118:6635230e06ba 260 }
Kojto 118:6635230e06ba 261
Kojto 118:6635230e06ba 262 void __iar_file_Mtxunlock(__iar_Rmtx *mutex) /* Unlock a file lock */
Kojto 118:6635230e06ba 263 {
Kojto 118:6635230e06ba 264 osMutexRelease(*(osMutexId*)*mutex);
Kojto 118:6635230e06ba 265 }
Kojto 118:6635230e06ba 266
Kojto 118:6635230e06ba 267 #endif
mbed_official 49:77c8e4604045 268
mbed_official 49:77c8e4604045 269 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 270 * RTX Optimizations (empty functions)
mbed_official 49:77c8e4604045 271 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 272
mbed_official 49:77c8e4604045 273 #if OS_ROBIN == 0
mbed_official 49:77c8e4604045 274 void rt_init_robin (void) {;}
mbed_official 49:77c8e4604045 275 void rt_chk_robin (void) {;}
mbed_official 49:77c8e4604045 276 #endif
mbed_official 49:77c8e4604045 277
mbed_official 49:77c8e4604045 278 #if OS_STKCHECK == 0
mbed_official 49:77c8e4604045 279 void rt_stk_check (void) {;}
mbed_official 49:77c8e4604045 280 #endif
mbed_official 49:77c8e4604045 281
mbed_official 49:77c8e4604045 282
mbed_official 49:77c8e4604045 283 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 284 * Standard Library multithreading interface
mbed_official 49:77c8e4604045 285 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 286
mbed_official 49:77c8e4604045 287 #if defined (__CC_ARM) && !defined (__MICROLIB)
mbed_official 112:53ace74b190c 288
mbed_official 112:53ace74b190c 289 /*--------------------------- __user_perthread_libspace ---------------------*/
mbed_official 112:53ace74b190c 290
mbed_official 112:53ace74b190c 291 void *__user_perthread_libspace (void) {
mbed_official 112:53ace74b190c 292 /* Provide a separate libspace for each task. */
mbed_official 112:53ace74b190c 293 uint32_t idx;
mbed_official 49:77c8e4604045 294
mbed_official 112:53ace74b190c 295 idx = (os_running != 0U) ? runtask_id () : 0U;
mbed_official 112:53ace74b190c 296 if (idx == 0U) {
mbed_official 112:53ace74b190c 297 /* RTX not running yet. */
mbed_official 112:53ace74b190c 298 return (&__libspace_start);
mbed_official 112:53ace74b190c 299 }
mbed_official 112:53ace74b190c 300 return ((void *)&std_libspace[idx-1]);
mbed_official 112:53ace74b190c 301 }
mbed_official 112:53ace74b190c 302
mbed_official 112:53ace74b190c 303 /*--------------------------- _mutex_initialize -----------------------------*/
mbed_official 49:77c8e4604045 304
mbed_official 49:77c8e4604045 305 int _mutex_initialize (OS_ID *mutex) {
mbed_official 49:77c8e4604045 306 /* Allocate and initialize a system mutex. */
mbed_official 49:77c8e4604045 307
mbed_official 49:77c8e4604045 308 if (nr_mutex >= OS_MUTEXCNT) {
mbed_official 49:77c8e4604045 309 /* If you are here, you need to increase the number OS_MUTEXCNT. */
mbed_official 49:77c8e4604045 310 error("Not enough stdlib mutexes\n");
mbed_official 49:77c8e4604045 311 }
mbed_official 49:77c8e4604045 312 *mutex = &std_libmutex[nr_mutex++];
mbed_official 49:77c8e4604045 313 mutex_init (*mutex);
mbed_official 49:77c8e4604045 314 return (1);
mbed_official 49:77c8e4604045 315 }
mbed_official 49:77c8e4604045 316
mbed_official 49:77c8e4604045 317
mbed_official 49:77c8e4604045 318 /*--------------------------- _mutex_acquire --------------------------------*/
mbed_official 49:77c8e4604045 319
mbed_official 49:77c8e4604045 320 __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
mbed_official 49:77c8e4604045 321 /* Acquire a system mutex, lock stdlib resources. */
mbed_official 112:53ace74b190c 322 if (os_running) {
mbed_official 49:77c8e4604045 323 /* RTX running, acquire a mutex. */
mbed_official 49:77c8e4604045 324 mutex_wait (*mutex);
mbed_official 49:77c8e4604045 325 }
mbed_official 49:77c8e4604045 326 }
mbed_official 49:77c8e4604045 327
mbed_official 49:77c8e4604045 328
mbed_official 49:77c8e4604045 329 /*--------------------------- _mutex_release --------------------------------*/
mbed_official 49:77c8e4604045 330
mbed_official 49:77c8e4604045 331 __attribute__((used)) void _mutex_release (OS_ID *mutex) {
mbed_official 49:77c8e4604045 332 /* Release a system mutex, unlock stdlib resources. */
mbed_official 112:53ace74b190c 333 if (os_running) {
mbed_official 49:77c8e4604045 334 /* RTX running, release a mutex. */
mbed_official 49:77c8e4604045 335 mutex_rel (*mutex);
mbed_official 49:77c8e4604045 336 }
mbed_official 49:77c8e4604045 337 }
mbed_official 49:77c8e4604045 338
mbed_official 49:77c8e4604045 339 #endif
mbed_official 49:77c8e4604045 340
mbed_official 49:77c8e4604045 341
mbed_official 49:77c8e4604045 342 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 343 * RTX Startup
mbed_official 49:77c8e4604045 344 *---------------------------------------------------------------------------*/
mbed_official 49:77c8e4604045 345
mbed_official 49:77c8e4604045 346 /* Main Thread definition */
mbed_official 116:0788b1a76461 347 extern void pre_main (void);
mbed_official 116:0788b1a76461 348 osThreadDef_t os_thread_def_main = {(os_pthread)pre_main, osPriorityNormal, 1U, 0U, NULL};
mbed_official 49:77c8e4604045 349
mbed_official 49:77c8e4604045 350 // This define should be probably moved to the CMSIS layer
mbed_official 49:77c8e4604045 351 #if defined(TARGET_LPC1768)
mbed_official 49:77c8e4604045 352 #define INITIAL_SP (0x10008000UL)
mbed_official 49:77c8e4604045 353
mbed_official 49:77c8e4604045 354 #elif defined(TARGET_LPC11U24)
mbed_official 49:77c8e4604045 355 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 356
mbed_official 49:77c8e4604045 357 #elif defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPCCAPPUCCINO)
mbed_official 49:77c8e4604045 358 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 359
mbed_official 49:77c8e4604045 360 #elif defined(TARGET_LPC1114)
mbed_official 49:77c8e4604045 361 #define INITIAL_SP (0x10001000UL)
mbed_official 49:77c8e4604045 362
mbed_official 49:77c8e4604045 363 #elif defined(TARGET_LPC812)
mbed_official 49:77c8e4604045 364 #define INITIAL_SP (0x10001000UL)
mbed_official 49:77c8e4604045 365
mbed_official 81:e45e4ac7c3c8 366 #elif defined(TARGET_LPC824) || defined(TARGET_SSCI824)
mbed_official 49:77c8e4604045 367 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 368
mbed_official 49:77c8e4604045 369 #elif defined(TARGET_KL25Z)
mbed_official 49:77c8e4604045 370 #define INITIAL_SP (0x20003000UL)
mbed_official 49:77c8e4604045 371
mbed_official 88:a21475017ae2 372 #elif defined(TARGET_KL26Z)
mbed_official 88:a21475017ae2 373 #define INITIAL_SP (0x20003000UL)
mbed_official 88:a21475017ae2 374
mbed_official 111:162b12aea5f2 375 #elif defined(TARGET_KL27Z)
mbed_official 111:162b12aea5f2 376 #define INITIAL_SP (0x20003000UL)
mbed_official 111:162b12aea5f2 377
mbed_official 49:77c8e4604045 378 #elif defined(TARGET_K64F)
Kojto 118:6635230e06ba 379 #if defined(FEATURE_UVISOR) && defined(TARGET_UVISOR_SUPPORTED)
Kojto 118:6635230e06ba 380 extern uint32_t __StackTop[];
Kojto 118:6635230e06ba 381 #define INITIAL_SP (__StackTop)
Kojto 118:6635230e06ba 382 #else
mbed_official 49:77c8e4604045 383 #define INITIAL_SP (0x20030000UL)
Kojto 118:6635230e06ba 384 #endif
mbed_official 49:77c8e4604045 385
mbed_official 52:02f5cf381388 386 #elif defined(TARGET_K22F)
mbed_official 52:02f5cf381388 387 #define INITIAL_SP (0x20010000UL)
mbed_official 52:02f5cf381388 388
mbed_official 49:77c8e4604045 389 #elif defined(TARGET_KL46Z)
mbed_official 49:77c8e4604045 390 #define INITIAL_SP (0x20006000UL)
mbed_official 49:77c8e4604045 391
mbed_official 49:77c8e4604045 392 #elif defined(TARGET_KL43Z)
mbed_official 49:77c8e4604045 393 #define INITIAL_SP (0x20006000UL)
mbed_official 49:77c8e4604045 394
mbed_official 49:77c8e4604045 395 #elif defined(TARGET_KL05Z)
mbed_official 49:77c8e4604045 396 #define INITIAL_SP (0x20000C00UL)
mbed_official 49:77c8e4604045 397
mbed_official 64:ab4200083b07 398 #elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
mbed_official 49:77c8e4604045 399 #define INITIAL_SP (0x10010000UL)
mbed_official 49:77c8e4604045 400
mbed_official 63:5448826aa700 401 #elif defined(TARGET_LPC4330)
mbed_official 63:5448826aa700 402 #define INITIAL_SP (0x10008000UL)
mbed_official 63:5448826aa700 403
mbed_official 49:77c8e4604045 404 #elif defined(TARGET_LPC4337)
mbed_official 49:77c8e4604045 405 #define INITIAL_SP (0x10008000UL)
mbed_official 49:77c8e4604045 406
mbed_official 49:77c8e4604045 407 #elif defined(TARGET_LPC1347)
mbed_official 49:77c8e4604045 408 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 409
mbed_official 49:77c8e4604045 410 #elif defined(TARGET_STM32F100RB) || defined(TARGET_STM32F051R8)
mbed_official 49:77c8e4604045 411 #define INITIAL_SP (0x20002000UL)
mbed_official 49:77c8e4604045 412
mbed_official 49:77c8e4604045 413 #elif defined(TARGET_DISCO_F303VC)
mbed_official 49:77c8e4604045 414 #define INITIAL_SP (0x2000A000UL)
mbed_official 49:77c8e4604045 415
mbed_official 49:77c8e4604045 416 #elif defined(TARGET_STM32F407) || defined(TARGET_F407VG)
mbed_official 49:77c8e4604045 417 #define INITIAL_SP (0x20020000UL)
mbed_official 49:77c8e4604045 418
mbed_official 49:77c8e4604045 419 #elif defined(TARGET_STM32F401RE)
mbed_official 49:77c8e4604045 420 #define INITIAL_SP (0x20018000UL)
mbed_official 49:77c8e4604045 421
mbed_official 49:77c8e4604045 422 #elif defined(TARGET_LPC1549)
mbed_official 49:77c8e4604045 423 #define INITIAL_SP (0x02009000UL)
mbed_official 49:77c8e4604045 424
mbed_official 49:77c8e4604045 425 #elif defined(TARGET_LPC11U68)
mbed_official 89:5aed8bae1001 426 #define INITIAL_SP (0x10008000UL)
mbed_official 49:77c8e4604045 427
mbed_official 49:77c8e4604045 428 #elif defined(TARGET_STM32F411RE)
mbed_official 49:77c8e4604045 429 #define INITIAL_SP (0x20020000UL)
mbed_official 49:77c8e4604045 430
mbed_official 97:6c35e082773a 431 #elif defined(TARGET_STM32F410RB)
mbed_official 97:6c35e082773a 432 #define INITIAL_SP (0x20008000UL)
mbed_official 97:6c35e082773a 433
mbed_official 106:dfc27975e193 434 #elif defined(TARGET_STM32F103RB) || defined(TARGET_STM32L073RZ)
mbed_official 49:77c8e4604045 435 #define INITIAL_SP (0x20005000UL)
mbed_official 49:77c8e4604045 436
mbed_official 49:77c8e4604045 437 #elif defined(TARGET_STM32F302R8)
mbed_official 49:77c8e4604045 438 #define INITIAL_SP (0x20004000UL)
mbed_official 49:77c8e4604045 439
mbed_official 49:77c8e4604045 440 #elif defined(TARGET_STM32F334R8)
mbed_official 49:77c8e4604045 441 #define INITIAL_SP (0x20003000UL)
mbed_official 49:77c8e4604045 442
mbed_official 49:77c8e4604045 443 #elif defined(TARGET_STM32F334C8)
mbed_official 49:77c8e4604045 444 #define INITIAL_SP (0x20003000UL)
mbed_official 49:77c8e4604045 445
mbed_official 50:64a5202c3676 446 #elif defined(TARGET_STM32F405RG)
mbed_official 50:64a5202c3676 447 #define INITIAL_SP (0x20020000UL)
mbed_official 50:64a5202c3676 448
mbed_official 57:430de2831ec7 449 #elif defined(TARGET_STM32F429ZI)
mbed_official 57:430de2831ec7 450 #define INITIAL_SP (0x20030000UL)
mbed_official 57:430de2831ec7 451
mbed_official 108:ac4f3830f9ff 452 #elif defined(TARGET_STM32L031K6) || defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)
mbed_official 59:28712e303960 453 #define INITIAL_SP (0x20002000UL)
mbed_official 59:28712e303960 454
mbed_official 59:28712e303960 455 #elif defined(TARGET_STM32F072RB)
mbed_official 59:28712e303960 456 #define INITIAL_SP (0x20004000UL)
mbed_official 59:28712e303960 457
mbed_official 59:28712e303960 458 #elif defined(TARGET_STM32F091RC)
mbed_official 59:28712e303960 459 #define INITIAL_SP (0x20008000UL)
mbed_official 59:28712e303960 460
mbed_official 60:f4d3d8971bc3 461 #elif defined(TARGET_STM32F401VC)
mbed_official 60:f4d3d8971bc3 462 #define INITIAL_SP (0x20010000UL)
mbed_official 60:f4d3d8971bc3 463
mbed_official 68:d3d0e710b443 464 #elif defined(TARGET_STM32F303RE)
mbed_official 68:d3d0e710b443 465 #define INITIAL_SP (0x20010000UL)
mbed_official 68:d3d0e710b443 466
mbed_official 95:12552ef4e980 467 #elif defined(TARGET_STM32F303K8)
mbed_official 95:12552ef4e980 468 #define INITIAL_SP (0x20003000UL)
mbed_official 95:12552ef4e980 469
mbed_official 105:b4c5542476ba 470 #elif (defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG))
mbed_official 98:c825593ece39 471 #define INITIAL_SP (0x20050000UL)
mbed_official 98:c825593ece39 472
mbed_official 73:34292fba723c 473 #elif defined(TARGET_MAX32610) || defined(TARGET_MAX32600)
mbed_official 71:570e569a5b59 474 #define INITIAL_SP (0x20008000UL)
mbed_official 71:570e569a5b59 475
mbed_official 74:899aee34da6a 476 #elif defined(TARGET_TEENSY3_1)
mbed_official 74:899aee34da6a 477 #define INITIAL_SP (0x20008000UL)
mbed_official 74:899aee34da6a 478
mbed_official 77:3516160e016b 479 #elif defined(TARGET_STM32L152RE)
mbed_official 77:3516160e016b 480 #define INITIAL_SP (0x20014000UL)
mbed_official 77:3516160e016b 481
mbed_official 104:07314541bd12 482 #elif defined(TARGET_NZ32_SC151)
mbed_official 78:2db19f47c2ba 483 #define INITIAL_SP (0x20008000UL)
mbed_official 78:2db19f47c2ba 484
Kojto 118:6635230e06ba 485 #elif defined(TARGET_STM32F446RE) || defined(TARGET_STM32F446VE) || defined(TARGET_STM32F446ZE)
mbed_official 79:c586ffeebfb4 486 #define INITIAL_SP (0x20020000UL)
mbed_official 79:c586ffeebfb4 487
mbed_official 87:e695cd34556b 488 #elif defined(TARGET_STM32F070RB) || defined(TARGET_STM32F030R8)
mbed_official 87:e695cd34556b 489 #define INITIAL_SP (0x20002000UL)
mbed_official 87:e695cd34556b 490
Kojto 118:6635230e06ba 491 #elif defined(TARGET_STM32L432KC)
Kojto 118:6635230e06ba 492 #define INITIAL_SP (0x2000C000UL)
Kojto 118:6635230e06ba 493
mbed_official 91:9d001ed5feec 494 #elif defined(TARGET_STM32L476VG)
mbed_official 91:9d001ed5feec 495 #define INITIAL_SP (0x20018000UL)
mbed_official 91:9d001ed5feec 496
mbed_official 91:9d001ed5feec 497 #elif defined(TARGET_STM32L476RG)
mbed_official 91:9d001ed5feec 498 #define INITIAL_SP (0x20018000UL)
mbed_official 91:9d001ed5feec 499
mbed_official 96:6d90423c236e 500 #elif defined(TARGET_STM32F469NI)
mbed_official 96:6d90423c236e 501 #define INITIAL_SP (0x20050000UL)
mbed_official 96:6d90423c236e 502
mbed_official 99:79d2f32f753f 503 #elif defined(TARGET_STM32L152RC)
mbed_official 99:79d2f32f753f 504 #define INITIAL_SP (0x20008000UL)
mbed_official 99:79d2f32f753f 505
Kojto 118:6635230e06ba 506 #elif defined(TARGET_EFM32GG_STK3700) || defined(TARGET_BEETLE)
Kojto 118:6635230e06ba 507 #define INITIAL_SP (0x20020000UL)
Kojto 118:6635230e06ba 508
Kojto 118:6635230e06ba 509 #elif defined(TARGET_EFM32HG_STK3400)
Kojto 118:6635230e06ba 510 #define INITIAL_SP (0x20002000UL)
Kojto 118:6635230e06ba 511
Kojto 118:6635230e06ba 512 #elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)
Kojto 118:6635230e06ba 513 #define INITIAL_SP (0x20008000UL)
Kojto 118:6635230e06ba 514
Kojto 118:6635230e06ba 515 #elif defined(TARGET_MCU_NORDIC_32K)
Kojto 118:6635230e06ba 516 #define INITIAL_SP (0x20008000UL)
Kojto 118:6635230e06ba 517
Kojto 118:6635230e06ba 518 #elif defined(TARGET_MCU_NORDIC_16K)
Kojto 118:6635230e06ba 519 #define INITIAL_SP (0x20004000UL)
Kojto 118:6635230e06ba 520
Kojto 118:6635230e06ba 521 #elif (defined(TARGET_STM32F767ZI))
Kojto 118:6635230e06ba 522 #define INITIAL_SP (0x20080000UL)
mbed_official 99:79d2f32f753f 523
mbed_official 49:77c8e4604045 524 #else
mbed_official 49:77c8e4604045 525 #error "no target defined"
mbed_official 49:77c8e4604045 526
mbed_official 49:77c8e4604045 527 #endif
mbed_official 49:77c8e4604045 528
mbed_official 49:77c8e4604045 529 #ifdef __CC_ARM
mbed_official 62:444020d511f5 530 extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
mbed_official 49:77c8e4604045 531 #define HEAP_START (Image$$RW_IRAM1$$ZI$$Limit)
mbed_official 49:77c8e4604045 532 #elif defined(__GNUC__)
mbed_official 62:444020d511f5 533 extern uint32_t __end__[];
mbed_official 49:77c8e4604045 534 #define HEAP_START (__end__)
mbed_official 53:c35dab33c427 535 #elif defined(__ICCARM__)
mbed_official 53:c35dab33c427 536 #pragma section="HEAP"
Kojto 118:6635230e06ba 537 #define HEAP_END (void *)__section_end("HEAP")
mbed_official 49:77c8e4604045 538 #endif
mbed_official 49:77c8e4604045 539
mbed_official 49:77c8e4604045 540 void set_main_stack(void) {
Kojto 118:6635230e06ba 541 uint32_t interrupt_stack_size = ((uint32_t)OS_MAINSTKSIZE * 4);
Kojto 118:6635230e06ba 542 #if defined(__ICCARM__)
Kojto 118:6635230e06ba 543 /* For IAR heap is defined .icf file */
Kojto 118:6635230e06ba 544 uint32_t main_stack_size = ((uint32_t)INITIAL_SP - (uint32_t)HEAP_END) - interrupt_stack_size;
Kojto 118:6635230e06ba 545 #else
Kojto 118:6635230e06ba 546 /* For ARM , uARM, or GCC_ARM , heap can grow and reach main stack */
Kojto 118:6635230e06ba 547 uint32_t heap_plus_stack_size = ((uint32_t)INITIAL_SP - (uint32_t)HEAP_START) - interrupt_stack_size;
Kojto 118:6635230e06ba 548 // Main thread's stack is 1/4 of the heap
Kojto 118:6635230e06ba 549 uint32_t main_stack_size = heap_plus_stack_size/4;
Kojto 118:6635230e06ba 550 #endif
Kojto 118:6635230e06ba 551 // The main thread must be 4 byte aligned
Kojto 118:6635230e06ba 552 uint32_t main_stack_start = ((uint32_t)INITIAL_SP - interrupt_stack_size - main_stack_size) & ~0x7;
Kojto 118:6635230e06ba 553
mbed_official 49:77c8e4604045 554 // That is the bottom of the main stack block: no collision detection
Kojto 118:6635230e06ba 555 os_thread_def_main.stack_pointer = (uint32_t*)main_stack_start;
mbed_official 49:77c8e4604045 556
mbed_official 112:53ace74b190c 557 // Leave OS_MAINSTKSIZE words for the scheduler and interrupts
Kojto 118:6635230e06ba 558 os_thread_def_main.stacksize = main_stack_size;
mbed_official 49:77c8e4604045 559 }
mbed_official 49:77c8e4604045 560
mbed_official 49:77c8e4604045 561 #if defined (__CC_ARM)
mbed_official 112:53ace74b190c 562
mbed_official 49:77c8e4604045 563 #ifdef __MICROLIB
mbed_official 116:0788b1a76461 564
mbed_official 116:0788b1a76461 565 int main(void);
mbed_official 49:77c8e4604045 566 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
mbed_official 116:0788b1a76461 567 void $Super$$__cpp_initialize__aeabi_(void);
mbed_official 116:0788b1a76461 568
mbed_official 49:77c8e4604045 569 void _main_init (void) {
mbed_official 49:77c8e4604045 570 osKernelInitialize();
mbed_official 112:53ace74b190c 571 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 49:77c8e4604045 572 set_main_stack();
mbed_official 112:53ace74b190c 573 #endif
mbed_official 49:77c8e4604045 574 osThreadCreate(&os_thread_def_main, NULL);
mbed_official 49:77c8e4604045 575 osKernelStart();
mbed_official 49:77c8e4604045 576 for (;;);
mbed_official 49:77c8e4604045 577 }
mbed_official 116:0788b1a76461 578
mbed_official 116:0788b1a76461 579 void $Sub$$__cpp_initialize__aeabi_(void)
mbed_official 116:0788b1a76461 580 {
mbed_official 116:0788b1a76461 581 // this should invoke C++ initializers prior _main_init, we keep this empty and
mbed_official 116:0788b1a76461 582 // invoke them after _main_init (=starts RTX kernel)
mbed_official 116:0788b1a76461 583 }
mbed_official 116:0788b1a76461 584
mbed_official 116:0788b1a76461 585 void pre_main()
mbed_official 116:0788b1a76461 586 {
mbed_official 116:0788b1a76461 587 $Super$$__cpp_initialize__aeabi_();
mbed_official 116:0788b1a76461 588 main();
mbed_official 116:0788b1a76461 589 }
mbed_official 116:0788b1a76461 590
mbed_official 49:77c8e4604045 591 #else
mbed_official 49:77c8e4604045 592
mbed_official 116:0788b1a76461 593 void * armcc_heap_base;
mbed_official 116:0788b1a76461 594 void * armcc_heap_top;
mbed_official 116:0788b1a76461 595
mbed_official 116:0788b1a76461 596 __asm void pre_main (void)
mbed_official 116:0788b1a76461 597 {
mbed_official 116:0788b1a76461 598 IMPORT __rt_lib_init
mbed_official 116:0788b1a76461 599 IMPORT main
mbed_official 116:0788b1a76461 600 IMPORT armcc_heap_base
mbed_official 116:0788b1a76461 601 IMPORT armcc_heap_top
mbed_official 116:0788b1a76461 602
mbed_official 116:0788b1a76461 603 LDR R0,=armcc_heap_base
mbed_official 116:0788b1a76461 604 LDR R1,=armcc_heap_top
mbed_official 116:0788b1a76461 605 LDR R0,[R0]
mbed_official 116:0788b1a76461 606 LDR R1,[R1]
mbed_official 116:0788b1a76461 607 /* Save link register (keep 8 byte alignment with dummy R4) */
mbed_official 116:0788b1a76461 608 PUSH {R4, LR}
mbed_official 116:0788b1a76461 609 BL __rt_lib_init
mbed_official 116:0788b1a76461 610 BL main
mbed_official 116:0788b1a76461 611 /* Return to the thread destroy function.
mbed_official 116:0788b1a76461 612 */
mbed_official 116:0788b1a76461 613 POP {R4, PC}
mbed_official 116:0788b1a76461 614 ALIGN
mbed_official 116:0788b1a76461 615 }
mbed_official 116:0788b1a76461 616
mbed_official 49:77c8e4604045 617 /* The single memory model is checking for stack collision at run time, verifing
mbed_official 49:77c8e4604045 618 that the heap pointer is underneath the stack pointer.
mbed_official 49:77c8e4604045 619
mbed_official 49:77c8e4604045 620 With the RTOS there is not only one stack above the heap, there are multiple
mbed_official 49:77c8e4604045 621 stacks and some of them are underneath the heap pointer.
mbed_official 49:77c8e4604045 622 */
mbed_official 49:77c8e4604045 623 #pragma import(__use_two_region_memory)
mbed_official 49:77c8e4604045 624
mbed_official 49:77c8e4604045 625 __asm void __rt_entry (void) {
mbed_official 49:77c8e4604045 626
mbed_official 49:77c8e4604045 627 IMPORT __user_setup_stackheap
mbed_official 116:0788b1a76461 628 IMPORT armcc_heap_base
mbed_official 116:0788b1a76461 629 IMPORT armcc_heap_top
mbed_official 49:77c8e4604045 630 IMPORT os_thread_def_main
mbed_official 49:77c8e4604045 631 IMPORT osKernelInitialize
mbed_official 112:53ace74b190c 632 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 49:77c8e4604045 633 IMPORT set_main_stack
mbed_official 112:53ace74b190c 634 #endif
mbed_official 49:77c8e4604045 635 IMPORT osKernelStart
mbed_official 49:77c8e4604045 636 IMPORT osThreadCreate
mbed_official 49:77c8e4604045 637
mbed_official 116:0788b1a76461 638 /* __user_setup_stackheap returns:
mbed_official 116:0788b1a76461 639 * - Heap base in r0 (if the program uses the heap).
mbed_official 116:0788b1a76461 640 * - Stack base in sp.
mbed_official 116:0788b1a76461 641 * - Heap limit in r2 (if the program uses the heap and uses two-region memory).
mbed_official 116:0788b1a76461 642 *
mbed_official 116:0788b1a76461 643 * More info can be found in:
mbed_official 116:0788b1a76461 644 * ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
mbed_official 116:0788b1a76461 645 */
mbed_official 49:77c8e4604045 646 BL __user_setup_stackheap
mbed_official 116:0788b1a76461 647 LDR R3,=armcc_heap_base
mbed_official 116:0788b1a76461 648 LDR R4,=armcc_heap_top
mbed_official 116:0788b1a76461 649 STR R0,[R3]
mbed_official 116:0788b1a76461 650 STR R2,[R4]
mbed_official 49:77c8e4604045 651 BL osKernelInitialize
mbed_official 112:53ace74b190c 652 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 49:77c8e4604045 653 BL set_main_stack
mbed_official 112:53ace74b190c 654 #endif
mbed_official 49:77c8e4604045 655 LDR R0,=os_thread_def_main
mbed_official 49:77c8e4604045 656 MOVS R1,#0
mbed_official 49:77c8e4604045 657 BL osThreadCreate
mbed_official 49:77c8e4604045 658 BL osKernelStart
mbed_official 116:0788b1a76461 659 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 660 B .
mbed_official 49:77c8e4604045 661
mbed_official 49:77c8e4604045 662 ALIGN
mbed_official 49:77c8e4604045 663 }
mbed_official 112:53ace74b190c 664
mbed_official 49:77c8e4604045 665 #endif
mbed_official 49:77c8e4604045 666
mbed_official 49:77c8e4604045 667 #elif defined (__GNUC__)
mbed_official 49:77c8e4604045 668
Kojto 118:6635230e06ba 669 osMutexDef(malloc_mutex);
Kojto 118:6635230e06ba 670 static osMutexId malloc_mutex_id;
Kojto 118:6635230e06ba 671 osMutexDef(env_mutex);
Kojto 118:6635230e06ba 672 static osMutexId env_mutex_id;
Kojto 118:6635230e06ba 673
mbed_official 116:0788b1a76461 674 extern void __libc_fini_array(void);
mbed_official 49:77c8e4604045 675 extern void __libc_init_array (void);
mbed_official 116:0788b1a76461 676 extern int main(int argc, char **argv);
mbed_official 49:77c8e4604045 677
mbed_official 116:0788b1a76461 678 void pre_main(void) {
Kojto 118:6635230e06ba 679 malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
Kojto 118:6635230e06ba 680 env_mutex_id = osMutexCreate(osMutex(env_mutex));
mbed_official 116:0788b1a76461 681 atexit(__libc_fini_array);
mbed_official 116:0788b1a76461 682 __libc_init_array();
mbed_official 116:0788b1a76461 683 main(0, NULL);
mbed_official 49:77c8e4604045 684 }
mbed_official 49:77c8e4604045 685
Kojto 118:6635230e06ba 686 __attribute__((naked)) void software_init_hook_rtos (void) {
mbed_official 49:77c8e4604045 687 __asm (
mbed_official 49:77c8e4604045 688 "bl osKernelInitialize\n"
mbed_official 112:53ace74b190c 689 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 49:77c8e4604045 690 "bl set_main_stack\n"
mbed_official 112:53ace74b190c 691 #endif
mbed_official 49:77c8e4604045 692 "ldr r0,=os_thread_def_main\n"
mbed_official 49:77c8e4604045 693 "movs r1,#0\n"
mbed_official 49:77c8e4604045 694 "bl osThreadCreate\n"
mbed_official 49:77c8e4604045 695 "bl osKernelStart\n"
mbed_official 116:0788b1a76461 696 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 697 "B .\n"
mbed_official 49:77c8e4604045 698 );
mbed_official 49:77c8e4604045 699 }
mbed_official 49:77c8e4604045 700
Kojto 118:6635230e06ba 701 // Opaque declaration of _reent structure
Kojto 118:6635230e06ba 702 struct _reent;
Kojto 118:6635230e06ba 703
Kojto 118:6635230e06ba 704 void __rtos_malloc_lock( struct _reent *_r )
Kojto 118:6635230e06ba 705 {
Kojto 118:6635230e06ba 706 osMutexWait(malloc_mutex_id, osWaitForever);
Kojto 118:6635230e06ba 707 }
Kojto 118:6635230e06ba 708
Kojto 118:6635230e06ba 709 void __rtos_malloc_unlock( struct _reent *_r )
Kojto 118:6635230e06ba 710 {
Kojto 118:6635230e06ba 711 osMutexRelease(malloc_mutex_id);
Kojto 118:6635230e06ba 712 }
Kojto 118:6635230e06ba 713
Kojto 118:6635230e06ba 714 void __rtos_env_lock( struct _reent *_r )
Kojto 118:6635230e06ba 715 {
Kojto 118:6635230e06ba 716 osMutexWait(env_mutex_id, osWaitForever);
Kojto 118:6635230e06ba 717 }
Kojto 118:6635230e06ba 718
Kojto 118:6635230e06ba 719 void __rtos_env_unlock( struct _reent *_r )
Kojto 118:6635230e06ba 720 {
Kojto 118:6635230e06ba 721 osMutexRelease(env_mutex_id);
Kojto 118:6635230e06ba 722 }
Kojto 118:6635230e06ba 723
mbed_official 49:77c8e4604045 724 #elif defined (__ICCARM__)
mbed_official 49:77c8e4604045 725
mbed_official 56:2b2a7cc13e28 726 extern void* __vector_table;
mbed_official 49:77c8e4604045 727 extern int __low_level_init(void);
mbed_official 49:77c8e4604045 728 extern void __iar_data_init3(void);
mbed_official 56:2b2a7cc13e28 729 extern __weak void __iar_init_core( void );
mbed_official 56:2b2a7cc13e28 730 extern __weak void __iar_init_vfp( void );
mbed_official 56:2b2a7cc13e28 731 extern void __iar_dynamic_initialization(void);
mbed_official 56:2b2a7cc13e28 732 extern void mbed_sdk_init(void);
Kojto 118:6635230e06ba 733 extern void mbed_main(void);
Kojto 118:6635230e06ba 734 extern int main(void);
mbed_official 49:77c8e4604045 735 extern void exit(int arg);
mbed_official 49:77c8e4604045 736
mbed_official 116:0788b1a76461 737 static uint8_t low_level_init_needed;
mbed_official 116:0788b1a76461 738
mbed_official 116:0788b1a76461 739 void pre_main(void) {
mbed_official 116:0788b1a76461 740 if (low_level_init_needed) {
mbed_official 116:0788b1a76461 741 __iar_dynamic_initialization();
mbed_official 116:0788b1a76461 742 }
Kojto 118:6635230e06ba 743 mbed_main();
mbed_official 116:0788b1a76461 744 main();
mbed_official 116:0788b1a76461 745 }
mbed_official 116:0788b1a76461 746
mbed_official 56:2b2a7cc13e28 747 #pragma required=__vector_table
mbed_official 56:2b2a7cc13e28 748 void __iar_program_start( void )
mbed_official 56:2b2a7cc13e28 749 {
mbed_official 112:53ace74b190c 750 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 56:2b2a7cc13e28 751 __iar_init_core();
mbed_official 56:2b2a7cc13e28 752 __iar_init_vfp();
mbed_official 56:2b2a7cc13e28 753
mbed_official 116:0788b1a76461 754 uint8_t low_level_init_needed_local;
mbed_official 49:77c8e4604045 755
mbed_official 116:0788b1a76461 756 low_level_init_needed_local = __low_level_init();
mbed_official 116:0788b1a76461 757 if (low_level_init_needed_local) {
mbed_official 49:77c8e4604045 758 __iar_data_init3();
mbed_official 56:2b2a7cc13e28 759 mbed_sdk_init();
mbed_official 68:d3d0e710b443 760 }
mbed_official 116:0788b1a76461 761 /* Store in a global variable after RAM has been initialized */
mbed_official 116:0788b1a76461 762 low_level_init_needed = low_level_init_needed_local;
mbed_official 112:53ace74b190c 763 #endif
mbed_official 49:77c8e4604045 764 osKernelInitialize();
mbed_official 112:53ace74b190c 765 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 53:c35dab33c427 766 set_main_stack();
mbed_official 112:53ace74b190c 767 #endif
mbed_official 49:77c8e4604045 768 osThreadCreate(&os_thread_def_main, NULL);
mbed_official 116:0788b1a76461 769 osKernelStart();
mbed_official 116:0788b1a76461 770 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 771 while (1);
mbed_official 49:77c8e4604045 772 }
mbed_official 49:77c8e4604045 773
mbed_official 49:77c8e4604045 774 #endif
mbed_official 49:77c8e4604045 775
mbed_official 49:77c8e4604045 776
mbed_official 49:77c8e4604045 777 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 778 * end of file
mbed_official 49:77c8e4604045 779 *---------------------------------------------------------------------------*/