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 *---------------------------------------------------------------------------*/
35 #if defined (__CC_ARM)
37 #define __USED __attribute__((used))
38 #elif defined (__GNUC__)
39 #pragma GCC optimize ("O3")
40 #define __USED __attribute__((used))
41 #elif defined (__ICCARM__)
46 /*----------------------------------------------------------------------------
48 *---------------------------------------------------------------------------*/
50 #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
51 #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
53 #define OS_TCB_SIZE 48
56 #if defined (__CC_ARM) && !defined (__MICROLIB)
59 typedef uint32_t OS_TID;
60 typedef uint32_t OS_MUT[3];
61 typedef uint32_t OS_RESULT;
63 #define runtask_id() rt_tsk_self()
64 #define mutex_init(m) rt_mut_init(m)
65 #define mutex_wait(m) os_mut_wait(m,0xFFFF)
66 #define mutex_rel(m) os_mut_release(m)
68 extern OS_TID rt_tsk_self (void);
69 extern void rt_mut_init (OS_ID mutex);
70 extern OS_RESULT rt_mut_release (OS_ID mutex);
71 extern OS_RESULT rt_mut_wait (OS_ID mutex, uint16_t timeout);
73 #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
74 #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex)
76 OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex) __svc_indirect(0);
77 OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0);
82 /*----------------------------------------------------------------------------
84 *---------------------------------------------------------------------------*/
87 #define OS_TASK_CNT (OS_TASKCNT + 1)
88 #define OS_PRIV_CNT (OS_PRIVCNT + 2)
89 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ))
91 #define OS_TASK_CNT OS_TASKCNT
92 #define OS_PRIV_CNT (OS_PRIVCNT + 1)
93 #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE))
96 uint16_t const os_maxtaskrun = OS_TASK_CNT;
97 uint32_t const os_stackinfo = (OS_STKCHECK<<24)| (OS_PRIV_CNT<<16) | (OS_STKSIZE*4);
98 uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT;
99 uint32_t const os_trv = OS_TRV;
100 uint8_t const os_flags = OS_RUNPRIV;
102 /* Export following defines to uVision debugger. */
103 __USED uint32_t const os_clockrate = OS_TICK;
104 __USED uint32_t const os_timernum = 0;
106 /* Memory pool for TCB allocation */
107 _declare_box (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT);
108 uint16_t const mp_tcb_size = sizeof(mp_tcb);
110 /* Memory pool for System stack allocation (+os_idle_demon). */
111 _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1);
112 uint32_t const mp_stk_size = sizeof(mp_stk);
114 /* Memory pool for user specified stack allocation (+main, +timer) */
115 uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
116 uint32_t const os_stack_sz = sizeof(os_stack_mem);
122 /* Fifo Queue buffer for ISR requests.*/
123 uint32_t os_fifo[OS_FIFOSZ*2+1];
124 uint8_t const os_fifo_size = OS_FIFOSZ;
126 /* An array of Active task pointers. */
127 void *os_active_TCB[OS_TASK_CNT];
129 /* User Timers Resources */
131 extern void osTimerThread (void const *argument);
132 #if defined (__MBED_CMSIS_RTOS_CA9)
133 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ);
135 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ);
137 osThreadId osThreadId_osTimerThread;
138 osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
139 osMessageQId osMessageQId_osTimerMessageQ;
141 osThreadDef_t os_thread_def_osTimerThread = { NULL };
142 osThreadId osThreadId_osTimerThread;
143 osMessageQDef(osTimerMessageQ, 0, void *);
144 osMessageQId osMessageQId_osTimerMessageQ;
147 /* Legacy RTX User Timers not used */
149 uint32_t const *m_tmr = NULL;
150 uint16_t const mp_tmr_size = 0;
152 #if defined (__CC_ARM) && !defined (__MICROLIB)
153 /* A memory space for arm standard library. */
154 static uint32_t std_libspace[OS_TASK_CNT][96/4];
155 static OS_MUT std_libmutex[OS_MUTEXCNT];
156 static uint32_t nr_mutex;
157 extern void *__libspace_start;
161 /*----------------------------------------------------------------------------
162 * RTX Optimizations (empty functions)
163 *---------------------------------------------------------------------------*/
166 void rt_init_robin (void) {;}
167 void rt_chk_robin (void) {;}
171 void rt_stk_check (void) {;}
175 /*----------------------------------------------------------------------------
176 * Standard Library multithreading interface
177 *---------------------------------------------------------------------------*/
179 #if defined (__CC_ARM) && !defined (__MICROLIB)
181 /*--------------------------- __user_perthread_libspace ---------------------*/
183 void *__user_perthread_libspace (void) {
184 /* Provide a separate libspace for each task. */
189 /* RTX not running yet. */
190 return (&__libspace_start);
192 return ((void *)&std_libspace[idx-1]);
195 /*--------------------------- _mutex_initialize -----------------------------*/
197 int _mutex_initialize (OS_ID *mutex) {
198 /* Allocate and initialize a system mutex. */
200 if (nr_mutex >= OS_MUTEXCNT) {
201 /* If you are here, you need to increase the number OS_MUTEXCNT. */
204 *mutex = &std_libmutex[nr_mutex++];
210 /*--------------------------- _mutex_acquire --------------------------------*/
212 __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
213 /* Acquire a system mutex, lock stdlib resources. */
215 /* RTX running, acquire a mutex. */
221 /*--------------------------- _mutex_release --------------------------------*/
223 __attribute__((used)) void _mutex_release (OS_ID *mutex) {
224 /* Release a system mutex, unlock stdlib resources. */
226 /* RTX running, release a mutex. */
234 /*----------------------------------------------------------------------------
236 *---------------------------------------------------------------------------*/
238 /* Main Thread definition */
239 extern int main (void);
240 osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1, 4*OS_MAINSTKSIZE };
243 #if defined (__CC_ARM)
246 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
247 #if __TARGET_ARCH_ARM
251 void _main_init (void) {
252 osKernelInitialize();
253 osThreadCreate(&os_thread_def_main, NULL);
257 #if __TARGET_ARCH_ARM
261 __asm void __rt_entry (void) {
263 IMPORT __user_setup_stackheap
265 IMPORT os_thread_def_main
266 IMPORT osKernelInitialize
268 IMPORT osThreadCreate
271 BL __user_setup_stackheap
274 BL osKernelInitialize
275 LDR R0,=os_thread_def_main
285 #elif defined (__GNUC__)
289 /* CS3 start_c routine.
291 * Copyright (c) 2006, 2007 CodeSourcery Inc
293 * The authors hereby grant permission to use, copy, modify, distribute,
294 * and license this software and its documentation for any purpose, provided
295 * that existing copyright notices are retained in all copies and that this
296 * notice is included verbatim in any distributions. No written agreement,
297 * license, or royalty fee is required for any of the authorized uses.
298 * Modifications to this software may be copyrighted by their authors
299 * and need not follow the licensing terms described here, provided that
300 * the new terms are clearly indicated on the first page of each file where
306 extern void __libc_init_array (void);
308 __attribute ((noreturn)) void __cs3_start_c (void){
309 unsigned regions = __cs3_region_num;
310 const struct __cs3_region *rptr = __cs3_regions;
312 /* Initialize memory */
313 for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
314 long long *src = (long long *)rptr->init;
315 long long *dst = (long long *)rptr->data;
316 unsigned limit = rptr->init_size;
320 for (count = 0; count != limit; count += sizeof (long long))
323 dst = (long long *)((char *)dst + limit);
324 limit = rptr->zero_size;
325 for (count = 0; count != limit; count += sizeof (long long))
329 /* Run initializers. */
330 __libc_init_array ();
332 osKernelInitialize();
333 osThreadCreate(&os_thread_def_main, NULL);
340 __attribute__((naked)) void software_init_hook (void) {
348 "ldr r0,= __libc_fini_array\n"
350 "bl __libc_init_array\n"
353 "bl osKernelInitialize\n"
354 "ldr r0,=os_thread_def_main\n"
356 "bl osThreadCreate\n"
364 #elif defined (__ICCARM__)
366 extern int __low_level_init(void);
367 extern void __iar_data_init3(void);
368 extern void exit(int arg);
370 __noreturn __stackless void __cmain(void) {
373 if (__low_level_init() != 0) {
376 osKernelInitialize();
377 osThreadCreate(&os_thread_def_main, NULL);
385 /*----------------------------------------------------------------------------
387 *---------------------------------------------------------------------------*/