RTX_CM_lib.h 12 KB

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