Nordic stack and drivers for the mbed BLE API

Dependents:   Sensen-classic-v8-IAQ_copy_4

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

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