rtx_lib.c 23 KB

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