rtx_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright (c) 2013-2019 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. #if (OS_ISR_FIFO_QUEUE < 4)
  49. #error "Invalid ISR FIFO Queue size!"
  50. #endif
  51. static void *os_isr_queue[OS_ISR_FIFO_QUEUE] \
  52. __attribute__((section(".bss.os")));
  53. // Thread Configuration
  54. // ====================
  55. #if (((OS_STACK_SIZE % 8) != 0) || (OS_STACK_SIZE < 72))
  56. #error "Invalid default Thread Stack size!"
  57. #endif
  58. #if (((OS_IDLE_THREAD_STACK_SIZE % 8) != 0) || (OS_IDLE_THREAD_STACK_SIZE < 72))
  59. #error "Invalid Idle Thread Stack size!"
  60. #endif
  61. #if (OS_THREAD_OBJ_MEM != 0)
  62. #if (OS_THREAD_NUM == 0)
  63. #error "Invalid number of user Threads!"
  64. #endif
  65. #if ((OS_THREAD_USER_STACK_SIZE != 0) && ((OS_THREAD_USER_STACK_SIZE % 8) != 0))
  66. #error "Invalid total Stack size!"
  67. #endif
  68. // Thread Control Blocks
  69. static osRtxThread_t os_thread_cb[OS_THREAD_NUM] \
  70. __attribute__((section(".bss.os.thread.cb")));
  71. // Thread Default Stack
  72. #if (OS_THREAD_DEF_STACK_NUM != 0)
  73. static uint64_t os_thread_def_stack[OS_THREAD_DEF_STACK_NUM*(OS_STACK_SIZE/8)] \
  74. __attribute__((section(".bss.os.thread.stack")));
  75. #endif
  76. // Memory Pool for Thread Control Blocks
  77. static osRtxMpInfo_t os_mpi_thread \
  78. __attribute__((section(".data.os.thread.mpi"))) =
  79. { (uint32_t)OS_THREAD_NUM, 0U, (uint32_t)osRtxThreadCbSize, &os_thread_cb[0], NULL, NULL };
  80. // Memory Pool for Thread Default Stack
  81. #if (OS_THREAD_DEF_STACK_NUM != 0)
  82. static osRtxMpInfo_t os_mpi_def_stack \
  83. __attribute__((section(".data.os.thread.mpi"))) =
  84. { (uint32_t)OS_THREAD_DEF_STACK_NUM, 0U, (uint32_t)OS_STACK_SIZE, &os_thread_def_stack[0], NULL, NULL };
  85. #endif
  86. // Memory Pool for Thread Stack
  87. #if (OS_THREAD_USER_STACK_SIZE != 0)
  88. static uint64_t os_thread_stack[2 + OS_THREAD_NUM + (OS_THREAD_USER_STACK_SIZE/8)] \
  89. __attribute__((section(".bss.os.thread.stack")));
  90. #endif
  91. #endif // (OS_THREAD_OBJ_MEM != 0)
  92. // Stack overrun checking
  93. #if (OS_STACK_CHECK == 0)
  94. // Override library function
  95. extern void osRtxThreadStackCheck (void);
  96. void osRtxThreadStackCheck (void) {}
  97. #endif
  98. // Idle Thread Control Block
  99. static osRtxThread_t os_idle_thread_cb \
  100. __attribute__((section(".bss.os.thread.cb")));
  101. // Idle Thread Stack
  102. static uint64_t os_idle_thread_stack[OS_IDLE_THREAD_STACK_SIZE/8] \
  103. __attribute__((section(".bss.os.thread.stack")));
  104. // Idle Thread Attributes
  105. static const osThreadAttr_t os_idle_thread_attr = {
  106. #if defined(OS_IDLE_THREAD_NAME)
  107. OS_IDLE_THREAD_NAME,
  108. #else
  109. NULL,
  110. #endif
  111. osThreadDetached,
  112. &os_idle_thread_cb,
  113. (uint32_t)sizeof(os_idle_thread_cb),
  114. &os_idle_thread_stack[0],
  115. (uint32_t)sizeof(os_idle_thread_stack),
  116. osPriorityIdle,
  117. #if defined(OS_IDLE_THREAD_TZ_MOD_ID)
  118. (uint32_t)OS_IDLE_THREAD_TZ_MOD_ID,
  119. #else
  120. 0U,
  121. #endif
  122. 0U
  123. };
  124. // Timer Configuration
  125. // ===================
  126. #if (OS_TIMER_OBJ_MEM != 0)
  127. #if (OS_TIMER_NUM == 0)
  128. #error "Invalid number of Timer objects!"
  129. #endif
  130. // Timer Control Blocks
  131. static osRtxTimer_t os_timer_cb[OS_TIMER_NUM] \
  132. __attribute__((section(".bss.os.timer.cb")));
  133. // Memory Pool for Timer Control Blocks
  134. static osRtxMpInfo_t os_mpi_timer \
  135. __attribute__((section(".data.os.timer.mpi"))) =
  136. { (uint32_t)OS_TIMER_NUM, 0U, (uint32_t)osRtxTimerCbSize, &os_timer_cb[0], NULL, NULL };
  137. #endif // (OS_TIMER_OBJ_MEM != 0)
  138. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  139. #if (((OS_TIMER_THREAD_STACK_SIZE % 8) != 0) || (OS_TIMER_THREAD_STACK_SIZE < 96))
  140. #error "Invalid Timer Thread Stack size!"
  141. #endif
  142. // Timer Thread Control Block
  143. static osRtxThread_t os_timer_thread_cb \
  144. __attribute__((section(".bss.os.thread.cb")));
  145. // Timer Thread Stack
  146. static uint64_t os_timer_thread_stack[OS_TIMER_THREAD_STACK_SIZE/8] \
  147. __attribute__((section(".bss.os.thread.stack")));
  148. // Timer Thread Attributes
  149. static const osThreadAttr_t os_timer_thread_attr = {
  150. #if defined(OS_TIMER_THREAD_NAME)
  151. OS_TIMER_THREAD_NAME,
  152. #else
  153. NULL,
  154. #endif
  155. osThreadDetached,
  156. &os_timer_thread_cb,
  157. (uint32_t)sizeof(os_timer_thread_cb),
  158. &os_timer_thread_stack[0],
  159. (uint32_t)sizeof(os_timer_thread_stack),
  160. //lint -e{9030} -e{9034} "cast from signed to enum"
  161. (osPriority_t)OS_TIMER_THREAD_PRIO,
  162. #if defined(OS_TIMER_THREAD_TZ_MOD_ID)
  163. (uint32_t)OS_TIMER_THREAD_TZ_MOD_ID,
  164. #else
  165. 0U,
  166. #endif
  167. 0U
  168. };
  169. // Timer Message Queue Control Block
  170. static osRtxMessageQueue_t os_timer_mq_cb \
  171. __attribute__((section(".bss.os.msgqueue.cb")));
  172. // Timer Message Queue Data
  173. static uint32_t os_timer_mq_data[osRtxMessageQueueMemSize(OS_TIMER_CB_QUEUE,8)/4] \
  174. __attribute__((section(".bss.os.msgqueue.mem")));
  175. // Timer Message Queue Attributes
  176. static const osMessageQueueAttr_t os_timer_mq_attr = {
  177. NULL,
  178. 0U,
  179. &os_timer_mq_cb,
  180. (uint32_t)sizeof(os_timer_mq_cb),
  181. &os_timer_mq_data[0],
  182. (uint32_t)sizeof(os_timer_mq_data)
  183. };
  184. #else
  185. extern void osRtxTimerThread (void *argument);
  186. void osRtxTimerThread (void *argument) { (void)argument; }
  187. #endif // ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  188. // Event Flags Configuration
  189. // =========================
  190. #if (OS_EVFLAGS_OBJ_MEM != 0)
  191. #if (OS_EVFLAGS_NUM == 0)
  192. #error "Invalid number of Event Flags objects!"
  193. #endif
  194. // Event Flags Control Blocks
  195. static osRtxEventFlags_t os_ef_cb[OS_EVFLAGS_NUM] \
  196. __attribute__((section(".bss.os.evflags.cb")));
  197. // Memory Pool for Event Flags Control Blocks
  198. static osRtxMpInfo_t os_mpi_ef \
  199. __attribute__((section(".data.os.evflags.mpi"))) =
  200. { (uint32_t)OS_EVFLAGS_NUM, 0U, (uint32_t)osRtxEventFlagsCbSize, &os_ef_cb[0], NULL, NULL };
  201. #endif // (OS_EVFLAGS_OBJ_MEM != 0)
  202. // Mutex Configuration
  203. // ===================
  204. #if (OS_MUTEX_OBJ_MEM != 0)
  205. #if (OS_MUTEX_NUM == 0)
  206. #error "Invalid number of Mutex objects!"
  207. #endif
  208. // Mutex Control Blocks
  209. static osRtxMutex_t os_mutex_cb[OS_MUTEX_NUM] \
  210. __attribute__((section(".bss.os.mutex.cb")));
  211. // Memory Pool for Mutex Control Blocks
  212. static osRtxMpInfo_t os_mpi_mutex \
  213. __attribute__((section(".data.os.mutex.mpi"))) =
  214. { (uint32_t)OS_MUTEX_NUM, 0U, (uint32_t)osRtxMutexCbSize, &os_mutex_cb[0], NULL, NULL };
  215. #endif // (OS_MUTEX_OBJ_MEM != 0)
  216. // Semaphore Configuration
  217. // =======================
  218. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  219. #if (OS_SEMAPHORE_NUM == 0)
  220. #error "Invalid number of Semaphore objects!"
  221. #endif
  222. // Semaphore Control Blocks
  223. static osRtxSemaphore_t os_semaphore_cb[OS_SEMAPHORE_NUM] \
  224. __attribute__((section(".bss.os.semaphore.cb")));
  225. // Memory Pool for Semaphore Control Blocks
  226. static osRtxMpInfo_t os_mpi_semaphore \
  227. __attribute__((section(".data.os.semaphore.mpi"))) =
  228. { (uint32_t)OS_SEMAPHORE_NUM, 0U, (uint32_t)osRtxSemaphoreCbSize, &os_semaphore_cb[0], NULL, NULL };
  229. #endif // (OS_SEMAPHORE_OBJ_MEM != 0)
  230. // Memory Pool Configuration
  231. // =========================
  232. #if (OS_MEMPOOL_OBJ_MEM != 0)
  233. #if (OS_MEMPOOL_NUM == 0)
  234. #error "Invalid number of Memory Pool objects!"
  235. #endif
  236. // Memory Pool Control Blocks
  237. static osRtxMemoryPool_t os_mp_cb[OS_MEMPOOL_NUM] \
  238. __attribute__((section(".bss.os.mempool.cb")));
  239. // Memory Pool for Memory Pool Control Blocks
  240. static osRtxMpInfo_t os_mpi_mp \
  241. __attribute__((section(".data.os.mempool.mpi"))) =
  242. { (uint32_t)OS_MEMPOOL_NUM, 0U, (uint32_t)osRtxMemoryPoolCbSize, &os_mp_cb[0], NULL, NULL };
  243. // Memory Pool for Memory Pool Data Storage
  244. #if (OS_MEMPOOL_DATA_SIZE != 0)
  245. #if ((OS_MEMPOOL_DATA_SIZE % 8) != 0)
  246. #error "Invalid Data Memory size for Memory Pools!"
  247. #endif
  248. static uint64_t os_mp_data[2 + OS_MEMPOOL_NUM + (OS_MEMPOOL_DATA_SIZE/8)] \
  249. __attribute__((section(".bss.os.mempool.mem")));
  250. #endif
  251. #endif // (OS_MEMPOOL_OBJ_MEM != 0)
  252. // Message Queue Configuration
  253. // ===========================
  254. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  255. #if (OS_MSGQUEUE_NUM == 0)
  256. #error "Invalid number of Message Queue objects!"
  257. #endif
  258. // Message Queue Control Blocks
  259. static osRtxMessageQueue_t os_mq_cb[OS_MSGQUEUE_NUM] \
  260. __attribute__((section(".bss.os.msgqueue.cb")));
  261. // Memory Pool for Message Queue Control Blocks
  262. static osRtxMpInfo_t os_mpi_mq \
  263. __attribute__((section(".data.os.msgqueue.mpi"))) =
  264. { (uint32_t)OS_MSGQUEUE_NUM, 0U, (uint32_t)osRtxMessageQueueCbSize, &os_mq_cb[0], NULL, NULL };
  265. // Memory Pool for Message Queue Data Storage
  266. #if (OS_MSGQUEUE_DATA_SIZE != 0)
  267. #if ((OS_MSGQUEUE_DATA_SIZE % 8) != 0)
  268. #error "Invalid Data Memory size for Message Queues!"
  269. #endif
  270. static uint64_t os_mq_data[2 + OS_MSGQUEUE_NUM + (OS_MSGQUEUE_DATA_SIZE/8)] \
  271. __attribute__((section(".bss.os.msgqueue.mem")));
  272. #endif
  273. #endif // (OS_MSGQUEUE_OBJ_MEM != 0)
  274. // Event Recorder Configuration
  275. // ============================
  276. #if (defined(OS_EVR_INIT) && (OS_EVR_INIT != 0))
  277. // Initial Thread configuration covered also Thread Flags and Generic Wait
  278. #if defined(OS_EVR_THREAD_FILTER)
  279. #if !defined(OS_EVR_THFLAGS_FILTER)
  280. #define OS_EVR_THFLAGS_FILTER OS_EVR_THREAD_FILTER
  281. #endif
  282. #if !defined(OS_EVR_WAIT_FILTER)
  283. #define OS_EVR_WAIT_FILTER OS_EVR_THREAD_FILTER
  284. #endif
  285. #endif
  286. // Migrate initial filter configuration
  287. #if defined(OS_EVR_MEMORY_FILTER)
  288. #define OS_EVR_MEMORY_LEVEL (((OS_EVR_MEMORY_FILTER & 0x80U) != 0U) ? (OS_EVR_MEMORY_FILTER & 0x0FU) : 0U)
  289. #endif
  290. #if defined(OS_EVR_KERNEL_FILTER)
  291. #define OS_EVR_KERNEL_LEVEL (((OS_EVR_KERNEL_FILTER & 0x80U) != 0U) ? (OS_EVR_KERNEL_FILTER & 0x0FU) : 0U)
  292. #endif
  293. #if defined(OS_EVR_THREAD_FILTER)
  294. #define OS_EVR_THREAD_LEVEL (((OS_EVR_THREAD_FILTER & 0x80U) != 0U) ? (OS_EVR_THREAD_FILTER & 0x0FU) : 0U)
  295. #endif
  296. #if defined(OS_EVR_WAIT_FILTER)
  297. #define OS_EVR_WAIT_LEVEL (((OS_EVR_WAIT_FILTER & 0x80U) != 0U) ? (OS_EVR_WAIT_FILTER & 0x0FU) : 0U)
  298. #endif
  299. #if defined(OS_EVR_THFLAGS_FILTER)
  300. #define OS_EVR_THFLAGS_LEVEL (((OS_EVR_THFLAGS_FILTER & 0x80U) != 0U) ? (OS_EVR_THFLAGS_FILTER & 0x0FU) : 0U)
  301. #endif
  302. #if defined(OS_EVR_EVFLAGS_FILTER)
  303. #define OS_EVR_EVFLAGS_LEVEL (((OS_EVR_EVFLAGS_FILTER & 0x80U) != 0U) ? (OS_EVR_EVFLAGS_FILTER & 0x0FU) : 0U)
  304. #endif
  305. #if defined(OS_EVR_TIMER_FILTER)
  306. #define OS_EVR_TIMER_LEVEL (((OS_EVR_TIMER_FILTER & 0x80U) != 0U) ? (OS_EVR_TIMER_FILTER & 0x0FU) : 0U)
  307. #endif
  308. #if defined(OS_EVR_MUTEX_FILTER)
  309. #define OS_EVR_MUTEX_LEVEL (((OS_EVR_MUTEX_FILTER & 0x80U) != 0U) ? (OS_EVR_MUTEX_FILTER & 0x0FU) : 0U)
  310. #endif
  311. #if defined(OS_EVR_SEMAPHORE_FILTER)
  312. #define OS_EVR_SEMAPHORE_LEVEL (((OS_EVR_SEMAPHORE_FILTER & 0x80U) != 0U) ? (OS_EVR_SEMAPHORE_FILTER & 0x0FU) : 0U)
  313. #endif
  314. #if defined(OS_EVR_MEMPOOL_FILTER)
  315. #define OS_EVR_MEMPOOL_LEVEL (((OS_EVR_MEMPOOL_FILTER & 0x80U) != 0U) ? (OS_EVR_MEMPOOL_FILTER & 0x0FU) : 0U)
  316. #endif
  317. #if defined(OS_EVR_MSGQUEUE_FILTER)
  318. #define OS_EVR_MSGQUEUE_LEVEL (((OS_EVR_MSGQUEUE_FILTER & 0x80U) != 0U) ? (OS_EVR_MSGQUEUE_FILTER & 0x0FU) : 0U)
  319. #endif
  320. #if defined(RTE_Compiler_EventRecorder)
  321. // Event Recorder Initialize
  322. __STATIC_INLINE void evr_initialize (void) {
  323. (void)EventRecorderInitialize(OS_EVR_LEVEL, (uint32_t)OS_EVR_START);
  324. (void)EventRecorderEnable(OS_EVR_MEMORY_LEVEL, EvtRtxMemoryNo, EvtRtxMemoryNo);
  325. (void)EventRecorderEnable(OS_EVR_KERNEL_LEVEL, EvtRtxKernelNo, EvtRtxKernelNo);
  326. (void)EventRecorderEnable(OS_EVR_THREAD_LEVEL, EvtRtxThreadNo, EvtRtxThreadNo);
  327. (void)EventRecorderEnable(OS_EVR_WAIT_LEVEL, EvtRtxWaitNo, EvtRtxWaitNo);
  328. (void)EventRecorderEnable(OS_EVR_THFLAGS_LEVEL, EvtRtxThreadFlagsNo, EvtRtxThreadFlagsNo);
  329. (void)EventRecorderEnable(OS_EVR_EVFLAGS_LEVEL, EvtRtxEventFlagsNo, EvtRtxEventFlagsNo);
  330. (void)EventRecorderEnable(OS_EVR_TIMER_LEVEL, EvtRtxTimerNo, EvtRtxTimerNo);
  331. (void)EventRecorderEnable(OS_EVR_MUTEX_LEVEL, EvtRtxMutexNo, EvtRtxMutexNo);
  332. (void)EventRecorderEnable(OS_EVR_SEMAPHORE_LEVEL, EvtRtxSemaphoreNo, EvtRtxSemaphoreNo);
  333. (void)EventRecorderEnable(OS_EVR_MEMPOOL_LEVEL, EvtRtxMemoryPoolNo, EvtRtxMemoryPoolNo);
  334. (void)EventRecorderEnable(OS_EVR_MSGQUEUE_LEVEL, EvtRtxMessageQueueNo, EvtRtxMessageQueueNo);
  335. }
  336. #else
  337. #warning "Event Recorder cannot be initialized (Event Recorder component is not selected)!"
  338. #define evr_initialize()
  339. #endif
  340. #endif // (OS_EVR_INIT != 0)
  341. // OS Configuration
  342. // ================
  343. const osRtxConfig_t osRtxConfig \
  344. __USED \
  345. __attribute__((section(".rodata"))) =
  346. {
  347. //lint -e{835} "Zero argument to operator"
  348. 0U // Flags
  349. #if (OS_PRIVILEGE_MODE != 0)
  350. | osRtxConfigPrivilegedMode
  351. #endif
  352. #if (OS_STACK_CHECK != 0)
  353. | osRtxConfigStackCheck
  354. #endif
  355. #if (OS_STACK_WATERMARK != 0)
  356. | osRtxConfigStackWatermark
  357. #endif
  358. ,
  359. (uint32_t)OS_TICK_FREQ,
  360. #if (OS_ROBIN_ENABLE != 0)
  361. (uint32_t)OS_ROBIN_TIMEOUT,
  362. #else
  363. 0U,
  364. #endif
  365. { &os_isr_queue[0], (uint16_t)(sizeof(os_isr_queue)/sizeof(void *)), 0U },
  366. {
  367. // Memory Pools (Variable Block Size)
  368. #if ((OS_THREAD_OBJ_MEM != 0) && (OS_THREAD_USER_STACK_SIZE != 0))
  369. &os_thread_stack[0], sizeof(os_thread_stack),
  370. #else
  371. NULL, 0U,
  372. #endif
  373. #if ((OS_MEMPOOL_OBJ_MEM != 0) && (OS_MEMPOOL_DATA_SIZE != 0))
  374. &os_mp_data[0], sizeof(os_mp_data),
  375. #else
  376. NULL, 0U,
  377. #endif
  378. #if ((OS_MSGQUEUE_OBJ_MEM != 0) && (OS_MSGQUEUE_DATA_SIZE != 0))
  379. &os_mq_data[0], sizeof(os_mq_data),
  380. #else
  381. NULL, 0U,
  382. #endif
  383. #if (OS_DYNAMIC_MEM_SIZE != 0)
  384. &os_mem[0], (uint32_t)OS_DYNAMIC_MEM_SIZE,
  385. #else
  386. NULL, 0U
  387. #endif
  388. },
  389. {
  390. // Memory Pools (Fixed Block Size)
  391. #if (OS_THREAD_OBJ_MEM != 0)
  392. #if (OS_THREAD_DEF_STACK_NUM != 0)
  393. &os_mpi_def_stack,
  394. #else
  395. NULL,
  396. #endif
  397. &os_mpi_thread,
  398. #else
  399. NULL,
  400. NULL,
  401. #endif
  402. #if (OS_TIMER_OBJ_MEM != 0)
  403. &os_mpi_timer,
  404. #else
  405. NULL,
  406. #endif
  407. #if (OS_EVFLAGS_OBJ_MEM != 0)
  408. &os_mpi_ef,
  409. #else
  410. NULL,
  411. #endif
  412. #if (OS_MUTEX_OBJ_MEM != 0)
  413. &os_mpi_mutex,
  414. #else
  415. NULL,
  416. #endif
  417. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  418. &os_mpi_semaphore,
  419. #else
  420. NULL,
  421. #endif
  422. #if (OS_MEMPOOL_OBJ_MEM != 0)
  423. &os_mpi_mp,
  424. #else
  425. NULL,
  426. #endif
  427. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  428. &os_mpi_mq,
  429. #else
  430. NULL,
  431. #endif
  432. },
  433. (uint32_t)OS_STACK_SIZE,
  434. &os_idle_thread_attr,
  435. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  436. &os_timer_thread_attr,
  437. &os_timer_mq_attr,
  438. (uint32_t)OS_TIMER_CB_QUEUE
  439. #else
  440. NULL,
  441. NULL,
  442. 0U
  443. #endif
  444. };
  445. // Non weak reference to library irq module
  446. //lint -esym(526,irqRtxLib) "Defined by Exception handlers"
  447. //lint -esym(714,irqRtxLibRef) "Non weak reference"
  448. //lint -esym(765,irqRtxLibRef) "Global scope"
  449. extern uint8_t irqRtxLib;
  450. extern const uint8_t *irqRtxLibRef;
  451. const uint8_t *irqRtxLibRef = &irqRtxLib;
  452. // Default User SVC Table
  453. //lint -esym(714,osRtxUserSVC) "Referenced by Exception handlers"
  454. //lint -esym(765,osRtxUserSVC) "Global scope"
  455. //lint -e{9067} "extern array declared without size"
  456. extern void * const osRtxUserSVC[];
  457. __WEAK void * const osRtxUserSVC[1] = { (void *)0 };
  458. // OS Sections
  459. // ===========
  460. #if defined(__CC_ARM) || \
  461. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  462. static uint32_t __os_thread_cb_start__ __attribute__((weakref(".bss.os.thread.cb$$Base"))); //lint -esym(728,__os_thread_cb_start__)
  463. static uint32_t __os_thread_cb_end__ __attribute__((weakref(".bss.os.thread.cb$$Limit"))); //lint -esym(728,__os_thread_cb_end__)
  464. static uint32_t __os_timer_cb_start__ __attribute__((weakref(".bss.os.timer.cb$$Base"))); //lint -esym(728,__os_timer_cb_start__)
  465. static uint32_t __os_timer_cb_end__ __attribute__((weakref(".bss.os.timer.cb$$Limit"))); //lint -esym(728,__os_timer_cb_end__)
  466. static uint32_t __os_evflags_cb_start__ __attribute__((weakref(".bss.os.evflags.cb$$Base"))); //lint -esym(728,__os_evflags_cb_start__)
  467. static uint32_t __os_evflags_cb_end__ __attribute__((weakref(".bss.os.evflags.cb$$Limit"))); //lint -esym(728,__os_evflags_cb_end__)
  468. static uint32_t __os_mutex_cb_start__ __attribute__((weakref(".bss.os.mutex.cb$$Base"))); //lint -esym(728,__os_mutex_cb_start__)
  469. static uint32_t __os_mutex_cb_end__ __attribute__((weakref(".bss.os.mutex.cb$$Limit"))); //lint -esym(728,__os_mutex_cb_end__)
  470. static uint32_t __os_semaphore_cb_start__ __attribute__((weakref(".bss.os.semaphore.cb$$Base"))); //lint -esym(728,__os_semaphore_cb_start__)
  471. static uint32_t __os_semaphore_cb_end__ __attribute__((weakref(".bss.os.semaphore.cb$$Limit"))); //lint -esym(728,__os_semaphore_cb_end__)
  472. static uint32_t __os_mempool_cb_start__ __attribute__((weakref(".bss.os.mempool.cb$$Base"))); //lint -esym(728,__os_mempool_cb_start__)
  473. static uint32_t __os_mempool_cb_end__ __attribute__((weakref(".bss.os.mempool.cb$$Limit"))); //lint -esym(728,__os_mempool_cb_end__)
  474. static uint32_t __os_msgqueue_cb_start__ __attribute__((weakref(".bss.os.msgqueue.cb$$Base"))); //lint -esym(728,__os_msgqueue_cb_start__)
  475. static uint32_t __os_msgqueue_cb_end__ __attribute__((weakref(".bss.os.msgqueue.cb$$Limit"))); //lint -esym(728,__os_msgqueue_cb_end__)
  476. #else
  477. extern uint32_t __os_thread_cb_start__ __attribute__((weak));
  478. extern uint32_t __os_thread_cb_end__ __attribute__((weak));
  479. extern uint32_t __os_timer_cb_start__ __attribute__((weak));
  480. extern uint32_t __os_timer_cb_end__ __attribute__((weak));
  481. extern uint32_t __os_evflags_cb_start__ __attribute__((weak));
  482. extern uint32_t __os_evflags_cb_end__ __attribute__((weak));
  483. extern uint32_t __os_mutex_cb_start__ __attribute__((weak));
  484. extern uint32_t __os_mutex_cb_end__ __attribute__((weak));
  485. extern uint32_t __os_semaphore_cb_start__ __attribute__((weak));
  486. extern uint32_t __os_semaphore_cb_end__ __attribute__((weak));
  487. extern uint32_t __os_mempool_cb_start__ __attribute__((weak));
  488. extern uint32_t __os_mempool_cb_end__ __attribute__((weak));
  489. extern uint32_t __os_msgqueue_cb_start__ __attribute__((weak));
  490. extern uint32_t __os_msgqueue_cb_end__ __attribute__((weak));
  491. #endif
  492. //lint -e{9067} "extern array declared without size"
  493. extern const uint32_t * const os_cb_sections[];
  494. //lint -esym(714,os_cb_sections) "Referenced by debugger"
  495. //lint -esym(765,os_cb_sections) "Global scope"
  496. const uint32_t * const os_cb_sections[] \
  497. __USED \
  498. __attribute__((section(".rodata"))) =
  499. {
  500. &__os_thread_cb_start__,
  501. &__os_thread_cb_end__,
  502. &__os_timer_cb_start__,
  503. &__os_timer_cb_end__,
  504. &__os_evflags_cb_start__,
  505. &__os_evflags_cb_end__,
  506. &__os_mutex_cb_start__,
  507. &__os_mutex_cb_end__,
  508. &__os_semaphore_cb_start__,
  509. &__os_semaphore_cb_end__,
  510. &__os_mempool_cb_start__,
  511. &__os_mempool_cb_end__,
  512. &__os_msgqueue_cb_start__,
  513. &__os_msgqueue_cb_end__
  514. };
  515. // OS Initialization
  516. // =================
  517. #if defined(__CC_ARM) || \
  518. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  519. #ifndef __MICROLIB
  520. //lint -esym(714,_platform_post_stackheap_init) "Referenced by C library"
  521. //lint -esym(765,_platform_post_stackheap_init) "Global scope"
  522. extern void _platform_post_stackheap_init (void);
  523. __WEAK void _platform_post_stackheap_init (void) {
  524. (void)osKernelInitialize();
  525. }
  526. #endif
  527. #elif defined(__GNUC__)
  528. extern void software_init_hook (void);
  529. __WEAK void software_init_hook (void) {
  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)osRtxErrorNotify(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)osRtxErrorNotify(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