app_scheduler.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /** @file
  13. *
  14. * @defgroup app_scheduler Scheduler
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief The scheduler is used for transferring execution from the interrupt context to the main
  19. * context.
  20. *
  21. * @details See @ref seq_diagrams_sched for sequence diagrams illustrating the flow of events
  22. * when using the Scheduler.
  23. *
  24. * @section app_scheduler_req Requirements:
  25. *
  26. * @subsection main_context_logic Logic in main context:
  27. *
  28. * - Define an event handler for each type of event expected.
  29. * - Initialize the scheduler by calling the APP_SCHED_INIT() macro before entering the
  30. * application main loop.
  31. * - Call app_sched_execute() from the main loop each time the application wakes up because of an
  32. * event (typically when sd_app_evt_wait() returns).
  33. *
  34. * @subsection int_context_logic Logic in interrupt context:
  35. *
  36. * - In the interrupt handler, call app_sched_event_put()
  37. * with the appropriate data and event handler. This will insert an event into the
  38. * scheduler's queue. The app_sched_execute() function will pull this event and call its
  39. * handler in the main context.
  40. *
  41. * @if (PERIPHERAL)
  42. * For an example usage of the scheduler, see the implementations of
  43. * @ref ble_sdk_app_hids_mouse and @ref ble_sdk_app_hids_keyboard.
  44. * @endif
  45. *
  46. * @image html scheduler_working.jpg The high level design of the scheduler
  47. */
  48. #ifndef APP_SCHEDULER_H__
  49. #define APP_SCHEDULER_H__
  50. #include <stdint.h>
  51. #include "app_error.h"
  52. #include "app_util.h"
  53. #define APP_SCHED_EVENT_HEADER_SIZE 8 /**< Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()). */
  54. /**@brief Compute number of bytes required to hold the scheduler buffer.
  55. *
  56. * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
  57. * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
  58. * that can be scheduled for execution).
  59. *
  60. * @return Required scheduler buffer size (in bytes).
  61. */
  62. #define APP_SCHED_BUF_SIZE(EVENT_SIZE, QUEUE_SIZE) \
  63. (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1))
  64. /**@brief Scheduler event handler type. */
  65. typedef void (*app_sched_event_handler_t)(void * p_event_data, uint16_t event_size);
  66. /**@brief Macro for initializing the event scheduler.
  67. *
  68. * @details It will also handle dimensioning and allocation of the memory buffer required by the
  69. * scheduler, making sure the buffer is correctly aligned.
  70. *
  71. * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
  72. * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
  73. * that can be scheduled for execution).
  74. *
  75. * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
  76. * several times as long as it is from the same location, e.g. to do a reinitialization).
  77. */
  78. #define APP_SCHED_INIT(EVENT_SIZE, QUEUE_SIZE) \
  79. do \
  80. { \
  81. static uint32_t APP_SCHED_BUF[CEIL_DIV(APP_SCHED_BUF_SIZE((EVENT_SIZE), (QUEUE_SIZE)), \
  82. sizeof(uint32_t))]; \
  83. uint32_t ERR_CODE = app_sched_init((EVENT_SIZE), (QUEUE_SIZE), APP_SCHED_BUF); \
  84. APP_ERROR_CHECK(ERR_CODE); \
  85. } while (0)
  86. /**@brief Function for initializing the Scheduler.
  87. *
  88. * @details It must be called before entering the main loop.
  89. *
  90. * @param[in] max_event_size Maximum size of events to be passed through the scheduler.
  91. * @param[in] queue_size Number of entries in scheduler queue (i.e. the maximum number of
  92. * events that can be scheduled for execution).
  93. * @param[in] p_evt_buffer Pointer to memory buffer for holding the scheduler queue. It must
  94. * be dimensioned using the APP_SCHED_BUFFER_SIZE() macro. The buffer
  95. * must be aligned to a 4 byte boundary.
  96. *
  97. * @note Normally initialization should be done using the APP_SCHED_INIT() macro, as that will both
  98. * allocate the scheduler buffer, and also align the buffer correctly.
  99. *
  100. * @retval NRF_SUCCESS Successful initialization.
  101. * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
  102. * boundary).
  103. */
  104. uint32_t app_sched_init(uint16_t max_event_size, uint16_t queue_size, void * p_evt_buffer);
  105. /**@brief Function for executing all scheduled events.
  106. *
  107. * @details This function must be called from within the main loop. It will execute all events
  108. * scheduled since the last time it was called.
  109. */
  110. void app_sched_execute(void);
  111. /**@brief Function for scheduling an event.
  112. *
  113. * @details Puts an event into the event queue.
  114. *
  115. * @param[in] p_event_data Pointer to event data to be scheduled.
  116. * @param[in] event_size Size of event data to be scheduled.
  117. * @param[in] handler Event handler to receive the event.
  118. *
  119. * @return NRF_SUCCESS on success, otherwise an error code.
  120. */
  121. uint32_t app_sched_event_put(void * p_event_data,
  122. uint16_t event_size,
  123. app_sched_event_handler_t handler);
  124. #ifdef APP_SCHEDULER_WITH_PROFILER
  125. /**@brief Function for getting the maximum observed queue utilization.
  126. *
  127. * Function for tuning the module and determining QUEUE_SIZE value and thus module RAM usage.
  128. *
  129. * @return Maximum number of events in queue observed so far.
  130. */
  131. uint16_t app_sched_queue_utilization_get(void);
  132. #endif
  133. #ifdef APP_SCHEDULER_WITH_PAUSE
  134. /**@brief A function to pause the scheduler.
  135. *
  136. * @details When the scheduler is paused events are not pulled from the scheduler queue for
  137. * processing. The function can be called multiple times. To unblock the scheduler the
  138. * function @ref app_sched_resume has to be called the same number of times.
  139. */
  140. void app_sched_pause(void);
  141. /**@brief A function to resume a scheduler.
  142. *
  143. * @details To unblock the scheduler this function has to be called the same number of times as
  144. * @ref app_sched_pause function.
  145. */
  146. void app_sched_resume(void);
  147. #endif
  148. #endif // APP_SCHEDULER_H__
  149. /** @} */