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:
Thu Aug 18 14:38:56 2016 +0100
Revision:
120:4dc938e301cc
Parent:
119:19af2d39a542
Child:
121:3da5f554d8bf
RTOS rev120

Compatible with the mbed library v124

Changes:
- Fix heap limit checking
- Fix set initial stack pointer K64F + stack pointer size
- Fix stack size for NRF51
- Turn on main thread stack check
- Use new deprecate macros

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);
Kojto 120:4dc938e301cc 353
Kojto 120:4dc938e301cc 354 #if defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
Kojto 120:4dc938e301cc 355 static uint32_t thread_stack_main[DEFAULT_STACK_SIZE / sizeof(uint32_t)];
Kojto 120:4dc938e301cc 356 #else
Kojto 120:4dc938e301cc 357 static uint32_t thread_stack_main[DEFAULT_STACK_SIZE * 2 / sizeof(uint32_t)];
Kojto 120:4dc938e301cc 358 #endif
Kojto 120:4dc938e301cc 359 osThreadDef_t os_thread_def_main = {(os_pthread)pre_main, osPriorityNormal, 1U, sizeof(thread_stack_main), thread_stack_main};
Kojto 120:4dc938e301cc 360
Kojto 120:4dc938e301cc 361 /*
Kojto 120:4dc938e301cc 362 * IAR Default Memory layout notes:
Kojto 120:4dc938e301cc 363 * -Heap defined by "HEAP" region in .icf file
Kojto 120:4dc938e301cc 364 * -Interrupt stack defined by "CSTACK" region in .icf file
Kojto 120:4dc938e301cc 365 * -Value INITIAL_SP is ignored
Kojto 120:4dc938e301cc 366 *
Kojto 120:4dc938e301cc 367 * IAR Custom Memory layout notes:
Kojto 120:4dc938e301cc 368 * -There is no custom layout available for IAR - everything must be defined in
Kojto 120:4dc938e301cc 369 * the .icf file and use the default layout
Kojto 120:4dc938e301cc 370 *
Kojto 120:4dc938e301cc 371 *
Kojto 120:4dc938e301cc 372 * GCC Default Memory layout notes:
Kojto 120:4dc938e301cc 373 * -Block of memory from symbol __end__ to define INITIAL_SP used to setup interrupt
Kojto 120:4dc938e301cc 374 * stack and heap in the function set_stack_heap()
Kojto 120:4dc938e301cc 375 * -ISR_STACK_SIZE can be overridden to be larger or smaller
Kojto 120:4dc938e301cc 376 *
Kojto 120:4dc938e301cc 377 * GCC Custom Memory layout notes:
Kojto 120:4dc938e301cc 378 * -Heap can be explicitly placed by defining both HEAP_START and HEAP_SIZE
Kojto 120:4dc938e301cc 379 * -Interrupt stack can be explicitly placed by defining both ISR_STACK_START and ISR_STACK_SIZE
Kojto 120:4dc938e301cc 380 *
Kojto 120:4dc938e301cc 381 *
Kojto 120:4dc938e301cc 382 * ARM Memory layout
Kojto 120:4dc938e301cc 383 * -Block of memory from end of region "RW_IRAM1" to define INITIAL_SP used to setup interrupt
Kojto 120:4dc938e301cc 384 * stack and heap in the function set_stack_heap()
Kojto 120:4dc938e301cc 385 * -ISR_STACK_SIZE can be overridden to be larger or smaller
Kojto 120:4dc938e301cc 386 *
Kojto 120:4dc938e301cc 387 * ARM Custom Memory layout notes:
Kojto 120:4dc938e301cc 388 * -Heap can be explicitly placed by defining both HEAP_START and HEAP_SIZE
Kojto 120:4dc938e301cc 389 * -Interrupt stack can be explicitly placed by defining both ISR_STACK_START and ISR_STACK_SIZE
Kojto 120:4dc938e301cc 390 *
Kojto 120:4dc938e301cc 391 */
Kojto 120:4dc938e301cc 392
mbed_official 49:77c8e4604045 393
mbed_official 49:77c8e4604045 394 // This define should be probably moved to the CMSIS layer
mbed_official 49:77c8e4604045 395 #if defined(TARGET_LPC1768)
mbed_official 49:77c8e4604045 396 #define INITIAL_SP (0x10008000UL)
mbed_official 49:77c8e4604045 397
mbed_official 49:77c8e4604045 398 #elif defined(TARGET_LPC11U24)
mbed_official 49:77c8e4604045 399 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 400
mbed_official 49:77c8e4604045 401 #elif defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPCCAPPUCCINO)
mbed_official 49:77c8e4604045 402 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 403
mbed_official 49:77c8e4604045 404 #elif defined(TARGET_LPC1114)
mbed_official 49:77c8e4604045 405 #define INITIAL_SP (0x10001000UL)
mbed_official 49:77c8e4604045 406
mbed_official 49:77c8e4604045 407 #elif defined(TARGET_LPC812)
mbed_official 49:77c8e4604045 408 #define INITIAL_SP (0x10001000UL)
mbed_official 49:77c8e4604045 409
mbed_official 81:e45e4ac7c3c8 410 #elif defined(TARGET_LPC824) || defined(TARGET_SSCI824)
mbed_official 49:77c8e4604045 411 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 412
mbed_official 49:77c8e4604045 413 #elif defined(TARGET_KL25Z)
mbed_official 49:77c8e4604045 414 #define INITIAL_SP (0x20003000UL)
mbed_official 49:77c8e4604045 415
mbed_official 88:a21475017ae2 416 #elif defined(TARGET_KL26Z)
mbed_official 88:a21475017ae2 417 #define INITIAL_SP (0x20003000UL)
mbed_official 88:a21475017ae2 418
mbed_official 111:162b12aea5f2 419 #elif defined(TARGET_KL27Z)
mbed_official 111:162b12aea5f2 420 #define INITIAL_SP (0x20003000UL)
mbed_official 111:162b12aea5f2 421
mbed_official 49:77c8e4604045 422 #elif defined(TARGET_K64F)
Kojto 120:4dc938e301cc 423 #if defined(__GNUC__) && !defined(__CC_ARM) /* GCC */
Kojto 118:6635230e06ba 424 extern uint32_t __StackTop[];
Kojto 118:6635230e06ba 425 #define INITIAL_SP (__StackTop)
Kojto 118:6635230e06ba 426 #else
mbed_official 49:77c8e4604045 427 #define INITIAL_SP (0x20030000UL)
Kojto 118:6635230e06ba 428 #endif
Kojto 120:4dc938e301cc 429 #if defined(__CC_ARM) || defined(__GNUC__)
Kojto 120:4dc938e301cc 430 #define ISR_STACK_SIZE (0x1000)
Kojto 120:4dc938e301cc 431 #endif
Kojto 120:4dc938e301cc 432
Kojto 120:4dc938e301cc 433 #elif defined(TARGET_K66F)
Kojto 120:4dc938e301cc 434 #define INITIAL_SP (0x20030000UL)
mbed_official 49:77c8e4604045 435
mbed_official 52:02f5cf381388 436 #elif defined(TARGET_K22F)
mbed_official 52:02f5cf381388 437 #define INITIAL_SP (0x20010000UL)
mbed_official 52:02f5cf381388 438
mbed_official 49:77c8e4604045 439 #elif defined(TARGET_KL46Z)
mbed_official 49:77c8e4604045 440 #define INITIAL_SP (0x20006000UL)
mbed_official 49:77c8e4604045 441
mbed_official 49:77c8e4604045 442 #elif defined(TARGET_KL43Z)
mbed_official 49:77c8e4604045 443 #define INITIAL_SP (0x20006000UL)
mbed_official 49:77c8e4604045 444
mbed_official 49:77c8e4604045 445 #elif defined(TARGET_KL05Z)
mbed_official 49:77c8e4604045 446 #define INITIAL_SP (0x20000C00UL)
mbed_official 49:77c8e4604045 447
mbed_official 64:ab4200083b07 448 #elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
mbed_official 49:77c8e4604045 449 #define INITIAL_SP (0x10010000UL)
mbed_official 49:77c8e4604045 450
mbed_official 63:5448826aa700 451 #elif defined(TARGET_LPC4330)
mbed_official 63:5448826aa700 452 #define INITIAL_SP (0x10008000UL)
mbed_official 63:5448826aa700 453
mbed_official 49:77c8e4604045 454 #elif defined(TARGET_LPC4337)
mbed_official 49:77c8e4604045 455 #define INITIAL_SP (0x10008000UL)
mbed_official 49:77c8e4604045 456
mbed_official 49:77c8e4604045 457 #elif defined(TARGET_LPC1347)
mbed_official 49:77c8e4604045 458 #define INITIAL_SP (0x10002000UL)
mbed_official 49:77c8e4604045 459
mbed_official 49:77c8e4604045 460 #elif defined(TARGET_STM32F100RB) || defined(TARGET_STM32F051R8)
mbed_official 49:77c8e4604045 461 #define INITIAL_SP (0x20002000UL)
mbed_official 49:77c8e4604045 462
mbed_official 49:77c8e4604045 463 #elif defined(TARGET_DISCO_F303VC)
mbed_official 49:77c8e4604045 464 #define INITIAL_SP (0x2000A000UL)
mbed_official 49:77c8e4604045 465
mbed_official 49:77c8e4604045 466 #elif defined(TARGET_STM32F407) || defined(TARGET_F407VG)
mbed_official 49:77c8e4604045 467 #define INITIAL_SP (0x20020000UL)
mbed_official 49:77c8e4604045 468
mbed_official 49:77c8e4604045 469 #elif defined(TARGET_STM32F401RE)
mbed_official 49:77c8e4604045 470 #define INITIAL_SP (0x20018000UL)
mbed_official 49:77c8e4604045 471
mbed_official 49:77c8e4604045 472 #elif defined(TARGET_LPC1549)
mbed_official 49:77c8e4604045 473 #define INITIAL_SP (0x02009000UL)
mbed_official 49:77c8e4604045 474
mbed_official 49:77c8e4604045 475 #elif defined(TARGET_LPC11U68)
mbed_official 89:5aed8bae1001 476 #define INITIAL_SP (0x10008000UL)
mbed_official 49:77c8e4604045 477
mbed_official 49:77c8e4604045 478 #elif defined(TARGET_STM32F411RE)
mbed_official 49:77c8e4604045 479 #define INITIAL_SP (0x20020000UL)
mbed_official 49:77c8e4604045 480
Kojto 119:19af2d39a542 481 #elif defined(TARGET_STM32F207ZG)
Kojto 119:19af2d39a542 482 #define INITIAL_SP (0x20020000UL)
Kojto 119:19af2d39a542 483
mbed_official 97:6c35e082773a 484 #elif defined(TARGET_STM32F410RB)
mbed_official 97:6c35e082773a 485 #define INITIAL_SP (0x20008000UL)
mbed_official 97:6c35e082773a 486
mbed_official 106:dfc27975e193 487 #elif defined(TARGET_STM32F103RB) || defined(TARGET_STM32L073RZ)
mbed_official 49:77c8e4604045 488 #define INITIAL_SP (0x20005000UL)
mbed_official 49:77c8e4604045 489
mbed_official 49:77c8e4604045 490 #elif defined(TARGET_STM32F302R8)
mbed_official 49:77c8e4604045 491 #define INITIAL_SP (0x20004000UL)
mbed_official 49:77c8e4604045 492
mbed_official 49:77c8e4604045 493 #elif defined(TARGET_STM32F334R8)
mbed_official 49:77c8e4604045 494 #define INITIAL_SP (0x20003000UL)
mbed_official 49:77c8e4604045 495
mbed_official 49:77c8e4604045 496 #elif defined(TARGET_STM32F334C8)
mbed_official 49:77c8e4604045 497 #define INITIAL_SP (0x20003000UL)
mbed_official 49:77c8e4604045 498
mbed_official 50:64a5202c3676 499 #elif defined(TARGET_STM32F405RG)
mbed_official 50:64a5202c3676 500 #define INITIAL_SP (0x20020000UL)
mbed_official 50:64a5202c3676 501
mbed_official 57:430de2831ec7 502 #elif defined(TARGET_STM32F429ZI)
mbed_official 57:430de2831ec7 503 #define INITIAL_SP (0x20030000UL)
mbed_official 57:430de2831ec7 504
mbed_official 108:ac4f3830f9ff 505 #elif defined(TARGET_STM32L031K6) || defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)
mbed_official 59:28712e303960 506 #define INITIAL_SP (0x20002000UL)
mbed_official 59:28712e303960 507
mbed_official 59:28712e303960 508 #elif defined(TARGET_STM32F072RB)
mbed_official 59:28712e303960 509 #define INITIAL_SP (0x20004000UL)
mbed_official 59:28712e303960 510
mbed_official 59:28712e303960 511 #elif defined(TARGET_STM32F091RC)
mbed_official 59:28712e303960 512 #define INITIAL_SP (0x20008000UL)
mbed_official 59:28712e303960 513
mbed_official 60:f4d3d8971bc3 514 #elif defined(TARGET_STM32F401VC)
mbed_official 60:f4d3d8971bc3 515 #define INITIAL_SP (0x20010000UL)
mbed_official 60:f4d3d8971bc3 516
mbed_official 68:d3d0e710b443 517 #elif defined(TARGET_STM32F303RE)
mbed_official 68:d3d0e710b443 518 #define INITIAL_SP (0x20010000UL)
mbed_official 68:d3d0e710b443 519
mbed_official 95:12552ef4e980 520 #elif defined(TARGET_STM32F303K8)
mbed_official 95:12552ef4e980 521 #define INITIAL_SP (0x20003000UL)
mbed_official 95:12552ef4e980 522
mbed_official 105:b4c5542476ba 523 #elif (defined(TARGET_STM32F746NG) || defined(TARGET_STM32F746ZG))
mbed_official 98:c825593ece39 524 #define INITIAL_SP (0x20050000UL)
mbed_official 98:c825593ece39 525
mbed_official 73:34292fba723c 526 #elif defined(TARGET_MAX32610) || defined(TARGET_MAX32600)
mbed_official 71:570e569a5b59 527 #define INITIAL_SP (0x20008000UL)
mbed_official 71:570e569a5b59 528
mbed_official 74:899aee34da6a 529 #elif defined(TARGET_TEENSY3_1)
mbed_official 74:899aee34da6a 530 #define INITIAL_SP (0x20008000UL)
mbed_official 74:899aee34da6a 531
mbed_official 77:3516160e016b 532 #elif defined(TARGET_STM32L152RE)
mbed_official 77:3516160e016b 533 #define INITIAL_SP (0x20014000UL)
mbed_official 77:3516160e016b 534
mbed_official 104:07314541bd12 535 #elif defined(TARGET_NZ32_SC151)
mbed_official 78:2db19f47c2ba 536 #define INITIAL_SP (0x20008000UL)
mbed_official 78:2db19f47c2ba 537
Kojto 118:6635230e06ba 538 #elif defined(TARGET_STM32F446RE) || defined(TARGET_STM32F446VE) || defined(TARGET_STM32F446ZE)
mbed_official 79:c586ffeebfb4 539 #define INITIAL_SP (0x20020000UL)
mbed_official 79:c586ffeebfb4 540
mbed_official 87:e695cd34556b 541 #elif defined(TARGET_STM32F070RB) || defined(TARGET_STM32F030R8)
mbed_official 87:e695cd34556b 542 #define INITIAL_SP (0x20002000UL)
mbed_official 87:e695cd34556b 543
Kojto 118:6635230e06ba 544 #elif defined(TARGET_STM32L432KC)
Kojto 118:6635230e06ba 545 #define INITIAL_SP (0x2000C000UL)
Kojto 118:6635230e06ba 546
mbed_official 91:9d001ed5feec 547 #elif defined(TARGET_STM32L476VG)
mbed_official 91:9d001ed5feec 548 #define INITIAL_SP (0x20018000UL)
mbed_official 91:9d001ed5feec 549
mbed_official 91:9d001ed5feec 550 #elif defined(TARGET_STM32L476RG)
mbed_official 91:9d001ed5feec 551 #define INITIAL_SP (0x20018000UL)
mbed_official 91:9d001ed5feec 552
mbed_official 96:6d90423c236e 553 #elif defined(TARGET_STM32F469NI)
mbed_official 96:6d90423c236e 554 #define INITIAL_SP (0x20050000UL)
mbed_official 96:6d90423c236e 555
mbed_official 99:79d2f32f753f 556 #elif defined(TARGET_STM32L152RC)
mbed_official 99:79d2f32f753f 557 #define INITIAL_SP (0x20008000UL)
mbed_official 99:79d2f32f753f 558
Kojto 118:6635230e06ba 559 #elif defined(TARGET_EFM32GG_STK3700) || defined(TARGET_BEETLE)
Kojto 118:6635230e06ba 560 #define INITIAL_SP (0x20020000UL)
Kojto 118:6635230e06ba 561
Kojto 118:6635230e06ba 562 #elif defined(TARGET_EFM32HG_STK3400)
Kojto 118:6635230e06ba 563 #define INITIAL_SP (0x20002000UL)
Kojto 118:6635230e06ba 564
Kojto 118:6635230e06ba 565 #elif defined(TARGET_EFM32LG_STK3600) || defined(TARGET_EFM32WG_STK3800) || defined(TARGET_EFM32PG_STK3401)
Kojto 118:6635230e06ba 566 #define INITIAL_SP (0x20008000UL)
Kojto 118:6635230e06ba 567
Kojto 118:6635230e06ba 568 #elif defined(TARGET_MCU_NORDIC_32K)
Kojto 118:6635230e06ba 569 #define INITIAL_SP (0x20008000UL)
Kojto 118:6635230e06ba 570
Kojto 118:6635230e06ba 571 #elif defined(TARGET_MCU_NORDIC_16K)
Kojto 118:6635230e06ba 572 #define INITIAL_SP (0x20004000UL)
Kojto 118:6635230e06ba 573
Kojto 119:19af2d39a542 574 #elif defined(TARGET_MCU_NRF52832)
Kojto 119:19af2d39a542 575 #define INITIAL_SP (0x20010000UL)
Kojto 119:19af2d39a542 576
Kojto 118:6635230e06ba 577 #elif (defined(TARGET_STM32F767ZI))
Kojto 118:6635230e06ba 578 #define INITIAL_SP (0x20080000UL)
mbed_official 99:79d2f32f753f 579
Kojto 119:19af2d39a542 580 #elif defined(TARGET_NUMAKER_PFM_NUC472)
Kojto 119:19af2d39a542 581 # if defined(__CC_ARM)
Kojto 120:4dc938e301cc 582 extern uint32_t Image$$ARM_LIB_HEAP$$Base[];
Kojto 120:4dc938e301cc 583 extern uint32_t Image$$ARM_LIB_HEAP$$Length[];
Kojto 120:4dc938e301cc 584 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
Kojto 120:4dc938e301cc 585 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
Kojto 120:4dc938e301cc 586 #define HEAP_START ((unsigned char*) Image$$ARM_LIB_HEAP$$Base)
Kojto 120:4dc938e301cc 587 #define HEAP_SIZE ((uint32_t) Image$$ARM_LIB_HEAP$$Length)
Kojto 120:4dc938e301cc 588 #define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
Kojto 120:4dc938e301cc 589 #define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
Kojto 119:19af2d39a542 590 # elif defined(__GNUC__)
Kojto 119:19af2d39a542 591 extern uint32_t __StackTop[];
Kojto 119:19af2d39a542 592 extern uint32_t __StackLimit[];
Kojto 120:4dc938e301cc 593 extern uint32_t __end__[];
Kojto 120:4dc938e301cc 594 extern uint32_t __HeapLimit[];
Kojto 120:4dc938e301cc 595 #define HEAP_START ((unsigned char*)__end__)
Kojto 120:4dc938e301cc 596 #define HEAP_SIZE ((uint32_t)((uint32_t)__HeapLimit - (uint32_t)HEAP_START))
Kojto 120:4dc938e301cc 597 #define ISR_STACK_START ((unsigned char*)__StackLimit)
Kojto 120:4dc938e301cc 598 #define ISR_STACK_SIZE ((uint32_t)((uint32_t)__StackTop - (uint32_t)__StackLimit))
Kojto 119:19af2d39a542 599 # elif defined(__ICCARM__)
Kojto 120:4dc938e301cc 600 /* No region declarations needed */
Kojto 119:19af2d39a542 601 # else
Kojto 119:19af2d39a542 602 #error "no toolchain defined"
Kojto 119:19af2d39a542 603 # endif
Kojto 119:19af2d39a542 604
mbed_official 49:77c8e4604045 605 #else
mbed_official 49:77c8e4604045 606 #error "no target defined"
mbed_official 49:77c8e4604045 607
mbed_official 49:77c8e4604045 608 #endif
mbed_official 49:77c8e4604045 609
Kojto 120:4dc938e301cc 610 extern unsigned char *mbed_heap_start;
Kojto 120:4dc938e301cc 611 extern uint32_t mbed_heap_size;
Kojto 120:4dc938e301cc 612
Kojto 120:4dc938e301cc 613 unsigned char *mbed_stack_isr_start = 0;
Kojto 120:4dc938e301cc 614 uint32_t mbed_stack_isr_size = 0;
Kojto 120:4dc938e301cc 615
Kojto 120:4dc938e301cc 616 /*
Kojto 120:4dc938e301cc 617 * Sanity check values
Kojto 120:4dc938e301cc 618 */
Kojto 120:4dc938e301cc 619 #if defined(__ICCARM__) && \
Kojto 120:4dc938e301cc 620 (defined(HEAP_START) || defined(HEAP_SIZE) || \
Kojto 120:4dc938e301cc 621 defined(ISR_STACK_START) && defined(ISR_STACK_SIZE))
Kojto 120:4dc938e301cc 622 #error "No custom layout allowed for IAR. Use .icf file instead"
Kojto 120:4dc938e301cc 623 #endif
Kojto 120:4dc938e301cc 624 #if defined(HEAP_START) && !defined(HEAP_SIZE)
Kojto 120:4dc938e301cc 625 #error "HEAP_SIZE must be defined if HEAP_START is defined"
Kojto 119:19af2d39a542 626 #endif
Kojto 120:4dc938e301cc 627 #if defined(ISR_STACK_START) && !defined(ISR_STACK_SIZE)
Kojto 120:4dc938e301cc 628 #error "ISR_STACK_SIZE must be defined if ISR_STACK_START is defined"
Kojto 120:4dc938e301cc 629 #endif
Kojto 120:4dc938e301cc 630 #if defined(HEAP_SIZE) && !defined(HEAP_START)
Kojto 120:4dc938e301cc 631 #error "HEAP_START must be defined if HEAP_SIZE is defined"
Kojto 120:4dc938e301cc 632 #endif
Kojto 120:4dc938e301cc 633
Kojto 120:4dc938e301cc 634 /* Interrupt stack and heap always defined for IAR
Kojto 120:4dc938e301cc 635 * Main thread defined here
Kojto 120:4dc938e301cc 636 */
Kojto 120:4dc938e301cc 637 #if defined(__ICCARM__)
Kojto 120:4dc938e301cc 638 #pragma section="CSTACK"
Kojto 120:4dc938e301cc 639 #pragma section="HEAP"
Kojto 120:4dc938e301cc 640 #define HEAP_START ((unsigned char*)__section_begin("HEAP"))
Kojto 120:4dc938e301cc 641 #define HEAP_SIZE ((uint32_t)__section_size("HEAP"))
Kojto 120:4dc938e301cc 642 #define ISR_STACK_START ((unsigned char*)__section_begin("CSTACK"))
Kojto 120:4dc938e301cc 643 #define ISR_STACK_SIZE ((uint32_t)__section_size("CSTACK"))
mbed_official 49:77c8e4604045 644 #endif
mbed_official 49:77c8e4604045 645
Kojto 120:4dc938e301cc 646 /* Define heap region if it has not been defined already */
Kojto 120:4dc938e301cc 647 #if !defined(HEAP_START)
Kojto 120:4dc938e301cc 648 #if defined(__ICCARM__)
Kojto 120:4dc938e301cc 649 #error "Heap should already be defined for IAR"
Kojto 120:4dc938e301cc 650 #elif defined(__CC_ARM)
Kojto 120:4dc938e301cc 651 extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
Kojto 120:4dc938e301cc 652 #define HEAP_START ((unsigned char*)Image$$RW_IRAM1$$ZI$$Limit)
Kojto 120:4dc938e301cc 653 #define HEAP_SIZE ((uint32_t)((uint32_t)INITIAL_SP - (uint32_t)HEAP_START))
Kojto 120:4dc938e301cc 654 #elif defined(__GNUC__)
Kojto 120:4dc938e301cc 655 extern uint32_t __end__[];
Kojto 120:4dc938e301cc 656 #define HEAP_START ((unsigned char*)__end__)
Kojto 120:4dc938e301cc 657 #define HEAP_SIZE ((uint32_t)((uint32_t)INITIAL_SP - (uint32_t)HEAP_START))
Kojto 120:4dc938e301cc 658 #endif
Kojto 120:4dc938e301cc 659 #endif
Kojto 120:4dc938e301cc 660
Kojto 120:4dc938e301cc 661 /* Define stack sizes if they haven't been set already */
Kojto 120:4dc938e301cc 662 #if !defined(ISR_STACK_SIZE)
Kojto 120:4dc938e301cc 663 #define ISR_STACK_SIZE ((uint32_t)OS_MAINSTKSIZE * 4)
Kojto 120:4dc938e301cc 664 #endif
Kojto 120:4dc938e301cc 665
Kojto 120:4dc938e301cc 666 /*
Kojto 120:4dc938e301cc 667 * set_stack_heap purpose is to set the following variables:
Kojto 120:4dc938e301cc 668 * -mbed_heap_start
Kojto 120:4dc938e301cc 669 * -mbed_heap_size
Kojto 120:4dc938e301cc 670 * -mbed_stack_isr_start
Kojto 120:4dc938e301cc 671 * -mbed_stack_isr_size
Kojto 120:4dc938e301cc 672 *
Kojto 120:4dc938e301cc 673 * Along with setting up os_thread_def_main
Kojto 120:4dc938e301cc 674 */
Kojto 120:4dc938e301cc 675 void set_stack_heap(void) {
Kojto 120:4dc938e301cc 676
Kojto 120:4dc938e301cc 677 unsigned char *free_start = HEAP_START;
Kojto 120:4dc938e301cc 678 uint32_t free_size = HEAP_SIZE;
Kojto 120:4dc938e301cc 679
Kojto 120:4dc938e301cc 680 #ifdef ISR_STACK_START
Kojto 120:4dc938e301cc 681 /* Interrupt stack explicitly specified */
Kojto 120:4dc938e301cc 682 mbed_stack_isr_size = ISR_STACK_SIZE;
Kojto 120:4dc938e301cc 683 mbed_stack_isr_start = ISR_STACK_START;
Kojto 118:6635230e06ba 684 #else
Kojto 120:4dc938e301cc 685 /* Interrupt stack - reserve space at the end of the free block */
Kojto 120:4dc938e301cc 686 mbed_stack_isr_size = ISR_STACK_SIZE;
Kojto 120:4dc938e301cc 687 mbed_stack_isr_start = free_start + free_size - mbed_stack_isr_size;
Kojto 120:4dc938e301cc 688 free_size -= mbed_stack_isr_size;
Kojto 118:6635230e06ba 689 #endif
Kojto 118:6635230e06ba 690
Kojto 120:4dc938e301cc 691 /* Heap - everything else */
Kojto 120:4dc938e301cc 692 mbed_heap_size = free_size;
Kojto 120:4dc938e301cc 693 mbed_heap_start = free_start;
mbed_official 49:77c8e4604045 694 }
mbed_official 49:77c8e4604045 695
mbed_official 49:77c8e4604045 696 #if defined (__CC_ARM)
mbed_official 112:53ace74b190c 697
mbed_official 49:77c8e4604045 698 #ifdef __MICROLIB
mbed_official 116:0788b1a76461 699
mbed_official 116:0788b1a76461 700 int main(void);
mbed_official 49:77c8e4604045 701 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
mbed_official 116:0788b1a76461 702 void $Super$$__cpp_initialize__aeabi_(void);
mbed_official 116:0788b1a76461 703
mbed_official 49:77c8e4604045 704 void _main_init (void) {
mbed_official 49:77c8e4604045 705 osKernelInitialize();
mbed_official 112:53ace74b190c 706 #ifdef __MBED_CMSIS_RTOS_CM
Kojto 120:4dc938e301cc 707 set_stack_heap();
mbed_official 112:53ace74b190c 708 #endif
mbed_official 49:77c8e4604045 709 osThreadCreate(&os_thread_def_main, NULL);
mbed_official 49:77c8e4604045 710 osKernelStart();
mbed_official 49:77c8e4604045 711 for (;;);
mbed_official 49:77c8e4604045 712 }
mbed_official 116:0788b1a76461 713
mbed_official 116:0788b1a76461 714 void $Sub$$__cpp_initialize__aeabi_(void)
mbed_official 116:0788b1a76461 715 {
mbed_official 116:0788b1a76461 716 // this should invoke C++ initializers prior _main_init, we keep this empty and
mbed_official 116:0788b1a76461 717 // invoke them after _main_init (=starts RTX kernel)
mbed_official 116:0788b1a76461 718 }
mbed_official 116:0788b1a76461 719
mbed_official 116:0788b1a76461 720 void pre_main()
mbed_official 116:0788b1a76461 721 {
Kojto 119:19af2d39a542 722 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
mbed_official 116:0788b1a76461 723 $Super$$__cpp_initialize__aeabi_();
mbed_official 116:0788b1a76461 724 main();
mbed_official 116:0788b1a76461 725 }
mbed_official 116:0788b1a76461 726
mbed_official 49:77c8e4604045 727 #else
mbed_official 49:77c8e4604045 728
Kojto 119:19af2d39a542 729 int main(void);
mbed_official 116:0788b1a76461 730
Kojto 119:19af2d39a542 731 void pre_main (void)
Kojto 119:19af2d39a542 732 {
Kojto 119:19af2d39a542 733 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
Kojto 120:4dc938e301cc 734 __rt_lib_init((unsigned)mbed_heap_start, (unsigned)(mbed_heap_start + mbed_heap_size));
Kojto 119:19af2d39a542 735 main();
mbed_official 116:0788b1a76461 736 }
mbed_official 116:0788b1a76461 737
mbed_official 49:77c8e4604045 738 /* The single memory model is checking for stack collision at run time, verifing
mbed_official 49:77c8e4604045 739 that the heap pointer is underneath the stack pointer.
mbed_official 49:77c8e4604045 740
mbed_official 49:77c8e4604045 741 With the RTOS there is not only one stack above the heap, there are multiple
mbed_official 49:77c8e4604045 742 stacks and some of them are underneath the heap pointer.
mbed_official 49:77c8e4604045 743 */
mbed_official 49:77c8e4604045 744 #pragma import(__use_two_region_memory)
mbed_official 49:77c8e4604045 745
mbed_official 49:77c8e4604045 746 __asm void __rt_entry (void) {
mbed_official 49:77c8e4604045 747
mbed_official 49:77c8e4604045 748 IMPORT __user_setup_stackheap
mbed_official 49:77c8e4604045 749 IMPORT os_thread_def_main
mbed_official 49:77c8e4604045 750 IMPORT osKernelInitialize
mbed_official 112:53ace74b190c 751 #ifdef __MBED_CMSIS_RTOS_CM
Kojto 120:4dc938e301cc 752 IMPORT set_stack_heap
mbed_official 112:53ace74b190c 753 #endif
mbed_official 49:77c8e4604045 754 IMPORT osKernelStart
mbed_official 49:77c8e4604045 755 IMPORT osThreadCreate
mbed_official 49:77c8e4604045 756
mbed_official 116:0788b1a76461 757 /* __user_setup_stackheap returns:
mbed_official 116:0788b1a76461 758 * - Heap base in r0 (if the program uses the heap).
mbed_official 116:0788b1a76461 759 * - Stack base in sp.
mbed_official 116:0788b1a76461 760 * - Heap limit in r2 (if the program uses the heap and uses two-region memory).
mbed_official 116:0788b1a76461 761 *
mbed_official 116:0788b1a76461 762 * More info can be found in:
mbed_official 116:0788b1a76461 763 * ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
mbed_official 116:0788b1a76461 764 */
mbed_official 49:77c8e4604045 765 BL __user_setup_stackheap
Kojto 120:4dc938e301cc 766 /* Ignore return value of __user_setup_stackheap since
Kojto 120:4dc938e301cc 767 * this will be setup by set_stack_heap
Kojto 120:4dc938e301cc 768 */
mbed_official 49:77c8e4604045 769 BL osKernelInitialize
mbed_official 112:53ace74b190c 770 #ifdef __MBED_CMSIS_RTOS_CM
Kojto 120:4dc938e301cc 771 BL set_stack_heap
mbed_official 112:53ace74b190c 772 #endif
mbed_official 49:77c8e4604045 773 LDR R0,=os_thread_def_main
mbed_official 49:77c8e4604045 774 MOVS R1,#0
mbed_official 49:77c8e4604045 775 BL osThreadCreate
mbed_official 49:77c8e4604045 776 BL osKernelStart
mbed_official 116:0788b1a76461 777 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 778 B .
mbed_official 49:77c8e4604045 779
mbed_official 49:77c8e4604045 780 ALIGN
mbed_official 49:77c8e4604045 781 }
mbed_official 112:53ace74b190c 782
mbed_official 49:77c8e4604045 783 #endif
mbed_official 49:77c8e4604045 784
mbed_official 49:77c8e4604045 785 #elif defined (__GNUC__)
mbed_official 49:77c8e4604045 786
Kojto 118:6635230e06ba 787 osMutexDef(malloc_mutex);
Kojto 118:6635230e06ba 788 static osMutexId malloc_mutex_id;
Kojto 118:6635230e06ba 789 osMutexDef(env_mutex);
Kojto 118:6635230e06ba 790 static osMutexId env_mutex_id;
Kojto 118:6635230e06ba 791
mbed_official 116:0788b1a76461 792 extern void __libc_fini_array(void);
mbed_official 49:77c8e4604045 793 extern void __libc_init_array (void);
mbed_official 116:0788b1a76461 794 extern int main(int argc, char **argv);
mbed_official 49:77c8e4604045 795
mbed_official 116:0788b1a76461 796 void pre_main(void) {
Kojto 119:19af2d39a542 797 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
Kojto 118:6635230e06ba 798 malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
Kojto 118:6635230e06ba 799 env_mutex_id = osMutexCreate(osMutex(env_mutex));
mbed_official 116:0788b1a76461 800 atexit(__libc_fini_array);
mbed_official 116:0788b1a76461 801 __libc_init_array();
mbed_official 116:0788b1a76461 802 main(0, NULL);
mbed_official 49:77c8e4604045 803 }
mbed_official 49:77c8e4604045 804
Kojto 118:6635230e06ba 805 __attribute__((naked)) void software_init_hook_rtos (void) {
mbed_official 49:77c8e4604045 806 __asm (
mbed_official 49:77c8e4604045 807 "bl osKernelInitialize\n"
mbed_official 112:53ace74b190c 808 #ifdef __MBED_CMSIS_RTOS_CM
Kojto 120:4dc938e301cc 809 "bl set_stack_heap\n"
mbed_official 112:53ace74b190c 810 #endif
mbed_official 49:77c8e4604045 811 "ldr r0,=os_thread_def_main\n"
mbed_official 49:77c8e4604045 812 "movs r1,#0\n"
mbed_official 49:77c8e4604045 813 "bl osThreadCreate\n"
mbed_official 49:77c8e4604045 814 "bl osKernelStart\n"
mbed_official 116:0788b1a76461 815 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 816 "B .\n"
mbed_official 49:77c8e4604045 817 );
mbed_official 49:77c8e4604045 818 }
mbed_official 49:77c8e4604045 819
Kojto 118:6635230e06ba 820 // Opaque declaration of _reent structure
Kojto 118:6635230e06ba 821 struct _reent;
Kojto 118:6635230e06ba 822
Kojto 118:6635230e06ba 823 void __rtos_malloc_lock( struct _reent *_r )
Kojto 118:6635230e06ba 824 {
Kojto 118:6635230e06ba 825 osMutexWait(malloc_mutex_id, osWaitForever);
Kojto 118:6635230e06ba 826 }
Kojto 118:6635230e06ba 827
Kojto 118:6635230e06ba 828 void __rtos_malloc_unlock( struct _reent *_r )
Kojto 118:6635230e06ba 829 {
Kojto 118:6635230e06ba 830 osMutexRelease(malloc_mutex_id);
Kojto 118:6635230e06ba 831 }
Kojto 118:6635230e06ba 832
Kojto 118:6635230e06ba 833 void __rtos_env_lock( struct _reent *_r )
Kojto 118:6635230e06ba 834 {
Kojto 118:6635230e06ba 835 osMutexWait(env_mutex_id, osWaitForever);
Kojto 118:6635230e06ba 836 }
Kojto 118:6635230e06ba 837
Kojto 118:6635230e06ba 838 void __rtos_env_unlock( struct _reent *_r )
Kojto 118:6635230e06ba 839 {
Kojto 118:6635230e06ba 840 osMutexRelease(env_mutex_id);
Kojto 118:6635230e06ba 841 }
Kojto 118:6635230e06ba 842
mbed_official 49:77c8e4604045 843 #elif defined (__ICCARM__)
mbed_official 49:77c8e4604045 844
mbed_official 56:2b2a7cc13e28 845 extern void* __vector_table;
mbed_official 49:77c8e4604045 846 extern int __low_level_init(void);
mbed_official 49:77c8e4604045 847 extern void __iar_data_init3(void);
mbed_official 56:2b2a7cc13e28 848 extern __weak void __iar_init_core( void );
mbed_official 56:2b2a7cc13e28 849 extern __weak void __iar_init_vfp( void );
mbed_official 56:2b2a7cc13e28 850 extern void __iar_dynamic_initialization(void);
mbed_official 56:2b2a7cc13e28 851 extern void mbed_sdk_init(void);
Kojto 118:6635230e06ba 852 extern void mbed_main(void);
Kojto 118:6635230e06ba 853 extern int main(void);
mbed_official 49:77c8e4604045 854 extern void exit(int arg);
mbed_official 49:77c8e4604045 855
mbed_official 116:0788b1a76461 856 static uint8_t low_level_init_needed;
mbed_official 116:0788b1a76461 857
mbed_official 116:0788b1a76461 858 void pre_main(void) {
Kojto 119:19af2d39a542 859 singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
mbed_official 116:0788b1a76461 860 if (low_level_init_needed) {
mbed_official 116:0788b1a76461 861 __iar_dynamic_initialization();
mbed_official 116:0788b1a76461 862 }
Kojto 118:6635230e06ba 863 mbed_main();
mbed_official 116:0788b1a76461 864 main();
mbed_official 116:0788b1a76461 865 }
mbed_official 116:0788b1a76461 866
mbed_official 56:2b2a7cc13e28 867 #pragma required=__vector_table
mbed_official 56:2b2a7cc13e28 868 void __iar_program_start( void )
mbed_official 56:2b2a7cc13e28 869 {
mbed_official 112:53ace74b190c 870 #ifdef __MBED_CMSIS_RTOS_CM
mbed_official 56:2b2a7cc13e28 871 __iar_init_core();
mbed_official 56:2b2a7cc13e28 872 __iar_init_vfp();
mbed_official 56:2b2a7cc13e28 873
mbed_official 116:0788b1a76461 874 uint8_t low_level_init_needed_local;
mbed_official 49:77c8e4604045 875
mbed_official 116:0788b1a76461 876 low_level_init_needed_local = __low_level_init();
mbed_official 116:0788b1a76461 877 if (low_level_init_needed_local) {
mbed_official 49:77c8e4604045 878 __iar_data_init3();
mbed_official 56:2b2a7cc13e28 879 mbed_sdk_init();
mbed_official 68:d3d0e710b443 880 }
mbed_official 116:0788b1a76461 881 /* Store in a global variable after RAM has been initialized */
mbed_official 116:0788b1a76461 882 low_level_init_needed = low_level_init_needed_local;
mbed_official 112:53ace74b190c 883 #endif
mbed_official 49:77c8e4604045 884 osKernelInitialize();
mbed_official 112:53ace74b190c 885 #ifdef __MBED_CMSIS_RTOS_CM
Kojto 120:4dc938e301cc 886 set_stack_heap();
mbed_official 112:53ace74b190c 887 #endif
mbed_official 49:77c8e4604045 888 osThreadCreate(&os_thread_def_main, NULL);
mbed_official 116:0788b1a76461 889 osKernelStart();
mbed_official 116:0788b1a76461 890 /* osKernelStart should not return */
mbed_official 116:0788b1a76461 891 while (1);
mbed_official 49:77c8e4604045 892 }
mbed_official 49:77c8e4604045 893
mbed_official 49:77c8e4604045 894 #endif
mbed_official 49:77c8e4604045 895
mbed_official 49:77c8e4604045 896
mbed_official 49:77c8e4604045 897 /*----------------------------------------------------------------------------
mbed_official 49:77c8e4604045 898 * end of file
mbed_official 49:77c8e4604045 899 *---------------------------------------------------------------------------*/