rtx_lib.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Copyright (c) 2013-2023 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. //lint -e{835} -e{845} "Zero argument to operator"
  104. #if defined(OS_IDLE_THREAD_NAME)
  105. OS_IDLE_THREAD_NAME,
  106. #else
  107. NULL,
  108. #endif
  109. #ifdef RTX_SAFETY_CLASS
  110. osSafetyClass((uint32_t)OS_IDLE_THREAD_CLASS) |
  111. #endif
  112. #ifdef RTX_EXECUTION_ZONE
  113. osThreadZone((uint32_t)OS_IDLE_THREAD_ZONE) |
  114. #endif
  115. osThreadDetached,
  116. &os_idle_thread_cb,
  117. (uint32_t)sizeof(os_idle_thread_cb),
  118. &os_idle_thread_stack[0],
  119. (uint32_t)sizeof(os_idle_thread_stack),
  120. osPriorityIdle,
  121. #if defined(OS_IDLE_THREAD_TZ_MOD_ID)
  122. (uint32_t)OS_IDLE_THREAD_TZ_MOD_ID,
  123. #else
  124. 0U,
  125. #endif
  126. 0U
  127. };
  128. // Timer Configuration
  129. // ===================
  130. #if (OS_TIMER_OBJ_MEM != 0)
  131. #if (OS_TIMER_NUM == 0)
  132. #error "Invalid number of Timer objects!"
  133. #endif
  134. // Timer Control Blocks
  135. static osRtxTimer_t os_timer_cb[OS_TIMER_NUM] \
  136. __attribute__((section(".bss.os.timer.cb")));
  137. // Memory Pool for Timer Control Blocks
  138. static osRtxMpInfo_t os_mpi_timer \
  139. __attribute__((section(".data.os.timer.mpi"))) =
  140. { (uint32_t)OS_TIMER_NUM, 0U, (uint32_t)osRtxTimerCbSize, &os_timer_cb[0], NULL, NULL };
  141. #endif // (OS_TIMER_OBJ_MEM != 0)
  142. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  143. #if (((OS_TIMER_THREAD_STACK_SIZE % 8) != 0) || (OS_TIMER_THREAD_STACK_SIZE < 96))
  144. #error "Invalid Timer Thread Stack size!"
  145. #endif
  146. // Timer Thread Control Block
  147. static osRtxThread_t os_timer_thread_cb \
  148. __attribute__((section(".bss.os.thread.cb")));
  149. // Timer Thread Stack
  150. static uint64_t os_timer_thread_stack[OS_TIMER_THREAD_STACK_SIZE/8] \
  151. __attribute__((section(".bss.os.thread.timer.stack")));
  152. // Timer Thread Attributes
  153. static const osThreadAttr_t os_timer_thread_attr = {
  154. //lint -e{835} -e{845} "Zero argument to operator"
  155. #if defined(OS_TIMER_THREAD_NAME)
  156. OS_TIMER_THREAD_NAME,
  157. #else
  158. NULL,
  159. #endif
  160. #ifdef RTX_SAFETY_CLASS
  161. osSafetyClass((uint32_t)OS_TIMER_THREAD_CLASS) |
  162. #endif
  163. #ifdef RTX_EXECUTION_ZONE
  164. osThreadZone((uint32_t)OS_TIMER_THREAD_ZONE) |
  165. #endif
  166. osThreadDetached,
  167. &os_timer_thread_cb,
  168. (uint32_t)sizeof(os_timer_thread_cb),
  169. &os_timer_thread_stack[0],
  170. (uint32_t)sizeof(os_timer_thread_stack),
  171. //lint -e{9030} -e{9034} "cast from signed to enum"
  172. (osPriority_t)OS_TIMER_THREAD_PRIO,
  173. #if defined(OS_TIMER_THREAD_TZ_MOD_ID)
  174. (uint32_t)OS_TIMER_THREAD_TZ_MOD_ID,
  175. #else
  176. 0U,
  177. #endif
  178. 0U
  179. };
  180. // Timer Message Queue Control Block
  181. static osRtxMessageQueue_t os_timer_mq_cb \
  182. __attribute__((section(".bss.os.msgqueue.cb")));
  183. // Timer Message Queue Data
  184. static uint32_t os_timer_mq_data[osRtxMessageQueueMemSize(OS_TIMER_CB_QUEUE,8)/4] \
  185. __attribute__((section(".bss.os.msgqueue.mem")));
  186. // Timer Message Queue Attributes
  187. static const osMessageQueueAttr_t os_timer_mq_attr = {
  188. //lint -e{835} -e{845} "Zero argument to operator"
  189. NULL,
  190. #ifdef RTX_SAFETY_CLASS
  191. osSafetyClass((uint32_t)OS_TIMER_THREAD_CLASS) |
  192. #endif
  193. 0U,
  194. &os_timer_mq_cb,
  195. (uint32_t)sizeof(os_timer_mq_cb),
  196. &os_timer_mq_data[0],
  197. (uint32_t)sizeof(os_timer_mq_data)
  198. };
  199. extern int32_t osRtxTimerSetup (void);
  200. extern void osRtxTimerThread (void *argument);
  201. #endif // ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  202. // Event Flags Configuration
  203. // =========================
  204. #if (OS_EVFLAGS_OBJ_MEM != 0)
  205. #if (OS_EVFLAGS_NUM == 0)
  206. #error "Invalid number of Event Flags objects!"
  207. #endif
  208. // Event Flags Control Blocks
  209. static osRtxEventFlags_t os_ef_cb[OS_EVFLAGS_NUM] \
  210. __attribute__((section(".bss.os.evflags.cb")));
  211. // Memory Pool for Event Flags Control Blocks
  212. static osRtxMpInfo_t os_mpi_ef \
  213. __attribute__((section(".data.os.evflags.mpi"))) =
  214. { (uint32_t)OS_EVFLAGS_NUM, 0U, (uint32_t)osRtxEventFlagsCbSize, &os_ef_cb[0], NULL, NULL };
  215. #endif // (OS_EVFLAGS_OBJ_MEM != 0)
  216. // Mutex Configuration
  217. // ===================
  218. #if (OS_MUTEX_OBJ_MEM != 0)
  219. #if (OS_MUTEX_NUM == 0)
  220. #error "Invalid number of Mutex objects!"
  221. #endif
  222. // Mutex Control Blocks
  223. static osRtxMutex_t os_mutex_cb[OS_MUTEX_NUM] \
  224. __attribute__((section(".bss.os.mutex.cb")));
  225. // Memory Pool for Mutex Control Blocks
  226. static osRtxMpInfo_t os_mpi_mutex \
  227. __attribute__((section(".data.os.mutex.mpi"))) =
  228. { (uint32_t)OS_MUTEX_NUM, 0U, (uint32_t)osRtxMutexCbSize, &os_mutex_cb[0], NULL, NULL };
  229. #endif // (OS_MUTEX_OBJ_MEM != 0)
  230. // Semaphore Configuration
  231. // =======================
  232. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  233. #if (OS_SEMAPHORE_NUM == 0)
  234. #error "Invalid number of Semaphore objects!"
  235. #endif
  236. // Semaphore Control Blocks
  237. static osRtxSemaphore_t os_semaphore_cb[OS_SEMAPHORE_NUM] \
  238. __attribute__((section(".bss.os.semaphore.cb")));
  239. // Memory Pool for Semaphore Control Blocks
  240. static osRtxMpInfo_t os_mpi_semaphore \
  241. __attribute__((section(".data.os.semaphore.mpi"))) =
  242. { (uint32_t)OS_SEMAPHORE_NUM, 0U, (uint32_t)osRtxSemaphoreCbSize, &os_semaphore_cb[0], NULL, NULL };
  243. #endif // (OS_SEMAPHORE_OBJ_MEM != 0)
  244. // Memory Pool Configuration
  245. // =========================
  246. #if (OS_MEMPOOL_OBJ_MEM != 0)
  247. #if (OS_MEMPOOL_NUM == 0)
  248. #error "Invalid number of Memory Pool objects!"
  249. #endif
  250. // Memory Pool Control Blocks
  251. static osRtxMemoryPool_t os_mp_cb[OS_MEMPOOL_NUM] \
  252. __attribute__((section(".bss.os.mempool.cb")));
  253. // Memory Pool for Memory Pool Control Blocks
  254. static osRtxMpInfo_t os_mpi_mp \
  255. __attribute__((section(".data.os.mempool.mpi"))) =
  256. { (uint32_t)OS_MEMPOOL_NUM, 0U, (uint32_t)osRtxMemoryPoolCbSize, &os_mp_cb[0], NULL, NULL };
  257. // Memory Pool for Memory Pool Data Storage
  258. #if (OS_MEMPOOL_DATA_SIZE != 0)
  259. #if ((OS_MEMPOOL_DATA_SIZE % 8) != 0)
  260. #error "Invalid Data Memory size for Memory Pools!"
  261. #endif
  262. static uint64_t os_mp_data[(16 + (8*OS_MEMPOOL_NUM) + OS_MEMPOOL_DATA_SIZE)/8] \
  263. __attribute__((section(".bss.os.mempool.mem")));
  264. #endif
  265. #endif // (OS_MEMPOOL_OBJ_MEM != 0)
  266. // Message Queue Configuration
  267. // ===========================
  268. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  269. #if (OS_MSGQUEUE_NUM == 0)
  270. #error "Invalid number of Message Queue objects!"
  271. #endif
  272. // Message Queue Control Blocks
  273. static osRtxMessageQueue_t os_mq_cb[OS_MSGQUEUE_NUM] \
  274. __attribute__((section(".bss.os.msgqueue.cb")));
  275. // Memory Pool for Message Queue Control Blocks
  276. static osRtxMpInfo_t os_mpi_mq \
  277. __attribute__((section(".data.os.msgqueue.mpi"))) =
  278. { (uint32_t)OS_MSGQUEUE_NUM, 0U, (uint32_t)osRtxMessageQueueCbSize, &os_mq_cb[0], NULL, NULL };
  279. // Memory Pool for Message Queue Data Storage
  280. #if (OS_MSGQUEUE_DATA_SIZE != 0)
  281. #if ((OS_MSGQUEUE_DATA_SIZE % 8) != 0)
  282. #error "Invalid Data Memory size for Message Queues!"
  283. #endif
  284. static uint64_t os_mq_data[(16 + ((8+12)*OS_MSGQUEUE_NUM) + OS_MSGQUEUE_DATA_SIZE + 7)/8] \
  285. __attribute__((section(".bss.os.msgqueue.mem")));
  286. #endif
  287. #endif // (OS_MSGQUEUE_OBJ_MEM != 0)
  288. // Event Recorder Configuration
  289. // ============================
  290. #if (defined(OS_EVR_INIT) && (OS_EVR_INIT != 0))
  291. #ifdef RTE_Compiler_EventRecorder
  292. // Event Recorder Initialize
  293. __STATIC_INLINE void evr_initialize (void) {
  294. (void)EventRecorderInitialize(OS_EVR_LEVEL, (uint32_t)OS_EVR_START);
  295. #if ((OS_EVR_MEMORY_LEVEL & 0x80U) != 0U)
  296. (void)EventRecorderEnable( OS_EVR_MEMORY_LEVEL & 0x0FU, EvtRtxMemoryNo, EvtRtxMemoryNo);
  297. (void)EventRecorderDisable(~OS_EVR_MEMORY_LEVEL & 0x0FU, EvtRtxMemoryNo, EvtRtxMemoryNo);
  298. #endif
  299. #if ((OS_EVR_KERNEL_LEVEL & 0x80U) != 0U)
  300. (void)EventRecorderEnable( OS_EVR_KERNEL_LEVEL & 0x0FU, EvtRtxKernelNo, EvtRtxKernelNo);
  301. (void)EventRecorderDisable(~OS_EVR_KERNEL_LEVEL & 0x0FU, EvtRtxKernelNo, EvtRtxKernelNo);
  302. #endif
  303. #if ((OS_EVR_THREAD_LEVEL & 0x80U) != 0U)
  304. (void)EventRecorderEnable( OS_EVR_THREAD_LEVEL & 0x0FU, EvtRtxThreadNo, EvtRtxThreadNo);
  305. (void)EventRecorderDisable(~OS_EVR_THREAD_LEVEL & 0x0FU, EvtRtxThreadNo, EvtRtxThreadNo);
  306. #endif
  307. #if ((OS_EVR_WAIT_LEVEL & 0x80U) != 0U)
  308. (void)EventRecorderEnable( OS_EVR_WAIT_LEVEL & 0x0FU, EvtRtxWaitNo, EvtRtxWaitNo);
  309. (void)EventRecorderDisable(~OS_EVR_WAIT_LEVEL & 0x0FU, EvtRtxWaitNo, EvtRtxWaitNo);
  310. #endif
  311. #if ((OS_EVR_THFLAGS_LEVEL & 0x80U) != 0U)
  312. (void)EventRecorderEnable( OS_EVR_THFLAGS_LEVEL & 0x0FU, EvtRtxThreadFlagsNo, EvtRtxThreadFlagsNo);
  313. (void)EventRecorderDisable(~OS_EVR_THFLAGS_LEVEL & 0x0FU, EvtRtxThreadFlagsNo, EvtRtxThreadFlagsNo);
  314. #endif
  315. #if ((OS_EVR_EVFLAGS_LEVEL & 0x80U) != 0U)
  316. (void)EventRecorderEnable( OS_EVR_EVFLAGS_LEVEL & 0x0FU, EvtRtxEventFlagsNo, EvtRtxEventFlagsNo);
  317. (void)EventRecorderDisable(~OS_EVR_EVFLAGS_LEVEL & 0x0FU, EvtRtxEventFlagsNo, EvtRtxEventFlagsNo);
  318. #endif
  319. #if ((OS_EVR_TIMER_LEVEL & 0x80U) != 0U)
  320. (void)EventRecorderEnable( OS_EVR_TIMER_LEVEL & 0x0FU, EvtRtxTimerNo, EvtRtxTimerNo);
  321. (void)EventRecorderDisable(~OS_EVR_TIMER_LEVEL & 0x0FU, EvtRtxTimerNo, EvtRtxTimerNo);
  322. #endif
  323. #if ((OS_EVR_MUTEX_LEVEL & 0x80U) != 0U)
  324. (void)EventRecorderEnable( OS_EVR_MUTEX_LEVEL & 0x0FU, EvtRtxMutexNo, EvtRtxMutexNo);
  325. (void)EventRecorderDisable(~OS_EVR_MUTEX_LEVEL & 0x0FU, EvtRtxMutexNo, EvtRtxMutexNo);
  326. #endif
  327. #if ((OS_EVR_SEMAPHORE_LEVEL & 0x80U) != 0U)
  328. (void)EventRecorderEnable( OS_EVR_SEMAPHORE_LEVEL & 0x0FU, EvtRtxSemaphoreNo, EvtRtxSemaphoreNo);
  329. (void)EventRecorderDisable(~OS_EVR_SEMAPHORE_LEVEL & 0x0FU, EvtRtxSemaphoreNo, EvtRtxSemaphoreNo);
  330. #endif
  331. #if ((OS_EVR_MEMPOOL_LEVEL & 0x80U) != 0U)
  332. (void)EventRecorderEnable( OS_EVR_MEMPOOL_LEVEL & 0x0FU, EvtRtxMemoryPoolNo, EvtRtxMemoryPoolNo);
  333. (void)EventRecorderDisable(~OS_EVR_MEMPOOL_LEVEL & 0x0FU, EvtRtxMemoryPoolNo, EvtRtxMemoryPoolNo);
  334. #endif
  335. #if ((OS_EVR_MSGQUEUE_LEVEL & 0x80U) != 0U)
  336. (void)EventRecorderEnable( OS_EVR_MSGQUEUE_LEVEL & 0x0FU, EvtRtxMessageQueueNo, EvtRtxMessageQueueNo);
  337. (void)EventRecorderDisable(~OS_EVR_MSGQUEUE_LEVEL & 0x0FU, EvtRtxMessageQueueNo, EvtRtxMessageQueueNo);
  338. #endif
  339. }
  340. #else
  341. #warning "Event Recorder cannot be initialized (Event Recorder component is not selected)!"
  342. #define evr_initialize()
  343. #endif
  344. #endif // (OS_EVR_INIT != 0)
  345. // OS Configuration
  346. // ================
  347. const osRtxConfig_t osRtxConfig \
  348. __USED \
  349. __attribute__((section(".rodata"))) =
  350. {
  351. //lint -e{835} "Zero argument to operator"
  352. 0U // Flags
  353. #if (OS_PRIVILEGE_MODE != 0)
  354. | osRtxConfigPrivilegedMode
  355. #endif
  356. #if (OS_STACK_CHECK != 0)
  357. | osRtxConfigStackCheck
  358. #endif
  359. #if (OS_STACK_WATERMARK != 0)
  360. | osRtxConfigStackWatermark
  361. #endif
  362. #ifdef RTX_SAFETY_FEATURES
  363. | osRtxConfigSafetyFeatures
  364. #ifdef RTX_SAFETY_CLASS
  365. | osRtxConfigSafetyClass
  366. #endif
  367. #ifdef RTX_EXECUTION_ZONE
  368. | osRtxConfigExecutionZone
  369. #endif
  370. #ifdef RTX_THREAD_WATCHDOG
  371. | osRtxConfigThreadWatchdog
  372. #endif
  373. #ifdef RTX_OBJ_PTR_CHECK
  374. | osRtxConfigObjPtrCheck
  375. #endif
  376. #ifdef RTX_SVC_PTR_CHECK
  377. | osRtxConfigSVCPtrCheck
  378. #endif
  379. #endif
  380. ,
  381. (uint32_t)OS_TICK_FREQ,
  382. #if (OS_ROBIN_ENABLE != 0)
  383. (uint32_t)OS_ROBIN_TIMEOUT,
  384. #else
  385. 0U,
  386. #endif
  387. { &os_isr_queue[0], (uint16_t)(sizeof(os_isr_queue)/sizeof(void *)), 0U },
  388. {
  389. // Memory Pools (Variable Block Size)
  390. #if ((OS_THREAD_OBJ_MEM != 0) && (OS_THREAD_USER_STACK_SIZE != 0))
  391. &os_thread_stack[0], sizeof(os_thread_stack),
  392. #else
  393. NULL, 0U,
  394. #endif
  395. #if ((OS_MEMPOOL_OBJ_MEM != 0) && (OS_MEMPOOL_DATA_SIZE != 0))
  396. &os_mp_data[0], sizeof(os_mp_data),
  397. #else
  398. NULL, 0U,
  399. #endif
  400. #if ((OS_MSGQUEUE_OBJ_MEM != 0) && (OS_MSGQUEUE_DATA_SIZE != 0))
  401. &os_mq_data[0], sizeof(os_mq_data),
  402. #else
  403. NULL, 0U,
  404. #endif
  405. #if (OS_DYNAMIC_MEM_SIZE != 0)
  406. &os_mem[0], (uint32_t)OS_DYNAMIC_MEM_SIZE,
  407. #else
  408. NULL, 0U
  409. #endif
  410. },
  411. {
  412. // Memory Pools (Fixed Block Size)
  413. #if (OS_THREAD_OBJ_MEM != 0)
  414. #if (OS_THREAD_DEF_STACK_NUM != 0)
  415. &os_mpi_def_stack,
  416. #else
  417. NULL,
  418. #endif
  419. &os_mpi_thread,
  420. #else
  421. NULL,
  422. NULL,
  423. #endif
  424. #if (OS_TIMER_OBJ_MEM != 0)
  425. &os_mpi_timer,
  426. #else
  427. NULL,
  428. #endif
  429. #if (OS_EVFLAGS_OBJ_MEM != 0)
  430. &os_mpi_ef,
  431. #else
  432. NULL,
  433. #endif
  434. #if (OS_MUTEX_OBJ_MEM != 0)
  435. &os_mpi_mutex,
  436. #else
  437. NULL,
  438. #endif
  439. #if (OS_SEMAPHORE_OBJ_MEM != 0)
  440. &os_mpi_semaphore,
  441. #else
  442. NULL,
  443. #endif
  444. #if (OS_MEMPOOL_OBJ_MEM != 0)
  445. &os_mpi_mp,
  446. #else
  447. NULL,
  448. #endif
  449. #if (OS_MSGQUEUE_OBJ_MEM != 0)
  450. &os_mpi_mq,
  451. #else
  452. NULL,
  453. #endif
  454. },
  455. (uint32_t)OS_STACK_SIZE,
  456. &os_idle_thread_attr,
  457. #if ((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0))
  458. &os_timer_thread_attr,
  459. osRtxTimerThread,
  460. osRtxTimerSetup,
  461. &os_timer_mq_attr,
  462. (uint32_t)OS_TIMER_CB_QUEUE
  463. #else
  464. NULL,
  465. NULL,
  466. NULL,
  467. NULL,
  468. 0U
  469. #endif
  470. };
  471. // Non weak reference to library irq module
  472. //lint -esym(526,irqRtxLib) "Defined by Exception handlers"
  473. //lint -esym(714,irqRtxLibRef) "Non weak reference"
  474. //lint -esym(765,irqRtxLibRef) "Global scope"
  475. extern const uint8_t irqRtxLib;
  476. extern const uint8_t * const irqRtxLibRef;
  477. const uint8_t * const irqRtxLibRef = &irqRtxLib;
  478. // Default User SVC Table
  479. //lint -esym(714,osRtxUserSVC) "Referenced by Exception handlers"
  480. //lint -esym(765,osRtxUserSVC) "Global scope"
  481. //lint -e{9067} "extern array declared without size"
  482. extern void * const osRtxUserSVC[];
  483. __WEAK void * const osRtxUserSVC[1] = { (void *)0 };
  484. #if (defined(RTX_SAFETY_CLASS) && defined(RTX_OBJ_PTR_CHECK) && \
  485. !((OS_TIMER_THREAD_STACK_SIZE != 0) && (OS_TIMER_CB_QUEUE != 0)))
  486. extern void osRtxTimerDeleteClass(uint32_t safety_class, uint32_t mode);
  487. // Default Timer Delete Class Function.
  488. __WEAK void osRtxTimerDeleteClass(uint32_t safety_class, uint32_t mode) {
  489. (void)safety_class;
  490. (void)mode;
  491. }
  492. #endif
  493. #ifdef RTX_THREAD_WATCHDOG
  494. // Default Watchdog Alarm Handler.
  495. __WEAK uint32_t osWatchdogAlarm_Handler (osThreadId_t thread_id) {
  496. (void)thread_id;
  497. return 0U;
  498. }
  499. #endif
  500. #ifdef RTX_EXECUTION_ZONE
  501. // Default Zone Setup Function.
  502. __WEAK void osZoneSetup_Callback (uint32_t zone) {
  503. (void)zone;
  504. }
  505. #endif
  506. // OS Sections
  507. // ===========
  508. #if defined(__CC_ARM) || \
  509. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  510. // Initialized through linker
  511. //lint -esym(728, __os_thread_cb_start__, __os_thread_cb_end__)
  512. //lint -esym(728, __os_timer_cb_start__, __os_timer_cb_end__)
  513. //lint -esym(728, __os_evflags_cb_start__, __os_evflags_cb_end__)
  514. //lint -esym(728, __os_mutex_cb_start__, __os_mutex_cb_end__)
  515. //lint -esym(728, __os_semaphore_cb_start__, __os_semaphore_cb_end__)
  516. //lint -esym(728, __os_mempool_cb_start__, __os_mempool_cb_end__)
  517. //lint -esym(728, __os_msgqueue_cb_start__, __os_msgqueue_cb_end__)
  518. static const uint32_t __os_thread_cb_start__ __attribute__((weakref(".bss.os.thread.cb$$Base")));
  519. static const uint32_t __os_thread_cb_end__ __attribute__((weakref(".bss.os.thread.cb$$Limit")));
  520. static const uint32_t __os_timer_cb_start__ __attribute__((weakref(".bss.os.timer.cb$$Base")));
  521. static const uint32_t __os_timer_cb_end__ __attribute__((weakref(".bss.os.timer.cb$$Limit")));
  522. static const uint32_t __os_evflags_cb_start__ __attribute__((weakref(".bss.os.evflags.cb$$Base")));
  523. static const uint32_t __os_evflags_cb_end__ __attribute__((weakref(".bss.os.evflags.cb$$Limit")));
  524. static const uint32_t __os_mutex_cb_start__ __attribute__((weakref(".bss.os.mutex.cb$$Base")));
  525. static const uint32_t __os_mutex_cb_end__ __attribute__((weakref(".bss.os.mutex.cb$$Limit")));
  526. static const uint32_t __os_semaphore_cb_start__ __attribute__((weakref(".bss.os.semaphore.cb$$Base")));
  527. static const uint32_t __os_semaphore_cb_end__ __attribute__((weakref(".bss.os.semaphore.cb$$Limit")));
  528. static const uint32_t __os_mempool_cb_start__ __attribute__((weakref(".bss.os.mempool.cb$$Base")));
  529. static const uint32_t __os_mempool_cb_end__ __attribute__((weakref(".bss.os.mempool.cb$$Limit")));
  530. static const uint32_t __os_msgqueue_cb_start__ __attribute__((weakref(".bss.os.msgqueue.cb$$Base")));
  531. static const uint32_t __os_msgqueue_cb_end__ __attribute__((weakref(".bss.os.msgqueue.cb$$Limit")));
  532. #else
  533. extern const uint32_t __os_thread_cb_start__ __attribute__((weak));
  534. extern const uint32_t __os_thread_cb_end__ __attribute__((weak));
  535. extern const uint32_t __os_timer_cb_start__ __attribute__((weak));
  536. extern const uint32_t __os_timer_cb_end__ __attribute__((weak));
  537. extern const uint32_t __os_evflags_cb_start__ __attribute__((weak));
  538. extern const uint32_t __os_evflags_cb_end__ __attribute__((weak));
  539. extern const uint32_t __os_mutex_cb_start__ __attribute__((weak));
  540. extern const uint32_t __os_mutex_cb_end__ __attribute__((weak));
  541. extern const uint32_t __os_semaphore_cb_start__ __attribute__((weak));
  542. extern const uint32_t __os_semaphore_cb_end__ __attribute__((weak));
  543. extern const uint32_t __os_mempool_cb_start__ __attribute__((weak));
  544. extern const uint32_t __os_mempool_cb_end__ __attribute__((weak));
  545. extern const uint32_t __os_msgqueue_cb_start__ __attribute__((weak));
  546. extern const uint32_t __os_msgqueue_cb_end__ __attribute__((weak));
  547. #endif
  548. //lint -e{9067} "extern array declared without size"
  549. extern const uint32_t * const os_cb_sections[];
  550. //lint -esym(714,os_cb_sections) "Referenced by debugger"
  551. //lint -esym(765,os_cb_sections) "Global scope"
  552. const uint32_t * const os_cb_sections[] \
  553. __USED \
  554. __attribute__((section(".rodata"))) =
  555. {
  556. &__os_thread_cb_start__,
  557. &__os_thread_cb_end__,
  558. &__os_timer_cb_start__,
  559. &__os_timer_cb_end__,
  560. &__os_evflags_cb_start__,
  561. &__os_evflags_cb_end__,
  562. &__os_mutex_cb_start__,
  563. &__os_mutex_cb_end__,
  564. &__os_semaphore_cb_start__,
  565. &__os_semaphore_cb_end__,
  566. &__os_mempool_cb_start__,
  567. &__os_mempool_cb_end__,
  568. &__os_msgqueue_cb_start__,
  569. &__os_msgqueue_cb_end__
  570. };
  571. // OS Initialization
  572. // =================
  573. #if defined(__CC_ARM) || \
  574. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
  575. #ifndef __MICROLIB
  576. //lint -esym(714,_platform_post_stackheap_init) "Referenced by C library"
  577. //lint -esym(765,_platform_post_stackheap_init) "Global scope"
  578. extern void _platform_post_stackheap_init (void);
  579. __WEAK void _platform_post_stackheap_init (void) {
  580. (void)osKernelInitialize();
  581. }
  582. #endif
  583. #elif defined(__GNUC__)
  584. extern void software_init_hook (void);
  585. __WEAK void software_init_hook (void) {
  586. (void)osKernelInitialize();
  587. }
  588. #elif defined(__ICCARM__)
  589. extern void $Super$$__iar_data_init3 (void);
  590. void $Sub$$__iar_data_init3 (void) {
  591. $Super$$__iar_data_init3();
  592. (void)osKernelInitialize();
  593. }
  594. #endif
  595. // OS Hooks
  596. // ========
  597. // RTOS Kernel Pre-Initialization Hook
  598. #if (defined(OS_EVR_INIT) && (OS_EVR_INIT != 0))
  599. void osRtxKernelBeforeInit (void);
  600. void osRtxKernelBeforeInit (void) {
  601. if (osKernelGetState() == osKernelInactive) {
  602. evr_initialize();
  603. }
  604. }
  605. #endif
  606. // C/C++ Standard Library Floating-point Initialization
  607. // ====================================================
  608. #if ( !defined(RTX_NO_FP_INIT_CLIB) && \
  609. ( defined(__CC_ARM) || \
  610. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))) && \
  611. !defined(__MICROLIB))
  612. #if (!defined(__ARM_ARCH_7A__) && \
  613. (defined(__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
  614. (defined(__FPU_USED ) && (__FPU_USED == 1U)))
  615. extern void $Super$$_fp_init (void);
  616. void $Sub$$_fp_init (void);
  617. void $Sub$$_fp_init (void) {
  618. $Super$$_fp_init();
  619. FPU->FPDSCR = __get_FPSCR();
  620. }
  621. #endif
  622. #endif
  623. // C/C++ Standard Library Multithreading Interface
  624. // ===============================================
  625. #if ( !defined(RTX_NO_MULTITHREAD_CLIB) && \
  626. ( defined(__CC_ARM) || \
  627. (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))) && \
  628. !defined(__MICROLIB))
  629. #define LIBSPACE_SIZE 96
  630. //lint -esym(714,__user_perthread_libspace,_mutex_*) "Referenced by C library"
  631. //lint -esym(765,__user_perthread_libspace,_mutex_*) "Global scope"
  632. //lint -esym(9003, os_libspace*) "variables 'os_libspace*' defined at module scope"
  633. // Memory for libspace
  634. static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/4] \
  635. __attribute__((section(".bss.os.libspace")));
  636. // Thread IDs for libspace
  637. static osThreadId_t os_libspace_id[OS_THREAD_LIBSPACE_NUM] \
  638. __attribute__((section(".bss.os.libspace")));
  639. // Check if Kernel has been started
  640. static uint32_t os_kernel_is_active (void) {
  641. static uint8_t os_kernel_active = 0U;
  642. if (os_kernel_active == 0U) {
  643. if (osKernelGetState() > osKernelReady) {
  644. os_kernel_active = 1U;
  645. }
  646. }
  647. return (uint32_t)os_kernel_active;
  648. }
  649. // Provide libspace for current thread
  650. void *__user_perthread_libspace (void);
  651. void *__user_perthread_libspace (void) {
  652. osThreadId_t id;
  653. uint32_t n;
  654. if (os_kernel_is_active() != 0U) {
  655. id = osThreadGetId();
  656. for (n = 0U; n < (uint32_t)OS_THREAD_LIBSPACE_NUM; n++) {
  657. if (os_libspace_id[n] == NULL) {
  658. os_libspace_id[n] = id;
  659. }
  660. if (os_libspace_id[n] == id) {
  661. break;
  662. }
  663. }
  664. if (n == (uint32_t)OS_THREAD_LIBSPACE_NUM) {
  665. (void)osRtxKernelErrorNotify(osRtxErrorClibSpace, id);
  666. }
  667. } else {
  668. n = OS_THREAD_LIBSPACE_NUM;
  669. }
  670. //lint -e{9087} "cast between pointers to different object types"
  671. return (void *)&os_libspace[n][0];
  672. }
  673. // Free libspace for specified thread
  674. static void user_perthread_libspace_free (osThreadId_t id) {
  675. uint32_t n;
  676. for (n = 0U; n < (uint32_t)OS_THREAD_LIBSPACE_NUM; n++) {
  677. if (os_libspace_id[n] == id) {
  678. os_libspace_id[n] = NULL;
  679. break;
  680. }
  681. }
  682. }
  683. /// RTOS Thread Before Free Hook
  684. void osRtxThreadBeforeFree (osThreadId_t id);
  685. void osRtxThreadBeforeFree (osThreadId_t id) {
  686. user_perthread_libspace_free(id);
  687. }
  688. // Mutex identifier
  689. typedef void *mutex;
  690. //lint -save "Function prototypes defined in C library"
  691. //lint -e970 "Use of 'int' outside of a typedef"
  692. //lint -e818 "Pointer 'm' could be declared as pointing to const"
  693. // Initialize mutex
  694. __USED
  695. int _mutex_initialize(mutex *m);
  696. int _mutex_initialize(mutex *m) {
  697. int result;
  698. *m = osMutexNew(NULL);
  699. if (*m != NULL) {
  700. result = 1;
  701. } else {
  702. result = 0;
  703. (void)osRtxKernelErrorNotify(osRtxErrorClibMutex, m);
  704. }
  705. return result;
  706. }
  707. // Acquire mutex
  708. __USED
  709. void _mutex_acquire(mutex *m);
  710. void _mutex_acquire(mutex *m) {
  711. if (os_kernel_is_active() != 0U) {
  712. (void)osMutexAcquire(*m, osWaitForever);
  713. }
  714. }
  715. // Release mutex
  716. __USED
  717. void _mutex_release(mutex *m);
  718. void _mutex_release(mutex *m) {
  719. if (os_kernel_is_active() != 0U) {
  720. (void)osMutexRelease(*m);
  721. }
  722. }
  723. // Free mutex
  724. __USED
  725. void _mutex_free(mutex *m);
  726. void _mutex_free(mutex *m) {
  727. (void)osMutexDelete(*m);
  728. }
  729. //lint -restore
  730. #endif