mediCAL's first BLE project

Dependents:   BLE_mediCAL

Fork of nRF51822 by Nordic Semiconductor

Committer:
bogdanm
Date:
Wed Mar 26 14:38:17 2014 +0000
Revision:
0:eff01767de02
Child:
37:c29c330d942c
Initial import of the nRF51822 code

Who changed what in which revision?

UserRevisionLine numberNew 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 "nordic_global.h"
bogdanm 0:eff01767de02 53 #include "app_error.h"
bogdanm 0:eff01767de02 54
bogdanm 0:eff01767de02 55 #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 56
bogdanm 0:eff01767de02 57 /**@brief Compute number of bytes required to hold the scheduler buffer.
bogdanm 0:eff01767de02 58 *
bogdanm 0:eff01767de02 59 * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
bogdanm 0:eff01767de02 60 * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
bogdanm 0:eff01767de02 61 * that can be scheduled for execution).
bogdanm 0:eff01767de02 62 *
bogdanm 0:eff01767de02 63 * @return Required scheduler buffer size (in bytes).
bogdanm 0:eff01767de02 64 */
bogdanm 0:eff01767de02 65 #define APP_SCHED_BUF_SIZE(EVENT_SIZE, QUEUE_SIZE) \
bogdanm 0:eff01767de02 66 (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1))
bogdanm 0:eff01767de02 67
bogdanm 0:eff01767de02 68 /**@brief Scheduler event handler type. */
bogdanm 0:eff01767de02 69 typedef void (*app_sched_event_handler_t)(void * p_event_data, uint16_t event_size);
bogdanm 0:eff01767de02 70
bogdanm 0:eff01767de02 71 /**@brief Macro for initializing the event scheduler.
bogdanm 0:eff01767de02 72 *
bogdanm 0:eff01767de02 73 * @details It will also handle dimensioning and allocation of the memory buffer required by the
bogdanm 0:eff01767de02 74 * scheduler, making sure the buffer is correctly aligned.
bogdanm 0:eff01767de02 75 *
bogdanm 0:eff01767de02 76 * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
bogdanm 0:eff01767de02 77 * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
bogdanm 0:eff01767de02 78 * that can be scheduled for execution).
bogdanm 0:eff01767de02 79 *
bogdanm 0:eff01767de02 80 * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
bogdanm 0:eff01767de02 81 * several times as long as it is from the same location, e.g. to do a reinitialization).
bogdanm 0:eff01767de02 82 */
bogdanm 0:eff01767de02 83 #define APP_SCHED_INIT(EVENT_SIZE, QUEUE_SIZE) \
bogdanm 0:eff01767de02 84 do \
bogdanm 0:eff01767de02 85 { \
bogdanm 0:eff01767de02 86 static uint32_t APP_SCHED_BUF[CEIL_DIV(APP_SCHED_BUF_SIZE((EVENT_SIZE), (QUEUE_SIZE)), \
bogdanm 0:eff01767de02 87 sizeof(uint32_t))]; \
bogdanm 0:eff01767de02 88 uint32_t ERR_CODE = app_sched_init((EVENT_SIZE), (QUEUE_SIZE), APP_SCHED_BUF); \
bogdanm 0:eff01767de02 89 APP_ERROR_CHECK(ERR_CODE); \
bogdanm 0:eff01767de02 90 } while (0)
bogdanm 0:eff01767de02 91
bogdanm 0:eff01767de02 92 /**@brief Function for initializing the Scheduler.
bogdanm 0:eff01767de02 93 *
bogdanm 0:eff01767de02 94 * @details It must be called before entering the main loop.
bogdanm 0:eff01767de02 95 *
bogdanm 0:eff01767de02 96 * @param[in] max_event_size Maximum size of events to be passed through the scheduler.
bogdanm 0:eff01767de02 97 * @param[in] queue_size Number of entries in scheduler queue (i.e. the maximum number of
bogdanm 0:eff01767de02 98 * events that can be scheduled for execution).
bogdanm 0:eff01767de02 99 * @param[in] p_event_buffer Pointer to memory buffer for holding the scheduler queue. It must
bogdanm 0:eff01767de02 100 * be dimensioned using the APP_SCHED_BUFFER_SIZE() macro. The buffer
bogdanm 0:eff01767de02 101 * must be aligned to a 4 byte boundary.
bogdanm 0:eff01767de02 102 *
bogdanm 0:eff01767de02 103 * @note Normally initialization should be done using the APP_SCHED_INIT() macro, as that will both
bogdanm 0:eff01767de02 104 * allocate the scheduler buffer, and also align the buffer correctly.
bogdanm 0:eff01767de02 105 *
bogdanm 0:eff01767de02 106 * @retval NRF_SUCCESS Successful initialization.
bogdanm 0:eff01767de02 107 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
bogdanm 0:eff01767de02 108 * boundary).
bogdanm 0:eff01767de02 109 */
bogdanm 0:eff01767de02 110 uint32_t app_sched_init(uint16_t max_event_size, uint16_t queue_size, void * p_evt_buffer);
bogdanm 0:eff01767de02 111
bogdanm 0:eff01767de02 112 /**@brief Function for executing all scheduled events.
bogdanm 0:eff01767de02 113 *
bogdanm 0:eff01767de02 114 * @details This function must be called from within the main loop. It will execute all events
bogdanm 0:eff01767de02 115 * scheduled since the last time it was called.
bogdanm 0:eff01767de02 116 */
bogdanm 0:eff01767de02 117 void app_sched_execute(void);
bogdanm 0:eff01767de02 118
bogdanm 0:eff01767de02 119 /**@brief Function for scheduling an event.
bogdanm 0:eff01767de02 120 *
bogdanm 0:eff01767de02 121 * @details Puts an event into the event queue.
bogdanm 0:eff01767de02 122 *
bogdanm 0:eff01767de02 123 * @param[in] p_event_data Pointer to event data to be scheduled.
bogdanm 0:eff01767de02 124 * @param[in] p_event_size Size of event data to be scheduled.
bogdanm 0:eff01767de02 125 * @param[in] handler Event handler to receive the event.
bogdanm 0:eff01767de02 126 *
bogdanm 0:eff01767de02 127 * @return NRF_SUCCESS on success, otherwise an error code.
bogdanm 0:eff01767de02 128 */
bogdanm 0:eff01767de02 129 uint32_t app_sched_event_put(void * p_event_data,
bogdanm 0:eff01767de02 130 uint16_t event_size,
bogdanm 0:eff01767de02 131 app_sched_event_handler_t handler);
bogdanm 0:eff01767de02 132
bogdanm 0:eff01767de02 133 #endif // APP_SCHEDULER_H__
bogdanm 0:eff01767de02 134
bogdanm 0:eff01767de02 135 /** @} */