rtx_kernel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (c) 2013-2016 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. * http://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: Kernel functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // OS Runtime Information
  27. os_info_t os_Info __attribute__((section(".data.os"))) =
  28. { .os_id = os_KernelId, .version = os_CMSIS_RTX, .kernel.state = os_KernelInactive };
  29. // Library reference to irq_cm module
  30. extern uint8_t os_irq_cm;
  31. extern const uint8_t *os_irq_cm_lib_ref;
  32. const uint8_t* os_irq_cm_lib_ref = &os_irq_cm;
  33. // ==== Helper functions ====
  34. /// Block Kernel (disable: thread switching, time tick, post ISR processing).
  35. static void os_KernelBlock (void) {
  36. if (os_Info.tick_irqn >= 0) {
  37. os_ExtTick_DisableIRQ(os_Info.tick_irqn);
  38. }
  39. os_SysTimerDisable();
  40. os_Info.kernel.blocked = 1U;
  41. __DSB();
  42. if (os_Info.tick_irqn < 0) {
  43. os_Info.kernel.pendISR = os_GetPendSV_ST();
  44. os_ClrPendSV_ST();
  45. } else {
  46. os_Info.kernel.pendISR = os_GetPendSV();
  47. os_ClrPendSV();
  48. }
  49. }
  50. /// Unblock Kernel
  51. static void os_KernelUnblock (void) {
  52. os_Info.kernel.blocked = 0U;
  53. __DSB();
  54. if (os_Info.kernel.pendSV != 0U) {
  55. os_Info.kernel.pendSV = 0U;
  56. os_SetPendSV();
  57. }
  58. if (os_Info.kernel.pendISR != 0U) {
  59. os_SetPendFlags(os_Info.kernel.pendISR);
  60. }
  61. if (os_Info.tick_irqn >= 0) {
  62. os_ExtTick_EnableIRQ(os_Info.tick_irqn);
  63. }
  64. os_SysTimerEnable();
  65. }
  66. // ==== Service Calls ====
  67. // Service Calls definitions
  68. SVC0_0 (KernelInitialize, osStatus_t)
  69. SVC0_3 (KernelGetInfo, osStatus_t, osVersion_t *, char *, uint32_t)
  70. SVC0_0 (KernelStart, osStatus_t)
  71. SVC0_0 (KernelLock, uint32_t)
  72. SVC0_0N(KernelUnlock, void)
  73. SVC0_0 (KernelSuspend, uint32_t)
  74. SVC0_1N(KernelResume, void, uint32_t)
  75. SVC0_0 (KernelGetState, osKernelState_t)
  76. SVC0_0 (KernelGetTickCount, uint64_t)
  77. SVC0_0 (KernelGetTickFreq, uint32_t)
  78. SVC0_0 (KernelGetSysTimerCount, uint32_t)
  79. SVC0_0 (KernelGetSysTimerFreq, uint32_t)
  80. /// Initialize the RTOS Kernel.
  81. /// \note API identical to osKernelInitialize
  82. osStatus_t os_svcKernelInitialize (void) {
  83. if (os_Info.kernel.state == os_KernelReady) {
  84. return osOK;
  85. }
  86. if (os_Info.kernel.state != osKernelInactive) {
  87. return osError;
  88. }
  89. // Initialize os_Info
  90. memset(&os_Info.kernel, 0, sizeof(os_Info) - offsetof(os_info_t, kernel));
  91. if (os_Config.thread_stack_size < (64U + 8U)) {
  92. return osError;
  93. }
  94. if ((os_Config.isr_queue.data == NULL) || (os_Config.isr_queue.max == 0U)) {
  95. return osError;
  96. }
  97. os_Info.isr_queue.data = os_Config.isr_queue.data;
  98. os_Info.isr_queue.max = os_Config.isr_queue.max;
  99. os_Info.thread.robin.timeout = os_Config.robin_timeout;
  100. // Initialize Memory Pools (Variable Block Size)
  101. if (os_MemoryInit(os_Config.mem.common_addr, os_Config.mem.common_size) != 0U) {
  102. os_Info.mem.common = os_Config.mem.common_addr;
  103. }
  104. if (os_MemoryInit(os_Config.mem.stack_addr, os_Config.mem.stack_size) != 0U) {
  105. os_Info.mem.stack = os_Config.mem.stack_addr;
  106. } else {
  107. os_Info.mem.stack = os_Info.mem.common;
  108. }
  109. if (os_MemoryInit(os_Config.mem.mp_data_addr, os_Config.mem.mp_data_size) != 0U) {
  110. os_Info.mem.mp_data = os_Config.mem.mp_data_addr;
  111. } else {
  112. os_Info.mem.mp_data = os_Info.mem.common;
  113. }
  114. if (os_MemoryInit(os_Config.mem.mq_data_addr, os_Config.mem.mq_data_size) != 0U) {
  115. os_Info.mem.mq_data = os_Config.mem.mq_data_addr;
  116. } else {
  117. os_Info.mem.mq_data = os_Info.mem.common;
  118. }
  119. // Initialize Memory Pools (Fixed Block Size)
  120. if ((os_Config.mpi.stack != NULL) &&
  121. (os_MemoryPoolInit(os_Config.mpi.stack,
  122. os_Config.mpi.stack->max_blocks,
  123. os_Config.mpi.stack->block_size,
  124. os_Config.mpi.stack->block_base) != 0U)) {
  125. os_Info.mpi.stack = os_Config.mpi.stack;
  126. }
  127. if ((os_Config.mpi.thread != NULL) &&
  128. (os_MemoryPoolInit(os_Config.mpi.thread,
  129. os_Config.mpi.thread->max_blocks,
  130. os_Config.mpi.thread->block_size,
  131. os_Config.mpi.thread->block_base) != 0U)) {
  132. os_Info.mpi.thread = os_Config.mpi.thread;
  133. }
  134. if ((os_Config.mpi.timer != NULL) &&
  135. (os_MemoryPoolInit(os_Config.mpi.timer,
  136. os_Config.mpi.timer->max_blocks,
  137. os_Config.mpi.timer->block_size,
  138. os_Config.mpi.timer->block_base) != 0U)) {
  139. os_Info.mpi.timer = os_Config.mpi.timer;
  140. }
  141. if ((os_Config.mpi.event_flags != NULL) &&
  142. (os_MemoryPoolInit(os_Config.mpi.event_flags,
  143. os_Config.mpi.event_flags->max_blocks,
  144. os_Config.mpi.event_flags->block_size,
  145. os_Config.mpi.event_flags->block_base) != 0U)) {
  146. os_Info.mpi.event_flags = os_Config.mpi.event_flags;
  147. }
  148. if ((os_Config.mpi.mutex != NULL) &&
  149. (os_MemoryPoolInit(os_Config.mpi.mutex,
  150. os_Config.mpi.mutex->max_blocks,
  151. os_Config.mpi.mutex->block_size,
  152. os_Config.mpi.mutex->block_base) != 0U)) {
  153. os_Info.mpi.mutex = os_Config.mpi.mutex;
  154. }
  155. if ((os_Config.mpi.semaphore != NULL) &&
  156. (os_MemoryPoolInit(os_Config.mpi.semaphore,
  157. os_Config.mpi.semaphore->max_blocks,
  158. os_Config.mpi.semaphore->block_size,
  159. os_Config.mpi.semaphore->block_base) != 0U)) {
  160. os_Info.mpi.semaphore = os_Config.mpi.semaphore;
  161. }
  162. if ((os_Config.mpi.memory_pool != NULL) &&
  163. (os_MemoryPoolInit(os_Config.mpi.memory_pool,
  164. os_Config.mpi.memory_pool->max_blocks,
  165. os_Config.mpi.memory_pool->block_size,
  166. os_Config.mpi.memory_pool->block_base) != 0U)) {
  167. os_Info.mpi.memory_pool = os_Config.mpi.memory_pool;
  168. }
  169. if ((os_Config.mpi.message_queue != NULL) &&
  170. (os_MemoryPoolInit(os_Config.mpi.message_queue,
  171. os_Config.mpi.message_queue->max_blocks,
  172. os_Config.mpi.message_queue->block_size,
  173. os_Config.mpi.message_queue->block_base) != 0U)) {
  174. os_Info.mpi.message_queue = os_Config.mpi.message_queue;
  175. }
  176. #if (__DOMAIN_NS == 1U)
  177. // Initialize Secure Process Stack
  178. if (TZ_InitContextSystem_S() == 0U) {
  179. return osError;
  180. }
  181. #endif
  182. // Create Idle Thread
  183. os_Info.thread.idle = (os_thread_t *)(os_svcThreadNew(
  184. os_IdleThread,
  185. NULL,
  186. os_Config.idle_thread_attr));
  187. if (os_Info.thread.idle == NULL) {
  188. return osError;
  189. }
  190. // Initialize SVC and PendSV System Service Calls
  191. os_SVC_Initialize();
  192. os_Info.kernel.state = os_KernelReady;
  193. return osOK;
  194. }
  195. /// Get RTOS Kernel Information.
  196. /// \note API identical to osKernelGetInfo
  197. osStatus_t os_svcKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
  198. if (version != NULL) {
  199. version->api = os_CMSIS_API;
  200. version->kernel = os_CMSIS_RTX;
  201. }
  202. if ((id_buf != NULL) && (id_size != 0U)) {
  203. if (id_size > sizeof(os_KernelId)) {
  204. id_size = sizeof(os_KernelId);
  205. }
  206. memcpy(id_buf, os_KernelId, id_size);
  207. }
  208. return osOK;
  209. }
  210. /// Get the current RTOS Kernel state.
  211. /// \note API identical to osKernelGetState
  212. osKernelState_t os_svcKernelGetState (void) {
  213. return ((osKernelState_t)(os_Info.kernel.state));
  214. }
  215. /// Start the RTOS Kernel scheduler.
  216. /// \note API identical to osKernelStart
  217. osStatus_t os_svcKernelStart (void) {
  218. os_thread_t *thread;
  219. if (os_Info.kernel.state != os_KernelReady) {
  220. return osError;
  221. }
  222. // Switch to Ready Thread with highest Priority
  223. thread = os_ThreadListGet(&os_Info.thread.ready);
  224. if (thread == NULL) {
  225. return osError;
  226. }
  227. os_ThreadSwitch(thread);
  228. if ((os_Config.flags & os_ConfigPrivilegedMode) != 0U) {
  229. // Privileged Thread mode & PSP
  230. __set_CONTROL(0x02U);
  231. } else {
  232. // Unprivileged Thread mode & PSP
  233. __set_CONTROL(0x03U);
  234. }
  235. __DSB();
  236. __ISB();
  237. os_Info.kernel.sys_freq = SystemCoreClock;
  238. // Setup and Enable System Timer
  239. os_Info.tick_irqn = os_SysTimerSetup();
  240. if (os_Info.tick_irqn >= 0) {
  241. os_ExtTick_EnableIRQ(os_Info.tick_irqn);
  242. }
  243. os_SysTimerEnable();
  244. os_Info.kernel.state = os_KernelRunning;
  245. return osOK;
  246. }
  247. /// Lock the RTOS Kernel scheduler.
  248. /// \note API identical to osKernelLock
  249. uint32_t os_svcKernelLock (void) {
  250. if (os_Info.kernel.state == osKernelRunning) {
  251. os_Info.kernel.state = os_KernelLocked;
  252. return 1U;
  253. }
  254. return 0U;
  255. }
  256. /// Unlock the RTOS Kernel scheduler.
  257. /// \note API identical to osKernelUnlock
  258. void os_svcKernelUnlock (void) {
  259. if (os_Info.kernel.state == os_KernelLocked) {
  260. os_Info.kernel.state = osKernelRunning;
  261. }
  262. }
  263. /// Suspend the RTOS Kernel scheduler.
  264. /// \note API identical to osKernelSuspend
  265. uint32_t os_svcKernelSuspend (void) {
  266. os_thread_t *thread;
  267. os_timer_t *timer;
  268. uint32_t delay;
  269. if (os_Info.kernel.state != os_KernelRunning) {
  270. return 0U;
  271. }
  272. os_KernelBlock();
  273. delay = osWaitForever;
  274. // Check Thread Delay list
  275. thread = os_Info.thread.delay_list;
  276. if (thread != NULL) {
  277. delay = thread->delay;
  278. }
  279. // Check Active Timer list
  280. timer = os_Info.timer.list;
  281. if (timer != NULL) {
  282. if (timer->tick < delay) {
  283. delay = timer->tick;
  284. }
  285. }
  286. os_Info.kernel.state = os_KernelSuspended;
  287. return delay;
  288. }
  289. /// Resume the RTOS Kernel scheduler.
  290. /// \note API identical to osKernelResume
  291. void os_svcKernelResume (uint32_t sleep_ticks) {
  292. os_thread_t *thread;
  293. os_timer_t *timer;
  294. uint32_t delay;
  295. if (os_Info.kernel.state != os_KernelSuspended) {
  296. return;
  297. }
  298. // Process Thread Delay list
  299. thread = os_Info.thread.delay_list;
  300. if (thread != NULL) {
  301. delay = sleep_ticks;
  302. if (delay >= thread->delay) {
  303. delay -= thread->delay;
  304. os_Info.kernel.tick += thread->delay;
  305. thread->delay = 1U;
  306. do {
  307. os_ThreadDelayTick();
  308. if (delay == 0U) {
  309. break;
  310. }
  311. delay--;
  312. os_Info.kernel.tick++;
  313. } while (os_Info.thread.delay_list != NULL);
  314. } else {
  315. thread->delay -= delay;
  316. os_Info.kernel.tick += delay;
  317. }
  318. } else {
  319. os_Info.kernel.tick += sleep_ticks;
  320. }
  321. // Process Active Timer list
  322. timer = os_Info.timer.list;
  323. if (timer != NULL) {
  324. if (sleep_ticks >= timer->tick) {
  325. sleep_ticks -= timer->tick;
  326. timer->tick = 1U;
  327. do {
  328. os_TimerTick();
  329. if (sleep_ticks == 0U) {
  330. break;
  331. }
  332. sleep_ticks--;
  333. } while (os_Info.timer.list != NULL);
  334. } else {
  335. timer->tick -= sleep_ticks;
  336. }
  337. }
  338. os_Info.kernel.state = os_KernelRunning;
  339. os_ThreadDispatch(NULL);
  340. os_KernelUnblock();
  341. }
  342. /// Get the RTOS kernel tick count.
  343. /// \note API identical to osKernelGetTickCount
  344. uint64_t os_svcKernelGetTickCount (void) {
  345. return os_Info.kernel.tick;
  346. }
  347. /// Get the RTOS kernel tick frequency.
  348. /// \note API identical to osKernelGetTickFreq
  349. uint32_t os_svcKernelGetTickFreq (void) {
  350. return os_Config.tick_freq;
  351. }
  352. /// Get the RTOS kernel system timer count.
  353. /// \note API identical to osKernelGetSysTimerCount
  354. uint32_t os_svcKernelGetSysTimerCount (void) {
  355. return os_SysTimerGetCount();
  356. }
  357. /// Get the RTOS kernel system timer frequency.
  358. /// \note API identical to osKernelGetSysTimerFreq
  359. uint32_t os_svcKernelGetSysTimerFreq (void) {
  360. return os_SysTimerGetFreq();
  361. }
  362. // ==== Public API ====
  363. /// Initialize the RTOS Kernel.
  364. osStatus_t osKernelInitialize (void) {
  365. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  366. return osErrorISR;
  367. }
  368. if (IS_PRIVILEGED()) {
  369. return os_svcKernelInitialize();
  370. } else {
  371. return __svcKernelInitialize();
  372. }
  373. }
  374. /// Get RTOS Kernel Information.
  375. osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
  376. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  377. return osErrorISR;
  378. }
  379. if (IS_PRIVILEGED()) {
  380. return os_svcKernelGetInfo(version, id_buf, id_size);
  381. } else {
  382. return __svcKernelGetInfo(version, id_buf, id_size);
  383. }
  384. }
  385. /// Get the current RTOS Kernel state.
  386. osKernelState_t osKernelGetState (void) {
  387. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  388. return osKernelError;
  389. }
  390. if (IS_PRIVILEGED()) {
  391. return os_svcKernelGetState();
  392. } else {
  393. return __svcKernelGetState();
  394. }
  395. }
  396. /// Start the RTOS Kernel scheduler.
  397. osStatus_t osKernelStart (void) {
  398. osStatus_t status;
  399. uint32_t stack[8];
  400. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  401. return osErrorISR;
  402. }
  403. switch (__get_CONTROL() & 0x03U) {
  404. case 0x00U: // Privileged Thread mode & MSP
  405. #if ( (__ARM_ARCH_8M_MAIN__ == 1U) || \
  406. ((__ARM_ARCH_8M_BASE__ == 1U) && (__DOMAIN_NS == 0U)))
  407. __set_PSPLIM((uint32_t)stack);
  408. #endif
  409. __set_PSP((uint32_t)(stack + 8)); // Initial PSP
  410. __set_CONTROL(0x02U); // Set Privileged Thread mode & PSP
  411. __DSB();
  412. __ISB();
  413. status = __svcKernelStart();
  414. if (status != osOK) {
  415. __set_CONTROL(0x00U); // Restore Privileged Thread mode & MSP
  416. }
  417. return status;
  418. case 0x01U: // Unprivileged Thread mode & MSP
  419. return osError;
  420. case 0x02U: // Privileged Thread mode & PSP
  421. case 0x03U: // Unprivileged Thread mode & PSP
  422. break;
  423. }
  424. return __svcKernelStart();
  425. }
  426. /// Lock the RTOS Kernel scheduler.
  427. uint32_t osKernelLock (void) {
  428. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  429. return 0U;
  430. }
  431. return __svcKernelLock();
  432. }
  433. /// Unlock the RTOS Kernel scheduler.
  434. void osKernelUnlock (void) {
  435. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  436. return;
  437. }
  438. __svcKernelUnlock();
  439. }
  440. /// Suspend the RTOS Kernel scheduler.
  441. uint32_t osKernelSuspend (void) {
  442. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  443. return 0U;
  444. }
  445. return __svcKernelSuspend();
  446. }
  447. /// Resume the RTOS Kernel scheduler.
  448. void osKernelResume (uint32_t sleep_ticks) {
  449. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  450. return;
  451. }
  452. __svcKernelResume(sleep_ticks);
  453. }
  454. /// Get the RTOS kernel tick count.
  455. uint64_t osKernelGetTickCount (void) {
  456. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  457. return os_svcKernelGetTickCount();
  458. } else {
  459. return __svcKernelGetTickCount();
  460. }
  461. }
  462. /// Get the RTOS kernel tick frequency.
  463. uint32_t osKernelGetTickFreq (void) {
  464. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  465. return os_svcKernelGetTickFreq();
  466. } else {
  467. return __svcKernelGetTickFreq();
  468. }
  469. }
  470. /// Get the RTOS kernel system timer count.
  471. uint32_t osKernelGetSysTimerCount (void) {
  472. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  473. return os_svcKernelGetSysTimerCount();
  474. } else {
  475. return __svcKernelGetSysTimerCount();
  476. }
  477. }
  478. /// Get the RTOS kernel system timer frequency.
  479. uint32_t osKernelGetSysTimerFreq (void) {
  480. if (IS_IRQ_MODE() || IS_IRQ_MASKED()) {
  481. return os_svcKernelGetSysTimerFreq();
  482. } else {
  483. return __svcKernelGetSysTimerFreq();
  484. }
  485. }