Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RTX_CM_lib.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #if defined (__CC_ARM)
  35. #pragma O3
  36. #define __USED __attribute__((used))
  37. #elif defined (__GNUC__)
  38. #pragma GCC optimize ("O3")
  39. #define __USED __attribute__((used))
  40. #elif defined (__ICCARM__)
  41. #define __USED __root
  42. #endif
  43. /*----------------------------------------------------------------------------
  44. * Definitions
  45. *---------------------------------------------------------------------------*/
  46. #define _declare_box(pool,size,cnt) uint32_t pool[(((size)+3)/4)*(cnt) + 3]
  47. #define _declare_box8(pool,size,cnt) uint64_t pool[(((size)+7)/8)*(cnt) + 2]
  48. #define OS_TCB_SIZE 48
  49. #define OS_TMR_SIZE 8
  50. #if defined (__CC_ARM) && !defined (__MICROLIB)
  51. typedef void *OS_ID;
  52. typedef uint32_t OS_TID;
  53. typedef uint32_t OS_MUT[3];
  54. typedef uint32_t OS_RESULT;
  55. #define runtask_id() rt_tsk_self()
  56. #define mutex_init(m) rt_mut_init(m)
  57. #define mutex_wait(m) os_mut_wait(m,0xFFFF)
  58. #define mutex_rel(m) os_mut_release(m)
  59. extern OS_TID rt_tsk_self (void);
  60. extern void rt_mut_init (OS_ID mutex);
  61. extern OS_RESULT rt_mut_release (OS_ID mutex);
  62. extern OS_RESULT rt_mut_wait (OS_ID mutex, uint16_t timeout);
  63. #define os_mut_wait(mutex,timeout) _os_mut_wait((uint32_t)rt_mut_wait,mutex,timeout)
  64. #define os_mut_release(mutex) _os_mut_release((uint32_t)rt_mut_release,mutex)
  65. OS_RESULT _os_mut_release (uint32_t p, OS_ID mutex) __svc_indirect(0);
  66. OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indirect(0);
  67. #endif
  68. /*----------------------------------------------------------------------------
  69. * Global Variables
  70. *---------------------------------------------------------------------------*/
  71. #if (OS_TIMERS != 0)
  72. #define OS_TASK_CNT (OS_TASKCNT + 1)
  73. #define OS_PRIV_CNT (OS_PRIVCNT + 2)
  74. #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ))
  75. #else
  76. #define OS_TASK_CNT OS_TASKCNT
  77. #define OS_PRIV_CNT (OS_PRIVCNT + 1)
  78. #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE))
  79. #endif
  80. uint16_t const os_maxtaskrun = OS_TASK_CNT;
  81. uint32_t const os_stackinfo = (OS_STKCHECK<<24)| (OS_PRIV_CNT<<16) | (OS_STKSIZE*4);
  82. uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT;
  83. uint32_t const os_trv = OS_TRV;
  84. uint8_t const os_flags = OS_RUNPRIV;
  85. /* Export following defines to uVision debugger. */
  86. __USED uint32_t const os_clockrate = OS_TICK;
  87. __USED uint32_t const os_timernum = 0;
  88. /* Memory pool for TCB allocation */
  89. _declare_box (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT);
  90. uint16_t const mp_tcb_size = sizeof(mp_tcb);
  91. /* Memory pool for System stack allocation (+os_idle_demon). */
  92. _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1);
  93. uint32_t const mp_stk_size = sizeof(mp_stk);
  94. /* Memory pool for user specified stack allocation (+main, +timer) */
  95. uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
  96. uint32_t const os_stack_sz = sizeof(os_stack_mem);
  97. #ifndef OS_FIFOSZ
  98. #define OS_FIFOSZ 16
  99. #endif
  100. /* Fifo Queue buffer for ISR requests.*/
  101. uint32_t os_fifo[OS_FIFOSZ*2+1];
  102. uint8_t const os_fifo_size = OS_FIFOSZ;
  103. /* An array of Active task pointers. */
  104. void *os_active_TCB[OS_TASK_CNT];
  105. /* User Timers Resources */
  106. #if (OS_TIMERS != 0)
  107. extern void osTimerThread (void const *argument);
  108. #if defined (__MBED_CMSIS_RTOS_CA9)
  109. osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ);
  110. #else
  111. osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ);
  112. #endif
  113. osThreadId osThreadId_osTimerThread;
  114. osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
  115. osMessageQId osMessageQId_osTimerMessageQ;
  116. #else
  117. osThreadDef_t os_thread_def_osTimerThread = { NULL };
  118. osThreadId osThreadId_osTimerThread;
  119. osMessageQDef(osTimerMessageQ, 0, void *);
  120. osMessageQId osMessageQId_osTimerMessageQ;
  121. #endif
  122. /* Legacy RTX User Timers not used */
  123. uint32_t os_tmr = 0;
  124. uint32_t const *m_tmr = NULL;
  125. uint16_t const mp_tmr_size = 0;
  126. #if defined (__CC_ARM) && !defined (__MICROLIB)
  127. /* A memory space for arm standard library. */
  128. static uint32_t std_libspace[OS_TASK_CNT][96/4];
  129. static OS_MUT std_libmutex[OS_MUTEXCNT];
  130. static uint32_t nr_mutex;
  131. extern void *__libspace_start;
  132. #endif
  133. /*----------------------------------------------------------------------------
  134. * RTX Optimizations (empty functions)
  135. *---------------------------------------------------------------------------*/
  136. #if OS_ROBIN == 0
  137. void rt_init_robin (void) {;}
  138. void rt_chk_robin (void) {;}
  139. #endif
  140. #if OS_STKCHECK == 0
  141. void rt_stk_check (void) {;}
  142. #endif
  143. /*----------------------------------------------------------------------------
  144. * Standard Library multithreading interface
  145. *---------------------------------------------------------------------------*/
  146. #if defined (__CC_ARM) && !defined (__MICROLIB)
  147. /*--------------------------- __user_perthread_libspace ---------------------*/
  148. void *__user_perthread_libspace (void) {
  149. /* Provide a separate libspace for each task. */
  150. uint32_t idx;
  151. idx = runtask_id ();
  152. if (idx == 0) {
  153. /* RTX not running yet. */
  154. return (&__libspace_start);
  155. }
  156. return ((void *)&std_libspace[idx-1]);
  157. }
  158. /*--------------------------- _mutex_initialize -----------------------------*/
  159. int _mutex_initialize (OS_ID *mutex) {
  160. /* Allocate and initialize a system mutex. */
  161. if (nr_mutex >= OS_MUTEXCNT) {
  162. /* If you are here, you need to increase the number OS_MUTEXCNT. */
  163. for (;;);
  164. }
  165. *mutex = &std_libmutex[nr_mutex++];
  166. mutex_init (*mutex);
  167. return (1);
  168. }
  169. /*--------------------------- _mutex_acquire --------------------------------*/
  170. __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
  171. /* Acquire a system mutex, lock stdlib resources. */
  172. if (runtask_id ()) {
  173. /* RTX running, acquire a mutex. */
  174. mutex_wait (*mutex);
  175. }
  176. }
  177. /*--------------------------- _mutex_release --------------------------------*/
  178. __attribute__((used)) void _mutex_release (OS_ID *mutex) {
  179. /* Release a system mutex, unlock stdlib resources. */
  180. if (runtask_id ()) {
  181. /* RTX running, release a mutex. */
  182. mutex_rel (*mutex);
  183. }
  184. }
  185. #endif
  186. /*----------------------------------------------------------------------------
  187. * RTX Startup
  188. *---------------------------------------------------------------------------*/
  189. /* Main Thread definition */
  190. extern int main (void);
  191. osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1, 4*OS_MAINSTKSIZE };
  192. #if defined (__CC_ARM)
  193. #ifdef __MICROLIB
  194. void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
  195. #if __TARGET_ARCH_ARM
  196. #pragma push
  197. #pragma arm
  198. #endif
  199. void _main_init (void) {
  200. osKernelInitialize();
  201. osThreadCreate(&os_thread_def_main, NULL);
  202. osKernelStart();
  203. for (;;);
  204. }
  205. #if __TARGET_ARCH_ARM
  206. #pragma pop
  207. #endif
  208. #else
  209. __asm void __rt_entry (void) {
  210. IMPORT __user_setup_stackheap
  211. IMPORT __rt_lib_init
  212. IMPORT os_thread_def_main
  213. IMPORT osKernelInitialize
  214. IMPORT osKernelStart
  215. IMPORT osThreadCreate
  216. IMPORT exit
  217. BL __user_setup_stackheap
  218. MOV R1,R2
  219. BL __rt_lib_init
  220. BL osKernelInitialize
  221. LDR R0,=os_thread_def_main
  222. MOVS R1,#0
  223. BL osThreadCreate
  224. BL osKernelStart
  225. BL exit
  226. ALIGN
  227. }
  228. #endif
  229. #elif defined (__GNUC__)
  230. #ifdef __CS3__
  231. /* CS3 start_c routine.
  232. *
  233. * Copyright (c) 2006, 2007 CodeSourcery Inc
  234. *
  235. * The authors hereby grant permission to use, copy, modify, distribute,
  236. * and license this software and its documentation for any purpose, provided
  237. * that existing copyright notices are retained in all copies and that this
  238. * notice is included verbatim in any distributions. No written agreement,
  239. * license, or royalty fee is required for any of the authorized uses.
  240. * Modifications to this software may be copyrighted by their authors
  241. * and need not follow the licensing terms described here, provided that
  242. * the new terms are clearly indicated on the first page of each file where
  243. * they apply.
  244. */
  245. #include "cs3.h"
  246. extern void __libc_init_array (void);
  247. __attribute ((noreturn)) void __cs3_start_c (void){
  248. unsigned regions = __cs3_region_num;
  249. const struct __cs3_region *rptr = __cs3_regions;
  250. /* Initialize memory */
  251. for (regions = __cs3_region_num, rptr = __cs3_regions; regions--; rptr++) {
  252. long long *src = (long long *)rptr->init;
  253. long long *dst = (long long *)rptr->data;
  254. unsigned limit = rptr->init_size;
  255. unsigned count;
  256. if (src != dst)
  257. for (count = 0; count != limit; count += sizeof (long long))
  258. *dst++ = *src++;
  259. else
  260. dst = (long long *)((char *)dst + limit);
  261. limit = rptr->zero_size;
  262. for (count = 0; count != limit; count += sizeof (long long))
  263. *dst++ = 0;
  264. }
  265. /* Run initializers. */
  266. __libc_init_array ();
  267. osKernelInitialize();
  268. osThreadCreate(&os_thread_def_main, NULL);
  269. osKernelStart();
  270. for (;;);
  271. }
  272. #else
  273. __attribute__((naked)) void software_init_hook (void) {
  274. __asm (
  275. ".syntax unified\n"
  276. ".arm\n"
  277. "movs r0,#0\n"
  278. "movs r1,#0\n"
  279. "mov r4,r0\n"
  280. "mov r5,r1\n"
  281. "ldr r0,= __libc_fini_array\n"
  282. "bl atexit\n"
  283. "bl __libc_init_array\n"
  284. "mov r0,r4\n"
  285. "mov r1,r5\n"
  286. "bl osKernelInitialize\n"
  287. "ldr r0,=os_thread_def_main\n"
  288. "movs r1,#0\n"
  289. "bl osThreadCreate\n"
  290. "bl osKernelStart\n"
  291. "bl exit\n"
  292. );
  293. }
  294. #endif
  295. #elif defined (__ICCARM__)
  296. extern int __low_level_init(void);
  297. extern void __iar_data_init3(void);
  298. extern void exit(int arg);
  299. __noreturn __stackless void __cmain(void) {
  300. int a;
  301. if (__low_level_init() != 0) {
  302. __iar_data_init3();
  303. }
  304. osKernelInitialize();
  305. osThreadCreate(&os_thread_def_main, NULL);
  306. a = osKernelStart();
  307. exit(a);
  308. }
  309. #endif
  310. /*----------------------------------------------------------------------------
  311. * end of file
  312. *---------------------------------------------------------------------------*/