app_scheduler_serconn.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "app_scheduler.h"
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include "nrf_soc.h"
  17. #include "nrf_assert.h"
  18. #include "app_util.h"
  19. #include "app_util_platform.h"
  20. /**@brief Structure for holding a scheduled event header. */
  21. typedef struct
  22. {
  23. app_sched_event_handler_t handler; /**< Pointer to event handler to receive the event. */
  24. uint16_t event_data_size; /**< Size of event data. */
  25. } event_header_t;
  26. STATIC_ASSERT(sizeof (event_header_t) <= APP_SCHED_EVENT_HEADER_SIZE);
  27. static event_header_t * m_queue_event_headers; /**< Array for holding the queue event headers. */
  28. static uint8_t * m_queue_event_data; /**< Array for holding the queue event data. */
  29. static volatile uint8_t m_queue_start_index; /**< Index of queue entry at the start of the queue. */
  30. static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the end of the queue. */
  31. static uint16_t m_queue_event_size; /**< Maximum event size in queue. */
  32. static uint16_t m_queue_size; /**< Number of queue entries. */
  33. #ifdef APP_SCHEDULER_WITH_PROFILER
  34. static uint16_t m_max_queue_utilization; /**< Maximum observed queue utilization. */
  35. #endif
  36. static uint32_t m_scheduler_paused_counter = 0; /**< Counter storing the difference between pausing
  37. and resuming the scheduler. */
  38. /**@brief Function for incrementing a queue index, and handle wrap-around.
  39. *
  40. * @param[in] index Old index.
  41. *
  42. * @return New (incremented) index.
  43. */
  44. static __INLINE uint8_t next_index(uint8_t index)
  45. {
  46. return (index < m_queue_size) ? (index + 1) : 0;
  47. }
  48. static __INLINE uint8_t app_sched_queue_full(void)
  49. {
  50. uint8_t tmp = m_queue_start_index;
  51. return next_index(m_queue_end_index) == tmp;
  52. }
  53. /**@brief Macro for checking if a queue is full. */
  54. #define APP_SCHED_QUEUE_FULL() app_sched_queue_full()
  55. static __INLINE uint8_t app_sched_queue_empty(void)
  56. {
  57. uint8_t tmp = m_queue_start_index;
  58. return m_queue_end_index == tmp;
  59. }
  60. /**@brief Macro for checking if a queue is empty. */
  61. #define APP_SCHED_QUEUE_EMPTY() app_sched_queue_empty()
  62. uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer)
  63. {
  64. uint16_t data_start_index = (queue_size + 1) * sizeof (event_header_t);
  65. //Check that buffer is correctly aligned
  66. if (!is_word_aligned(p_event_buffer))
  67. {
  68. return NRF_ERROR_INVALID_PARAM;
  69. }
  70. //Initialize event scheduler
  71. m_queue_event_headers = p_event_buffer;
  72. m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index];
  73. m_queue_end_index = 0;
  74. m_queue_start_index = 0;
  75. m_queue_event_size = event_size;
  76. m_queue_size = queue_size;
  77. #ifdef APP_SCHEDULER_WITH_PROFILER
  78. m_max_queue_utilization = 0;
  79. #endif
  80. return NRF_SUCCESS;
  81. }
  82. #ifdef APP_SCHEDULER_WITH_PROFILER
  83. static void check_queue_utilization(void)
  84. {
  85. uint16_t start = m_queue_start_index;
  86. uint16_t end = m_queue_end_index;
  87. uint16_t queue_utilization = (end >= start) ? (end - start) :
  88. (m_queue_size + 1 - start + end);
  89. if (queue_utilization > m_max_queue_utilization)
  90. {
  91. m_max_queue_utilization = queue_utilization;
  92. }
  93. }
  94. uint16_t app_sched_queue_utilization_get(void)
  95. {
  96. return m_max_queue_utilization;
  97. }
  98. #endif
  99. uint32_t app_sched_event_put(void * p_event_data,
  100. uint16_t event_data_size,
  101. app_sched_event_handler_t handler)
  102. {
  103. uint32_t err_code;
  104. if (event_data_size <= m_queue_event_size)
  105. {
  106. uint16_t event_index = 0xFFFF;
  107. CRITICAL_REGION_ENTER();
  108. if (!APP_SCHED_QUEUE_FULL())
  109. {
  110. event_index = m_queue_end_index;
  111. m_queue_end_index = next_index(m_queue_end_index);
  112. }
  113. CRITICAL_REGION_EXIT();
  114. if (event_index != 0xFFFF)
  115. {
  116. //NOTE: This can be done outside the critical region since the event consumer will
  117. //always be called from the main loop, and will thus never interrupt this code.
  118. m_queue_event_headers[event_index].handler = handler;
  119. if ((p_event_data != NULL) && (event_data_size > 0))
  120. {
  121. memcpy(&m_queue_event_data[event_index * m_queue_event_size],
  122. p_event_data,
  123. event_data_size);
  124. m_queue_event_headers[event_index].event_data_size = event_data_size;
  125. }
  126. else
  127. {
  128. m_queue_event_headers[event_index].event_data_size = 0;
  129. }
  130. #ifdef APP_SCHEDULER_WITH_PROFILER
  131. check_queue_utilization();
  132. #endif
  133. err_code = NRF_SUCCESS;
  134. }
  135. else
  136. {
  137. err_code = NRF_ERROR_NO_MEM;
  138. }
  139. }
  140. else
  141. {
  142. err_code = NRF_ERROR_INVALID_LENGTH;
  143. }
  144. return err_code;
  145. }
  146. /**@brief Function for reading the next event from specified event queue.
  147. *
  148. * @param[out] pp_event_data Pointer to pointer to event data.
  149. * @param[out] p_event_data_size Pointer to size of event data.
  150. * @param[out] p_event_handler Pointer to event handler function pointer.
  151. *
  152. * @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty.
  153. */
  154. static uint32_t app_sched_event_get(void * * pp_event_data,
  155. uint16_t * p_event_data_size,
  156. app_sched_event_handler_t * p_event_handler)
  157. {
  158. uint32_t err_code = NRF_ERROR_NOT_FOUND;
  159. if (!APP_SCHED_QUEUE_EMPTY())
  160. {
  161. uint16_t event_index;
  162. //NOTE: There is no need for a critical region here, as this function will only be called
  163. //from app_sched_execute() from inside the main loop, so it will never interrupt
  164. //app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be
  165. //an atomic operation.
  166. event_index = m_queue_start_index;
  167. m_queue_start_index = next_index(m_queue_start_index);
  168. *pp_event_data = &m_queue_event_data[event_index * m_queue_event_size];
  169. *p_event_data_size = m_queue_event_headers[event_index].event_data_size;
  170. *p_event_handler = m_queue_event_headers[event_index].handler;
  171. err_code = NRF_SUCCESS;
  172. }
  173. return err_code;
  174. }
  175. void app_sched_pause(void)
  176. {
  177. CRITICAL_REGION_ENTER();
  178. if (m_scheduler_paused_counter < UINT32_MAX)
  179. {
  180. m_scheduler_paused_counter++;
  181. }
  182. CRITICAL_REGION_EXIT();
  183. }
  184. void app_sched_resume(void)
  185. {
  186. CRITICAL_REGION_ENTER();
  187. if (m_scheduler_paused_counter > 0)
  188. {
  189. m_scheduler_paused_counter--;
  190. }
  191. CRITICAL_REGION_EXIT();
  192. }
  193. /**@brief Function for checking if scheduler is paused which means that should break processing
  194. * events.
  195. *
  196. * @return Boolean value - true if scheduler is paused, false otherwise.
  197. */
  198. static __INLINE bool is_app_sched_paused(void)
  199. {
  200. return (m_scheduler_paused_counter > 0);
  201. }
  202. void app_sched_execute(void)
  203. {
  204. void * p_event_data;
  205. uint16_t event_data_size;
  206. app_sched_event_handler_t event_handler;
  207. //Get next event (if any), and execute handler
  208. while ((!is_app_sched_paused()) &&
  209. (app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS))
  210. {
  211. event_handler(p_event_data, event_data_size);
  212. }
  213. }