app_scheduler.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Copyright (c) 2012 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_SCHEDULER)
  42. #include "app_scheduler.h"
  43. #include <stdlib.h>
  44. #include <stdint.h>
  45. #include <string.h>
  46. #include "nrf_soc.h"
  47. #include "nrf_assert.h"
  48. #include "app_util_platform.h"
  49. /**@brief Structure for holding a scheduled event header. */
  50. typedef struct
  51. {
  52. app_sched_event_handler_t handler; /**< Pointer to event handler to receive the event. */
  53. uint16_t event_data_size; /**< Size of event data. */
  54. } event_header_t;
  55. STATIC_ASSERT(sizeof(event_header_t) <= APP_SCHED_EVENT_HEADER_SIZE);
  56. static event_header_t * m_queue_event_headers; /**< Array for holding the queue event headers. */
  57. static uint8_t * m_queue_event_data; /**< Array for holding the queue event data. */
  58. static volatile uint8_t m_queue_start_index; /**< Index of queue entry at the start of the queue. */
  59. static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the end of the queue. */
  60. static uint16_t m_queue_event_size; /**< Maximum event size in queue. */
  61. static uint16_t m_queue_size; /**< Number of queue entries. */
  62. #if APP_SCHEDULER_WITH_PROFILER
  63. static uint16_t m_max_queue_utilization; /**< Maximum observed queue utilization. */
  64. #endif
  65. #if APP_SCHEDULER_WITH_PAUSE
  66. static uint32_t m_scheduler_paused_counter = 0; /**< Counter storing the difference between pausing
  67. and resuming the scheduler. */
  68. #endif
  69. /**@brief Function for incrementing a queue index, and handle wrap-around.
  70. *
  71. * @param[in] index Old index.
  72. *
  73. * @return New (incremented) index.
  74. */
  75. static __INLINE uint8_t next_index(uint8_t index)
  76. {
  77. return (index < m_queue_size) ? (index + 1) : 0;
  78. }
  79. static __INLINE uint8_t app_sched_queue_full()
  80. {
  81. uint8_t tmp = m_queue_start_index;
  82. return next_index(m_queue_end_index) == tmp;
  83. }
  84. /**@brief Macro for checking if a queue is full. */
  85. #define APP_SCHED_QUEUE_FULL() app_sched_queue_full()
  86. static __INLINE uint8_t app_sched_queue_empty()
  87. {
  88. uint8_t tmp = m_queue_start_index;
  89. return m_queue_end_index == tmp;
  90. }
  91. /**@brief Macro for checking if a queue is empty. */
  92. #define APP_SCHED_QUEUE_EMPTY() app_sched_queue_empty()
  93. uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer)
  94. {
  95. uint16_t data_start_index = (queue_size + 1) * sizeof(event_header_t);
  96. // Check that buffer is correctly aligned
  97. if (!is_word_aligned(p_event_buffer))
  98. {
  99. return NRF_ERROR_INVALID_PARAM;
  100. }
  101. // Initialize event scheduler
  102. m_queue_event_headers = p_event_buffer;
  103. m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index];
  104. m_queue_end_index = 0;
  105. m_queue_start_index = 0;
  106. m_queue_event_size = event_size;
  107. m_queue_size = queue_size;
  108. #if APP_SCHEDULER_WITH_PROFILER
  109. m_max_queue_utilization = 0;
  110. #endif
  111. return NRF_SUCCESS;
  112. }
  113. uint16_t app_sched_queue_space_get()
  114. {
  115. uint16_t start = m_queue_start_index;
  116. uint16_t end = m_queue_end_index;
  117. uint16_t free_space = m_queue_size - ((end >= start) ?
  118. (end - start) : (m_queue_size + 1 - start + end));
  119. return free_space;
  120. }
  121. #if APP_SCHEDULER_WITH_PROFILER
  122. static void queue_utilization_check(void)
  123. {
  124. uint16_t start = m_queue_start_index;
  125. uint16_t end = m_queue_end_index;
  126. uint16_t queue_utilization = (end >= start) ? (end - start) :
  127. (m_queue_size + 1 - start + end);
  128. if (queue_utilization > m_max_queue_utilization)
  129. {
  130. m_max_queue_utilization = queue_utilization;
  131. }
  132. }
  133. uint16_t app_sched_queue_utilization_get(void)
  134. {
  135. return m_max_queue_utilization;
  136. }
  137. #endif // APP_SCHEDULER_WITH_PROFILER
  138. uint32_t app_sched_event_put(void const * p_event_data,
  139. uint16_t event_data_size,
  140. app_sched_event_handler_t handler)
  141. {
  142. uint32_t err_code;
  143. if (event_data_size <= m_queue_event_size)
  144. {
  145. uint16_t event_index = 0xFFFF;
  146. CRITICAL_REGION_ENTER();
  147. if (!APP_SCHED_QUEUE_FULL())
  148. {
  149. event_index = m_queue_end_index;
  150. m_queue_end_index = next_index(m_queue_end_index);
  151. #if APP_SCHEDULER_WITH_PROFILER
  152. // This function call must be protected with critical region because
  153. // it modifies 'm_max_queue_utilization'.
  154. queue_utilization_check();
  155. #endif
  156. }
  157. CRITICAL_REGION_EXIT();
  158. if (event_index != 0xFFFF)
  159. {
  160. // NOTE: This can be done outside the critical region since the event consumer will
  161. // always be called from the main loop, and will thus never interrupt this code.
  162. m_queue_event_headers[event_index].handler = handler;
  163. if ((p_event_data != NULL) && (event_data_size > 0))
  164. {
  165. memcpy(&m_queue_event_data[event_index * m_queue_event_size],
  166. p_event_data,
  167. event_data_size);
  168. m_queue_event_headers[event_index].event_data_size = event_data_size;
  169. }
  170. else
  171. {
  172. m_queue_event_headers[event_index].event_data_size = 0;
  173. }
  174. err_code = NRF_SUCCESS;
  175. }
  176. else
  177. {
  178. err_code = NRF_ERROR_NO_MEM;
  179. }
  180. }
  181. else
  182. {
  183. err_code = NRF_ERROR_INVALID_LENGTH;
  184. }
  185. return err_code;
  186. }
  187. #if APP_SCHEDULER_WITH_PAUSE
  188. void app_sched_pause(void)
  189. {
  190. CRITICAL_REGION_ENTER();
  191. if (m_scheduler_paused_counter < UINT32_MAX)
  192. {
  193. m_scheduler_paused_counter++;
  194. }
  195. CRITICAL_REGION_EXIT();
  196. }
  197. void app_sched_resume(void)
  198. {
  199. CRITICAL_REGION_ENTER();
  200. if (m_scheduler_paused_counter > 0)
  201. {
  202. m_scheduler_paused_counter--;
  203. }
  204. CRITICAL_REGION_EXIT();
  205. }
  206. #endif //APP_SCHEDULER_WITH_PAUSE
  207. /**@brief Function for checking if scheduler is paused which means that should break processing
  208. * events.
  209. *
  210. * @return Boolean value - true if scheduler is paused, false otherwise.
  211. */
  212. static __INLINE bool is_app_sched_paused(void)
  213. {
  214. #if APP_SCHEDULER_WITH_PAUSE
  215. return (m_scheduler_paused_counter > 0);
  216. #else
  217. return false;
  218. #endif
  219. }
  220. void app_sched_execute(void)
  221. {
  222. while (!is_app_sched_paused() && !APP_SCHED_QUEUE_EMPTY())
  223. {
  224. // Since this function is only called from the main loop, there is no
  225. // need for a critical region here, however a special care must be taken
  226. // regarding update of the queue start index (see the end of the loop).
  227. uint16_t event_index = m_queue_start_index;
  228. void * p_event_data;
  229. uint16_t event_data_size;
  230. app_sched_event_handler_t event_handler;
  231. p_event_data = &m_queue_event_data[event_index * m_queue_event_size];
  232. event_data_size = m_queue_event_headers[event_index].event_data_size;
  233. event_handler = m_queue_event_headers[event_index].handler;
  234. event_handler(p_event_data, event_data_size);
  235. // Event processed, now it is safe to move the queue start index,
  236. // so the queue entry occupied by this event can be used to store
  237. // a next one.
  238. m_queue_start_index = next_index(m_queue_start_index);
  239. }
  240. }
  241. #endif //NRF_MODULE_ENABLED(APP_SCHEDULER)