Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:40 2016 +0100
Revision:
19:47192cb9def7
Parent:
10:233fefd8162b
Child:
20:a90c48eb1d30
Synchronized with git rev 9251259f
Author: Liyou Zhou
Copy over coresponding files from nordic-sdk 9.0.0

Who changed what in which revision?

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