app_scheduler.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /** @file
  41. *
  42. * @defgroup app_scheduler Scheduler
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief The scheduler is used for transferring execution from the interrupt context to the main
  47. * context.
  48. *
  49. * @details See @ref seq_diagrams_sched for sequence diagrams illustrating the flow of events
  50. * when using the Scheduler.
  51. *
  52. * @section app_scheduler_req Requirements:
  53. *
  54. * @subsection main_context_logic Logic in main context:
  55. *
  56. * - Define an event handler for each type of event expected.
  57. * - Initialize the scheduler by calling the APP_SCHED_INIT() macro before entering the
  58. * application main loop.
  59. * - Call app_sched_execute() from the main loop each time the application wakes up because of an
  60. * event (typically when sd_app_evt_wait() returns).
  61. *
  62. * @subsection int_context_logic Logic in interrupt context:
  63. *
  64. * - In the interrupt handler, call app_sched_event_put()
  65. * with the appropriate data and event handler. This will insert an event into the
  66. * scheduler's queue. The app_sched_execute() function will pull this event and call its
  67. * handler in the main context.
  68. *
  69. * @if (PERIPHERAL)
  70. * For an example usage of the scheduler, see the implementations of
  71. * @ref ble_sdk_app_hids_mouse and @ref ble_sdk_app_hids_keyboard.
  72. * @endif
  73. *
  74. * @image html scheduler_working.svg The high level design of the scheduler
  75. */
  76. #ifndef APP_SCHEDULER_H__
  77. #define APP_SCHEDULER_H__
  78. #include "sdk_config.h"
  79. #include <stdint.h>
  80. #include "app_error.h"
  81. #include "app_util.h"
  82. #ifdef __cplusplus
  83. extern "C" {
  84. #endif
  85. #define APP_SCHED_EVENT_HEADER_SIZE 8 /**< Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()). */
  86. /**@brief Compute number of bytes required to hold the scheduler buffer.
  87. *
  88. * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
  89. * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
  90. * that can be scheduled for execution).
  91. *
  92. * @return Required scheduler buffer size (in bytes).
  93. */
  94. #define APP_SCHED_BUF_SIZE(EVENT_SIZE, QUEUE_SIZE) \
  95. (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1))
  96. /**@brief Scheduler event handler type. */
  97. typedef void (*app_sched_event_handler_t)(void * p_event_data, uint16_t event_size);
  98. /**@brief Macro for initializing the event scheduler.
  99. *
  100. * @details It will also handle dimensioning and allocation of the memory buffer required by the
  101. * scheduler, making sure the buffer is correctly aligned.
  102. *
  103. * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
  104. * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
  105. * that can be scheduled for execution).
  106. *
  107. * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
  108. * several times as long as it is from the same location, e.g. to do a reinitialization).
  109. */
  110. #define APP_SCHED_INIT(EVENT_SIZE, QUEUE_SIZE) \
  111. do \
  112. { \
  113. static uint32_t APP_SCHED_BUF[CEIL_DIV(APP_SCHED_BUF_SIZE((EVENT_SIZE), (QUEUE_SIZE)), \
  114. sizeof(uint32_t))]; \
  115. uint32_t ERR_CODE = app_sched_init((EVENT_SIZE), (QUEUE_SIZE), APP_SCHED_BUF); \
  116. APP_ERROR_CHECK(ERR_CODE); \
  117. } while (0)
  118. /**@brief Function for initializing the Scheduler.
  119. *
  120. * @details It must be called before entering the main loop.
  121. *
  122. * @param[in] max_event_size Maximum size of events to be passed through the scheduler.
  123. * @param[in] queue_size Number of entries in scheduler queue (i.e. the maximum number of
  124. * events that can be scheduled for execution).
  125. * @param[in] p_evt_buffer Pointer to memory buffer for holding the scheduler queue. It must
  126. * be dimensioned using the APP_SCHED_BUFFER_SIZE() macro. The buffer
  127. * must be aligned to a 4 byte boundary.
  128. *
  129. * @note Normally initialization should be done using the APP_SCHED_INIT() macro, as that will both
  130. * allocate the scheduler buffer, and also align the buffer correctly.
  131. *
  132. * @retval NRF_SUCCESS Successful initialization.
  133. * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
  134. * boundary).
  135. */
  136. uint32_t app_sched_init(uint16_t max_event_size, uint16_t queue_size, void * p_evt_buffer);
  137. /**@brief Function for executing all scheduled events.
  138. *
  139. * @details This function must be called from within the main loop. It will execute all events
  140. * scheduled since the last time it was called.
  141. */
  142. void app_sched_execute(void);
  143. /**@brief Function for scheduling an event.
  144. *
  145. * @details Puts an event into the event queue.
  146. *
  147. * @param[in] p_event_data Pointer to event data to be scheduled.
  148. * @param[in] event_size Size of event data to be scheduled.
  149. * @param[in] handler Event handler to receive the event.
  150. *
  151. * @return NRF_SUCCESS on success, otherwise an error code.
  152. */
  153. uint32_t app_sched_event_put(void const * p_event_data,
  154. uint16_t event_size,
  155. app_sched_event_handler_t handler);
  156. /**@brief Function for getting the maximum observed queue utilization.
  157. *
  158. * Function for tuning the module and determining QUEUE_SIZE value and thus module RAM usage.
  159. *
  160. * @note @ref APP_SCHEDULER_WITH_PROFILER must be enabled to use this functionality.
  161. *
  162. * @return Maximum number of events in queue observed so far.
  163. */
  164. uint16_t app_sched_queue_utilization_get(void);
  165. /**@brief Function for getting the current amount of free space in the queue.
  166. *
  167. * @details The real amount of free space may be less if entries are being added from an interrupt.
  168. * To get the sxact value, this function should be called from the critical section.
  169. *
  170. * @return Amount of free space in the queue.
  171. */
  172. uint16_t app_sched_queue_space_get(void);
  173. /**@brief A function to pause the scheduler.
  174. *
  175. * @details When the scheduler is paused events are not pulled from the scheduler queue for
  176. * processing. The function can be called multiple times. To unblock the scheduler the
  177. * function @ref app_sched_resume has to be called the same number of times.
  178. *
  179. * @note @ref APP_SCHEDULER_WITH_PAUSE must be enabled to use this functionality.
  180. */
  181. void app_sched_pause(void);
  182. /**@brief A function to resume a scheduler.
  183. *
  184. * @details To unblock the scheduler this function has to be called the same number of times as
  185. * @ref app_sched_pause function.
  186. *
  187. * @note @ref APP_SCHEDULER_WITH_PAUSE must be enabled to use this functionality.
  188. */
  189. void app_sched_resume(void);
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. #endif // APP_SCHEDULER_H__
  194. /** @} */