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