meh

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Fri Sep 12 16:41:52 2014 +0100
Revision:
89:552587b429a1
Release 89 of the mbed library

Main changes:

- low power optimizations for Nordic targets
- code structure changes for Freescale K64F targets
- bug fixes in various backends

Who changed what in which revision?

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