The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

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