Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.
source/nordic_sdk/components/libraries/scheduler/app_scheduler.h@19:47192cb9def7, 2016-04-07 (annotated)
- Committer:
- vcoubard
- Date:
- Thu Apr 07 17:37:40 2016 +0100
- Revision:
- 19:47192cb9def7
- Parent:
- 10:233fefd8162b
- Child:
- 20:a90c48eb1d30
Synchronized with git rev 9251259f
Author: Liyou Zhou
Copy over coresponding files from nordic-sdk 9.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 19:47192cb9def7 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
vcoubard | 19:47192cb9def7 | 2 | * |
vcoubard | 19:47192cb9def7 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
vcoubard | 19:47192cb9def7 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
vcoubard | 19:47192cb9def7 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
vcoubard | 19:47192cb9def7 | 6 | * |
vcoubard | 19:47192cb9def7 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
vcoubard | 19:47192cb9def7 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
vcoubard | 19:47192cb9def7 | 9 | * the file. |
vcoubard | 19:47192cb9def7 | 10 | * |
Vincent Coubard |
0:f2542974c862 | 11 | */ |
Vincent Coubard |
0:f2542974c862 | 12 | |
Vincent Coubard |
0:f2542974c862 | 13 | /** @file |
Vincent Coubard |
0:f2542974c862 | 14 | * |
Vincent Coubard |
0:f2542974c862 | 15 | * @defgroup app_scheduler Scheduler |
Vincent Coubard |
0:f2542974c862 | 16 | * @{ |
Vincent Coubard |
0:f2542974c862 | 17 | * @ingroup app_common |
Vincent Coubard |
0:f2542974c862 | 18 | * |
Vincent Coubard |
0:f2542974c862 | 19 | * @brief The scheduler is used for transferring execution from the interrupt context to the main |
Vincent Coubard |
0:f2542974c862 | 20 | * context. |
Vincent Coubard |
0:f2542974c862 | 21 | * |
Vincent Coubard |
0:f2542974c862 | 22 | * @details See @ref seq_diagrams_sched for sequence diagrams illustrating the flow of events |
Vincent Coubard |
0:f2542974c862 | 23 | * when using the Scheduler. |
Vincent Coubard |
0:f2542974c862 | 24 | * |
Vincent Coubard |
0:f2542974c862 | 25 | * @section app_scheduler_req Requirements: |
Vincent Coubard |
0:f2542974c862 | 26 | * |
Vincent Coubard |
0:f2542974c862 | 27 | * @subsection main_context_logic Logic in main context: |
Vincent Coubard |
0:f2542974c862 | 28 | * |
Vincent Coubard |
0:f2542974c862 | 29 | * - Define an event handler for each type of event expected. |
Vincent Coubard |
0:f2542974c862 | 30 | * - Initialize the scheduler by calling the APP_SCHED_INIT() macro before entering the |
Vincent Coubard |
0:f2542974c862 | 31 | * application main loop. |
Vincent Coubard |
0:f2542974c862 | 32 | * - Call app_sched_execute() from the main loop each time the application wakes up because of an |
Vincent Coubard |
0:f2542974c862 | 33 | * event (typically when sd_app_evt_wait() returns). |
Vincent Coubard |
0:f2542974c862 | 34 | * |
Vincent Coubard |
0:f2542974c862 | 35 | * @subsection int_context_logic Logic in interrupt context: |
Vincent Coubard |
0:f2542974c862 | 36 | * |
Vincent Coubard |
0:f2542974c862 | 37 | * - In the interrupt handler, call app_sched_event_put() |
Vincent Coubard |
0:f2542974c862 | 38 | * with the appropriate data and event handler. This will insert an event into the |
Vincent Coubard |
0:f2542974c862 | 39 | * scheduler's queue. The app_sched_execute() function will pull this event and call its |
Vincent Coubard |
0:f2542974c862 | 40 | * handler in the main context. |
Vincent Coubard |
0:f2542974c862 | 41 | * |
vcoubard | 19:47192cb9def7 | 42 | * @if (PERIPHERAL) |
Vincent Coubard |
0:f2542974c862 | 43 | * For an example usage of the scheduler, see the implementations of |
Vincent Coubard |
0:f2542974c862 | 44 | * @ref ble_sdk_app_hids_mouse and @ref ble_sdk_app_hids_keyboard. |
Vincent Coubard |
0:f2542974c862 | 45 | * @endif |
Vincent Coubard |
0:f2542974c862 | 46 | * |
Vincent Coubard |
0:f2542974c862 | 47 | * @image html scheduler_working.jpg The high level design of the scheduler |
Vincent Coubard |
0:f2542974c862 | 48 | */ |
Vincent Coubard |
0:f2542974c862 | 49 | |
Vincent Coubard |
0:f2542974c862 | 50 | #ifndef APP_SCHEDULER_H__ |
Vincent Coubard |
0:f2542974c862 | 51 | #define APP_SCHEDULER_H__ |
Vincent Coubard |
0:f2542974c862 | 52 | |
Vincent Coubard |
0:f2542974c862 | 53 | #include <stdint.h> |
Vincent Coubard |
0:f2542974c862 | 54 | #include "app_error.h" |
vcoubard | 19:47192cb9def7 | 55 | #include "app_util.h" |
Vincent Coubard |
0:f2542974c862 | 56 | |
Vincent Coubard |
0:f2542974c862 | 57 | #define APP_SCHED_EVENT_HEADER_SIZE 8 /**< Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()). */ |
Vincent Coubard |
0:f2542974c862 | 58 | |
Vincent Coubard |
0:f2542974c862 | 59 | /**@brief Compute number of bytes required to hold the scheduler buffer. |
Vincent Coubard |
0:f2542974c862 | 60 | * |
Vincent Coubard |
0:f2542974c862 | 61 | * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler. |
Vincent Coubard |
0:f2542974c862 | 62 | * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events |
Vincent Coubard |
0:f2542974c862 | 63 | * that can be scheduled for execution). |
Vincent Coubard |
0:f2542974c862 | 64 | * |
Vincent Coubard |
0:f2542974c862 | 65 | * @return Required scheduler buffer size (in bytes). |
Vincent Coubard |
0:f2542974c862 | 66 | */ |
Vincent Coubard |
0:f2542974c862 | 67 | #define APP_SCHED_BUF_SIZE(EVENT_SIZE, QUEUE_SIZE) \ |
Vincent Coubard |
0:f2542974c862 | 68 | (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1)) |
Vincent Coubard |
0:f2542974c862 | 69 | |
Vincent Coubard |
0:f2542974c862 | 70 | /**@brief Scheduler event handler type. */ |
Vincent Coubard |
0:f2542974c862 | 71 | typedef void (*app_sched_event_handler_t)(void * p_event_data, uint16_t event_size); |
Vincent Coubard |
0:f2542974c862 | 72 | |
Vincent Coubard |
0:f2542974c862 | 73 | /**@brief Macro for initializing the event scheduler. |
Vincent Coubard |
0:f2542974c862 | 74 | * |
Vincent Coubard |
0:f2542974c862 | 75 | * @details It will also handle dimensioning and allocation of the memory buffer required by the |
Vincent Coubard |
0:f2542974c862 | 76 | * scheduler, making sure the buffer is correctly aligned. |
Vincent Coubard |
0:f2542974c862 | 77 | * |
Vincent Coubard |
0:f2542974c862 | 78 | * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler. |
Vincent Coubard |
0:f2542974c862 | 79 | * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events |
Vincent Coubard |
0:f2542974c862 | 80 | * that can be scheduled for execution). |
Vincent Coubard |
0:f2542974c862 | 81 | * |
Vincent Coubard |
0:f2542974c862 | 82 | * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it |
Vincent Coubard |
0:f2542974c862 | 83 | * several times as long as it is from the same location, e.g. to do a reinitialization). |
Vincent Coubard |
0:f2542974c862 | 84 | */ |
Vincent Coubard |
0:f2542974c862 | 85 | #define APP_SCHED_INIT(EVENT_SIZE, QUEUE_SIZE) \ |
Vincent Coubard |
0:f2542974c862 | 86 | do \ |
Vincent Coubard |
0:f2542974c862 | 87 | { \ |
Vincent Coubard |
0:f2542974c862 | 88 | static uint32_t APP_SCHED_BUF[CEIL_DIV(APP_SCHED_BUF_SIZE((EVENT_SIZE), (QUEUE_SIZE)), \ |
Vincent Coubard |
0:f2542974c862 | 89 | sizeof(uint32_t))]; \ |
Vincent Coubard |
0:f2542974c862 | 90 | uint32_t ERR_CODE = app_sched_init((EVENT_SIZE), (QUEUE_SIZE), APP_SCHED_BUF); \ |
Vincent Coubard |
0:f2542974c862 | 91 | APP_ERROR_CHECK(ERR_CODE); \ |
Vincent Coubard |
0:f2542974c862 | 92 | } while (0) |
Vincent Coubard |
0:f2542974c862 | 93 | |
Vincent Coubard |
0:f2542974c862 | 94 | /**@brief Function for initializing the Scheduler. |
Vincent Coubard |
0:f2542974c862 | 95 | * |
Vincent Coubard |
0:f2542974c862 | 96 | * @details It must be called before entering the main loop. |
Vincent Coubard |
0:f2542974c862 | 97 | * |
Vincent Coubard |
0:f2542974c862 | 98 | * @param[in] max_event_size Maximum size of events to be passed through the scheduler. |
Vincent Coubard |
0:f2542974c862 | 99 | * @param[in] queue_size Number of entries in scheduler queue (i.e. the maximum number of |
Vincent Coubard |
0:f2542974c862 | 100 | * events that can be scheduled for execution). |
Vincent Coubard |
0:f2542974c862 | 101 | * @param[in] p_evt_buffer Pointer to memory buffer for holding the scheduler queue. It must |
Vincent Coubard |
0:f2542974c862 | 102 | * be dimensioned using the APP_SCHED_BUFFER_SIZE() macro. The buffer |
Vincent Coubard |
0:f2542974c862 | 103 | * must be aligned to a 4 byte boundary. |
Vincent Coubard |
0:f2542974c862 | 104 | * |
Vincent Coubard |
0:f2542974c862 | 105 | * @note Normally initialization should be done using the APP_SCHED_INIT() macro, as that will both |
Vincent Coubard |
0:f2542974c862 | 106 | * allocate the scheduler buffer, and also align the buffer correctly. |
Vincent Coubard |
0:f2542974c862 | 107 | * |
Vincent Coubard |
0:f2542974c862 | 108 | * @retval NRF_SUCCESS Successful initialization. |
Vincent Coubard |
0:f2542974c862 | 109 | * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte |
Vincent Coubard |
0:f2542974c862 | 110 | * boundary). |
Vincent Coubard |
0:f2542974c862 | 111 | */ |
Vincent Coubard |
0:f2542974c862 | 112 | uint32_t app_sched_init(uint16_t max_event_size, uint16_t queue_size, void * p_evt_buffer); |
Vincent Coubard |
0:f2542974c862 | 113 | |
Vincent Coubard |
0:f2542974c862 | 114 | /**@brief Function for executing all scheduled events. |
Vincent Coubard |
0:f2542974c862 | 115 | * |
Vincent Coubard |
0:f2542974c862 | 116 | * @details This function must be called from within the main loop. It will execute all events |
Vincent Coubard |
0:f2542974c862 | 117 | * scheduled since the last time it was called. |
Vincent Coubard |
0:f2542974c862 | 118 | */ |
Vincent Coubard |
0:f2542974c862 | 119 | void app_sched_execute(void); |
Vincent Coubard |
0:f2542974c862 | 120 | |
Vincent Coubard |
0:f2542974c862 | 121 | /**@brief Function for scheduling an event. |
Vincent Coubard |
0:f2542974c862 | 122 | * |
Vincent Coubard |
0:f2542974c862 | 123 | * @details Puts an event into the event queue. |
Vincent Coubard |
0:f2542974c862 | 124 | * |
Vincent Coubard |
0:f2542974c862 | 125 | * @param[in] p_event_data Pointer to event data to be scheduled. |
Vincent Coubard |
0:f2542974c862 | 126 | * @param[in] event_size Size of event data to be scheduled. |
Vincent Coubard |
0:f2542974c862 | 127 | * @param[in] handler Event handler to receive the event. |
Vincent Coubard |
0:f2542974c862 | 128 | * |
Vincent Coubard |
0:f2542974c862 | 129 | * @return NRF_SUCCESS on success, otherwise an error code. |
Vincent Coubard |
0:f2542974c862 | 130 | */ |
Vincent Coubard |
0:f2542974c862 | 131 | uint32_t app_sched_event_put(void * p_event_data, |
Vincent Coubard |
0:f2542974c862 | 132 | uint16_t event_size, |
Vincent Coubard |
0:f2542974c862 | 133 | app_sched_event_handler_t handler); |
Vincent Coubard |
0:f2542974c862 | 134 | |
Vincent Coubard |
0:f2542974c862 | 135 | #ifdef APP_SCHEDULER_WITH_PAUSE |
Vincent Coubard |
0:f2542974c862 | 136 | /**@brief A function to pause the scheduler. |
Vincent Coubard |
0:f2542974c862 | 137 | * |
Vincent Coubard |
0:f2542974c862 | 138 | * @details When the scheduler is paused events are not pulled from the scheduler queue for |
Vincent Coubard |
0:f2542974c862 | 139 | * processing. The function can be called multiple times. To unblock the scheduler the |
Vincent Coubard |
0:f2542974c862 | 140 | * function @ref app_sched_resume has to be called the same number of times. |
Vincent Coubard |
0:f2542974c862 | 141 | */ |
Vincent Coubard |
0:f2542974c862 | 142 | void app_sched_pause(void); |
Vincent Coubard |
0:f2542974c862 | 143 | |
Vincent Coubard |
0:f2542974c862 | 144 | /**@brief A function to resume a scheduler. |
Vincent Coubard |
0:f2542974c862 | 145 | * |
Vincent Coubard |
0:f2542974c862 | 146 | * @details To unblock the scheduler this function has to be called the same number of times as |
Vincent Coubard |
0:f2542974c862 | 147 | * @ref app_sched_pause function. |
Vincent Coubard |
0:f2542974c862 | 148 | */ |
Vincent Coubard |
0:f2542974c862 | 149 | void app_sched_resume(void); |
Vincent Coubard |
0:f2542974c862 | 150 | #endif |
Vincent Coubard |
0:f2542974c862 | 151 | #endif // APP_SCHEDULER_H__ |
Vincent Coubard |
0:f2542974c862 | 152 | |
vcoubard | 1:ebc0e0ef0a11 | 153 | /** @} */ |