BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:a607cd9655d7 1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
yihui 1:a607cd9655d7 2 *
yihui 1:a607cd9655d7 3 * The information contained herein is property of Nordic Semiconductor ASA.
yihui 1:a607cd9655d7 4 * Terms and conditions of usage are described in detail in NORDIC
yihui 1:a607cd9655d7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
yihui 1:a607cd9655d7 6 *
yihui 1:a607cd9655d7 7 * Licensees are granted free, non-transferable use of the information. NO
yihui 1:a607cd9655d7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
yihui 1:a607cd9655d7 9 * the file.
yihui 1:a607cd9655d7 10 *
yihui 1:a607cd9655d7 11 */
yihui 1:a607cd9655d7 12
yihui 1:a607cd9655d7 13 /** @file
yihui 1:a607cd9655d7 14 *
yihui 1:a607cd9655d7 15 * @defgroup softdevice_handler SoftDevice Event Handler
yihui 1:a607cd9655d7 16 * @{
yihui 1:a607cd9655d7 17 * @ingroup app_common
yihui 1:a607cd9655d7 18 * @brief API for initializing and disabling the SoftDevice
yihui 1:a607cd9655d7 19 *
yihui 1:a607cd9655d7 20 * @details This API contains the functions and defines exposed by the @ref lib_softdevice_handler.
yihui 1:a607cd9655d7 21 * For more information on the library and how the application should use it, please refer
yihui 1:a607cd9655d7 22 * @ref lib_softdevice_handler.
yihui 1:a607cd9655d7 23 *
yihui 1:a607cd9655d7 24 * @note Use the USE_SCHEDULER parameter of the SOFTDEVICE_HANDLER_INIT() macro to select if
yihui 1:a607cd9655d7 25 * the @ref app_scheduler is to be used or not.
yihui 1:a607cd9655d7 26 *
yihui 1:a607cd9655d7 27 * @note Even if the scheduler is not used, softdevice_handler.h will include app_scheduler.h.
yihui 1:a607cd9655d7 28 * So when compiling, app_scheduler.h must be available in one of the compiler include
yihui 1:a607cd9655d7 29 * paths.
yihui 1:a607cd9655d7 30 */
yihui 1:a607cd9655d7 31
yihui 1:a607cd9655d7 32 #ifndef SOFTDEVICE_HANDLER_H__
yihui 1:a607cd9655d7 33 #define SOFTDEVICE_HANDLER_H__
yihui 1:a607cd9655d7 34
yihui 1:a607cd9655d7 35 #include <stdlib.h>
yihui 1:a607cd9655d7 36 #include "nordic_common.h"
yihui 1:a607cd9655d7 37 #include "nrf_sdm.h"
yihui 1:a607cd9655d7 38 #include "app_error.h"
yihui 1:a607cd9655d7 39 #include "app_scheduler.h"
yihui 1:a607cd9655d7 40 #include "app_util.h"
yihui 1:a607cd9655d7 41 #include "ble_stack_handler_types.h"
yihui 1:a607cd9655d7 42 #include "ant_stack_handler_types.h"
yihui 1:a607cd9655d7 43
yihui 1:a607cd9655d7 44 #ifdef __cplusplus
yihui 1:a607cd9655d7 45 extern "C" {
yihui 1:a607cd9655d7 46 #endif // #ifdef __cplusplus
yihui 1:a607cd9655d7 47
yihui 1:a607cd9655d7 48 #define SOFTDEVICE_SCHED_EVT_SIZE 0 /**< Size of button events being passed through the scheduler (is to be used for computing the maximum size of scheduler events). For SoftDevice events, this size is 0, since the events are being pulled in the event handler. */
yihui 1:a607cd9655d7 49 #define SYS_EVT_MSG_BUF_SIZE sizeof(uint32_t) /**< Size of System (SOC) event message buffer. */
yihui 1:a607cd9655d7 50
yihui 1:a607cd9655d7 51 /**@brief Type of function for passing events from the stack handler module to the scheduler. */
yihui 1:a607cd9655d7 52 typedef uint32_t (*softdevice_evt_schedule_func_t) (void);
yihui 1:a607cd9655d7 53
yihui 1:a607cd9655d7 54 /**@brief Application System (SOC) event handler type. */
yihui 1:a607cd9655d7 55 typedef void (*sys_evt_handler_t) (uint32_t evt_id);
yihui 1:a607cd9655d7 56
yihui 1:a607cd9655d7 57
yihui 1:a607cd9655d7 58 /**@brief Macro for initializing the stack event handler.
yihui 1:a607cd9655d7 59 *
yihui 1:a607cd9655d7 60 * @details It will handle dimensioning and allocation of the memory buffer required for reading
yihui 1:a607cd9655d7 61 * events from the stack, making sure the buffer is correctly aligned. It will also
yihui 1:a607cd9655d7 62 * connect the stack event handler to the scheduler (if specified).
yihui 1:a607cd9655d7 63 *
yihui 1:a607cd9655d7 64 * @param[in] CLOCK_SOURCE Low frequency clock source and accuracy (type nrf_clock_lfclksrc_t,
yihui 1:a607cd9655d7 65 * see sd_softdevice_enable() for details).
yihui 1:a607cd9655d7 66 * @param[in] USE_SCHEDULER TRUE if the application is using the event scheduler, FALSE
yihui 1:a607cd9655d7 67 * otherwise.
yihui 1:a607cd9655d7 68 *
yihui 1:a607cd9655d7 69 * @note Since this macro allocates a buffer, it must only be called once (it is OK to call it
yihui 1:a607cd9655d7 70 * several times as long as it is from the same location, that is to do a
yihui 1:a607cd9655d7 71 * reinitialization).
yihui 1:a607cd9655d7 72 */
yihui 1:a607cd9655d7 73 /*lint -emacro(506, SOFTDEVICE_HANDLER_INIT) */ /* Suppress "Constant value Boolean */
yihui 1:a607cd9655d7 74 #define SOFTDEVICE_HANDLER_INIT(CLOCK_SOURCE, \
yihui 1:a607cd9655d7 75 USE_SCHEDULER) \
yihui 1:a607cd9655d7 76 do \
yihui 1:a607cd9655d7 77 { \
yihui 1:a607cd9655d7 78 static uint32_t EVT_BUFFER[CEIL_DIV(MAX( \
yihui 1:a607cd9655d7 79 MAX(BLE_STACK_EVT_MSG_BUF_SIZE, \
yihui 1:a607cd9655d7 80 ANT_STACK_EVT_STRUCT_SIZE), \
yihui 1:a607cd9655d7 81 SYS_EVT_MSG_BUF_SIZE \
yihui 1:a607cd9655d7 82 ), \
yihui 1:a607cd9655d7 83 sizeof(uint32_t))]; \
yihui 1:a607cd9655d7 84 uint32_t ERR_CODE; \
yihui 1:a607cd9655d7 85 ERR_CODE = softdevice_handler_init((CLOCK_SOURCE), \
yihui 1:a607cd9655d7 86 EVT_BUFFER, \
yihui 1:a607cd9655d7 87 sizeof(EVT_BUFFER), \
yihui 1:a607cd9655d7 88 (USE_SCHEDULER) ? softdevice_evt_schedule : NULL); \
yihui 1:a607cd9655d7 89 APP_ERROR_CHECK(ERR_CODE); \
yihui 1:a607cd9655d7 90 } while (0)
yihui 1:a607cd9655d7 91
yihui 1:a607cd9655d7 92
yihui 1:a607cd9655d7 93 /**@brief Function for initializing the stack handler module.
yihui 1:a607cd9655d7 94 *
yihui 1:a607cd9655d7 95 * @details Enables the SoftDevice and the stack event interrupt handler.
yihui 1:a607cd9655d7 96 *
yihui 1:a607cd9655d7 97 * @note This function must be called before calling any function in the SoftDevice API.
yihui 1:a607cd9655d7 98 *
yihui 1:a607cd9655d7 99 * @note Normally initialization should be done using the SOFTDEVICE_HANDLER_INIT() macro,
yihui 1:a607cd9655d7 100 * as that will both allocate the event buffer, and also align the buffer correctly.
yihui 1:a607cd9655d7 101 *
yihui 1:a607cd9655d7 102 * @param[in] clock_source Low frequency clock source to be used by the SoftDevice.
yihui 1:a607cd9655d7 103 * @param[in] p_evt_buffer Buffer for holding one stack event. Since heap is not being
yihui 1:a607cd9655d7 104 * used, this buffer must be provided by the application. The
yihui 1:a607cd9655d7 105 * buffer must be large enough to hold the biggest stack event the
yihui 1:a607cd9655d7 106 * application is supposed to handle. The buffer must be aligned to
yihui 1:a607cd9655d7 107 * a 4 byte boundary. This parameter is unused if neither BLE nor
yihui 1:a607cd9655d7 108 * ANT stack support is required.
yihui 1:a607cd9655d7 109 * @param[in] evt_buffer_size Size of SoftDevice event buffer. This parameter is unused if
yihui 1:a607cd9655d7 110 * BLE stack support is not required.
yihui 1:a607cd9655d7 111 * @param[in] evt_schedule_func Function for passing events to the scheduler. Point to
yihui 1:a607cd9655d7 112 * ble_ant_stack_evt_schedule() to connect to the scheduler.
yihui 1:a607cd9655d7 113 * Set to NULL to make the stack handler module call the event
yihui 1:a607cd9655d7 114 * handler directly from the stack event interrupt handler.
yihui 1:a607cd9655d7 115 *
yihui 1:a607cd9655d7 116 * @retval NRF_SUCCESS Successful initialization.
yihui 1:a607cd9655d7 117 * @retval NRF_ERROR_INVALID_PARAM Invalid parameter (buffer not aligned to a 4 byte
yihui 1:a607cd9655d7 118 * boundary) or NULL.
yihui 1:a607cd9655d7 119 */
yihui 1:a607cd9655d7 120 uint32_t softdevice_handler_init(nrf_clock_lfclksrc_t clock_source,
yihui 1:a607cd9655d7 121 void * p_evt_buffer,
yihui 1:a607cd9655d7 122 uint16_t evt_buffer_size,
yihui 1:a607cd9655d7 123 softdevice_evt_schedule_func_t evt_schedule_func);
yihui 1:a607cd9655d7 124
yihui 1:a607cd9655d7 125
yihui 1:a607cd9655d7 126 /**@brief Function for disabling the SoftDevice.
yihui 1:a607cd9655d7 127 *
yihui 1:a607cd9655d7 128 * @details This function will disable the SoftDevice. It will also update the internal state
yihui 1:a607cd9655d7 129 * of this module.
yihui 1:a607cd9655d7 130 */
yihui 1:a607cd9655d7 131 uint32_t softdevice_handler_sd_disable(void);
yihui 1:a607cd9655d7 132
yihui 1:a607cd9655d7 133
yihui 1:a607cd9655d7 134 /**@brief Function for registering for System (SOC) events.
yihui 1:a607cd9655d7 135 *
yihui 1:a607cd9655d7 136 * @details The application should use this function to register for receiving System (SOC)
yihui 1:a607cd9655d7 137 * events from the SoftDevice. If the application does not call this function, then any
yihui 1:a607cd9655d7 138 * System (SOC) events that may be generated by the SoftDevice will NOT be fetched. Once
yihui 1:a607cd9655d7 139 * the application has registered for the events, it is not possible to possible to
yihui 1:a607cd9655d7 140 * cancel the registration. However, it is possible to register a different function for
yihui 1:a607cd9655d7 141 * handling the events at any point of time.
yihui 1:a607cd9655d7 142 *
yihui 1:a607cd9655d7 143 * @param[in] sys_evt_handler Function to be called for each received System (SOC) event.
yihui 1:a607cd9655d7 144 *
yihui 1:a607cd9655d7 145 * @retval NRF_SUCCESS Successful registration.
yihui 1:a607cd9655d7 146 * @retval NRF_ERROR_NULL Null pointer provided as input.
yihui 1:a607cd9655d7 147 */
yihui 1:a607cd9655d7 148 uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler);
yihui 1:a607cd9655d7 149
yihui 1:a607cd9655d7 150
yihui 1:a607cd9655d7 151 // Functions for connecting the Stack Event Handler to the scheduler:
yihui 1:a607cd9655d7 152 /**@cond NO_DOXYGEN */
yihui 1:a607cd9655d7 153 void intern_softdevice_events_execute(void);
yihui 1:a607cd9655d7 154
yihui 1:a607cd9655d7 155 static __INLINE void softdevice_evt_get(void * p_event_data, uint16_t event_size)
yihui 1:a607cd9655d7 156 {
yihui 1:a607cd9655d7 157 APP_ERROR_CHECK_BOOL(event_size == 0);
yihui 1:a607cd9655d7 158 intern_softdevice_events_execute();
yihui 1:a607cd9655d7 159 }
yihui 1:a607cd9655d7 160
yihui 1:a607cd9655d7 161 static __INLINE uint32_t softdevice_evt_schedule(void)
yihui 1:a607cd9655d7 162 {
yihui 1:a607cd9655d7 163 return app_sched_event_put(NULL, 0, softdevice_evt_get);
yihui 1:a607cd9655d7 164 }
yihui 1:a607cd9655d7 165 /**@endcond */
yihui 1:a607cd9655d7 166
yihui 1:a607cd9655d7 167 #ifdef __cplusplus
yihui 1:a607cd9655d7 168 }
yihui 1:a607cd9655d7 169 #endif // #ifdef __cplusplus
yihui 1:a607cd9655d7 170
yihui 1:a607cd9655d7 171 #endif // SOFTDEVICE_HANDLER_H__
yihui 1:a607cd9655d7 172
yihui 1:a607cd9655d7 173 /** @} */