bugfix for duplicate symbol

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Thu Apr 30 08:34:37 2015 +0100
Revision:
112:737b08b3b995
Parent:
103:138bdc859cc9
Synchronized with git rev 4ae92f89
Author: Rohit Grover
porting to v8.0 of the SDK

Who changed what in which revision?

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