project for nrf51822 qfab

Dependencies:   eddystone_URL mbed

Fork of eddystone_URL by vo dung

Committer:
tridung141196
Date:
Thu Nov 23 15:38:48 2017 +0000
Revision:
5:267bdacf5508
Parent:
0:76dfa9657d9d
ibeacon

Who changed what in which revision?

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