rtx_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * -----------------------------------------------------------------------------
  19. *
  20. * Project: CMSIS-RTOS RTX
  21. * Title: RTX Library Configuration
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "cmsis_compiler.h"
  26. #include "RTX_Config.h"
  27. #include "rtx_os.h"
  28. #ifdef RTE_Compiler_EventRecorder
  29. #include "EventRecorder.h"
  30. #include "EventRecorderConf.h"
  31. #endif
  32. #include "rtx_evr.h"
  33. // System Configuration
  34. // ====================
  35. // Dynamic Memory
  36. #if (OS_DYNAMIC_MEM_SIZE != 0)
  37. #if ((OS_DYNAMIC_MEM_SIZE % 8) != 0)
  38. #error "Invalid Dynamic Memory size!"
  39. #endif
  40. static uint64_t os_mem[OS_DYNAMIC_MEM_SIZE/8] \
  41. __attribute__((section(".bss.os")));
  42. #endif
  43. // Kernel Tick Frequency
  44. #if (OS_TICK_FREQ < 1)
  45. #error "Invalid Kernel Tick Frequency!"
  46. #endif
  47. // ISR FIFO Queue
  48. static void *os_isr_queue[OS_ISR_FIFO_QUEUE] \
  49. __attribute__((section(".bss.os")));
  50. // Thread Configuration
  51. // ====================
  52. #if (((OS_STACK_SIZE % 8) != 0) || (OS_STACK_SIZE < 72))
  53. #error "Invalid default Thread Stack size!"
  54. #endif
  55. #if (((OS_IDLE_THREAD_STACK_SIZE % 8) != 0) || (OS_IDLE_THREAD_STACK_SIZE < 72))
  56. #error "Invalid Idle Thread Stack size!"
  57. #endif
  58. #if (OS_THREAD_OBJ_MEM != 0)
  59. #if (OS_THREAD_NUM == 0)
  60. #error "Invalid number of user Threads!"
  61. #endif
  62. #if ((OS_THREAD_USER_STACK_SIZE != 0) && ((OS_THREAD_USER_STACK_SIZE % 8) != 0))
  63. #error "Invalid total Stack size!"
  64. #endif
  65. // Thread Control Blocks
  66. static osRtxThread_t os_thread_cb[OS_THREAD_NUM] \
  67. __attribute__((section(".bss.os.thread.cb")));
  68. // Thread Default Stack
  69. #if (OS_THREAD_DEF_STACK_NUM != 0)
  70. static uint64_t os_thread_def_stack[OS_THREAD_DEF_STACK_NUM*(OS_STACK_SIZE/8)] \
  71. __attribute__((section(".bss.os.thread.stack")));
  72. #endif
  73. // Memory Pool for Thread Control Blocks
  74. static osRtxMpInfo_t os_mpi_thread \
  75. __attribute__((section(".data.os.thread.mpi"))) =
  76. { (uint32_t)OS_THREAD_NUM, 0U, (uint32_t)osRtxThreadCbSize, &os_thread_cb[0], NULL, NULL };
  77. // Memory Pool for Thread Default Stack
  78. #if (OS_THREAD_DEF_STACK_NUM != 0)
  79. static osRtxMpInfo_t os_mpi_def_stack \
  80. __attribute__((section(".data.os.thread.mpi"))) =
  81. { (uint32_t)OS_THREAD_DEF_STACK_NUM, 0U, (uint32_t)OS_STACK_SIZE, &os_thread_def_stack[0], NULL, NULL };
  82. #endif
  83. // Memory Pool for Thread Stack
  84. #if (OS_THREAD_USER_STACK_SIZE != 0)
  85. static uint64_t os_thread_stack[2 + OS_THREAD_NUM + (OS_THREAD_USER_STACK_SIZE/8)] \
  86. __attribute__((section(".bss.os.thread.stack")));
  87. #endif
  88. #endif // (OS_THREAD_OBJ_MEM != 0)
  89. // Stack overrun checking
  90. #if (OS_STACK_CHECK == 0)
  91. // Override library function
  92. extern void osRtxThreadStackCheck (void);
  93. void osRtxThreadStackCheck (void) {}
  94. #endif
  95. // Idle Thread Control Block
  96. static osRtxThread_t os_idle_thread_cb \
  97. __attribute__((section(".bss.os.thread.cb")));
  98. // Idle Thread Stack
  99. static uint64_t os_idle_thread_stack[OS_IDLE_THREAD_STACK_SIZE/8] \
  100. __attribute__((section(".bss.os.thread.stack")));
  101. // Idle Thread Attributes
  102. static const osThreadAttr_t os_idle_thread_attr = {
  103. #if defined(OS_IDLE_THREAD_NAME)
  104. OS_IDLE_THREAD_NAME,
  105. #else
  106. NULL,
  107. #endif
  108. osThreadDetached,
  109. &os_idle_thread_cb,
  110. (uint32_t)sizeof(os_idle_thread_cb),
  111. &os_idle_thread_stack[0],
  112. (uint32_t)sizeof(os_idle_thread_stack),
  113. osPriorityIdle,
  114. #if defined(OS_IDLE_THREAD_TZ_MOD_ID)
  115. (uint32_t)OS_IDLE_THREAD_TZ_MOD_ID,
  116. #else
  117. 0U,
  118. #endif
  119. 0U
  120. };
  121. // Timer Configuration
  122. // ===================
  123. #if (OS_TIMER_OBJ_MEM != 0)
  124. #if (OS_TIMER_NUM == 0)
  125. #error "Invalid number of Timer objects!"
  126. #endif
  127. // Timer Control Blocks
  128. static osRtxTimer_t os_timer_cb[OS_TIMER_NUM] \
  129. __attribute__((section(".bss.os.timer.cb")));
  130. // Memory Pool for Timer Control Blocks
  131. static osRtxMpInfo_t os_mpi_timer \
  132. __attribute__((section(".data.os.timer.mpi"))) =
  133. { (uint32_t)OS_TIMER_NUM, 0U, (uint32_t)osRtxTimerCbSize, &os_timer_cb[0], NULL, NULL };
  134. #endif // (OS_TIMER_OBJ_MEM != 0)
  135. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  136. #if (((OS_TIMER_THREAD_STACK_SIZE % 8) != 0) || (OS_TIMER_THREAD_STACK_SIZE < 96))
  137. #error "Invalid Timer Thread Stack size!"
  138. #endif
  139. // Timer Thread Control Block
  140. static osRtxThread_t os_timer_thread_cb \
  141. __attribute__((section(".bss.os.thread.cb")));
  142. // Timer Thread Stack
  143. static uint64_t os_timer_thread_stack[OS_TIMER_THREAD_STACK_SIZE/8] \
  144. __attribute__((section(".bss.os.thread.stack")));
  145. // Timer Thread Attributes
  146. static const osThreadAttr_t os_timer_thread_attr = {
  147. #if defined(OS_TIMER_THREAD_NAME)
  148. OS_TIMER_THREAD_NAME,
  149. #else
  150. NULL,
  151. #endif
  152. osThreadDetached,
  153. &os_timer_thread_cb,
  154. (uint32_t)sizeof(os_timer_thread_cb),
  155. &os_timer_thread_stack[0],
  156. (uint32_t)sizeof(os_timer_thread_stack),
  157. //lint -e{9030} -e{9034} "cast from signed to enum"
  158. (osPriority_t)OS_TIMER_THREAD_PRIO,
  159. #if defined(OS_TIMER_THREAD_TZ_MOD_ID)
  160. (uint32_t)OS_TIMER_THREAD_TZ_MOD_ID,
  161. #else
  162. 0U,
  163. #endif
  164. 0U
  165. };
  166. // Timer Message Queue Control Block
  167. static osRtxMessageQueue_t os_timer_mq_cb \
  168. __attribute__((section(".bss.os.msgqueue.cb")));
  169. // Timer Message Queue Data
  170. static uint32_t os_timer_mq_data[osRtxMessageQueueMemSize(OS_TIMER_CB_QUEUE,8)/4] \
  171. __attribute__((section(".bss.os.msgqueue.mem")));
  172. // Timer Message Queue Attributes
  173. static const osMessageQueueAttr_t os_timer_mq_attr = {
  174. NULL,
  175. 0U,
  176. &os_timer_mq_cb,
  177. (uint32_t)sizeof(os_timer_mq_cb),
  178. &os_timer_mq_data[0],
  179. (uint32_t)sizeof(os_timer_mq_data)
  180. };
  181. #else
  182. extern void osRtxTimerThread (void *argument);
  183. void osRtxTimerThread (void *argument) { (void)argument; }
  184. #endif // ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  185. // Event Flags Configuration
  186. // =========================
  187. #if (OS_EVFLAGS_OBJ_MEM != 0)
  188. #if (OS_EVFLAGS_NUM == 0)
  189. #error "Invalid number of Event Flags objects!"
  190. #endif
  191. // Event Flags Control Blocks
  192. static osRtxEventFlags_t os_ef_cb[OS_EVFLAGS_NUM] \
  193. __attribute__((section(".bss.os.evflags.cb")));
  194. // Memory Pool for Event Flags Control Blocks
  195. static osRtxMpInfo_t os_mpi_ef \
  196. __attribute__((section(".data.os.evflags.mpi"))) =
  197. { (uint32_t)OS_EVFLAGS_NUM, 0U, (uint32_t)osRtxEventFlagsCbSize, &os_ef_cb[0], NULL, NULL };
  198. #endif // (OS_EVFLAGS_OBJ_MEM != 0)
  199. // Mutex Configuration
  200. // ===================
  201. #if (OS_MUTEX_OBJ_MEM != 0)
  202. #if (OS_MUTEX_NUM == 0)
  203. #error "Invalid number of Mutex objects!"
  204. #endif
  205. // Mutex Control Blocks
  206. static osRtxMutex_t os_mutex_cb[OS_MUTEX_NUM] \
  207. __attribute__((section(".bss.os.mutex.cb")));
  208. // Memory Pool for Mutex Control Blocks
  209. static osRtxMpInfo_t os_mpi_mutex \
  210. __attribute__((section(".data.os.mutex.mpi"))) =
  211. { (uint32_t)OS_MUTEX_NUM, 0U, (uint32_t)osRtxMutexCbSize, &os_mutex_cb[0], NULL, NULL };
  212. #endif // (OS_MUTEX_OBJ_MEM != 0)
  213. // Semaphore Configuration
  214. // =======================
  215. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  216. #if (OS_SEMAPHORE_NUM == 0)
  217. #error "Invalid number of Semaphore objects!"
  218. #endif
  219. // Semaphore Control Blocks
  220. static osRtxSemaphore_t os_semaphore_cb[OS_SEMAPHORE_NUM] \
  221. __attribute__((section(".bss.os.semaphore.cb")));
  222. // Memory Pool for Semaphore Control Blocks
  223. static osRtxMpInfo_t os_mpi_semaphore \
  224. __attribute__((section(".data.os.semaphore.mpi"))) =
  225. { (uint32_t)OS_SEMAPHORE_NUM, 0U, (uint32_t)osRtxSemaphoreCbSize, &os_semaphore_cb[0], NULL, NULL };
  226. #endif // (OS_SEMAPHORE_OBJ_MEM != 0)
  227. // Memory Pool Configuration
  228. // =========================
  229. #if (OS_MEMPOOL_OBJ_MEM != 0)
  230. #if (OS_MEMPOOL_NUM == 0)
  231. #error "Invalid number of Memory Pool objects!"
  232. #endif
  233. // Memory Pool Control Blocks
  234. static osRtxMemoryPool_t os_mp_cb[OS_MEMPOOL_NUM] \
  235. __attribute__((section(".bss.os.mempool.cb")));
  236. // Memory Pool for Memory Pool Control Blocks
  237. static osRtxMpInfo_t os_mpi_mp \
  238. __attribute__((section(".data.os.mempool.mpi"))) =
  239. { (uint32_t)OS_MEMPOOL_NUM, 0U, (uint32_t)osRtxMemoryPoolCbSize, &os_mp_cb[0], NULL, NULL };
  240. // Memory Pool for Memory Pool Data Storage
  241. #if (OS_MEMPOOL_DATA_SIZE != 0)
  242. #if ((OS_MEMPOOL_DATA_SIZE % 8) != 0)
  243. #error "Invalid Data Memory size for Memory Pools!"
  244. #endif
  245. static uint64_t os_mp_data[2 + OS_MEMPOOL_NUM + (OS_MEMPOOL_DATA_SIZE/8)] \
  246. __attribute__((section(".bss.os.mempool.mem")));
  247. #endif
  248. #endif // (OS_MEMPOOL_OBJ_MEM != 0)
  249. // Message Queue Configuration
  250. // ===========================
  251. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  252. #if (OS_MSGQUEUE_NUM == 0)
  253. #error "Invalid number of Message Queue objects!"
  254. #endif
  255. // Message Queue Control Blocks
  256. static osRtxMessageQueue_t os_mq_cb[OS_MSGQUEUE_NUM] \
  257. __attribute__((section(".bss.os.msgqueue.cb")));
  258. // Memory Pool for Message Queue Control Blocks
  259. static osRtxMpInfo_t os_mpi_mq \
  260. __attribute__((section(".data.os.msgqueue.mpi"))) =
  261. { (uint32_t)OS_MSGQUEUE_NUM, 0U, (uint32_t)osRtxMessageQueueCbSize, &os_mq_cb[0], NULL, NULL };
  262. // Memory Pool for Message Queue Data Storage
  263. #if (OS_MSGQUEUE_DATA_SIZE != 0)
  264. #if ((OS_MSGQUEUE_DATA_SIZE % 8) != 0)
  265. #error "Invalid Data Memory size for Message Queues!"
  266. #endif
  267. static uint64_t os_mq_data[2 + OS_MSGQUEUE_NUM + (OS_MSGQUEUE_DATA_SIZE/8)] \
  268. __attribute__((section(".bss.os.msgqueue.mem")));
  269. #endif
  270. #endif // (OS_MSGQUEUE_OBJ_MEM != 0)
  271. // Event Recorder Configuration
  272. // ============================
  273. #if (defined(OS_EVR_INIT) && (OS_EVR_INIT != 0))
  274. #if defined(RTE_Compiler_EventRecorder)
  275. // Event Recorder Initialize
  276. __STATIC_INLINE void evr_initialize (void) {
  277. (void)EventRecorderInitialize(OS_EVR_LEVEL, (uint32_t)OS_EVR_START);
  278. #if ((OS_EVR_MEMORY_FILTER & 0x80U) != 0U)
  279. (void)EventRecorderEnable(OS_EVR_MEMORY_FILTER & 0x0FU, EvtRtxMemoryNo, EvtRtxMemoryNo);
  280. #endif
  281. #if ((OS_EVR_KERNEL_FILTER & 0x80U) != 0U)
  282. (void)EventRecorderEnable(OS_EVR_KERNEL_FILTER & 0x0FU, EvtRtxKernelNo, EvtRtxKernelNo);
  283. #endif
  284. #if ((OS_EVR_THREAD_FILTER & 0x80U) != 0U)
  285. (void)EventRecorderEnable(OS_EVR_THREAD_FILTER & 0x0FU, EvtRtxThreadNo, EvtRtxThreadNo);
  286. #endif
  287. #if ((OS_EVR_TIMER_FILTER & 0x80U) != 0U)
  288. (void)EventRecorderEnable(OS_EVR_TIMER_FILTER & 0x0FU, EvtRtxTimerNo, EvtRtxTimerNo);
  289. #endif
  290. #if ((OS_EVR_EVFLAGS_FILTER & 0x80U) != 0U)
  291. (void)EventRecorderEnable(OS_EVR_EVFLAGS_FILTER & 0x0FU, EvtRtxEventFlagsNo, EvtRtxEventFlagsNo);
  292. #endif
  293. #if ((OS_EVR_MUTEX_FILTER & 0x80U) != 0U)
  294. (void)EventRecorderEnable(OS_EVR_MUTEX_FILTER & 0x0FU, EvtRtxMutexNo, EvtRtxMutexNo);
  295. #endif
  296. #if ((OS_EVR_SEMAPHORE_FILTER & 0x80U) != 0U)
  297. (void)EventRecorderEnable(OS_EVR_SEMAPHORE_FILTER & 0x0FU, EvtRtxSemaphoreNo, EvtRtxSemaphoreNo);
  298. #endif
  299. #if ((OS_EVR_MEMPOOL_FILTER & 0x80U) != 0U)
  300. (void)EventRecorderEnable(OS_EVR_MEMPOOL_FILTER & 0x0FU, EvtRtxMemoryPoolNo, EvtRtxMemoryPoolNo);
  301. #endif
  302. #if ((OS_EVR_MSGQUEUE_FILTER & 0x80U) != 0U)
  303. (void)EventRecorderEnable(OS_EVR_MSGQUEUE_FILTER & 0x0FU, EvtRtxMessageQueueNo, EvtRtxMessageQueueNo);
  304. #endif
  305. }
  306. #else
  307. #warning "Event Recorder cannot be initialized (Event Recorder component is not selected)!"
  308. #define evr_initialize()
  309. #endif
  310. #endif // (OS_EVR_INIT != 0)
  311. // OS Configuration
  312. // ================
  313. const osRtxConfig_t osRtxConfig \
  314. __USED \
  315. __attribute__((section(".rodata"))) =
  316. {
  317. //lint -e{835} "Zero argument to operator"
  318. 0U // Flags
  319. #if (OS_PRIVILEGE_MODE != 0)
  320. | osRtxConfigPrivilegedMode
  321. #endif
  322. #if (OS_STACK_CHECK != 0)
  323. | osRtxConfigStackCheck
  324. #endif
  325. #if (OS_STACK_WATERMARK != 0)
  326. | osRtxConfigStackWatermark
  327. #endif
  328. ,
  329. (uint32_t)OS_TICK_FREQ,
  330. #if (OS_ROBIN_ENABLE != 0)
  331. (uint32_t)OS_ROBIN_TIMEOUT,
  332. #else
  333. 0U,
  334. #endif
  335. { &os_isr_queue[0], (uint16_t)(sizeof(os_isr_queue)/sizeof(void *)), 0U },
  336. {
  337. // Memory Pools (Variable Block Size)
  338. #if ((OS_THREAD_OBJ_MEM != 0) && (OS_THREAD_USER_STACK_SIZE != 0))
  339. &os_thread_stack[0], sizeof(os_thread_stack),
  340. #else
  341. NULL, 0U,
  342. #endif
  343. #if ((OS_MEMPOOL_OBJ_MEM != 0) && (OS_MEMPOOL_DATA_SIZE != 0))
  344. &os_mp_data[0], sizeof(os_mp_data),
  345. #else
  346. NULL, 0U,
  347. #endif
  348. #if ((OS_MSGQUEUE_OBJ_MEM != 0) && (OS_MSGQUEUE_DATA_SIZE != 0))
  349. &os_mq_data[0], sizeof(os_mq_data),
  350. #else
  351. NULL, 0U,
  352. #endif
  353. #if (OS_DYNAMIC_MEM_SIZE != 0)
  354. &os_mem[0], (uint32_t)OS_DYNAMIC_MEM_SIZE,
  355. #else
  356. NULL, 0U
  357. #endif
  358. },
  359. {
  360. // Memory Pools (Fixed Block Size)
  361. #if (OS_THREAD_OBJ_MEM != 0)
  362. #if (OS_THREAD_DEF_STACK_NUM != 0)
  363. &os_mpi_def_stack,
  364. #else
  365. NULL,
  366. #endif
  367. &os_mpi_thread,
  368. #else
  369. NULL,
  370. NULL,
  371. #endif
  372. #if (OS_TIMER_OBJ_MEM != 0)
  373. &os_mpi_timer,
  374. #else
  375. NULL,
  376. #endif
  377. #if (OS_EVFLAGS_OBJ_MEM != 0)
  378. &os_mpi_ef,
  379. #else
  380. NULL,
  381. #endif
  382. #if (OS_MUTEX_OBJ_MEM != 0)
  383. &os_mpi_mutex,
  384. #else
  385. NULL,
  386. #endif
  387. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  388. &os_mpi_semaphore,
  389. #else
  390. NULL,
  391. #endif
  392. #if (OS_MEMPOOL_OBJ_MEM != 0)
  393. &os_mpi_mp,
  394. #else
  395. NULL,
  396. #endif
  397. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  398. &os_mpi_mq,
  399. #else
  400. NULL,
  401. #endif
  402. },
  403. (uint32_t)OS_STACK_SIZE,
  404. &os_idle_thread_attr,
  405. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  406. &os_timer_thread_attr,
  407. &os_timer_mq_attr,
  408. (uint32_t)OS_TIMER_CB_QUEUE
  409. #else
  410. NULL,
  411. NULL,
  412. 0U
  413. #endif
  414. };
  415. // Non weak reference to library irq module
  416. //lint -esym(526,irqRtxLib) "Defined by Exception handlers"
  417. //lint -esym(714,irqRtxLibRef) "Non weak reference"
  418. //lint -esym(765,irqRtxLibRef) "Global scope"
  419. extern uint8_t irqRtxLib;
  420. extern const uint8_t *irqRtxLibRef;
  421. const uint8_t *irqRtxLibRef = &irqRtxLib;
  422. // Default User SVC Table
  423. //lint -esym(714,osRtxUserSVC) "Referenced by Exception handlers"
  424. //lint -esym(765,osRtxUserSVC) "Global scope"
  425. //lint -e{9067} "extern array declared without size"
  426. extern void * const osRtxUserSVC[];
  427. __WEAK void * const osRtxUserSVC[1] = { (void *)0 };
  428. // OS Sections
  429. // ===========
  430. #if defined(__CC_ARM)
  431. __asm void os_cb_sections_wrapper (void) {
  432. EXTERN ||.bss.os.thread.cb$$Base|| [WEAK]
  433. EXTERN ||.bss.os.thread.cb$$Limit|| [WEAK]
  434. EXTERN ||.bss.os.timer.cb$$Base|| [WEAK]
  435. EXTERN ||.bss.os.timer.cb$$Limit|| [WEAK]
  436. EXTERN ||.bss.os.evflags.cb$$Base|| [WEAK]
  437. EXTERN ||.bss.os.evflags.cb$$Limit|| [WEAK]
  438. EXTERN ||.bss.os.mutex.cb$$Base|| [WEAK]
  439. EXTERN ||.bss.os.mutex.cb$$Limit|| [WEAK]
  440. EXTERN ||.bss.os.semaphore.cb$$Base|| [WEAK]
  441. EXTERN ||.bss.os.semaphore.cb$$Limit|| [WEAK]
  442. EXTERN ||.bss.os.mempool.cb$$Base|| [WEAK]
  443. EXTERN ||.bss.os.mempool.cb$$Limit|| [WEAK]
  444. EXTERN ||.bss.os.msgqueue.cb$$Base|| [WEAK]
  445. EXTERN ||.bss.os.msgqueue.cb$$Limit|| [WEAK]
  446. AREA ||.rodata||, DATA, READONLY
  447. EXPORT os_cb_sections
  448. os_cb_sections
  449. DCD ||.bss.os.thread.cb$$Base||
  450. DCD ||.bss.os.thread.cb$$Limit||
  451. DCD ||.bss.os.timer.cb$$Base||
  452. DCD ||.bss.os.timer.cb$$Limit||
  453. DCD ||.bss.os.evflags.cb$$Base||
  454. DCD ||.bss.os.evflags.cb$$Limit||
  455. DCD ||.bss.os.mutex.cb$$Base||
  456. DCD ||.bss.os.mutex.cb$$Limit||
  457. DCD ||.bss.os.semaphore.cb$$Base||
  458. DCD ||.bss.os.semaphore.cb$$Limit||
  459. DCD ||.bss.os.mempool.cb$$Base||
  460. DCD ||.bss.os.mempool.cb$$Limit||
  461. DCD ||.bss.os.msgqueue.cb$$Base||
  462. DCD ||.bss.os.msgqueue.cb$$Limit||
  463. AREA ||.emb_text||, CODE
  464. };
  465. #endif
  466. #if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  467. //lint -e{19} "Linker symbols"
  468. __asm (
  469. ".weakref __os_thread_cb_start__, .bss.os.thread.cb$$Base\n\t"
  470. ".weakref __os_thread_cb_end__, .bss.os.thread.cb$$Limit\n\t"
  471. ".weakref __os_timer_cb_start__, .bss.os.timer.cb$$Base\n\t"
  472. ".weakref __os_timer_cb_end__, .bss.os.timer.cb$$Limit\n\t"
  473. ".weakref __os_evflags_cb_start__, .bss.os.evflags.cb$$Base\n\t"
  474. ".weakref __os_evflags_cb_end__, .bss.os.evflags.cb$$Limit\n\t"
  475. ".weakref __os_mutex_cb_start__, .bss.os.mutex.cb$$Base\n\t"
  476. ".weakref __os_mutex_cb_end__, .bss.os.mutex.cb$$Limit\n\t"
  477. ".weakref __os_semaphore_cb_start__, .bss.os.semaphore.cb$$Base\n\t"
  478. ".weakref __os_semaphore_cb_end__, .bss.os.semaphore.cb$$Limit\n\t"
  479. ".weakref __os_mempool_cb_start__, .bss.os.mempool.cb$$Base\n\t"
  480. ".weakref __os_mempool_cb_end__, .bss.os.mempool.cb$$Limit\n\t"
  481. ".weakref __os_msgqueue_cb_start__, .bss.os.msgqueue.cb$$Base\n\t"
  482. ".weakref __os_msgqueue_cb_end__, .bss.os.msgqueue.cb$$Limit\n\t"
  483. );
  484. #endif
  485. #if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || \
  486. (defined(__GNUC__) && !defined(__CC_ARM))
  487. extern __attribute__((weak)) uint32_t __os_thread_cb_start__; //lint -esym(526,__os_thread_cb_start__)
  488. extern __attribute__((weak)) uint32_t __os_thread_cb_end__; //lint -esym(526,__os_thread_cb_end__)
  489. extern __attribute__((weak)) uint32_t __os_timer_cb_start__; //lint -esym(526,__os_timer_cb_start__)
  490. extern __attribute__((weak)) uint32_t __os_timer_cb_end__; //lint -esym(526,__os_timer_cb_end__)
  491. extern __attribute__((weak)) uint32_t __os_evflags_cb_start__; //lint -esym(526,__os_evflags_cb_start__)
  492. extern __attribute__((weak)) uint32_t __os_evflags_cb_end__; //lint -esym(526,__os_evflags_cb_end__)
  493. extern __attribute__((weak)) uint32_t __os_mutex_cb_start__; //lint -esym(526,__os_mutex_cb_start__)
  494. extern __attribute__((weak)) uint32_t __os_mutex_cb_end__; //lint -esym(526,__os_mutex_cb_end__)
  495. extern __attribute__((weak)) uint32_t __os_semaphore_cb_start__; //lint -esym(526,__os_semaphore_cb_start__)
  496. extern __attribute__((weak)) uint32_t __os_semaphore_cb_end__; //lint -esym(526,__os_semaphore_cb_end__)
  497. extern __attribute__((weak)) uint32_t __os_mempool_cb_start__; //lint -esym(526,__os_mempool_cb_start__)
  498. extern __attribute__((weak)) uint32_t __os_mempool_cb_end__; //lint -esym(526,__os_mempool_cb_end__)
  499. extern __attribute__((weak)) uint32_t __os_msgqueue_cb_start__; //lint -esym(526,__os_msgqueue_cb_start__)
  500. extern __attribute__((weak)) uint32_t __os_msgqueue_cb_end__; //lint -esym(526,__os_msgqueue_cb_end__)
  501. //lint -e{19} "Global symbol"
  502. __asm (".global os_cb_sections");
  503. //lint -e{9067} "extern array declared without size"
  504. extern const uint32_t os_cb_sections[];
  505. //lint -esym(714,os_cb_sections) "Referenced by debugger"
  506. //lint -esym(765,os_cb_sections) "Global scope"
  507. //lint -e{923} -e{9078} "cast from pointer to unsigned int"
  508. const uint32_t os_cb_sections[] \
  509. __attribute__((section(".rodata"))) =
  510. {
  511. (uint32_t)&__os_thread_cb_start__,
  512. (uint32_t)&__os_thread_cb_end__,
  513. (uint32_t)&__os_timer_cb_start__,
  514. (uint32_t)&__os_timer_cb_end__,
  515. (uint32_t)&__os_evflags_cb_start__,
  516. (uint32_t)&__os_evflags_cb_end__,
  517. (uint32_t)&__os_mutex_cb_start__,
  518. (uint32_t)&__os_mutex_cb_end__,
  519. (uint32_t)&__os_semaphore_cb_start__,
  520. (uint32_t)&__os_semaphore_cb_end__,
  521. (uint32_t)&__os_mempool_cb_start__,
  522. (uint32_t)&__os_mempool_cb_end__,
  523. (uint32_t)&__os_msgqueue_cb_start__,
  524. (uint32_t)&__os_msgqueue_cb_end__
  525. };
  526. #endif
  527. // OS Initialization
  528. // =================
  529. #if defined(__CC_ARM) || \
  530. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  531. #ifndef __MICROLIB
  532. //lint -esym(714,_platform_post_stackheap_init) "Referenced by C library"
  533. //lint -esym(765,_platform_post_stackheap_init) "Global scope"
  534. extern void _platform_post_stackheap_init (void);
  535. __WEAK void _platform_post_stackheap_init (void) {
  536. (void)osKernelInitialize();
  537. }
  538. #endif
  539. #elif defined(__GNUC__)
  540. extern void software_init_hook (void);
  541. __WEAK void software_init_hook (void) {
  542. (void)osKernelInitialize();
  543. }
  544. #endif
  545. // OS Hooks
  546. // ========
  547. // RTOS Kernel Pre-Initialization Hook
  548. void osRtxKernelPreInit (void);
  549. void osRtxKernelPreInit (void) {
  550. #if (defined(OS_EVR_INIT) && (OS_EVR_INIT != 0))
  551. if (osKernelGetState() == osKernelInactive) {
  552. evr_initialize();
  553. }
  554. #endif
  555. }
  556. // C/C++ Standard Library Multithreading Interface
  557. // ===============================================
  558. #if ( !defined(RTX_NO_MULTITHREAD_CLIB) && \
  559. ( defined(__CC_ARM) || \
  560. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))) && \
  561. !defined(__MICROLIB))
  562. #define LIBSPACE_SIZE 96
  563. //lint -esym(714,__user_perthread_libspace,_mutex_*) "Referenced by C library"
  564. //lint -esym(765,__user_perthread_libspace,_mutex_*) "Global scope"
  565. //lint -esym(9003, os_libspace*) "variables 'os_libspace*' defined at module scope"
  566. // Memory for libspace
  567. static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/4] \
  568. __attribute__((section(".bss.os.libspace")));
  569. // Thread IDs for libspace
  570. static osThreadId_t os_libspace_id[OS_THREAD_LIBSPACE_NUM] \
  571. __attribute__((section(".bss.os.libspace")));
  572. // Check if Kernel has been started
  573. static uint32_t os_kernel_is_active (void) {
  574. static uint8_t os_kernel_active = 0U;
  575. if (os_kernel_active == 0U) {
  576. if (osKernelGetState() > osKernelReady) {
  577. os_kernel_active = 1U;
  578. }
  579. }
  580. return (uint32_t)os_kernel_active;
  581. }
  582. // Provide libspace for current thread
  583. void *__user_perthread_libspace (void);
  584. void *__user_perthread_libspace (void) {
  585. osThreadId_t id;
  586. uint32_t n;
  587. if (os_kernel_is_active() != 0U) {
  588. id = osThreadGetId();
  589. for (n = 0U; n < (uint32_t)OS_THREAD_LIBSPACE_NUM; n++) {
  590. if (os_libspace_id[n] == NULL) {
  591. os_libspace_id[n] = id;
  592. }
  593. if (os_libspace_id[n] == id) {
  594. break;
  595. }
  596. }
  597. if (n == (uint32_t)OS_THREAD_LIBSPACE_NUM) {
  598. (void)osRtxErrorNotify(osRtxErrorClibSpace, id);
  599. }
  600. } else {
  601. n = OS_THREAD_LIBSPACE_NUM;
  602. }
  603. //lint -e{9087} "cast between pointers to different object types"
  604. return (void *)&os_libspace[n][0];
  605. }
  606. // Mutex identifier
  607. typedef void *mutex;
  608. //lint -save "Function prototypes defined in C library"
  609. //lint -e970 "Use of 'int' outside of a typedef"
  610. //lint -e818 "Pointer 'm' could be declared as pointing to const"
  611. // Initialize mutex
  612. __USED
  613. int _mutex_initialize(mutex *m);
  614. int _mutex_initialize(mutex *m) {
  615. int result;
  616. *m = osMutexNew(NULL);
  617. if (*m != NULL) {
  618. result = 1;
  619. } else {
  620. result = 0;
  621. (void)osRtxErrorNotify(osRtxErrorClibMutex, m);
  622. }
  623. return result;
  624. }
  625. // Acquire mutex
  626. __USED
  627. void _mutex_acquire(mutex *m);
  628. void _mutex_acquire(mutex *m) {
  629. if (os_kernel_is_active() != 0U) {
  630. (void)osMutexAcquire(*m, osWaitForever);
  631. }
  632. }
  633. // Release mutex
  634. __USED
  635. void _mutex_release(mutex *m);
  636. void _mutex_release(mutex *m) {
  637. if (os_kernel_is_active() != 0U) {
  638. (void)osMutexRelease(*m);
  639. }
  640. }
  641. // Free mutex
  642. __USED
  643. void _mutex_free(mutex *m);
  644. void _mutex_free(mutex *m) {
  645. (void)osMutexDelete(*m);
  646. }
  647. //lint -restore
  648. #endif