1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: RTX Kernel System Configuration
7 *----------------------------------------------------------------------------
9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
10 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * - Neither the name of ARM nor the names of its contributors may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *---------------------------------------------------------------------------*/
34 #include "mbed_error.h"
36 #if defined (__CC_ARM)
38 #define __USED __attribute__((used))
39 #elif defined (__GNUC__)
40 #pragma GCC optimize ("O3")
41 #define __USED __attribute__((used))
42 #elif defined (__ICCARM__)
47 /*----------------------------------------------------------------------------
49 *---------------------------------------------------------------------------*/
51 #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
52 #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
54 #define OS_TCB_SIZE 48
57 #if defined (__CC_ARM) && !defined (__MICROLIB)
60 typedef uint32_t OS_TID
;
61 typedef uint32_t OS_MUT
[3];
62 typedef uint32_t OS_RESULT
;
64 #define runtask_id() rt_tsk_self()
65 #define mutex_init(m) rt_mut_init(m)
66 #define mutex_wait(m) os_mut_wait(m,0xFFFF)
67 #define mutex_rel(m) os_mut_release(m)
69 extern OS_TID
rt_tsk_self (void);
70 extern void rt_mut_init (OS_ID mutex
);
71 extern OS_RESULT
rt_mut_release (OS_ID mutex
);
72 extern OS_RESULT
rt_mut_wait (OS_ID mutex
, uint16_t timeout
);
74 #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
75 #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex)
77 OS_RESULT
_os_mut_release (uint32_t p
, OS_ID mutex
) __svc_indirect(0);
78 OS_RESULT
_os_mut_wait (uint32_t p
, OS_ID mutex
, uint16_t timeout
) __svc_indirect(0);
83 /*----------------------------------------------------------------------------
85 *---------------------------------------------------------------------------*/
88 #define OS_TASK_CNT (OS_TASKCNT + 1)
90 #define OS_TASK_CNT OS_TASKCNT
93 uint16_t const os_maxtaskrun
= OS_TASK_CNT
;
94 uint32_t const os_rrobin
= (OS_ROBIN
<< 16) | OS_ROBINTOUT
;
95 uint32_t const os_trv
= OS_TRV
;
96 uint8_t const os_flags
= OS_RUNPRIV
;
98 /* Export following defines to uVision debugger. */
99 __USED
uint32_t const os_clockrate
= OS_TICK
;
100 __USED
uint32_t const os_timernum
= 0;
102 /* Stack for the os_idle_demon */
103 unsigned int idle_task_stack
[OS_IDLESTKSIZE
];
104 unsigned short const idle_task_stack_size
= OS_IDLESTKSIZE
;
110 /* Fifo Queue buffer for ISR requests.*/
111 uint32_t os_fifo
[OS_FIFOSZ
*2+1];
112 uint8_t const os_fifo_size
= OS_FIFOSZ
;
114 /* An array of Active task pointers. */
115 void *os_active_TCB
[OS_TASK_CNT
];
117 /* User Timers Resources */
119 extern void osTimerThread (void const *argument
);
120 osThreadDef(osTimerThread
, (osPriority
)(OS_TIMERPRIO
-3), 4*OS_TIMERSTKSZ
);
121 osThreadId osThreadId_osTimerThread
;
122 osMessageQDef(osTimerMessageQ
, OS_TIMERCBQS
, void *);
123 osMessageQId osMessageQId_osTimerMessageQ
;
125 osThreadDef_t os_thread_def_osTimerThread
= { NULL
};
126 osThreadId osThreadId_osTimerThread
;
127 osMessageQDef(osTimerMessageQ
, 0, void *);
128 osMessageQId osMessageQId_osTimerMessageQ
;
132 /*----------------------------------------------------------------------------
133 * RTX Optimizations (empty functions)
134 *---------------------------------------------------------------------------*/
137 void rt_init_robin (void) {;}
138 void rt_chk_robin (void) {;}
142 void rt_stk_check (void) {;}
146 /*----------------------------------------------------------------------------
147 * Standard Library multithreading interface
148 *---------------------------------------------------------------------------*/
150 #if defined (__CC_ARM) && !defined (__MICROLIB)
151 static OS_MUT std_libmutex
[OS_MUTEXCNT
];
152 static uint32_t nr_mutex
;
154 /*--------------------------- _mutex_initialize -----------------------------*/
156 int _mutex_initialize (OS_ID
*mutex
) {
157 /* Allocate and initialize a system mutex. */
159 if (nr_mutex
>= OS_MUTEXCNT
) {
160 /* If you are here, you need to increase the number OS_MUTEXCNT. */
161 error("Not enough stdlib mutexes\n");
163 *mutex
= &std_libmutex
[nr_mutex
++];
169 /*--------------------------- _mutex_acquire --------------------------------*/
171 __attribute__((used
)) void _mutex_acquire (OS_ID
*mutex
) {
172 /* Acquire a system mutex, lock stdlib resources. */
174 /* RTX running, acquire a mutex. */
180 /*--------------------------- _mutex_release --------------------------------*/
182 __attribute__((used
)) void _mutex_release (OS_ID
*mutex
) {
183 /* Release a system mutex, unlock stdlib resources. */
185 /* RTX running, release a mutex. */
193 /*----------------------------------------------------------------------------
195 *---------------------------------------------------------------------------*/
197 /* Main Thread definition */
198 extern int main (void);
199 osThreadDef_t os_thread_def_main
= {(os_pthread
)main
, osPriorityNormal
, 0, NULL
};
201 // This define should be probably moved to the CMSIS layer
202 #if defined(TARGET_LPC1768)
203 #define INITIAL_SP (0x10008000UL)
205 #elif defined(TARGET_LPC11U24)
206 #define INITIAL_SP (0x10002000UL)
208 #elif defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPCCAPPUCCINO)
209 #define INITIAL_SP (0x10002000UL)
211 #elif defined(TARGET_LPC1114)
212 #define INITIAL_SP (0x10001000UL)
214 #elif defined(TARGET_LPC812)
215 #define INITIAL_SP (0x10001000UL)
217 #elif defined(TARGET_LPC824)
218 #define INITIAL_SP (0x10002000UL)
220 #elif defined(TARGET_KL25Z)
221 #define INITIAL_SP (0x20003000UL)
223 #elif defined(TARGET_K64F)
224 #define INITIAL_SP (0x20030000UL)
226 #elif defined(TARGET_K22F)
227 #define INITIAL_SP (0x20010000UL)
229 #elif defined(TARGET_KL46Z)
230 #define INITIAL_SP (0x20006000UL)
232 #elif defined(TARGET_KL43Z)
233 #define INITIAL_SP (0x20006000UL)
235 #elif defined(TARGET_KL05Z)
236 #define INITIAL_SP (0x20000C00UL)
238 #elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
239 #define INITIAL_SP (0x10010000UL)
241 #elif defined(TARGET_LPC4330)
242 #define INITIAL_SP (0x10008000UL)
244 #elif defined(TARGET_LPC4337)
245 #define INITIAL_SP (0x10008000UL)
247 #elif defined(TARGET_LPC1347)
248 #define INITIAL_SP (0x10002000UL)
250 #elif defined(TARGET_STM32F100RB) || defined(TARGET_STM32F051R8)
251 #define INITIAL_SP (0x20002000UL)
253 #elif defined(TARGET_DISCO_F303VC)
254 #define INITIAL_SP (0x2000A000UL)
256 #elif defined(TARGET_STM32F407) || defined(TARGET_F407VG)
257 #define INITIAL_SP (0x20020000UL)
259 #elif defined(TARGET_STM32F401RE)
260 #define INITIAL_SP (0x20018000UL)
262 #elif defined(TARGET_LPC1549)
263 #define INITIAL_SP (0x02009000UL)
265 #elif defined(TARGET_LPC11U68)
266 #define INITIAL_SP (0x10004000UL)
268 #elif defined(TARGET_STM32F411RE)
269 #define INITIAL_SP (0x20020000UL)
271 #elif defined(TARGET_STM32F103RB)
272 #define INITIAL_SP (0x20005000UL)
274 #elif defined(TARGET_STM32F302R8)
275 #define INITIAL_SP (0x20004000UL)
277 #elif defined(TARGET_STM32F334R8)
278 #define INITIAL_SP (0x20003000UL)
280 #elif defined(TARGET_STM32F334C8)
281 #define INITIAL_SP (0x20003000UL)
283 #elif defined(TARGET_STM32F405RG)
284 #define INITIAL_SP (0x20020000UL)
286 #elif defined(TARGET_STM32F429ZI)
287 #define INITIAL_SP (0x20030000UL)
289 #elif defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)
290 #define INITIAL_SP (0x20002000UL)
292 #elif defined(TARGET_STM32F072RB)
293 #define INITIAL_SP (0x20004000UL)
295 #elif defined(TARGET_STM32F091RC)
296 #define INITIAL_SP (0x20008000UL)
298 #elif defined(TARGET_STM32F401VC)
299 #define INITIAL_SP (0x20010000UL)
301 #elif defined(TARGET_STM32F303RE)
302 #define INITIAL_SP (0x20010000UL)
304 #elif defined(TARGET_MAX32610) || defined(TARGET_MAX32600)
305 #define INITIAL_SP (0x20008000UL)
307 #elif defined(TARGET_TEENSY3_1)
308 #define INITIAL_SP (0x20008000UL)
311 #error "no target defined"
316 extern uint32_t Image$$RW_IRAM1$$ZI$$Limit
[];
317 #define HEAP_START (Image$$RW_IRAM1$$ZI$$Limit)
318 #elif defined(__GNUC__)
319 extern uint32_t __end__
[];
320 #define HEAP_START (__end__)
321 #elif defined(__ICCARM__)
322 #pragma section="HEAP"
323 #define HEAP_START (void *)__section_begin("HEAP")
326 void set_main_stack(void) {
327 // That is the bottom of the main stack block: no collision detection
328 os_thread_def_main
.stack_pointer
= HEAP_START
;
330 // Leave OS_SCHEDULERSTKSIZE words for the scheduler and interrupts
331 os_thread_def_main
.stacksize
= (INITIAL_SP
- (unsigned int)HEAP_START
) - (OS_SCHEDULERSTKSIZE
* 4);
334 #if defined (__CC_ARM)
336 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
337 void _main_init (void) {
338 osKernelInitialize();
340 osThreadCreate(&os_thread_def_main
, NULL
);
346 /* The single memory model is checking for stack collision at run time, verifing
347 that the heap pointer is underneath the stack pointer.
349 With the RTOS there is not only one stack above the heap, there are multiple
350 stacks and some of them are underneath the heap pointer.
352 #pragma import(__use_two_region_memory)
354 __asm
void __rt_entry (void) {
356 IMPORT __user_setup_stackheap
358 IMPORT os_thread_def_main
359 IMPORT osKernelInitialize
360 IMPORT set_main_stack
362 IMPORT osThreadCreate
365 BL __user_setup_stackheap
368 BL osKernelInitialize
370 LDR R0
,=os_thread_def_main
380 #elif defined (__GNUC__)
384 /* CS3 start_c routine.
386 * Copyright (c) 2006, 2007 CodeSourcery Inc
388 * The authors hereby grant permission to use, copy, modify, distribute,
389 * and license this software and its documentation for any purpose, provided
390 * that existing copyright notices are retained in all copies and that this
391 * notice is included verbatim in any distributions. No written agreement,
392 * license, or royalty fee is required for any of the authorized uses.
393 * Modifications to this software may be copyrighted by their authors
394 * and need not follow the licensing terms described here, provided that
395 * the new terms are clearly indicated on the first page of each file where
401 extern void __libc_init_array (void);
403 __attribute ((noreturn
)) void __cs3_start_c (void){
404 unsigned regions
= __cs3_region_num
;
405 const struct __cs3_region
*rptr
= __cs3_regions
;
407 /* Initialize memory */
408 for (regions
= __cs3_region_num
, rptr
= __cs3_regions
; regions
--; rptr
++) {
409 long long *src
= (long long *)rptr
->init
;
410 long long *dst
= (long long *)rptr
->data
;
411 unsigned limit
= rptr
->init_size
;
415 for (count
= 0; count
!= limit
; count
+= sizeof (long long))
418 dst
= (long long *)((char *)dst
+ limit
);
419 limit
= rptr
->zero_size
;
420 for (count
= 0; count
!= limit
; count
+= sizeof (long long))
424 /* Run initializers. */
425 __libc_init_array ();
427 osKernelInitialize();
429 osThreadCreate(&os_thread_def_main
, NULL
);
436 __attribute__((naked
)) void software_init_hook (void) {
444 "ldr r0,= __libc_fini_array\n"
446 "bl __libc_init_array\n"
449 "bl osKernelInitialize\n"
450 "bl set_main_stack\n"
451 "ldr r0,=os_thread_def_main\n"
453 "bl osThreadCreate\n"
461 #elif defined (__ICCARM__)
463 extern void* __vector_table
;
464 extern int __low_level_init(void);
465 extern void __iar_data_init3(void);
466 extern __weak
void __iar_init_core( void );
467 extern __weak
void __iar_init_vfp( void );
468 extern void __iar_dynamic_initialization(void);
469 extern void mbed_sdk_init(void);
470 extern void exit(int arg
);
472 #pragma required=__vector_table
473 void __iar_program_start( void )
480 if (__low_level_init() != 0) {
483 __iar_dynamic_initialization();
485 osKernelInitialize();
487 osThreadCreate(&os_thread_def_main
, NULL
);
496 /*----------------------------------------------------------------------------
498 *---------------------------------------------------------------------------*/