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 148:fd96258d940d 1 /*
Kojto 148:fd96258d940d 2 * Copyright (c) 2012 Nordic Semiconductor ASA
Kojto 148:fd96258d940d 3 * All rights reserved.
Kojto 148:fd96258d940d 4 *
Kojto 148:fd96258d940d 5 * Redistribution and use in source and binary forms, with or without modification,
Kojto 148:fd96258d940d 6 * are permitted provided that the following conditions are met:
Kojto 148:fd96258d940d 7 *
Kojto 148:fd96258d940d 8 * 1. Redistributions of source code must retain the above copyright notice, this list
Kojto 148:fd96258d940d 9 * of conditions and the following disclaimer.
Kojto 148:fd96258d940d 10 *
Kojto 148:fd96258d940d 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
Kojto 148:fd96258d940d 12 * integrated circuit in a product or a software update for such product, must reproduce
Kojto 148:fd96258d940d 13 * the above copyright notice, this list of conditions and the following disclaimer in
Kojto 148:fd96258d940d 14 * the documentation and/or other materials provided with the distribution.
Kojto 148:fd96258d940d 15 *
Kojto 148:fd96258d940d 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
Kojto 148:fd96258d940d 17 * used to endorse or promote products derived from this software without specific prior
Kojto 148:fd96258d940d 18 * written permission.
Kojto 148:fd96258d940d 19 *
Kojto 148:fd96258d940d 20 * 4. This software, with or without modification, must only be used with a
Kojto 148:fd96258d940d 21 * Nordic Semiconductor ASA integrated circuit.
Kojto 148:fd96258d940d 22 *
Kojto 148:fd96258d940d 23 * 5. Any software provided in binary or object form under this license must not be reverse
Kojto 148:fd96258d940d 24 * engineered, decompiled, modified and/or disassembled.
Kojto 148:fd96258d940d 25 *
Kojto 148:fd96258d940d 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Kojto 148:fd96258d940d 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Kojto 148:fd96258d940d 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Kojto 148:fd96258d940d 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Kojto 148:fd96258d940d 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Kojto 148:fd96258d940d 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Kojto 148:fd96258d940d 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Kojto 148:fd96258d940d 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Kojto 148:fd96258d940d 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Kojto 148:fd96258d940d 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kojto 148:fd96258d940d 36 *
Kojto 148:fd96258d940d 37 */
Kojto 148:fd96258d940d 38
Kojto 148:fd96258d940d 39
Kojto 148:fd96258d940d 40 /** @file
Kojto 148:fd96258d940d 41 *
Kojto 148:fd96258d940d 42 * @defgroup app_scheduler Scheduler
Kojto 148:fd96258d940d 43 * @{
Kojto 148:fd96258d940d 44 * @ingroup app_common
Kojto 148:fd96258d940d 45 *
Kojto 148:fd96258d940d 46 * @brief The scheduler is used for transferring execution from the interrupt context to the main
Kojto 148:fd96258d940d 47 * context.
Kojto 148:fd96258d940d 48 *
Kojto 148:fd96258d940d 49 * @details See @ref seq_diagrams_sched for sequence diagrams illustrating the flow of events
Kojto 148:fd96258d940d 50 * when using the Scheduler.
Kojto 148:fd96258d940d 51 *
Kojto 148:fd96258d940d 52 * @section app_scheduler_req Requirements:
Kojto 148:fd96258d940d 53 *
Kojto 148:fd96258d940d 54 * @subsection main_context_logic Logic in main context:
Kojto 148:fd96258d940d 55 *
Kojto 148:fd96258d940d 56 * - Define an event handler for each type of event expected.
Kojto 148:fd96258d940d 57 * - Initialize the scheduler by calling the APP_SCHED_INIT() macro before entering the
Kojto 148:fd96258d940d 58 * application main loop.
Kojto 148:fd96258d940d 59 * - Call app_sched_execute() from the main loop each time the application wakes up because of an
Kojto 148:fd96258d940d 60 * event (typically when sd_app_evt_wait() returns).
Kojto 148:fd96258d940d 61 *
Kojto 148:fd96258d940d 62 * @subsection int_context_logic Logic in interrupt context:
Kojto 148:fd96258d940d 63 *
Kojto 148:fd96258d940d 64 * - In the interrupt handler, call app_sched_event_put()
Kojto 148:fd96258d940d 65 * with the appropriate data and event handler. This will insert an event into the
Kojto 148:fd96258d940d 66 * scheduler's queue. The app_sched_execute() function will pull this event and call its
Kojto 148:fd96258d940d 67 * handler in the main context.
Kojto 148:fd96258d940d 68 *
Kojto 148:fd96258d940d 69 * @if (PERIPHERAL)
Kojto 148:fd96258d940d 70 * For an example usage of the scheduler, see the implementations of
Kojto 148:fd96258d940d 71 * @ref ble_sdk_app_hids_mouse and @ref ble_sdk_app_hids_keyboard.
Kojto 148:fd96258d940d 72 * @endif
Kojto 148:fd96258d940d 73 *
Kojto 148:fd96258d940d 74 * @image html scheduler_working.jpg The high level design of the scheduler
Kojto 148:fd96258d940d 75 */
Kojto 148:fd96258d940d 76
Kojto 148:fd96258d940d 77 #ifndef APP_SCHEDULER_H__
Kojto 148:fd96258d940d 78 #define APP_SCHEDULER_H__
Kojto 148:fd96258d940d 79
Kojto 148:fd96258d940d 80 #include <stdint.h>
Kojto 148:fd96258d940d 81 #include "app_error.h"
Kojto 148:fd96258d940d 82 #include "app_util.h"
Kojto 148:fd96258d940d 83
Kojto 148:fd96258d940d 84 #define APP_SCHED_EVENT_HEADER_SIZE 8 /**< Size of app_scheduler.event_header_t (only for use inside APP_SCHED_BUF_SIZE()). */
Kojto 148:fd96258d940d 85
Kojto 148:fd96258d940d 86 /**@brief Compute number of bytes required to hold the scheduler buffer.
Kojto 148:fd96258d940d 87 *
Kojto 148:fd96258d940d 88 * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
Kojto 148:fd96258d940d 89 * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
Kojto 148:fd96258d940d 90 * that can be scheduled for execution).
Kojto 148:fd96258d940d 91 *
Kojto 148:fd96258d940d 92 * @return Required scheduler buffer size (in bytes).
Kojto 148:fd96258d940d 93 */
Kojto 148:fd96258d940d 94 #define APP_SCHED_BUF_SIZE(EVENT_SIZE, QUEUE_SIZE) \
Kojto 148:fd96258d940d 95 (((EVENT_SIZE) + APP_SCHED_EVENT_HEADER_SIZE) * ((QUEUE_SIZE) + 1))
Kojto 148:fd96258d940d 96
Kojto 148:fd96258d940d 97 /**@brief Scheduler event handler type. */
Kojto 148:fd96258d940d 98 typedef void (*app_sched_event_handler_t)(void * p_event_data, uint16_t event_size);
Kojto 148:fd96258d940d 99
Kojto 148:fd96258d940d 100 /**@brief Macro for initializing the event scheduler.
Kojto 148:fd96258d940d 101 *
Kojto 148:fd96258d940d 102 * @details It will also handle dimensioning and allocation of the memory buffer required by the
Kojto 148:fd96258d940d 103 * scheduler, making sure the buffer is correctly aligned.
Kojto 148:fd96258d940d 104 *
Kojto 148:fd96258d940d 105 * @param[in] EVENT_SIZE Maximum size of events to be passed through the scheduler.
Kojto 148:fd96258d940d 106 * @param[in] QUEUE_SIZE Number of entries in scheduler queue (i.e. the maximum number of events
Kojto 148:fd96258d940d 107 * that can be scheduled for execution).
Kojto 148:fd96258d940d 108 *
Kojto 148:fd96258d940d 109 * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
Kojto 148:fd96258d940d 110 * several times as long as it is from the same location, e.g. to do a reinitialization).
Kojto 148:fd96258d940d 111 */
Kojto 148:fd96258d940d 112 #define APP_SCHED_INIT(EVENT_SIZE, QUEUE_SIZE) \
Kojto 148:fd96258d940d 113 do \
Kojto 148:fd96258d940d 114 { \
Kojto 148:fd96258d940d 115 static uint32_t APP_SCHED_BUF[CEIL_DIV(APP_SCHED_BUF_SIZE((EVENT_SIZE), (QUEUE_SIZE)), \
Kojto 148:fd96258d940d 116 sizeof(uint32_t))]; \
Kojto 148:fd96258d940d 117 uint32_t ERR_CODE = app_sched_init((EVENT_SIZE), (QUEUE_SIZE), APP_SCHED_BUF); \
Kojto 148:fd96258d940d 118 APP_ERROR_CHECK(ERR_CODE); \
Kojto 148:fd96258d940d 119 } while (0)
Kojto 148:fd96258d940d 120
Kojto 148:fd96258d940d 121 /**@brief Function for initializing the Scheduler.
Kojto 148:fd96258d940d 122 *
Kojto 148:fd96258d940d 123 * @details It must be called before entering the main loop.
Kojto 148:fd96258d940d 124 *
Kojto 148:fd96258d940d 125 * @param[in] max_event_size Maximum size of events to be passed through the scheduler.
Kojto 148:fd96258d940d 126 * @param[in] queue_size Number of entries in scheduler queue (i.e. the maximum number of
Kojto 148:fd96258d940d 127 * events that can be scheduled for execution).
Kojto 148:fd96258d940d 128 * @param[in] p_evt_buffer Pointer to memory buffer for holding the scheduler queue. It must
Kojto 148:fd96258d940d 129 * be dimensioned using the APP_SCHED_BUFFER_SIZE() macro. The buffer
Kojto 148:fd96258d940d 130 * must be aligned to a 4 byte boundary.
Kojto 148:fd96258d940d 131 *
Kojto 148:fd96258d940d 132 * @note Normally initialization should be done using the APP_SCHED_INIT() macro, as that will both
Kojto 148:fd96258d940d 133 * allocate the scheduler buffer, and also align the buffer correctly.
Kojto 148:fd96258d940d 134 *
Kojto 148:fd96258d940d 135 * @retval NRF_SUCCESS Successful initialization.
Kojto 148:fd96258d940d 136 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
Kojto 148:fd96258d940d 137 * boundary).
Kojto 148:fd96258d940d 138 */
Kojto 148:fd96258d940d 139 uint32_t app_sched_init(uint16_t max_event_size, uint16_t queue_size, void * p_evt_buffer);
Kojto 148:fd96258d940d 140
Kojto 148:fd96258d940d 141 /**@brief Function for executing all scheduled events.
Kojto 148:fd96258d940d 142 *
Kojto 148:fd96258d940d 143 * @details This function must be called from within the main loop. It will execute all events
Kojto 148:fd96258d940d 144 * scheduled since the last time it was called.
Kojto 148:fd96258d940d 145 */
Kojto 148:fd96258d940d 146 void app_sched_execute(void);
Kojto 148:fd96258d940d 147
Kojto 148:fd96258d940d 148 /**@brief Function for scheduling an event.
Kojto 148:fd96258d940d 149 *
Kojto 148:fd96258d940d 150 * @details Puts an event into the event queue.
Kojto 148:fd96258d940d 151 *
Kojto 148:fd96258d940d 152 * @param[in] p_event_data Pointer to event data to be scheduled.
Kojto 148:fd96258d940d 153 * @param[in] event_size Size of event data to be scheduled.
Kojto 148:fd96258d940d 154 * @param[in] handler Event handler to receive the event.
Kojto 148:fd96258d940d 155 *
Kojto 148:fd96258d940d 156 * @return NRF_SUCCESS on success, otherwise an error code.
Kojto 148:fd96258d940d 157 */
Kojto 148:fd96258d940d 158 uint32_t app_sched_event_put(void * p_event_data,
Kojto 148:fd96258d940d 159 uint16_t event_size,
Kojto 148:fd96258d940d 160 app_sched_event_handler_t handler);
Kojto 148:fd96258d940d 161
Kojto 148:fd96258d940d 162 #ifdef APP_SCHEDULER_WITH_PROFILER
Kojto 148:fd96258d940d 163 /**@brief Function for getting the maximum observed queue utilization.
Kojto 148:fd96258d940d 164 *
Kojto 148:fd96258d940d 165 * Function for tuning the module and determining QUEUE_SIZE value and thus module RAM usage.
Kojto 148:fd96258d940d 166 *
Kojto 148:fd96258d940d 167 * @return Maximum number of events in queue observed so far.
Kojto 148:fd96258d940d 168 */
Kojto 148:fd96258d940d 169 uint16_t app_sched_queue_utilization_get(void);
Kojto 148:fd96258d940d 170 #endif
Kojto 148:fd96258d940d 171
Kojto 148:fd96258d940d 172 #ifdef APP_SCHEDULER_WITH_PAUSE
Kojto 148:fd96258d940d 173 /**@brief A function to pause the scheduler.
Kojto 148:fd96258d940d 174 *
Kojto 148:fd96258d940d 175 * @details When the scheduler is paused events are not pulled from the scheduler queue for
Kojto 148:fd96258d940d 176 * processing. The function can be called multiple times. To unblock the scheduler the
Kojto 148:fd96258d940d 177 * function @ref app_sched_resume has to be called the same number of times.
Kojto 148:fd96258d940d 178 */
Kojto 148:fd96258d940d 179 void app_sched_pause(void);
Kojto 148:fd96258d940d 180
Kojto 148:fd96258d940d 181 /**@brief A function to resume a scheduler.
Kojto 148:fd96258d940d 182 *
Kojto 148:fd96258d940d 183 * @details To unblock the scheduler this function has to be called the same number of times as
Kojto 148:fd96258d940d 184 * @ref app_sched_pause function.
Kojto 148:fd96258d940d 185 */
Kojto 148:fd96258d940d 186 void app_sched_resume(void);
Kojto 148:fd96258d940d 187 #endif
Kojto 148:fd96258d940d 188 #endif // APP_SCHEDULER_H__
Kojto 148:fd96258d940d 189
Kojto 148:fd96258d940d 190 /** @} */