テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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