rtx_system.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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: System functions
  22. *
  23. * -----------------------------------------------------------------------------
  24. */
  25. #include "rtx_lib.h"
  26. // ==== Helper functions ====
  27. /// Put Object into ISR Queue.
  28. /// \param[in] object object.
  29. /// \return 1 - success, 0 - failure.
  30. static uint32_t os_isr_queue_put (void *object) {
  31. #if (__EXCLUSIVE_ACCESS == 0U)
  32. uint32_t primask = __get_PRIMASK();
  33. #else
  34. uint32_t n;
  35. #endif
  36. uint16_t max;
  37. uint32_t ret;
  38. max = os_Info.isr_queue.max;
  39. #if (__EXCLUSIVE_ACCESS == 0U)
  40. __disable_irq();
  41. if (os_Info.isr_queue.cnt < max) {
  42. os_Info.isr_queue.cnt++;
  43. os_Info.isr_queue.data[os_Info.isr_queue.in] = object;
  44. if (++os_Info.isr_queue.in == max) {
  45. os_Info.isr_queue.in = 0U;
  46. }
  47. ret = 1U;
  48. } else {
  49. ret = 0U;
  50. }
  51. if (primask == 0U) {
  52. __enable_irq();
  53. }
  54. #else
  55. if (os_exc_inc16_lt(&os_Info.isr_queue.cnt, max) < max) {
  56. n = os_exc_inc16_lim(&os_Info.isr_queue.in, max);
  57. os_Info.isr_queue.data[n] = object;
  58. ret = 1U;
  59. } else {
  60. ret = 0U;
  61. }
  62. #endif
  63. return ret;
  64. }
  65. /// Get Object from ISR Queue.
  66. /// \return object or NULL.
  67. static void *os_isr_queue_get (void) {
  68. #if (__EXCLUSIVE_ACCESS == 0U)
  69. uint32_t primask = __get_PRIMASK();
  70. #else
  71. uint32_t n;
  72. #endif
  73. uint16_t max;
  74. void *ret;
  75. max = os_Info.isr_queue.max;
  76. #if (__EXCLUSIVE_ACCESS == 0U)
  77. __disable_irq();
  78. if (os_Info.isr_queue.cnt != 0U) {
  79. os_Info.isr_queue.cnt--;
  80. ret = os_Info.isr_queue.data[os_Info.isr_queue.out];
  81. if (++os_Info.isr_queue.out == max) {
  82. os_Info.isr_queue.out = 0U;
  83. }
  84. } else {
  85. ret = NULL;
  86. }
  87. if (primask == 0U) {
  88. __enable_irq();
  89. }
  90. #else
  91. if (os_exc_dec16_nz(&os_Info.isr_queue.cnt) != 0U) {
  92. n = os_exc_inc16_lim(&os_Info.isr_queue.out, max);
  93. ret = os_Info.isr_queue.data[n];
  94. } else {
  95. ret = NULL;
  96. }
  97. #endif
  98. return ret;
  99. }
  100. // ==== Library Functions ====
  101. /// Tick Handler.
  102. void os_Tick_Handler (void) {
  103. os_thread_t *thread;
  104. os_SysTimerAckIRQ();
  105. os_Info.kernel.tick++;
  106. // Process Thread Delays
  107. os_ThreadDelayTick();
  108. // Process Timers
  109. os_TimerTick();
  110. os_ThreadDispatch(NULL);
  111. // Check Round Robin timeout
  112. if (os_Info.thread.robin.timeout != 0U) {
  113. if (os_Info.thread.robin.thread != os_Info.thread.run.next) {
  114. // Reset Round Robin
  115. os_Info.thread.robin.thread = os_Info.thread.run.next;
  116. os_Info.thread.robin.tick = os_Info.thread.robin.timeout;
  117. } else {
  118. if (os_Info.thread.robin.tick != 0U) {
  119. os_Info.thread.robin.tick--;
  120. }
  121. if (os_Info.thread.robin.tick == 0U) {
  122. // Round Robin Timeout
  123. if (os_KernelGetState() == os_KernelRunning) {
  124. thread = os_Info.thread.ready.thread_list;
  125. if ((thread != NULL) && (thread->priority == os_Info.thread.robin.thread->priority)) {
  126. os_ThreadListRemove(thread);
  127. os_ThreadReadyPut(os_Info.thread.robin.thread);
  128. os_ThreadSwitch(thread);
  129. os_Info.thread.robin.thread = thread;
  130. os_Info.thread.robin.tick = os_Info.thread.robin.timeout;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /// Pending Service Call Handler.
  138. void os_PendSV_Handler (void) {
  139. os_object_t *object;
  140. for (;;) {
  141. object = os_isr_queue_get();
  142. if (object == NULL) {
  143. break;
  144. }
  145. switch (object->id) {
  146. case os_IdThread:
  147. os_Info.post_process.thread((os_thread_t *)object);
  148. break;
  149. case os_IdEventFlags:
  150. os_Info.post_process.event_flags((os_event_flags_t *)object);
  151. break;
  152. case os_IdSemaphore:
  153. os_Info.post_process.semaphore((os_semaphore_t *)object);
  154. break;
  155. case os_IdMemoryPool:
  156. os_Info.post_process.memory_pool((os_memory_pool_t *)object);
  157. break;
  158. case os_IdMessage:
  159. os_Info.post_process.message_queue((os_message_t *)object);
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. os_ThreadDispatch(NULL);
  166. }
  167. /// Register post ISR processing.
  168. /// \param[in] object generic object.
  169. void os_PostProcess (os_object_t *object) {
  170. if (os_isr_queue_put(object) != 0U) {
  171. if (os_Info.kernel.blocked == 0U) {
  172. os_SetPendSV();
  173. } else {
  174. os_Info.kernel.pendSV = 1U;
  175. }
  176. } else {
  177. os_Error(os_ErrorISRQueueOverflow, object);
  178. }
  179. }
  180. // ==== Public API ====
  181. /// Setup System Timer.
  182. __WEAK int32_t os_SysTimerSetup (void) {
  183. // Setup SysTick Timer
  184. os_SysTick_Setup(os_Info.kernel.sys_freq / os_Config.tick_freq);
  185. return SysTick_IRQn; // Return IRQ number of SysTick
  186. }
  187. /// Enable System Timer.
  188. __WEAK void os_SysTimerEnable (void) {
  189. os_SysTick_Enable();
  190. }
  191. /// Disable System Timer.
  192. __WEAK void os_SysTimerDisable (void) {
  193. os_SysTick_Disable();
  194. }
  195. /// Acknowledge System Timer IRQ.
  196. __WEAK void os_SysTimerAckIRQ (void) {
  197. os_SysTick_GetOvf();
  198. }
  199. /// Get System Timer count.
  200. __WEAK uint32_t os_SysTimerGetCount (void) {
  201. uint32_t tick;
  202. uint32_t val;
  203. tick = (uint32_t)os_Info.kernel.tick;
  204. val = os_SysTick_GetVal();
  205. if (os_SysTick_GetOvf()) {
  206. val = os_SysTick_GetVal();
  207. tick++;
  208. }
  209. val += tick * os_SysTick_GetPeriod();
  210. return val;
  211. }
  212. /// Get System Timer frequency.
  213. __WEAK uint32_t os_SysTimerGetFreq (void) {
  214. return os_Info.kernel.sys_freq;
  215. }