Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RTX_CM_lib.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. #if defined (__CC_ARM)
  36. #pragma O3
  37. #define __USED __attribute__((used))
  38. #elif defined (__GNUC__)
  39. #pragma GCC optimize ("O3")
  40. #define __USED __attribute__((used))
  41. #elif defined (__ICCARM__)
  42. #define __USED __root
  43. #endif
  44. /*----------------------------------------------------------------------------
  45. * Definitions
  46. *---------------------------------------------------------------------------*/
  47. #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
  48. #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
  49. #define OS_TCB_SIZE 48
  50. #define OS_TMR_SIZE 8
  51. #if defined (__CC_ARM) && !defined (__MICROLIB)
  52. typedef void *OS_ID;
  53. typedef uint32_t OS_TID;
  54. typedef uint32_t OS_MUT[3];
  55. typedef uint32_t OS_RESULT;
  56. #define runtask_id() rt_tsk_self()
  57. #define mutex_init(m) rt_mut_init(m)
  58. #define mutex_wait(m) os_mut_wait(m,0xFFFF)
  59. #define mutex_rel(m) os_mut_release(m)
  60. extern OS_TID rt_tsk_self (void);
  61. extern void rt_mut_init (OS_ID mutex);
  62. extern OS_RESULT rt_mut_release (OS_ID mutex);
  63. extern OS_RESULT rt_mut_wait (OS_ID mutex, uint16_t timeout);
  64. #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
  65. #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex)
  66. OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex) __svc_indirect(0);
  67. OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0);
  68. #endif
  69. /*----------------------------------------------------------------------------
  70. * Global Variables
  71. *---------------------------------------------------------------------------*/
  72. #if (OS_TIMERS != 0)
  73. #define OS_TASK_CNT (OS_TASKCNT + 1)
  74. #else
  75. #define OS_TASK_CNT OS_TASKCNT
  76. #endif
  77. uint16_t const os_maxtaskrun = OS_TASK_CNT;
  78. uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT;
  79. uint32_t const os_trv = OS_TRV;
  80. uint8_t const os_flags = OS_RUNPRIV;
  81. /* Export following defines to uVision debugger. */
  82. __USED uint32_t const os_clockrate = OS_TICK;
  83. __USED uint32_t const os_timernum = 0;
  84. /* Stack for the os_idle_demon */
  85. unsigned int idle_task_stack[OS_IDLESTKSIZE];
  86. unsigned short const idle_task_stack_size = OS_IDLESTKSIZE;
  87. #ifndef OS_FIFOSZ
  88. #define OS_FIFOSZ 16
  89. #endif
  90. /* Fifo Queue buffer for ISR requests.*/
  91. uint32_t os_fifo[OS_FIFOSZ*2+1];
  92. uint8_t const os_fifo_size = OS_FIFOSZ;
  93. /* An array of Active task pointers. */
  94. void *os_active_TCB[OS_TASK_CNT];
  95. /* User Timers Resources */
  96. #if (OS_TIMERS != 0)
  97. extern void osTimerThread (void const *argument);
  98. osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ);
  99. osThreadId osThreadId_osTimerThread;
  100. osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
  101. osMessageQId osMessageQId_osTimerMessageQ;
  102. #else
  103. osThreadDef_t os_thread_def_osTimerThread = { NULL };
  104. osThreadId osThreadId_osTimerThread;
  105. osMessageQDef(osTimerMessageQ, 0, void *);
  106. osMessageQId osMessageQId_osTimerMessageQ;
  107. #endif
  108. /*----------------------------------------------------------------------------
  109. * RTX Optimizations (empty functions)
  110. *---------------------------------------------------------------------------*/
  111. #if OS_ROBIN == 0
  112. void rt_init_robin (void) {;}
  113. void rt_chk_robin (void) {;}
  114. #endif
  115. #if OS_STKCHECK == 0
  116. void rt_stk_check (void) {;}
  117. #endif
  118. /*----------------------------------------------------------------------------
  119. * Standard Library multithreading interface
  120. *---------------------------------------------------------------------------*/
  121. #if defined (__CC_ARM) && !defined (__MICROLIB)
  122. static OS_MUT std_libmutex[OS_MUTEXCNT];
  123. static uint32_t nr_mutex;
  124. /*--------------------------- _mutex_initialize -----------------------------*/
  125. int _mutex_initialize (OS_ID *mutex) {
  126. /* Allocate and initialize a system mutex. */
  127. if (nr_mutex >= OS_MUTEXCNT) {
  128. /* If you are here, you need to increase the number OS_MUTEXCNT. */
  129. error("Not enough stdlib mutexes\n");
  130. }
  131. *mutex = &std_libmutex[nr_mutex++];
  132. mutex_init (*mutex);
  133. return (1);
  134. }
  135. /*--------------------------- _mutex_acquire --------------------------------*/
  136. __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
  137. /* Acquire a system mutex, lock stdlib resources. */
  138. if (runtask_id ()) {
  139. /* RTX running, acquire a mutex. */
  140. mutex_wait (*mutex);
  141. }
  142. }
  143. /*--------------------------- _mutex_release --------------------------------*/
  144. __attribute__((used)) void _mutex_release (OS_ID *mutex) {
  145. /* Release a system mutex, unlock stdlib resources. */
  146. if (runtask_id ()) {
  147. /* RTX running, release a mutex. */
  148. mutex_rel (*mutex);
  149. }
  150. }
  151. #endif
  152. /*----------------------------------------------------------------------------
  153. * RTX Startup
  154. *---------------------------------------------------------------------------*/
  155. /* Main Thread definition */
  156. extern int main (void);
  157. osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 0, NULL};
  158. // This define should be probably moved to the CMSIS layer
  159. #if defined(TARGET_LPC1768)
  160. #define INITIAL_SP (0x10008000UL)
  161. #elif defined(TARGET_LPC11U24)
  162. #define INITIAL_SP (0x10002000UL)
  163. #elif defined(TARGET_LPC11U35_401) || defined(TARGET_LPC11U35_501) || defined(TARGET_LPCCAPPUCCINO)
  164. #define INITIAL_SP (0x10002000UL)
  165. #elif defined(TARGET_LPC1114)
  166. #define INITIAL_SP (0x10001000UL)
  167. #elif defined(TARGET_LPC812)
  168. #define INITIAL_SP (0x10001000UL)
  169. #elif defined(TARGET_LPC824)
  170. #define INITIAL_SP (0x10002000UL)
  171. #elif defined(TARGET_KL25Z)
  172. #define INITIAL_SP (0x20003000UL)
  173. #elif defined(TARGET_K64F)
  174. #define INITIAL_SP (0x20030000UL)
  175. #elif defined(TARGET_K22F)
  176. #define INITIAL_SP (0x20010000UL)
  177. #elif defined(TARGET_KL46Z)
  178. #define INITIAL_SP (0x20006000UL)
  179. #elif defined(TARGET_KL43Z)
  180. #define INITIAL_SP (0x20006000UL)
  181. #elif defined(TARGET_KL05Z)
  182. #define INITIAL_SP (0x20000C00UL)
  183. #elif defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM)
  184. #define INITIAL_SP (0x10010000UL)
  185. #elif defined(TARGET_LPC4330)
  186. #define INITIAL_SP (0x10008000UL)
  187. #elif defined(TARGET_LPC4337)
  188. #define INITIAL_SP (0x10008000UL)
  189. #elif defined(TARGET_LPC1347)
  190. #define INITIAL_SP (0x10002000UL)
  191. #elif defined(TARGET_STM32F100RB) || defined(TARGET_STM32F051R8)
  192. #define INITIAL_SP (0x20002000UL)
  193. #elif defined(TARGET_DISCO_F303VC)
  194. #define INITIAL_SP (0x2000A000UL)
  195. #elif defined(TARGET_STM32F407) || defined(TARGET_F407VG)
  196. #define INITIAL_SP (0x20020000UL)
  197. #elif defined(TARGET_STM32F401RE)
  198. #define INITIAL_SP (0x20018000UL)
  199. #elif defined(TARGET_LPC1549)
  200. #define INITIAL_SP (0x02009000UL)
  201. #elif defined(TARGET_LPC11U68)
  202. #define INITIAL_SP (0x10004000UL)
  203. #elif defined(TARGET_STM32F411RE)
  204. #define INITIAL_SP (0x20020000UL)
  205. #elif defined(TARGET_STM32F103RB)
  206. #define INITIAL_SP (0x20005000UL)
  207. #elif defined(TARGET_STM32F302R8)
  208. #define INITIAL_SP (0x20004000UL)
  209. #elif defined(TARGET_STM32F334R8)
  210. #define INITIAL_SP (0x20003000UL)
  211. #elif defined(TARGET_STM32F334C8)
  212. #define INITIAL_SP (0x20003000UL)
  213. #elif defined(TARGET_STM32F405RG)
  214. #define INITIAL_SP (0x20020000UL)
  215. #elif defined(TARGET_STM32F429ZI)
  216. #define INITIAL_SP (0x20030000UL)
  217. #elif defined(TARGET_STM32L053R8) || defined(TARGET_STM32L053C8)
  218. #define INITIAL_SP (0x20002000UL)
  219. #elif defined(TARGET_STM32F072RB)
  220. #define INITIAL_SP (0x20004000UL)
  221. #elif defined(TARGET_STM32F091RC)
  222. #define INITIAL_SP (0x20008000UL)
  223. #elif defined(TARGET_STM32F401VC)
  224. #define INITIAL_SP (0x20010000UL)
  225. #elif defined(TARGET_STM32F303RE)
  226. #define INITIAL_SP (0x20010000UL)
  227. #elif defined(TARGET_MAX32610) || defined(TARGET_MAX32600)
  228. #define INITIAL_SP (0x20008000UL)
  229. #elif defined(TARGET_TEENSY3_1)
  230. #define INITIAL_SP (0x20008000UL)
  231. #else
  232. #error "no target defined"
  233. #endif
  234. #ifdef __CC_ARM
  235. extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
  236. #define HEAP_START (Image$$RW_IRAM1$$ZI$$Limit)
  237. #elif defined(__GNUC__)
  238. extern uint32_t __end__[];
  239. #define HEAP_START (__end__)
  240. #elif defined(__ICCARM__)
  241. #pragma section="HEAP"
  242. #define HEAP_START (void *)__section_begin("HEAP")
  243. #endif
  244. void set_main_stack(void) {
  245. // That is the bottom of the main stack block: no collision detection
  246. os_thread_def_main.stack_pointer = HEAP_START;
  247. // Leave OS_SCHEDULERSTKSIZE words for the scheduler and interrupts
  248. os_thread_def_main.stacksize = (INITIAL_SP - (unsigned int)HEAP_START) - (OS_SCHEDULERSTKSIZE * 4);
  249. }
  250. #if defined (__CC_ARM)
  251. #ifdef __MICROLIB
  252. void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
  253. void _main_init (void) {
  254. osKernelInitialize();
  255. set_main_stack();
  256. osThreadCreate(&os_thread_def_main, NULL);
  257. osKernelStart();
  258. for (;;);
  259. }
  260. #else
  261. /* The single memory model is checking for stack collision at run time, verifing
  262. that the heap pointer is underneath the stack pointer.
  263. With the RTOS there is not only one stack above the heap, there are multiple
  264. stacks and some of them are underneath the heap pointer.
  265. */
  266. #pragma import(__use_two_region_memory)
  267. __asm void __rt_entry (void) {
  268. IMPORT __user_setup_stackheap
  269. IMPORT __rt_lib_init
  270. IMPORT os_thread_def_main
  271. IMPORT osKernelInitialize
  272. IMPORT set_main_stack
  273. IMPORT osKernelStart
  274. IMPORT osThreadCreate
  275. IMPORT exit
  276. BL __user_setup_stackheap
  277. MOV R1,R2
  278. BL __rt_lib_init
  279. BL osKernelInitialize
  280. BL set_main_stack
  281. LDR R0,=os_thread_def_main
  282. MOVS R1,#0
  283. BL osThreadCreate
  284. BL osKernelStart
  285. BL exit
  286. ALIGN
  287. }
  288. #endif
  289. #elif defined (__GNUC__)
  290. #ifdef __CS3__
  291. /* CS3 start_c routine.
  292. *
  293. * Copyright (c) 2006, 2007 CodeSourcery Inc
  294. *
  295. * The authors hereby grant permission to use, copy, modify, distribute,
  296. * and license this software and its documentation for any purpose, provided
  297. * that existing copyright notices are retained in all copies and that this
  298. * notice is included verbatim in any distributions. No written agreement,
  299. * license, or royalty fee is required for any of the authorized uses.
  300. * Modifications to this software may be copyrighted by their authors
  301. * and need not follow the licensing terms described here, provided that
  302. * the new terms are clearly indicated on the first page of each file where
  303. * they apply.
  304. */
  305. #include "cs3.h"
  306. extern void __libc_init_array (void);
  307. __attribute ((noreturn)) void __cs3_start_c (void){
  308. unsigned regions = __cs3_region_num;
  309. const struct __cs3_region *rptr = __cs3_regions;
  310. /* Initialize memory */
  311. for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
  312. long long *src = (long long *)rptr->init;
  313. long long *dst = (long long *)rptr->data;
  314. unsigned limit = rptr->init_size;
  315. unsigned count;
  316. if (src != dst)
  317. for (count = 0; count != limit; count += sizeof (long long))
  318. *dst++ = *src++;
  319. else
  320. dst = (long long *)((char *)dst + limit);
  321. limit = rptr->zero_size;
  322. for (count = 0; count != limit; count += sizeof (long long))
  323. *dst++ = 0;
  324. }
  325. /* Run initializers. */
  326. __libc_init_array ();
  327. osKernelInitialize();
  328. set_main_stack();
  329. osThreadCreate(&os_thread_def_main, NULL);
  330. osKernelStart();
  331. for (;;);
  332. }
  333. #else
  334. __attribute__((naked)) void software_init_hook (void) {
  335. __asm (
  336. ".syntax unified\n"
  337. ".thumb\n"
  338. "movs r0,#0\n"
  339. "movs r1,#0\n"
  340. "mov r4,r0\n"
  341. "mov r5,r1\n"
  342. "ldr r0,= __libc_fini_array\n"
  343. "bl atexit\n"
  344. "bl __libc_init_array\n"
  345. "mov r0,r4\n"
  346. "mov r1,r5\n"
  347. "bl osKernelInitialize\n"
  348. "bl set_main_stack\n"
  349. "ldr r0,=os_thread_def_main\n"
  350. "movs r1,#0\n"
  351. "bl osThreadCreate\n"
  352. "bl osKernelStart\n"
  353. "bl exit\n"
  354. );
  355. }
  356. #endif
  357. #elif defined (__ICCARM__)
  358. extern void* __vector_table;
  359. extern int __low_level_init(void);
  360. extern void __iar_data_init3(void);
  361. extern __weak void __iar_init_core( void );
  362. extern __weak void __iar_init_vfp( void );
  363. extern void __iar_dynamic_initialization(void);
  364. extern void mbed_sdk_init(void);
  365. extern void exit(int arg);
  366. #pragma required=__vector_table
  367. void __iar_program_start( void )
  368. {
  369. __iar_init_core();
  370. __iar_init_vfp();
  371. int a;
  372. if (__low_level_init() != 0) {
  373. __iar_data_init3();
  374. mbed_sdk_init();
  375. __iar_dynamic_initialization();
  376. }
  377. osKernelInitialize();
  378. set_main_stack();
  379. osThreadCreate(&os_thread_def_main, NULL);
  380. a = osKernelStart();
  381. exit(a);
  382. }
  383. #endif
  384. /*----------------------------------------------------------------------------
  385. * end of file
  386. *---------------------------------------------------------------------------*/