rtx_lib.c 22 KB

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