rtx_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * Copyright (c) 2013-2017 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. #include "RTX_Config.h"
  28. // System Configuration
  29. // ====================
  30. // Dynamic Memory
  31. #if (OS_DYNAMIC_MEM_SIZE != 0)
  32. #if ((OS_DYNAMIC_MEM_SIZE & 7) != 0)
  33. #error "Invalid Dynamic Memory size!"
  34. #endif
  35. static uint64_t os_mem[OS_DYNAMIC_MEM_SIZE/8] \
  36. __attribute__((section(".bss.os")));
  37. #endif
  38. // Kernel Tick Frequency
  39. #if (OS_TICK_FREQ < 1)
  40. #error "Invalid Kernel Tick Frequency!"
  41. #endif
  42. // ISR FIFO Queue
  43. static void *os_isr_queue[OS_ISR_FIFO_QUEUE] \
  44. __attribute__((section(".bss.os")));
  45. // Thread Configuration
  46. // ====================
  47. #if (((OS_STACK_SIZE & 7) != 0) || (OS_STACK_SIZE < 72))
  48. #error "Invalid default Thread Stack size!"
  49. #endif
  50. #if (((OS_IDLE_THREAD_STACK_SIZE & 7) != 0) || (OS_IDLE_THREAD_STACK_SIZE < 72))
  51. #error "Invalid Idle Thread Stack size!"
  52. #endif
  53. #if (OS_THREAD_OBJ_MEM != 0)
  54. #if (OS_THREAD_NUM == 0)
  55. #error "Invalid number of user Threads!"
  56. #endif
  57. #if ((OS_THREAD_USER_STACK_SIZE != 0) && ((OS_THREAD_USER_STACK_SIZE & 7) != 0))
  58. #error "Invalid total Stack size!"
  59. #endif
  60. // Thread Control Blocks
  61. static osRtxThread_t os_thread_cb[OS_THREAD_NUM] \
  62. __attribute__((section(".bss.os.thread.cb")));
  63. // Thread Default Stack
  64. #if (OS_THREAD_DEF_STACK_NUM != 0)
  65. static uint64_t os_thread_def_stack[OS_THREAD_DEF_STACK_NUM*(OS_STACK_SIZE/8)] \
  66. __attribute__((section(".bss.os.thread.stack")));
  67. #endif
  68. // Memory Pool for Thread Control Blocks
  69. static osRtxMpInfo_t os_mpi_thread \
  70. __attribute__((section(".data.os.thread.mpi"))) =
  71. { (uint32_t)OS_THREAD_NUM, 0U, (uint32_t)osRtxThreadCbSize, &os_thread_cb, NULL, NULL };
  72. // Memory Pool for Thread Default Stack
  73. #if (OS_THREAD_DEF_STACK_NUM != 0)
  74. static osRtxMpInfo_t os_mpi_def_stack \
  75. __attribute__((section(".data.os.thread.mpi"))) =
  76. { (uint32_t)OS_THREAD_DEF_STACK_NUM, 0U, (uint32_t)OS_STACK_SIZE, &os_thread_def_stack, NULL, NULL };
  77. #endif
  78. // Memory Pool for Thread Stack
  79. #if (OS_THREAD_USER_STACK_SIZE != 0)
  80. static uint64_t os_thread_stack[OS_THREAD_USER_STACK_SIZE/8] \
  81. __attribute__((section(".bss.os.thread.stack")));
  82. #endif
  83. #endif // (OS_THREAD_OBJ_MEM != 0)
  84. // Stack overrun checking
  85. #if (OS_STACK_CHECK == 0)
  86. // Override library function
  87. void osRtxThreadStackCheck (void);
  88. void osRtxThreadStackCheck (void) {}
  89. #endif
  90. // Idle Thread Control Block
  91. static osRtxThread_t os_idle_thread_cb \
  92. __attribute__((section(".bss.os.thread.cb")));
  93. // Idle Thread Stack
  94. static uint64_t os_idle_thread_stack[OS_IDLE_THREAD_STACK_SIZE/8] \
  95. __attribute__((section(".bss.os.thread.stack")));
  96. // Idle Thread Attributes
  97. static const osThreadAttr_t os_idle_thread_attr = {
  98. NULL,
  99. osThreadDetached,
  100. &os_idle_thread_cb,
  101. (uint32_t)sizeof(os_idle_thread_cb),
  102. &os_idle_thread_stack,
  103. (uint32_t)sizeof(os_idle_thread_stack),
  104. osPriorityIdle,
  105. 0U, 0U
  106. };
  107. // Timer Configuration
  108. // ===================
  109. #if (OS_TIMER_OBJ_MEM != 0)
  110. #if (OS_TIMER_NUM == 0)
  111. #error "Invalid number of Timer objects!"
  112. #endif
  113. // Timer Control Blocks
  114. static osRtxTimer_t os_timer_cb[OS_TIMER_NUM] \
  115. __attribute__((section(".bss.os.timer.cb")));
  116. // Memory Pool for Timer Control Blocks
  117. static osRtxMpInfo_t os_mpi_timer \
  118. __attribute__((section(".data.os.timer.mpi"))) =
  119. { (uint32_t)OS_TIMER_NUM, 0U, (uint32_t)osRtxTimerCbSize, &os_timer_cb, NULL, NULL };
  120. #endif // (OS_TIMER_OBJ_MEM != 0)
  121. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  122. #if (((OS_TIMER_THREAD_STACK_SIZE & 7) != 0) || (OS_TIMER_THREAD_STACK_SIZE < 96))
  123. #error "Invalid Timer Thread Stack size!"
  124. #endif
  125. // Timer Thread Control Block
  126. static osRtxThread_t os_timer_thread_cb \
  127. __attribute__((section(".bss.os.thread.cb")));
  128. // Timer Thread Stack
  129. static uint64_t os_timer_thread_stack[OS_TIMER_THREAD_STACK_SIZE/8] \
  130. __attribute__((section(".bss.os.thread.stack")));
  131. // Timer Thread Attributes
  132. static const osThreadAttr_t os_timer_thread_attr = {
  133. NULL,
  134. osThreadDetached,
  135. &os_timer_thread_cb,
  136. (uint32_t)sizeof(os_timer_thread_cb),
  137. &os_timer_thread_stack,
  138. (uint32_t)sizeof(os_timer_thread_stack),
  139. (osPriority_t)OS_TIMER_THREAD_PRIO,
  140. 0U, 0U
  141. };
  142. // Timer Message Queue Control Block
  143. static osRtxMessageQueue_t os_timer_mq_cb \
  144. __attribute__((section(".bss.os.msgqueue.cb")));
  145. // Timer Message Queue Data
  146. static uint32_t os_timer_mq_data[osRtxMessageQueueMemSize(OS_TIMER_CB_QUEUE,8)/4] \
  147. __attribute__((section(".bss.os.msgqueue.mem")));
  148. // Timer Message Queue Attributes
  149. static const osMessageQueueAttr_t os_timer_mq_attr = {
  150. NULL,
  151. 0U,
  152. &os_timer_mq_cb,
  153. (uint32_t)sizeof(os_timer_mq_cb),
  154. &os_timer_mq_data,
  155. (uint32_t)sizeof(os_timer_mq_data)
  156. };
  157. #else
  158. extern void osRtxTimerThread (void *argument);
  159. void osRtxTimerThread (void *argument) {}
  160. #endif // ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  161. // Event Flags Configuration
  162. // =========================
  163. #if (OS_EVFLAGS_OBJ_MEM != 0)
  164. #if (OS_EVFLAGS_NUM == 0)
  165. #error "Invalid number of Event Flags objects!"
  166. #endif
  167. // Event Flags Control Blocks
  168. static osRtxEventFlags_t os_ef_cb[OS_EVFLAGS_NUM] \
  169. __attribute__((section(".bss.os.evflags.cb")));
  170. // Memory Pool for Event Flags Control Blocks
  171. static osRtxMpInfo_t os_mpi_ef \
  172. __attribute__((section(".data.os.evflags.mpi"))) =
  173. { (uint32_t)OS_EVFLAGS_NUM, 0U, (uint32_t)osRtxEventFlagsCbSize, &os_ef_cb, NULL, NULL };
  174. #endif // (OS_EVFLAGS_OBJ_MEM != 0)
  175. // Mutex Configuration
  176. // ===================
  177. #if (OS_MUTEX_OBJ_MEM != 0)
  178. #if (OS_MUTEX_NUM == 0)
  179. #error "Invalid number of Mutex objects!"
  180. #endif
  181. // Mutex Control Blocks
  182. static osRtxMutex_t os_mutex_cb[OS_MUTEX_NUM] \
  183. __attribute__((section(".bss.os.mutex.cb")));
  184. // Memory Pool for Mutex Control Blocks
  185. static osRtxMpInfo_t os_mpi_mutex \
  186. __attribute__((section(".data.os.mutex.mpi"))) =
  187. { (uint32_t)OS_MUTEX_NUM, 0U, (uint32_t)osRtxMutexCbSize, &os_mutex_cb, NULL, NULL };
  188. #endif // (OS_MUTEX_OBJ_MEM != 0)
  189. // Semaphore Configuration
  190. // =======================
  191. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  192. #if (OS_SEMAPHORE_NUM == 0)
  193. #error "Invalid number of Semaphore objects!"
  194. #endif
  195. // Semaphore Control Blocks
  196. static osRtxSemaphore_t os_semaphore_cb[OS_SEMAPHORE_NUM] \
  197. __attribute__((section(".bss.os.semaphore.cb")));
  198. // Memory Pool for Semaphore Control Blocks
  199. static osRtxMpInfo_t os_mpi_semaphore \
  200. __attribute__((section(".data.os.semaphore.mpi"))) =
  201. { (uint32_t)OS_SEMAPHORE_NUM, 0U, (uint32_t)osRtxSemaphoreCbSize, &os_semaphore_cb, NULL, NULL };
  202. #endif // (OS_SEMAPHORE_OBJ_MEM != 0)
  203. // Memory Pool Configuration
  204. // =========================
  205. #if (OS_MEMPOOL_OBJ_MEM != 0)
  206. #if (OS_MEMPOOL_NUM == 0)
  207. #error "Invalid number of Memory Pool objects!"
  208. #endif
  209. // Memory Pool Control Blocks
  210. static osRtxMemoryPool_t os_mp_cb[OS_MEMPOOL_NUM] \
  211. __attribute__((section(".bss.os.mempool.cb")));
  212. // Memory Pool for Memory Pool Control Blocks
  213. static osRtxMpInfo_t os_mpi_mp \
  214. __attribute__((section(".data.os.mempool.mpi"))) =
  215. { (uint32_t)OS_MEMPOOL_NUM, 0U, (uint32_t)osRtxMemoryPoolCbSize, &os_mp_cb, NULL, NULL };
  216. // Memory Pool for Memory Pool Data Storage
  217. #if (OS_MEMPOOL_DATA_SIZE != 0)
  218. #if ((OS_MEMPOOL_DATA_SIZE & 7) != 0)
  219. #error "Invalid Data Memory size for Memory Pools!"
  220. #endif
  221. static uint64_t os_mp_data[OS_MEMPOOL_DATA_SIZE/8] \
  222. __attribute__((section(".bss.os.mempool.mem")));
  223. #endif
  224. #endif // (OS_MEMPOOL_OBJ_MEM != 0)
  225. // Message Queue Configuration
  226. // ===========================
  227. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  228. #if (OS_MSGQUEUE_NUM == 0)
  229. #error "Invalid number of Message Queue objects!"
  230. #endif
  231. // Message Queue Control Blocks
  232. static osRtxMessageQueue_t os_mq_cb[OS_MSGQUEUE_NUM] \
  233. __attribute__((section(".bss.os.msgqueue.cb")));
  234. // Memory Pool for Message Queue Control Blocks
  235. static osRtxMpInfo_t os_mpi_mq \
  236. __attribute__((section(".data.os.msgqueue.mpi"))) =
  237. { (uint32_t)OS_MSGQUEUE_NUM, 0U, (uint32_t)osRtxMessageQueueCbSize, &os_mq_cb, NULL, NULL };
  238. // Memory Pool for Message Queue Data Storage
  239. #if (OS_MSGQUEUE_DATA_SIZE != 0)
  240. #if ((OS_MSGQUEUE_DATA_SIZE & 7) != 0)
  241. #error "Invalid Data Memory size for Message Queues!"
  242. #endif
  243. static uint64_t os_mq_data[OS_MSGQUEUE_DATA_SIZE/8] \
  244. __attribute__((section(".bss.os.msgqueue.mem")));
  245. #endif
  246. #endif // (OS_MSGQUEUE_OBJ_MEM != 0)
  247. // OS Configuration
  248. // ================
  249. __USED
  250. __attribute__((section(".rodata")))
  251. const osRtxConfig_t osRtxConfig = {
  252. 0U // Flags
  253. #if (OS_PRIVILEGE_MODE != 0)
  254. | osRtxConfigPrivilegedMode
  255. #endif
  256. #if (OS_STACK_CHECK != 0)
  257. | osRtxConfigStackCheck
  258. #endif
  259. #if (OS_STACK_WATERMARK != 0)
  260. | osRtxConfigStackWatermark
  261. #endif
  262. ,
  263. (uint32_t)OS_TICK_FREQ,
  264. #if (OS_ROBIN_ENABLE != 0)
  265. (uint32_t)OS_ROBIN_TIMEOUT,
  266. #else
  267. 0U,
  268. #endif
  269. { &os_isr_queue[0], sizeof(os_isr_queue)/sizeof(void *), 0U },
  270. {
  271. // Memory Pools (Variable Block Size)
  272. #if ((OS_THREAD_OBJ_MEM != 0) && (OS_THREAD_USER_STACK_SIZE != 0))
  273. &os_thread_stack, (uint32_t)OS_THREAD_USER_STACK_SIZE,
  274. #else
  275. NULL, 0U,
  276. #endif
  277. #if ((OS_MEMPOOL_OBJ_MEM != 0) && (OS_MEMPOOL_DATA_SIZE != 0))
  278. &os_mp_data, (uint32_t)OS_MEMPOOL_DATA_SIZE,
  279. #else
  280. NULL, 0U,
  281. #endif
  282. #if ((OS_MSGQUEUE_OBJ_MEM != 0) && (OS_MSGQUEUE_DATA_SIZE != 0))
  283. &os_mq_data, (uint32_t)OS_MSGQUEUE_DATA_SIZE,
  284. #else
  285. NULL, 0U,
  286. #endif
  287. #if (OS_DYNAMIC_MEM_SIZE != 0)
  288. &os_mem, (uint32_t)OS_DYNAMIC_MEM_SIZE,
  289. #else
  290. NULL, 0U
  291. #endif
  292. },
  293. {
  294. // Memory Pools (Fixed Block Size)
  295. #if (OS_THREAD_OBJ_MEM != 0)
  296. #if (OS_THREAD_DEF_STACK_NUM != 0)
  297. &os_mpi_def_stack,
  298. #else
  299. NULL,
  300. #endif
  301. &os_mpi_thread,
  302. #else
  303. NULL,
  304. NULL,
  305. #endif
  306. #if (OS_TIMER_OBJ_MEM != 0)
  307. &os_mpi_timer,
  308. #else
  309. NULL,
  310. #endif
  311. #if (OS_EVFLAGS_OBJ_MEM != 0)
  312. &os_mpi_ef,
  313. #else
  314. NULL,
  315. #endif
  316. #if (OS_MUTEX_OBJ_MEM != 0)
  317. &os_mpi_mutex,
  318. #else
  319. NULL,
  320. #endif
  321. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  322. &os_mpi_semaphore,
  323. #else
  324. NULL,
  325. #endif
  326. #if (OS_MEMPOOL_OBJ_MEM != 0)
  327. &os_mpi_mp,
  328. #else
  329. NULL,
  330. #endif
  331. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  332. &os_mpi_mq,
  333. #else
  334. NULL,
  335. #endif
  336. },
  337. (uint32_t)OS_STACK_SIZE,
  338. &os_idle_thread_attr,
  339. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  340. &os_timer_thread_attr,
  341. &os_timer_mq_attr,
  342. (uint32_t)OS_TIMER_CB_QUEUE
  343. #else
  344. NULL,
  345. NULL,
  346. 0U
  347. #endif
  348. };
  349. // Non weak reference to library irq module
  350. extern uint8_t irqRtxLib;
  351. extern const uint8_t *irqRtxLibRef;
  352. const uint8_t *irqRtxLibRef = &irqRtxLib;
  353. // Default User SVC Table
  354. extern void * const osRtxUserSVC[];
  355. __WEAK void * const osRtxUserSVC[1] = { (void *)0 };
  356. // OS Sections
  357. // ===========
  358. #if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  359. __asm (
  360. ".weakref __os_thread_cb_start__, .bss.os.thread.cb$$Base\n\t"
  361. ".weakref __os_thread_cb_end__, .bss.os.thread.cb$$Limit\n\t"
  362. ".weakref __os_timer_cb_start__, .bss.os.timer.cb$$Base\n\t"
  363. ".weakref __os_timer_cb_end__, .bss.os.timer.cb$$Limit\n\t"
  364. ".weakref __os_evflags_cb_start__, .bss.os.evflags.cb$$Base\n\t"
  365. ".weakref __os_evflags_cb_end__, .bss.os.evflags.cb$$Limit\n\t"
  366. ".weakref __os_mutex_cb_start__, .bss.os.mutex.cb$$Base\n\t"
  367. ".weakref __os_mutex_cb_end__, .bss.os.mutex.cb$$Limit\n\t"
  368. ".weakref __os_semaphore_cb_start__, .bss.os.semaphore.cb$$Base\n\t"
  369. ".weakref __os_semaphore_cb_end__, .bss.os.semaphore.cb$$Limit\n\t"
  370. ".weakref __os_mempool_cb_start__, .bss.os.mempool.cb$$Base\n\t"
  371. ".weakref __os_mempool_cb_end__, .bss.os.mempool.cb$$Limit\n\t"
  372. ".weakref __os_msgqueue_cb_start__, .bss.os.msgqueue.cb$$Base\n\t"
  373. ".weakref __os_msgqueue_cb_end__, .bss.os.msgqueue.cb$$Limit\n\t"
  374. );
  375. #endif
  376. #if (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || \
  377. (defined(__GNUC__) && !defined(__CC_ARM))
  378. extern __attribute__((weak)) uint32_t __os_thread_cb_start__;
  379. extern __attribute__((weak)) uint32_t __os_thread_cb_end__;
  380. extern __attribute__((weak)) uint32_t __os_timer_cb_start__;
  381. extern __attribute__((weak)) uint32_t __os_timer_cb_end__;
  382. extern __attribute__((weak)) uint32_t __os_evflags_cb_start__;
  383. extern __attribute__((weak)) uint32_t __os_evflags_cb_end__;
  384. extern __attribute__((weak)) uint32_t __os_mutex_cb_start__;
  385. extern __attribute__((weak)) uint32_t __os_mutex_cb_end__;
  386. extern __attribute__((weak)) uint32_t __os_semaphore_cb_start__;
  387. extern __attribute__((weak)) uint32_t __os_semaphore_cb_end__;
  388. extern __attribute__((weak)) uint32_t __os_mempool_cb_start__;
  389. extern __attribute__((weak)) uint32_t __os_mempool_cb_end__;
  390. extern __attribute__((weak)) uint32_t __os_msgqueue_cb_start__;
  391. extern __attribute__((weak)) uint32_t __os_msgqueue_cb_end__;
  392. __asm (".global os_cb_sections");
  393. extern const uint32_t os_cb_sections[];
  394. __attribute__((section(".rodata")))
  395. const uint32_t os_cb_sections[] = {
  396. (uint32_t)&__os_thread_cb_start__,
  397. (uint32_t)&__os_thread_cb_end__,
  398. (uint32_t)&__os_timer_cb_start__,
  399. (uint32_t)&__os_timer_cb_end__,
  400. (uint32_t)&__os_evflags_cb_start__,
  401. (uint32_t)&__os_evflags_cb_end__,
  402. (uint32_t)&__os_mutex_cb_start__,
  403. (uint32_t)&__os_mutex_cb_end__,
  404. (uint32_t)&__os_semaphore_cb_start__,
  405. (uint32_t)&__os_semaphore_cb_end__,
  406. (uint32_t)&__os_mempool_cb_start__,
  407. (uint32_t)&__os_mempool_cb_end__,
  408. (uint32_t)&__os_msgqueue_cb_start__,
  409. (uint32_t)&__os_msgqueue_cb_end__
  410. };
  411. #endif
  412. // OS Initialization
  413. // =================
  414. #if defined(__CC_ARM) || \
  415. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  416. #ifndef __MICROLIB
  417. extern void _platform_post_stackheap_init (void);
  418. __WEAK void _platform_post_stackheap_init (void) {
  419. osKernelInitialize();
  420. }
  421. #endif
  422. #elif defined(__GNUC__)
  423. extern void software_init_hook (void);
  424. __WEAK void software_init_hook (void) {
  425. osKernelInitialize();
  426. }
  427. #endif
  428. // C/C++ Standard Library Multithreading Interface
  429. // ===============================================
  430. #if (( defined(__CC_ARM) || \
  431. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))) && \
  432. !defined(__MICROLIB))
  433. #define LIBSPACE_SIZE 96
  434. // Memory for libspace
  435. static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/sizeof(uint32_t)] \
  436. __attribute__((section(".bss.os")));
  437. // Thread IDs for libspace
  438. static osThreadId_t os_libspace_id[OS_THREAD_LIBSPACE_NUM] \
  439. __attribute__((section(".bss.os")));
  440. // Check if Kernel has been started
  441. static uint32_t os_kernel_is_active (void) {
  442. static uint8_t os_kernel_active = 0U;
  443. if (os_kernel_active == 0U) {
  444. if (osKernelGetState() > osKernelReady) {
  445. os_kernel_active = 1U;
  446. return 1U;
  447. }
  448. return 0U;
  449. } else {
  450. return 1U;
  451. }
  452. }
  453. // Provide libspace for current thread
  454. void *__user_perthread_libspace (void);
  455. void *__user_perthread_libspace (void) {
  456. osThreadId_t id;
  457. uint32_t n;
  458. if (!os_kernel_is_active()) {
  459. return (void *)&os_libspace[OS_THREAD_LIBSPACE_NUM][0];
  460. }
  461. id = osThreadGetId();
  462. for (n = 0U; n < OS_THREAD_LIBSPACE_NUM; n++) {
  463. if (os_libspace_id[n] == NULL) {
  464. os_libspace_id[n] = id;
  465. return (void *)&os_libspace[n][0];
  466. }
  467. if (os_libspace_id[n] == id) {
  468. return (void *)&os_libspace[n][0];
  469. }
  470. }
  471. if (n == OS_THREAD_LIBSPACE_NUM) {
  472. osRtxErrorNotify(osRtxErrorClibSpace, id);
  473. }
  474. return (void *)&os_libspace[n][0];
  475. }
  476. // Mutex identifier
  477. typedef void *mutex;
  478. // Initialize mutex
  479. __USED
  480. int _mutex_initialize(mutex *m);
  481. int _mutex_initialize(mutex *m) {
  482. *m = osMutexNew(NULL);
  483. if (*m == NULL) {
  484. osRtxErrorNotify(osRtxErrorClibMutex, m);
  485. return 0;
  486. }
  487. return 1;
  488. }
  489. // Acquire mutex
  490. __USED
  491. void _mutex_acquire(mutex *m);
  492. void _mutex_acquire(mutex *m) {
  493. if (os_kernel_is_active()) {
  494. osMutexAcquire(*m, osWaitForever);
  495. }
  496. }
  497. // Release mutex
  498. __USED
  499. void _mutex_release(mutex *m);
  500. void _mutex_release(mutex *m) {
  501. if (os_kernel_is_active()) {
  502. osMutexRelease(*m);
  503. }
  504. }
  505. // Free mutex
  506. __USED
  507. void _mutex_free(mutex *m);
  508. void _mutex_free(mutex *m) {
  509. osMutexDelete(*m);
  510. }
  511. #endif