Forking mbed-dev-bin

Fork of mbed-dev-bin by Lancaster University

Committer:
bluetooth_kyo
Date:
Wed Nov 29 22:40:30 2017 +0000
Revision:
4:1c89fe53240e
Parent:
0:e1a608bb55e8
forking mbed-dev-bin

Who changed what in which revision?

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