]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_M / RTX_CM_lib.h
1 /*----------------------------------------------------------------------------
2  *      RL-ARM - RTX
3  *----------------------------------------------------------------------------
4  *      Name:    RTX_CM_LIB.H
5  *      Purpose: RTX Kernel System Configuration
6  *      Rev.:    V4.60
7  *----------------------------------------------------------------------------
8  *
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.
21  *
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"
35
36 #if   defined (__CC_ARM)
37 #pragma O3
38 #define __USED __attribute__((used))
39 #elif defined (__GNUC__)
40 #pragma GCC optimize ("O3")
41 #define __USED __attribute__((used))
42 #elif defined (__ICCARM__)
43 #define __USED __root
44 #endif
45
46
47 /*----------------------------------------------------------------------------
48  *      Definitions
49  *---------------------------------------------------------------------------*/
50
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]
53
54 #define OS_TCB_SIZE     48
55 #define OS_TMR_SIZE     8
56
57 #if defined (__CC_ARM) && !defined (__MICROLIB)
58
59 typedef void    *OS_ID;
60 typedef uint32_t OS_TID;
61 typedef uint32_t OS_MUT[3];
62 typedef uint32_t OS_RESULT;
63
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)
68
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);
73
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)
76
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);
79
80 #endif
81
82
83 /*----------------------------------------------------------------------------
84  *      Global Variables
85  *---------------------------------------------------------------------------*/
86
87 #if (OS_TIMERS != 0)
88 #define OS_TASK_CNT (OS_TASKCNT + 1)
89 #else
90 #define OS_TASK_CNT  OS_TASKCNT
91 #endif
92
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;
97
98 /* Export following defines to uVision debugger. */
99 __USED uint32_t const os_clockrate = OS_TICK;
100 __USED uint32_t const os_timernum  = 0;
101
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;
105
106 #ifndef OS_FIFOSZ
107  #define OS_FIFOSZ      16
108 #endif
109
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;
113
114 /* An array of Active task pointers. */
115 void *os_active_TCB[OS_TASK_CNT];
116
117 /* User Timers Resources */
118 #if (OS_TIMERS != 0)
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;
124 #else
125 osThreadDef_t os_thread_def_osTimerThread = { NULL };
126 osThreadId osThreadId_osTimerThread;
127 osMessageQDef(osTimerMessageQ, 0, void *);
128 osMessageQId osMessageQId_osTimerMessageQ;
129 #endif
130
131
132 /*----------------------------------------------------------------------------
133  *      RTX Optimizations (empty functions)
134  *---------------------------------------------------------------------------*/
135
136 #if OS_ROBIN == 0
137  void rt_init_robin (void) {;}
138  void rt_chk_robin  (void) {;}
139 #endif
140
141 #if OS_STKCHECK == 0
142  void rt_stk_check  (void) {;}
143 #endif
144
145
146 /*----------------------------------------------------------------------------
147  *      Standard Library multithreading interface
148  *---------------------------------------------------------------------------*/
149
150 #if defined (__CC_ARM) && !defined (__MICROLIB)
151  static OS_MUT   std_libmutex[OS_MUTEXCNT];
152  static uint32_t nr_mutex;
153
154  /*--------------------------- _mutex_initialize -----------------------------*/
155
156 int _mutex_initialize (OS_ID *mutex) {
157   /* Allocate and initialize a system mutex. */
158
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");
162   }
163   *mutex = &std_libmutex[nr_mutex++];
164   mutex_init (*mutex);
165   return (1);
166 }
167
168
169 /*--------------------------- _mutex_acquire --------------------------------*/
170
171 __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
172   /* Acquire a system mutex, lock stdlib resources. */
173   if (runtask_id ()) {
174     /* RTX running, acquire a mutex. */
175     mutex_wait (*mutex);
176   }
177 }
178
179
180 /*--------------------------- _mutex_release --------------------------------*/
181
182 __attribute__((used)) void _mutex_release (OS_ID *mutex) {
183   /* Release a system mutex, unlock stdlib resources. */
184   if (runtask_id ()) {
185     /* RTX running, release a mutex. */
186     mutex_rel (*mutex);
187   }
188 }
189
190 #endif
191
192
193 /*----------------------------------------------------------------------------
194  *      RTX Startup
195  *---------------------------------------------------------------------------*/
196
197 /* Main Thread definition */
198 extern int main (void);
199 osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 0, NULL};
200
201 // This define should be probably moved to the CMSIS layer
202 #if   defined(TARGET_LPC1768)
203 #define INITIAL_SP            (0x10008000UL)
204
205 #elif defined(TARGET_LPC11U24)
206 #define INITIAL_SP            (0x10002000UL)
207
208 #elif defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPCCAPPUCCINO)
209 #define INITIAL_SP            (0x10002000UL)
210
211 #elif defined(TARGET_LPC1114)
212 #define INITIAL_SP            (0x10001000UL)
213
214 #elif defined(TARGET_LPC812)
215 #define INITIAL_SP            (0x10001000UL)
216
217 #elif defined(TARGET_LPC824)
218 #define INITIAL_SP            (0x10002000UL)
219
220 #elif defined(TARGET_KL25Z)
221 #define INITIAL_SP            (0x20003000UL)
222
223 #elif defined(TARGET_K64F)
224 #define INITIAL_SP            (0x20030000UL)
225
226 #elif defined(TARGET_K22F)
227 #define INITIAL_SP            (0x20010000UL)
228
229 #elif defined(TARGET_KL46Z)
230 #define INITIAL_SP            (0x20006000UL)
231
232 #elif defined(TARGET_KL43Z)
233 #define INITIAL_SP            (0x20006000UL)
234
235 #elif defined(TARGET_KL05Z)
236 #define INITIAL_SP            (0x20000C00UL)
237
238 #elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
239 #define INITIAL_SP            (0x10010000UL)
240
241 #elif defined(TARGET_LPC4330)
242 #define INITIAL_SP            (0x10008000UL)
243
244 #elif defined(TARGET_LPC4337)
245 #define INITIAL_SP            (0x10008000UL)
246
247 #elif defined(TARGET_LPC1347)
248 #define INITIAL_SP            (0x10002000UL)
249
250 #elif defined(TARGET_STM32F100RB) || defined(TARGET_STM32F051R8)
251 #define INITIAL_SP            (0x20002000UL)
252
253 #elif defined(TARGET_DISCO_F303VC)
254 #define INITIAL_SP            (0x2000A000UL)
255
256 #elif defined(TARGET_STM32F407) || defined(TARGET_F407VG)
257 #define INITIAL_SP            (0x20020000UL)
258
259 #elif defined(TARGET_STM32F401RE)
260 #define INITIAL_SP            (0x20018000UL)
261
262 #elif defined(TARGET_LPC1549)
263 #define INITIAL_SP            (0x02009000UL)
264
265 #elif defined(TARGET_LPC11U68)
266 #define INITIAL_SP            (0x10004000UL)
267
268 #elif defined(TARGET_STM32F411RE)
269 #define INITIAL_SP            (0x20020000UL)
270
271 #elif defined(TARGET_STM32F103RB)
272 #define INITIAL_SP            (0x20005000UL)
273
274 #elif defined(TARGET_STM32F302R8)
275 #define INITIAL_SP            (0x20004000UL)
276
277 #elif  defined(TARGET_STM32F334R8)
278 #define INITIAL_SP            (0x20003000UL)
279
280 #elif  defined(TARGET_STM32F334C8)
281 #define INITIAL_SP            (0x20003000UL)
282
283 #elif  defined(TARGET_STM32F405RG)
284 #define INITIAL_SP            (0x20020000UL)
285
286 #elif defined(TARGET_STM32F429ZI)
287 #define INITIAL_SP            (0x20030000UL)
288
289 #elif defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)
290 #define INITIAL_SP            (0x20002000UL)
291
292 #elif defined(TARGET_STM32F072RB)
293 #define INITIAL_SP            (0x20004000UL)
294
295 #elif defined(TARGET_STM32F091RC)
296 #define INITIAL_SP            (0x20008000UL)
297
298 #elif defined(TARGET_STM32F401VC)
299 #define INITIAL_SP            (0x20010000UL)
300
301 #elif defined(TARGET_STM32F303RE)
302 #define INITIAL_SP            (0x20010000UL)
303
304 #elif defined(TARGET_MAX32610) || defined(TARGET_MAX32600)
305 #define INITIAL_SP            (0x20008000UL)
306
307 #elif defined(TARGET_TEENSY3_1)
308 #define INITIAL_SP            (0x20008000UL)
309
310 #else
311 #error "no target defined"
312
313 #endif
314
315 #ifdef __CC_ARM
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")
324 #endif
325
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;
329
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);
332 }
333
334 #if defined (__CC_ARM)
335 #ifdef __MICROLIB
336 void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
337 void _main_init (void) {
338   osKernelInitialize();
339   set_main_stack();
340   osThreadCreate(&os_thread_def_main, NULL);
341   osKernelStart();
342   for (;;);
343 }
344 #else
345
346 /* The single memory model is checking for stack collision at run time, verifing
347    that the heap pointer is underneath the stack pointer.
348
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.
351 */
352 #pragma import(__use_two_region_memory)
353
354 __asm void __rt_entry (void) {
355
356   IMPORT  __user_setup_stackheap
357   IMPORT  __rt_lib_init
358   IMPORT  os_thread_def_main
359   IMPORT  osKernelInitialize
360   IMPORT  set_main_stack
361   IMPORT  osKernelStart
362   IMPORT  osThreadCreate
363   IMPORT  exit
364
365   BL      __user_setup_stackheap
366   MOV     R1,R2
367   BL      __rt_lib_init
368   BL      osKernelInitialize
369   BL      set_main_stack
370   LDR     R0,=os_thread_def_main
371   MOVS    R1,#0
372   BL      osThreadCreate
373   BL      osKernelStart
374   BL      exit
375
376   ALIGN
377 }
378 #endif
379
380 #elif defined (__GNUC__)
381
382 #ifdef __CS3__
383
384 /* CS3 start_c routine.
385  *
386  * Copyright (c) 2006, 2007 CodeSourcery Inc
387  *
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
396  * they apply.
397  */
398
399 #include "cs3.h"
400
401 extern void __libc_init_array (void);
402
403 __attribute ((noreturn)) void __cs3_start_c (void){
404   unsigned regions = __cs3_region_num;
405   const struct __cs3_region *rptr = __cs3_regions;
406
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;
412     unsigned count;
413
414     if (src != dst)
415       for (count = 0; count != limit; count += sizeof (long long))
416         *dst++ = *src++;
417     else
418       dst = (long long *)((char *)dst + limit);
419     limit = rptr->zero_size;
420     for (count = 0; count != limit; count += sizeof (long long))
421       *dst++ = 0;
422   }
423
424   /* Run initializers.  */
425   __libc_init_array ();
426
427   osKernelInitialize();
428   set_main_stack();
429   osThreadCreate(&os_thread_def_main, NULL);
430   osKernelStart();
431   for (;;);
432 }
433
434 #else
435
436 __attribute__((naked)) void software_init_hook (void) {
437   __asm (
438     ".syntax unified\n"
439     ".thumb\n"
440     "movs r0,#0\n"
441     "movs r1,#0\n"
442     "mov  r4,r0\n"
443     "mov  r5,r1\n"
444     "ldr  r0,= __libc_fini_array\n"
445     "bl   atexit\n"
446     "bl   __libc_init_array\n"
447     "mov  r0,r4\n"
448     "mov  r1,r5\n"
449     "bl   osKernelInitialize\n"
450     "bl   set_main_stack\n"
451     "ldr  r0,=os_thread_def_main\n"
452     "movs r1,#0\n"
453     "bl   osThreadCreate\n"
454     "bl   osKernelStart\n"
455     "bl   exit\n"
456   );
457 }
458
459 #endif
460
461 #elif defined (__ICCARM__)
462
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);
471
472 #pragma required=__vector_table
473 void __iar_program_start( void )
474 {
475   __iar_init_core();
476   __iar_init_vfp();
477
478   int a;
479
480   if (__low_level_init() != 0) {
481     __iar_data_init3();
482     mbed_sdk_init();
483     __iar_dynamic_initialization();
484   }
485   osKernelInitialize();
486   set_main_stack();
487   osThreadCreate(&os_thread_def_main, NULL);
488   a = osKernelStart();
489   exit(a);
490
491 }
492
493 #endif
494
495
496 /*----------------------------------------------------------------------------
497  * end of file
498  *---------------------------------------------------------------------------*/
499
500