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