RTX_CM_lib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RTX_CM_LIB.H
  5. * Purpose: RTX Kernel System Configuration
  6. * Rev.: V4.74
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 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 52
  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[4];
  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_TASKCNT == 0)
  72. #error "Invalid number of concurrent running threads!"
  73. #endif
  74. #if (OS_PRIVCNT >= OS_TASKCNT)
  75. #error "Too many threads with user-provided stack size!"
  76. #endif
  77. #if (OS_TIMERS != 0)
  78. #define OS_TASK_CNT (OS_TASKCNT + 1)
  79. #define OS_PRIV_CNT (OS_PRIVCNT + 2)
  80. #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ))
  81. #else
  82. #define OS_TASK_CNT OS_TASKCNT
  83. #define OS_PRIV_CNT (OS_PRIVCNT + 1)
  84. #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE))
  85. #endif
  86. uint16_t const os_maxtaskrun = OS_TASK_CNT;
  87. uint32_t const os_stackinfo = (OS_STKCHECK<<24)| (OS_PRIV_CNT<<16) | (OS_STKSIZE*4);
  88. uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT;
  89. uint32_t const os_tickfreq = OS_CLOCK;
  90. uint16_t const os_tickus_i = OS_CLOCK/1000000;
  91. uint16_t const os_tickus_f = (((uint64_t)(OS_CLOCK-1000000*(OS_CLOCK/1000000)))<<16)/1000000;
  92. uint32_t const os_trv = OS_TRV;
  93. uint8_t const os_flags = OS_RUNPRIV;
  94. /* Export following defines to uVision debugger. */
  95. __USED uint32_t const CMSIS_RTOS_API_Version = osCMSIS;
  96. __USED uint32_t const CMSIS_RTOS_RTX_Version = osCMSIS_RTX;
  97. __USED uint32_t const os_clockrate = OS_TICK;
  98. __USED uint32_t const os_timernum = 0;
  99. /* Memory pool for TCB allocation */
  100. _declare_box (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT);
  101. uint16_t const mp_tcb_size = sizeof(mp_tcb);
  102. /* Memory pool for System stack allocation (+os_idle_demon). */
  103. _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1);
  104. uint32_t const mp_stk_size = sizeof(mp_stk);
  105. /* Memory pool for user specified stack allocation (+main, +timer) */
  106. uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
  107. uint32_t const os_stack_sz = sizeof(os_stack_mem);
  108. #ifndef OS_FIFOSZ
  109. #define OS_FIFOSZ 16
  110. #endif
  111. /* Fifo Queue buffer for ISR requests.*/
  112. uint32_t os_fifo[OS_FIFOSZ*2+1];
  113. uint8_t const os_fifo_size = OS_FIFOSZ;
  114. /* An array of Active task pointers. */
  115. void *os_active_TCB[OS_TASK_CNT];
  116. /* User Timers Resources */
  117. #if (OS_TIMERS != 0)
  118. extern void osTimerThread (void const *argument);
  119. osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ);
  120. osThreadId osThreadId_osTimerThread;
  121. osMessageQDef(osTimerMessageQ, OS_TIMERCBQS, void *);
  122. osMessageQId osMessageQId_osTimerMessageQ;
  123. #else
  124. osThreadDef_t os_thread_def_osTimerThread = { NULL };
  125. osThreadId osThreadId_osTimerThread;
  126. osMessageQDef(osTimerMessageQ, 0, void *);
  127. osMessageQId osMessageQId_osTimerMessageQ;
  128. #endif
  129. /* Legacy RTX User Timers not used */
  130. uint32_t os_tmr = 0;
  131. uint32_t const *m_tmr = NULL;
  132. uint16_t const mp_tmr_size = 0;
  133. #if defined (__CC_ARM) && !defined (__MICROLIB)
  134. /* A memory space for arm standard library. */
  135. static uint32_t std_libspace[OS_TASK_CNT][96/4];
  136. static OS_MUT std_libmutex[OS_MUTEXCNT];
  137. static uint32_t nr_mutex;
  138. extern void *__libspace_start;
  139. #endif
  140. /*----------------------------------------------------------------------------
  141. * RTX Optimizations (empty functions)
  142. *---------------------------------------------------------------------------*/
  143. #if OS_ROBIN == 0
  144. void rt_init_robin (void) {;}
  145. void rt_chk_robin (void) {;}
  146. #endif
  147. #if OS_STKCHECK == 0
  148. void rt_stk_check (void) {;}
  149. #endif
  150. /*----------------------------------------------------------------------------
  151. * Standard Library multithreading interface
  152. *---------------------------------------------------------------------------*/
  153. #if defined (__CC_ARM) && !defined (__MICROLIB)
  154. /*--------------------------- __user_perthread_libspace ---------------------*/
  155. void *__user_perthread_libspace (void) {
  156. /* Provide a separate libspace for each task. */
  157. uint32_t idx;
  158. idx = runtask_id ();
  159. if (idx == 0) {
  160. /* RTX not running yet. */
  161. return (&__libspace_start);
  162. }
  163. return ((void *)&std_libspace[idx-1]);
  164. }
  165. /*--------------------------- _mutex_initialize -----------------------------*/
  166. int _mutex_initialize (OS_ID *mutex) {
  167. /* Allocate and initialize a system mutex. */
  168. if (nr_mutex >= OS_MUTEXCNT) {
  169. /* If you are here, you need to increase the number OS_MUTEXCNT. */
  170. for (;;);
  171. }
  172. *mutex = &std_libmutex[nr_mutex++];
  173. mutex_init (*mutex);
  174. return (1);
  175. }
  176. /*--------------------------- _mutex_acquire --------------------------------*/
  177. __attribute__((used)) void _mutex_acquire (OS_ID *mutex) {
  178. /* Acquire a system mutex, lock stdlib resources. */
  179. if (runtask_id ()) {
  180. /* RTX running, acquire a mutex. */
  181. mutex_wait (*mutex);
  182. }
  183. }
  184. /*--------------------------- _mutex_release --------------------------------*/
  185. __attribute__((used)) void _mutex_release (OS_ID *mutex) {
  186. /* Release a system mutex, unlock stdlib resources. */
  187. if (runtask_id ()) {
  188. /* RTX running, release a mutex. */
  189. mutex_rel (*mutex);
  190. }
  191. }
  192. #endif
  193. /*----------------------------------------------------------------------------
  194. * RTX Startup
  195. *---------------------------------------------------------------------------*/
  196. /* Main Thread definition */
  197. extern int main (void);
  198. osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1, 4*OS_MAINSTKSIZE };
  199. #if defined (__CC_ARM)
  200. #ifdef __MICROLIB
  201. void _main_init (void) __attribute__((section(".ARM.Collect$$$$000000FF")));
  202. void _main_init (void) {
  203. osKernelInitialize();
  204. osThreadCreate(&os_thread_def_main, NULL);
  205. osKernelStart();
  206. for (;;);
  207. }
  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. ".thumb\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. *---------------------------------------------------------------------------*/