rtx_kernel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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(".os.data"))) =
  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_TickDisable();
  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_TickEnable();
  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 (KernelGetTime, uint64_t)
  77. SVC0_0 (KernelGetTick, uint32_t)
  78. SVC0_1 (KernelTickMicroSec, uint32_t, uint32_t)
  79. /// Initialize the RTOS Kernel.
  80. /// \note API identical to osKernelInitialize
  81. osStatus_t os_svcKernelInitialize (void) {
  82. if (os_Info.kernel.state == os_KernelReady) {
  83. return osOK;
  84. }
  85. if (os_Info.kernel.state != osKernelInactive) {
  86. return osError;
  87. }
  88. // Initialize os_Info
  89. memset(&os_Info.kernel, 0, sizeof(os_Info) - offsetof(os_info_t, kernel));
  90. if (os_Config.thread_stack_size < (64U + 8U)) {
  91. return osError;
  92. }
  93. if ((os_Config.isr_queue.data == NULL) || (os_Config.isr_queue.max == 0U)) {
  94. return osError;
  95. }
  96. os_Info.isr_queue.data = os_Config.isr_queue.data;
  97. os_Info.isr_queue.max = os_Config.isr_queue.max;
  98. os_Info.thread.robin.timeout = os_Config.robin_timeout;
  99. // Initialize Memory Pools (Variable Block Size)
  100. if (os_MemoryInit(os_Config.mem.common_addr, os_Config.mem.common_size) != 0U) {
  101. os_Info.mem.common = os_Config.mem.common_addr;
  102. }
  103. if (os_MemoryInit(os_Config.mem.cb_addr, os_Config.mem.cb_size) != 0U) {
  104. os_Info.mem.cb = os_Config.mem.cb_addr;
  105. } else {
  106. os_Info.mem.cb = os_Info.mem.common;
  107. }
  108. if (os_MemoryInit(os_Config.mem.data_addr, os_Config.mem.data_size) != 0U) {
  109. os_Info.mem.data = os_Config.mem.data_addr;
  110. } else {
  111. os_Info.mem.data = os_Info.mem.common;
  112. }
  113. if (os_MemoryInit(os_Config.mem.stack_addr, os_Config.mem.stack_size) != 0U) {
  114. os_Info.mem.stack = os_Config.mem.stack_addr;
  115. } else {
  116. os_Info.mem.stack = os_Info.mem.common;
  117. }
  118. // Initialize Memory Pools (Fixed Block Size)
  119. if ((os_Config.mpi.stack != NULL) &&
  120. (os_MemoryPoolInit(os_Config.mpi.stack,
  121. os_Config.mpi.stack->max_blocks,
  122. os_Config.mpi.stack->block_size,
  123. os_Config.mpi.stack->block_base) != 0U)) {
  124. os_Info.mpi.stack = os_Config.mpi.stack;
  125. }
  126. if ((os_Config.mpi.thread != NULL) &&
  127. (os_MemoryPoolInit(os_Config.mpi.thread,
  128. os_Config.mpi.thread->max_blocks,
  129. os_Config.mpi.thread->block_size,
  130. os_Config.mpi.thread->block_base) != 0U)) {
  131. os_Info.mpi.thread = os_Config.mpi.thread;
  132. }
  133. if ((os_Config.mpi.timer != NULL) &&
  134. (os_MemoryPoolInit(os_Config.mpi.timer,
  135. os_Config.mpi.timer->max_blocks,
  136. os_Config.mpi.timer->block_size,
  137. os_Config.mpi.timer->block_base) != 0U)) {
  138. os_Info.mpi.timer = os_Config.mpi.timer;
  139. }
  140. if ((os_Config.mpi.event_flags != NULL) &&
  141. (os_MemoryPoolInit(os_Config.mpi.event_flags,
  142. os_Config.mpi.event_flags->max_blocks,
  143. os_Config.mpi.event_flags->block_size,
  144. os_Config.mpi.event_flags->block_base) != 0U)) {
  145. os_Info.mpi.event_flags = os_Config.mpi.event_flags;
  146. }
  147. if ((os_Config.mpi.mutex != NULL) &&
  148. (os_MemoryPoolInit(os_Config.mpi.mutex,
  149. os_Config.mpi.mutex->max_blocks,
  150. os_Config.mpi.mutex->block_size,
  151. os_Config.mpi.mutex->block_base) != 0U)) {
  152. os_Info.mpi.mutex = os_Config.mpi.mutex;
  153. }
  154. if ((os_Config.mpi.semaphore != NULL) &&
  155. (os_MemoryPoolInit(os_Config.mpi.semaphore,
  156. os_Config.mpi.semaphore->max_blocks,
  157. os_Config.mpi.semaphore->block_size,
  158. os_Config.mpi.semaphore->block_base) != 0U)) {
  159. os_Info.mpi.semaphore = os_Config.mpi.semaphore;
  160. }
  161. if ((os_Config.mpi.memory_pool != NULL) &&
  162. (os_MemoryPoolInit(os_Config.mpi.memory_pool,
  163. os_Config.mpi.memory_pool->max_blocks,
  164. os_Config.mpi.memory_pool->block_size,
  165. os_Config.mpi.memory_pool->block_base) != 0U)) {
  166. os_Info.mpi.memory_pool = os_Config.mpi.memory_pool;
  167. }
  168. if ((os_Config.mpi.message_queue != NULL) &&
  169. (os_MemoryPoolInit(os_Config.mpi.message_queue,
  170. os_Config.mpi.message_queue->max_blocks,
  171. os_Config.mpi.message_queue->block_size,
  172. os_Config.mpi.message_queue->block_base) != 0U)) {
  173. os_Info.mpi.message_queue = os_Config.mpi.message_queue;
  174. }
  175. // Create Idle Thread
  176. os_Info.thread.idle = (os_thread_t *)(os_svcThreadNew(
  177. os_IdleThread,
  178. NULL,
  179. os_Config.idle_thread_attr));
  180. if (os_Info.thread.idle == NULL) {
  181. return osError;
  182. }
  183. // Initialize SVC and PendSV System Service Calls
  184. os_SVC_Initialize();
  185. os_Info.kernel.state = os_KernelReady;
  186. return osOK;
  187. }
  188. /// Get RTOS Kernel Information.
  189. /// \note API identical to osKernelGetInfo
  190. osStatus_t os_svcKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
  191. if (version != NULL) {
  192. version->api = os_CMSIS_API;
  193. version->kernel = os_CMSIS_RTX;
  194. }
  195. if ((id_buf != NULL) && (id_size != 0U)) {
  196. if (id_size > sizeof(os_KernelId)) {
  197. id_size = sizeof(os_KernelId);
  198. }
  199. memcpy(id_buf, os_KernelId, id_size);
  200. }
  201. return osOK;
  202. }
  203. /// Get the current RTOS Kernel state.
  204. /// \note API identical to osKernelGetState
  205. osKernelState_t os_svcKernelGetState (void) {
  206. return ((osKernelState_t)(os_Info.kernel.state));
  207. }
  208. /// Start the RTOS Kernel scheduler.
  209. /// \note API identical to osKernelStart
  210. osStatus_t os_svcKernelStart (void) {
  211. os_thread_t *thread;
  212. if (os_Info.kernel.state != os_KernelReady) {
  213. return osError;
  214. }
  215. // Switch to Ready Thread with highest Priority
  216. thread = os_ThreadListGet(&os_Info.thread.ready);
  217. if (thread == NULL) {
  218. return osError;
  219. }
  220. os_ThreadSwitch(thread);
  221. __set_PSP(thread->sp + (8U*4U));
  222. if ((os_Config.flags & os_ConfigPrivilegedMode) != 0U) {
  223. // Privileged Thread mode & PSP
  224. __set_CONTROL(0x02U);
  225. } else {
  226. // Unprivileged Thread mode & PSP
  227. __set_CONTROL(0x03U);
  228. }
  229. __DSB();
  230. __ISB();
  231. // Setup and Enable Tick Timer
  232. os_Info.tick_irqn = os_TickSetup();
  233. if (os_Info.tick_irqn >= 0) {
  234. os_ExtTick_EnableIRQ(os_Info.tick_irqn);
  235. }
  236. os_TickEnable();
  237. os_Info.kernel.state = os_KernelRunning;
  238. return osOK;
  239. }
  240. /// Lock the RTOS Kernel scheduler.
  241. /// \note API identical to osKernelLock
  242. uint32_t os_svcKernelLock (void) {
  243. if (os_Info.kernel.state == osKernelRunning) {
  244. os_Info.kernel.state = os_KernelLocked;
  245. return 1U;
  246. }
  247. return 0U;
  248. }
  249. /// Unlock the RTOS Kernel scheduler.
  250. /// \note API identical to osKernelUnlock
  251. void os_svcKernelUnlock (void) {
  252. if (os_Info.kernel.state == os_KernelLocked) {
  253. os_Info.kernel.state = osKernelRunning;
  254. }
  255. }
  256. /// Suspend the RTOS Kernel scheduler.
  257. /// \note API identical to osKernelSuspend
  258. uint32_t os_svcKernelSuspend (void) {
  259. os_thread_t *thread;
  260. os_timer_t *timer;
  261. uint32_t delay;
  262. if (os_Info.kernel.state != os_KernelRunning) {
  263. return 0U;
  264. }
  265. os_KernelBlock();
  266. delay = osWaitForever;
  267. // Check Thread Delay list
  268. thread = os_Info.thread.delay_list;
  269. if (thread != NULL) {
  270. delay = thread->delay;
  271. }
  272. // Check Active Timer list
  273. timer = os_Info.timer.list;
  274. if (timer != NULL) {
  275. if (timer->tick < delay) {
  276. delay = timer->tick;
  277. }
  278. }
  279. os_Info.kernel.state = os_KernelSuspended;
  280. return delay;
  281. }
  282. /// Resume the RTOS Kernel scheduler.
  283. /// \note API identical to osKernelResume
  284. void os_svcKernelResume (uint32_t sleep_time) {
  285. os_thread_t *thread;
  286. os_timer_t *timer;
  287. uint32_t delay;
  288. if (os_Info.kernel.state != os_KernelSuspended) {
  289. return;
  290. }
  291. // Process Thread Delay list
  292. thread = os_Info.thread.delay_list;
  293. if (thread != NULL) {
  294. delay = sleep_time;
  295. if (delay >= thread->delay) {
  296. delay -= thread->delay;
  297. os_Info.kernel.time += thread->delay;
  298. thread->delay = 1U;
  299. do {
  300. os_ThreadDelayTick();
  301. if (delay == 0U) {
  302. break;
  303. }
  304. delay--;
  305. os_Info.kernel.time++;
  306. } while (os_Info.thread.delay_list != NULL);
  307. } else {
  308. thread->delay -= delay;
  309. os_Info.kernel.time += delay;
  310. }
  311. } else {
  312. os_Info.kernel.time += sleep_time;
  313. }
  314. // Process Active Timer list
  315. timer = os_Info.timer.list;
  316. if (timer != NULL) {
  317. if (sleep_time >= timer->tick) {
  318. sleep_time -= timer->tick;
  319. timer->tick = 1U;
  320. do {
  321. os_TimerTick();
  322. if (sleep_time == 0U) {
  323. break;
  324. }
  325. sleep_time--;
  326. } while (os_Info.timer.list != NULL);
  327. } else {
  328. timer->tick -= sleep_time;
  329. }
  330. }
  331. os_Info.kernel.state = os_KernelRunning;
  332. os_ThreadDispatch(NULL);
  333. os_KernelUnblock();
  334. }
  335. /// Get the RTOS kernel time.
  336. /// \note API identical to osKernelGetTime
  337. uint64_t os_svcKernelGetTime (void) {
  338. return os_Info.kernel.time;
  339. }
  340. /// Get the RTOS kernel system timer counter.
  341. /// \note API identical to osKernelGetSysTick
  342. uint32_t os_svcKernelGetTick (void) {
  343. return os_TickGetVal();
  344. }
  345. /// Convert a microseconds value to a RTOS kernel system timer value.
  346. /// \note API identical to osKernelInitialize
  347. uint32_t os_svcKernelTickMicroSec (uint32_t microsec) {
  348. return os_TickMicroSec(microsec);
  349. }
  350. // ==== Public API ====
  351. /// Initialize the RTOS Kernel.
  352. osStatus_t osKernelInitialize (void) {
  353. if (__get_IPSR() != 0U) {
  354. return osErrorISR; // Not allowed in ISR
  355. }
  356. if ((__get_CONTROL() & 1U) == 0U) { // Privileged mode
  357. return os_svcKernelInitialize();
  358. } else { // Unprivileged mode
  359. return __svcKernelInitialize();
  360. }
  361. }
  362. /// Get RTOS Kernel Information.
  363. osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
  364. if (__get_IPSR() != 0U) {
  365. return osErrorISR; // Not allowed in ISR
  366. }
  367. if ((__get_CONTROL() & 1U) == 0U) { // Privileged mode
  368. return os_svcKernelGetInfo(version, id_buf, id_size);
  369. } else { // Unprivileged mode
  370. return __svcKernelGetInfo(version, id_buf, id_size);
  371. }
  372. }
  373. /// Get the current RTOS Kernel state.
  374. osKernelState_t osKernelGetState (void) {
  375. if (__get_IPSR() != 0U) {
  376. return osKernelError; // Not allowed in ISR
  377. }
  378. if ((__get_CONTROL() & 1U) == 0U) { // Privileged mode
  379. return os_svcKernelGetState();
  380. } else {
  381. return __svcKernelGetState();
  382. }
  383. }
  384. /// Start the RTOS Kernel scheduler.
  385. osStatus_t osKernelStart (void) {
  386. osStatus_t status;
  387. uint32_t stack[8];
  388. if (__get_IPSR() != 0U) {
  389. return osErrorISR; // Not allowed in ISR
  390. }
  391. switch (__get_CONTROL() & 0x03U) {
  392. case 0x00U: // Privileged Thread mode & MSP
  393. __set_PSP((uint32_t)(stack + 8)); // Initial PSP
  394. __set_CONTROL(0x02U); // Set Privileged Thread mode & PSP
  395. __DSB();
  396. __ISB();
  397. status = __svcKernelStart();
  398. if (status != osOK) {
  399. __set_CONTROL(0x00U); // Restore Privileged Thread mode & MSP
  400. }
  401. return status;
  402. case 0x01U: // Unprivileged Thread mode & MSP
  403. return osError;
  404. case 0x02U: // Privileged Thread mode & PSP
  405. case 0x03U: // Unprivileged Thread mode & PSP
  406. break;
  407. }
  408. return __svcKernelStart();
  409. }
  410. /// Lock the RTOS Kernel scheduler.
  411. uint32_t osKernelLock (void) {
  412. if (__get_IPSR() != 0U) {
  413. return 0U; // Not allowed in ISR
  414. }
  415. return __svcKernelLock();
  416. }
  417. /// Unlock the RTOS Kernel scheduler.
  418. void osKernelUnlock (void) {
  419. if (__get_IPSR() != 0U) {
  420. return; // Not allowed in ISR
  421. }
  422. __svcKernelUnlock();
  423. }
  424. /// Suspend the RTOS Kernel scheduler.
  425. uint32_t osKernelSuspend (void) {
  426. if (__get_IPSR() != 0U) {
  427. return 0U; // Not allowed in ISR
  428. }
  429. return __svcKernelSuspend();
  430. }
  431. /// Resume the RTOS Kernel scheduler.
  432. void osKernelResume (uint32_t sleep_time) {
  433. if (__get_IPSR() != 0U) {
  434. return; // Not allowed in ISR
  435. }
  436. __svcKernelResume(sleep_time);
  437. }
  438. /// Get the RTOS kernel time.
  439. uint64_t osKernelGetTime (void) {
  440. if (__get_IPSR() != 0U) {
  441. return 0U; // Not allowed in ISR
  442. }
  443. return __svcKernelGetTime();
  444. }
  445. /// Get the RTOS kernel system timer counter.
  446. uint32_t osKernelGetTick (void) {
  447. if (__get_IPSR() != 0U) {
  448. return 0U; // Not allowed in ISR
  449. }
  450. return __svcKernelGetTick();
  451. }
  452. /// Convert a microseconds value to a RTOS kernel system timer value.
  453. uint32_t osKernelTickMicroSec (uint32_t microsec) {
  454. if (__get_IPSR() != 0U) {
  455. return 0U; // Not allowed in ISR
  456. }
  457. return __svcKernelTickMicroSec(microsec);
  458. }