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 #include "app_scheduler.h"
yihui 1:a607cd9655d7 14 #include <stdlib.h>
yihui 1:a607cd9655d7 15 #include <stdint.h>
yihui 1:a607cd9655d7 16 #include <string.h>
yihui 1:a607cd9655d7 17 #include "nrf_soc.h"
yihui 1:a607cd9655d7 18 #include "nrf_assert.h"
yihui 1:a607cd9655d7 19 #include "app_util.h"
yihui 1:a607cd9655d7 20 #include "app_util_platform.h"
yihui 1:a607cd9655d7 21
yihui 1:a607cd9655d7 22 /**@brief Structure for holding a scheduled event header. */
yihui 1:a607cd9655d7 23 typedef struct
yihui 1:a607cd9655d7 24 {
yihui 1:a607cd9655d7 25 app_sched_event_handler_t handler; /**< Pointer to event handler to receive the event. */
yihui 1:a607cd9655d7 26 uint16_t event_data_size; /**< Size of event data. */
yihui 1:a607cd9655d7 27 } event_header_t;
yihui 1:a607cd9655d7 28
yihui 1:a607cd9655d7 29 STATIC_ASSERT(sizeof(event_header_t) <= APP_SCHED_EVENT_HEADER_SIZE);
yihui 1:a607cd9655d7 30
yihui 1:a607cd9655d7 31 static event_header_t * m_queue_event_headers; /**< Array for holding the queue event headers. */
yihui 1:a607cd9655d7 32 static uint8_t * m_queue_event_data; /**< Array for holding the queue event data. */
yihui 1:a607cd9655d7 33 static volatile uint8_t m_queue_start_index; /**< Index of queue entry at the start of the queue. */
yihui 1:a607cd9655d7 34 static volatile uint8_t m_queue_end_index; /**< Index of queue entry at the end of the queue. */
yihui 1:a607cd9655d7 35 static uint16_t m_queue_event_size; /**< Maximum event size in queue. */
yihui 1:a607cd9655d7 36 static uint16_t m_queue_size; /**< Number of queue entries. */
yihui 1:a607cd9655d7 37
yihui 1:a607cd9655d7 38 /**@brief Macro for checking if a queue is full. */
yihui 1:a607cd9655d7 39 #define APP_SCHED_QUEUE_FULL() (next_index(m_queue_end_index) == m_queue_start_index)
yihui 1:a607cd9655d7 40
yihui 1:a607cd9655d7 41 /**@brief Macro for checking if a queue is empty. */
yihui 1:a607cd9655d7 42 #define APP_SCHED_QUEUE_EMPTY() (m_queue_end_index == m_queue_start_index)
yihui 1:a607cd9655d7 43
yihui 1:a607cd9655d7 44
yihui 1:a607cd9655d7 45 /**@brief Function for incrementing a queue index, and handle wrap-around.
yihui 1:a607cd9655d7 46 *
yihui 1:a607cd9655d7 47 * @param[in] index Old index.
yihui 1:a607cd9655d7 48 *
yihui 1:a607cd9655d7 49 * @return New (incremented) index.
yihui 1:a607cd9655d7 50 */
yihui 1:a607cd9655d7 51 static __INLINE uint8_t next_index(uint8_t index)
yihui 1:a607cd9655d7 52 {
yihui 1:a607cd9655d7 53 return (index < m_queue_size) ? (index + 1) : 0;
yihui 1:a607cd9655d7 54 }
yihui 1:a607cd9655d7 55
yihui 1:a607cd9655d7 56
yihui 1:a607cd9655d7 57 uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer)
yihui 1:a607cd9655d7 58 {
yihui 1:a607cd9655d7 59 uint16_t data_start_index = (queue_size + 1) * sizeof(event_header_t);
yihui 1:a607cd9655d7 60
yihui 1:a607cd9655d7 61 // Check that buffer is correctly aligned
yihui 1:a607cd9655d7 62 if (!is_word_aligned(p_event_buffer))
yihui 1:a607cd9655d7 63 {
yihui 1:a607cd9655d7 64 return NRF_ERROR_INVALID_PARAM;
yihui 1:a607cd9655d7 65 }
yihui 1:a607cd9655d7 66
yihui 1:a607cd9655d7 67 // Initialize event scheduler
yihui 1:a607cd9655d7 68 m_queue_event_headers = p_event_buffer;
yihui 1:a607cd9655d7 69 m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index];
yihui 1:a607cd9655d7 70 m_queue_end_index = 0;
yihui 1:a607cd9655d7 71 m_queue_start_index = 0;
yihui 1:a607cd9655d7 72 m_queue_event_size = event_size;
yihui 1:a607cd9655d7 73 m_queue_size = queue_size;
yihui 1:a607cd9655d7 74
yihui 1:a607cd9655d7 75 return NRF_SUCCESS;
yihui 1:a607cd9655d7 76 }
yihui 1:a607cd9655d7 77
yihui 1:a607cd9655d7 78
yihui 1:a607cd9655d7 79 uint32_t app_sched_event_put(void * p_event_data,
yihui 1:a607cd9655d7 80 uint16_t event_data_size,
yihui 1:a607cd9655d7 81 app_sched_event_handler_t handler)
yihui 1:a607cd9655d7 82 {
yihui 1:a607cd9655d7 83 uint32_t err_code;
yihui 1:a607cd9655d7 84
yihui 1:a607cd9655d7 85 if (event_data_size <= m_queue_event_size)
yihui 1:a607cd9655d7 86 {
yihui 1:a607cd9655d7 87 uint16_t event_index = 0xFFFF;
yihui 1:a607cd9655d7 88
yihui 1:a607cd9655d7 89 CRITICAL_REGION_ENTER();
yihui 1:a607cd9655d7 90
yihui 1:a607cd9655d7 91 if (!APP_SCHED_QUEUE_FULL())
yihui 1:a607cd9655d7 92 {
yihui 1:a607cd9655d7 93 event_index = m_queue_end_index;
yihui 1:a607cd9655d7 94 m_queue_end_index = next_index(m_queue_end_index);
yihui 1:a607cd9655d7 95 }
yihui 1:a607cd9655d7 96
yihui 1:a607cd9655d7 97 CRITICAL_REGION_EXIT();
yihui 1:a607cd9655d7 98
yihui 1:a607cd9655d7 99 if (event_index != 0xFFFF)
yihui 1:a607cd9655d7 100 {
yihui 1:a607cd9655d7 101 // NOTE: This can be done outside the critical region since the event consumer will
yihui 1:a607cd9655d7 102 // always be called from the main loop, and will thus never interrupt this code.
yihui 1:a607cd9655d7 103 m_queue_event_headers[event_index].handler = handler;
yihui 1:a607cd9655d7 104 if ((p_event_data != NULL) && (event_data_size > 0))
yihui 1:a607cd9655d7 105 {
yihui 1:a607cd9655d7 106 memcpy(&m_queue_event_data[event_index * m_queue_event_size],
yihui 1:a607cd9655d7 107 p_event_data,
yihui 1:a607cd9655d7 108 event_data_size);
yihui 1:a607cd9655d7 109 m_queue_event_headers[event_index].event_data_size = event_data_size;
yihui 1:a607cd9655d7 110 }
yihui 1:a607cd9655d7 111 else
yihui 1:a607cd9655d7 112 {
yihui 1:a607cd9655d7 113 m_queue_event_headers[event_index].event_data_size = 0;
yihui 1:a607cd9655d7 114 }
yihui 1:a607cd9655d7 115
yihui 1:a607cd9655d7 116 err_code = NRF_SUCCESS;
yihui 1:a607cd9655d7 117 }
yihui 1:a607cd9655d7 118 else
yihui 1:a607cd9655d7 119 {
yihui 1:a607cd9655d7 120 err_code = NRF_ERROR_NO_MEM;
yihui 1:a607cd9655d7 121 }
yihui 1:a607cd9655d7 122 }
yihui 1:a607cd9655d7 123 else
yihui 1:a607cd9655d7 124 {
yihui 1:a607cd9655d7 125 err_code = NRF_ERROR_INVALID_LENGTH;
yihui 1:a607cd9655d7 126 }
yihui 1:a607cd9655d7 127
yihui 1:a607cd9655d7 128 return err_code;
yihui 1:a607cd9655d7 129 }
yihui 1:a607cd9655d7 130
yihui 1:a607cd9655d7 131
yihui 1:a607cd9655d7 132 /**@brief Function for reading the next event from specified event queue.
yihui 1:a607cd9655d7 133 *
yihui 1:a607cd9655d7 134 * @param[out] pp_event_data Pointer to pointer to event data.
yihui 1:a607cd9655d7 135 * @param[out] p_event_data_size Pointer to size of event data.
yihui 1:a607cd9655d7 136 * @param[out] p_event_handler Pointer to event handler function pointer.
yihui 1:a607cd9655d7 137 *
yihui 1:a607cd9655d7 138 * @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty.
yihui 1:a607cd9655d7 139 */
yihui 1:a607cd9655d7 140 static uint32_t app_sched_event_get(void ** pp_event_data,
yihui 1:a607cd9655d7 141 uint16_t * p_event_data_size,
yihui 1:a607cd9655d7 142 app_sched_event_handler_t * p_event_handler)
yihui 1:a607cd9655d7 143 {
yihui 1:a607cd9655d7 144 uint32_t err_code = NRF_ERROR_NOT_FOUND;
yihui 1:a607cd9655d7 145
yihui 1:a607cd9655d7 146 if (!APP_SCHED_QUEUE_EMPTY())
yihui 1:a607cd9655d7 147 {
yihui 1:a607cd9655d7 148 uint16_t event_index;
yihui 1:a607cd9655d7 149
yihui 1:a607cd9655d7 150 // NOTE: There is no need for a critical region here, as this function will only be called
yihui 1:a607cd9655d7 151 // from app_sched_execute() from inside the main loop, so it will never interrupt
yihui 1:a607cd9655d7 152 // app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be
yihui 1:a607cd9655d7 153 // an atomic operation.
yihui 1:a607cd9655d7 154 event_index = m_queue_start_index;
yihui 1:a607cd9655d7 155 m_queue_start_index = next_index(m_queue_start_index);
yihui 1:a607cd9655d7 156
yihui 1:a607cd9655d7 157 *pp_event_data = &m_queue_event_data[event_index * m_queue_event_size];
yihui 1:a607cd9655d7 158 *p_event_data_size = m_queue_event_headers[event_index].event_data_size;
yihui 1:a607cd9655d7 159 *p_event_handler = m_queue_event_headers[event_index].handler;
yihui 1:a607cd9655d7 160
yihui 1:a607cd9655d7 161 err_code = NRF_SUCCESS;
yihui 1:a607cd9655d7 162 }
yihui 1:a607cd9655d7 163
yihui 1:a607cd9655d7 164 return err_code;
yihui 1:a607cd9655d7 165 }
yihui 1:a607cd9655d7 166
yihui 1:a607cd9655d7 167
yihui 1:a607cd9655d7 168 void app_sched_execute(void)
yihui 1:a607cd9655d7 169 {
yihui 1:a607cd9655d7 170 void * p_event_data;
yihui 1:a607cd9655d7 171 uint16_t event_data_size;
yihui 1:a607cd9655d7 172 app_sched_event_handler_t event_handler;
yihui 1:a607cd9655d7 173
yihui 1:a607cd9655d7 174 // Get next event (if any), and execute handler
yihui 1:a607cd9655d7 175 while ((app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS))
yihui 1:a607cd9655d7 176 {
yihui 1:a607cd9655d7 177 event_handler(p_event_data, event_data_size);
yihui 1:a607cd9655d7 178 }
yihui 1:a607cd9655d7 179 }