rtx_system.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2013-2018 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: 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 isr_queue_put (os_object_t *object) {
  31. #if (EXCLUSIVE_ACCESS == 0)
  32. uint32_t primask = __get_PRIMASK();
  33. #else
  34. uint32_t n;
  35. #endif
  36. uint16_t max;
  37. uint32_t ret;
  38. max = osRtxInfo.isr_queue.max;
  39. #if (EXCLUSIVE_ACCESS == 0)
  40. __disable_irq();
  41. if (osRtxInfo.isr_queue.cnt < max) {
  42. osRtxInfo.isr_queue.cnt++;
  43. osRtxInfo.isr_queue.data[osRtxInfo.isr_queue.in] = object;
  44. if (++osRtxInfo.isr_queue.in == max) {
  45. osRtxInfo.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 (atomic_inc16_lt(&osRtxInfo.isr_queue.cnt, max) < max) {
  56. n = atomic_inc16_lim(&osRtxInfo.isr_queue.in, max);
  57. osRtxInfo.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 os_object_t *isr_queue_get (void) {
  68. #if (EXCLUSIVE_ACCESS == 0)
  69. uint32_t primask = __get_PRIMASK();
  70. #else
  71. uint32_t n;
  72. #endif
  73. uint16_t max;
  74. os_object_t *ret;
  75. max = osRtxInfo.isr_queue.max;
  76. #if (EXCLUSIVE_ACCESS == 0)
  77. __disable_irq();
  78. if (osRtxInfo.isr_queue.cnt != 0U) {
  79. osRtxInfo.isr_queue.cnt--;
  80. ret = osRtxObject(osRtxInfo.isr_queue.data[osRtxInfo.isr_queue.out]);
  81. if (++osRtxInfo.isr_queue.out == max) {
  82. osRtxInfo.isr_queue.out = 0U;
  83. }
  84. } else {
  85. ret = NULL;
  86. }
  87. if (primask == 0U) {
  88. __enable_irq();
  89. }
  90. #else
  91. if (atomic_dec16_nz(&osRtxInfo.isr_queue.cnt) != 0U) {
  92. n = atomic_inc16_lim(&osRtxInfo.isr_queue.out, max);
  93. ret = osRtxObject(osRtxInfo.isr_queue.data[n]);
  94. } else {
  95. ret = NULL;
  96. }
  97. #endif
  98. return ret;
  99. }
  100. // ==== Library Functions ====
  101. /// Tick Handler.
  102. //lint -esym(714,osRtxTick_Handler) "Referenced by Exception handlers"
  103. //lint -esym(759,osRtxTick_Handler) "Prototype in header"
  104. //lint -esym(765,osRtxTick_Handler) "Global scope"
  105. void osRtxTick_Handler (void) {
  106. os_thread_t *thread;
  107. OS_Tick_AcknowledgeIRQ();
  108. osRtxInfo.kernel.tick++;
  109. // Process Timers
  110. if (osRtxInfo.timer.tick != NULL) {
  111. osRtxInfo.timer.tick();
  112. }
  113. // Process Thread Delays
  114. osRtxThreadDelayTick();
  115. osRtxThreadDispatch(NULL);
  116. // Check Round Robin timeout
  117. if (osRtxInfo.thread.robin.timeout != 0U) {
  118. if (osRtxInfo.thread.robin.thread != osRtxInfo.thread.run.next) {
  119. // Reset Round Robin
  120. osRtxInfo.thread.robin.thread = osRtxInfo.thread.run.next;
  121. osRtxInfo.thread.robin.tick = osRtxInfo.thread.robin.timeout;
  122. } else {
  123. if (osRtxInfo.thread.robin.tick != 0U) {
  124. osRtxInfo.thread.robin.tick--;
  125. }
  126. if (osRtxInfo.thread.robin.tick == 0U) {
  127. // Round Robin Timeout
  128. if (osRtxKernelGetState() == osRtxKernelRunning) {
  129. thread = osRtxInfo.thread.ready.thread_list;
  130. if ((thread != NULL) && (thread->priority == osRtxInfo.thread.robin.thread->priority)) {
  131. osRtxThreadListRemove(thread);
  132. osRtxThreadReadyPut(osRtxInfo.thread.robin.thread);
  133. EvrRtxThreadPreempted(osRtxInfo.thread.robin.thread);
  134. osRtxThreadSwitch(thread);
  135. osRtxInfo.thread.robin.thread = thread;
  136. osRtxInfo.thread.robin.tick = osRtxInfo.thread.robin.timeout;
  137. }
  138. }
  139. }
  140. }
  141. }
  142. }
  143. /// Pending Service Call Handler.
  144. //lint -esym(714,osRtxPendSV_Handler) "Referenced by Exception handlers"
  145. //lint -esym(759,osRtxPendSV_Handler) "Prototype in header"
  146. //lint -esym(765,osRtxPendSV_Handler) "Global scope"
  147. void osRtxPendSV_Handler (void) {
  148. os_object_t *object;
  149. for (;;) {
  150. object = isr_queue_get();
  151. if (object == NULL) {
  152. break;
  153. }
  154. switch (object->id) {
  155. case osRtxIdThread:
  156. osRtxInfo.post_process.thread(osRtxThreadObject(object));
  157. break;
  158. case osRtxIdEventFlags:
  159. osRtxInfo.post_process.event_flags(osRtxEventFlagsObject(object));
  160. break;
  161. case osRtxIdSemaphore:
  162. osRtxInfo.post_process.semaphore(osRtxSemaphoreObject(object));
  163. break;
  164. case osRtxIdMemoryPool:
  165. osRtxInfo.post_process.memory_pool(osRtxMemoryPoolObject(object));
  166. break;
  167. case osRtxIdMessage:
  168. osRtxInfo.post_process.message(osRtxMessageObject(object));
  169. break;
  170. default:
  171. // Should never come here
  172. break;
  173. }
  174. }
  175. osRtxThreadDispatch(NULL);
  176. }
  177. /// Register post ISR processing.
  178. /// \param[in] object generic object.
  179. void osRtxPostProcess (os_object_t *object) {
  180. if (isr_queue_put(object) != 0U) {
  181. if (osRtxInfo.kernel.blocked == 0U) {
  182. SetPendSV();
  183. } else {
  184. osRtxInfo.kernel.pendSV = 1U;
  185. }
  186. } else {
  187. (void)osRtxErrorNotify(osRtxErrorISRQueueOverflow, object);
  188. }
  189. }