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:
Wed Aug 10 16:09:20 2016 +0100
Revision:
119:19af2d39a542
Parent:
118:6635230e06ba
Child:
120:4dc938e301cc
RTOS rev119

Compatible with the mbed library v123

Changes:
- new targets: NRF52 and NUC472
- mbed singleton mutex addition
- main thread with timers fix
- Thread - mutex addition for synchronization
- Semaphore - default count argument set to 0
- RTOSTimer - add new ctor with Callback

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