bugfix for duplicate symbol
Fork of nRF51822 by
nordic-sdk/components/libraries/scheduler/app_scheduler.c@177:7a1917171a20, 2015-05-15 (annotated)
- Committer:
- finneyj
- Date:
- Fri May 15 12:12:21 2015 +0000
- Revision:
- 177:7a1917171a20
- Parent:
- 103:138bdc859cc9
- Child:
- 112:737b08b3b995
bug fix - redefined symbol
Who changed what in which revision?
User | Revision | Line number | New 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 Macro for checking if a queue is full. */ |
rgrover1 | 103:138bdc859cc9 | 39 | #define APP_SCHED_QUEUE_FULL() (next_index(m_queue_end_index) == m_queue_start_index) |
rgrover1 | 103:138bdc859cc9 | 40 | |
rgrover1 | 103:138bdc859cc9 | 41 | /**@brief Macro for checking if a queue is empty. */ |
rgrover1 | 103:138bdc859cc9 | 42 | #define APP_SCHED_QUEUE_EMPTY() (m_queue_end_index == m_queue_start_index) |
rgrover1 | 103:138bdc859cc9 | 43 | |
rgrover1 | 103:138bdc859cc9 | 44 | |
rgrover1 | 103:138bdc859cc9 | 45 | /**@brief Function for incrementing a queue index, and handle wrap-around. |
rgrover1 | 103:138bdc859cc9 | 46 | * |
rgrover1 | 103:138bdc859cc9 | 47 | * @param[in] index Old index. |
rgrover1 | 103:138bdc859cc9 | 48 | * |
rgrover1 | 103:138bdc859cc9 | 49 | * @return New (incremented) index. |
rgrover1 | 103:138bdc859cc9 | 50 | */ |
rgrover1 | 103:138bdc859cc9 | 51 | static __INLINE uint8_t next_index(uint8_t index) |
rgrover1 | 103:138bdc859cc9 | 52 | { |
rgrover1 | 103:138bdc859cc9 | 53 | return (index < m_queue_size) ? (index + 1) : 0; |
rgrover1 | 103:138bdc859cc9 | 54 | } |
rgrover1 | 103:138bdc859cc9 | 55 | |
rgrover1 | 103:138bdc859cc9 | 56 | |
rgrover1 | 103:138bdc859cc9 | 57 | uint32_t app_sched_init(uint16_t event_size, uint16_t queue_size, void * p_event_buffer) |
rgrover1 | 103:138bdc859cc9 | 58 | { |
rgrover1 | 103:138bdc859cc9 | 59 | uint16_t data_start_index = (queue_size + 1) * sizeof(event_header_t); |
rgrover1 | 103:138bdc859cc9 | 60 | |
rgrover1 | 103:138bdc859cc9 | 61 | // Check that buffer is correctly aligned |
rgrover1 | 103:138bdc859cc9 | 62 | if (!is_word_aligned(p_event_buffer)) |
rgrover1 | 103:138bdc859cc9 | 63 | { |
rgrover1 | 103:138bdc859cc9 | 64 | return NRF_ERROR_INVALID_PARAM; |
rgrover1 | 103:138bdc859cc9 | 65 | } |
rgrover1 | 103:138bdc859cc9 | 66 | |
rgrover1 | 103:138bdc859cc9 | 67 | // Initialize event scheduler |
rgrover1 | 103:138bdc859cc9 | 68 | m_queue_event_headers = p_event_buffer; |
rgrover1 | 103:138bdc859cc9 | 69 | m_queue_event_data = &((uint8_t *)p_event_buffer)[data_start_index]; |
rgrover1 | 103:138bdc859cc9 | 70 | m_queue_end_index = 0; |
rgrover1 | 103:138bdc859cc9 | 71 | m_queue_start_index = 0; |
rgrover1 | 103:138bdc859cc9 | 72 | m_queue_event_size = event_size; |
rgrover1 | 103:138bdc859cc9 | 73 | m_queue_size = queue_size; |
rgrover1 | 103:138bdc859cc9 | 74 | |
rgrover1 | 103:138bdc859cc9 | 75 | return NRF_SUCCESS; |
rgrover1 | 103:138bdc859cc9 | 76 | } |
rgrover1 | 103:138bdc859cc9 | 77 | |
rgrover1 | 103:138bdc859cc9 | 78 | |
rgrover1 | 103:138bdc859cc9 | 79 | uint32_t app_sched_event_put(void * p_event_data, |
rgrover1 | 103:138bdc859cc9 | 80 | uint16_t event_data_size, |
rgrover1 | 103:138bdc859cc9 | 81 | app_sched_event_handler_t handler) |
rgrover1 | 103:138bdc859cc9 | 82 | { |
rgrover1 | 103:138bdc859cc9 | 83 | uint32_t err_code; |
rgrover1 | 103:138bdc859cc9 | 84 | |
rgrover1 | 103:138bdc859cc9 | 85 | if (event_data_size <= m_queue_event_size) |
rgrover1 | 103:138bdc859cc9 | 86 | { |
rgrover1 | 103:138bdc859cc9 | 87 | uint16_t event_index = 0xFFFF; |
rgrover1 | 103:138bdc859cc9 | 88 | |
rgrover1 | 103:138bdc859cc9 | 89 | CRITICAL_REGION_ENTER(); |
rgrover1 | 103:138bdc859cc9 | 90 | |
rgrover1 | 103:138bdc859cc9 | 91 | if (!APP_SCHED_QUEUE_FULL()) |
rgrover1 | 103:138bdc859cc9 | 92 | { |
rgrover1 | 103:138bdc859cc9 | 93 | event_index = m_queue_end_index; |
rgrover1 | 103:138bdc859cc9 | 94 | m_queue_end_index = next_index(m_queue_end_index); |
rgrover1 | 103:138bdc859cc9 | 95 | } |
rgrover1 | 103:138bdc859cc9 | 96 | |
rgrover1 | 103:138bdc859cc9 | 97 | CRITICAL_REGION_EXIT(); |
rgrover1 | 103:138bdc859cc9 | 98 | |
rgrover1 | 103:138bdc859cc9 | 99 | if (event_index != 0xFFFF) |
rgrover1 | 103:138bdc859cc9 | 100 | { |
rgrover1 | 103:138bdc859cc9 | 101 | // NOTE: This can be done outside the critical region since the event consumer will |
rgrover1 | 103:138bdc859cc9 | 102 | // always be called from the main loop, and will thus never interrupt this code. |
rgrover1 | 103:138bdc859cc9 | 103 | m_queue_event_headers[event_index].handler = handler; |
rgrover1 | 103:138bdc859cc9 | 104 | if ((p_event_data != NULL) && (event_data_size > 0)) |
rgrover1 | 103:138bdc859cc9 | 105 | { |
rgrover1 | 103:138bdc859cc9 | 106 | memcpy(&m_queue_event_data[event_index * m_queue_event_size], |
rgrover1 | 103:138bdc859cc9 | 107 | p_event_data, |
rgrover1 | 103:138bdc859cc9 | 108 | event_data_size); |
rgrover1 | 103:138bdc859cc9 | 109 | m_queue_event_headers[event_index].event_data_size = event_data_size; |
rgrover1 | 103:138bdc859cc9 | 110 | } |
rgrover1 | 103:138bdc859cc9 | 111 | else |
rgrover1 | 103:138bdc859cc9 | 112 | { |
rgrover1 | 103:138bdc859cc9 | 113 | m_queue_event_headers[event_index].event_data_size = 0; |
rgrover1 | 103:138bdc859cc9 | 114 | } |
rgrover1 | 103:138bdc859cc9 | 115 | |
rgrover1 | 103:138bdc859cc9 | 116 | err_code = NRF_SUCCESS; |
rgrover1 | 103:138bdc859cc9 | 117 | } |
rgrover1 | 103:138bdc859cc9 | 118 | else |
rgrover1 | 103:138bdc859cc9 | 119 | { |
rgrover1 | 103:138bdc859cc9 | 120 | err_code = NRF_ERROR_NO_MEM; |
rgrover1 | 103:138bdc859cc9 | 121 | } |
rgrover1 | 103:138bdc859cc9 | 122 | } |
rgrover1 | 103:138bdc859cc9 | 123 | else |
rgrover1 | 103:138bdc859cc9 | 124 | { |
rgrover1 | 103:138bdc859cc9 | 125 | err_code = NRF_ERROR_INVALID_LENGTH; |
rgrover1 | 103:138bdc859cc9 | 126 | } |
rgrover1 | 103:138bdc859cc9 | 127 | |
rgrover1 | 103:138bdc859cc9 | 128 | return err_code; |
rgrover1 | 103:138bdc859cc9 | 129 | } |
rgrover1 | 103:138bdc859cc9 | 130 | |
rgrover1 | 103:138bdc859cc9 | 131 | |
rgrover1 | 103:138bdc859cc9 | 132 | /**@brief Function for reading the next event from specified event queue. |
rgrover1 | 103:138bdc859cc9 | 133 | * |
rgrover1 | 103:138bdc859cc9 | 134 | * @param[out] pp_event_data Pointer to pointer to event data. |
rgrover1 | 103:138bdc859cc9 | 135 | * @param[out] p_event_data_size Pointer to size of event data. |
rgrover1 | 103:138bdc859cc9 | 136 | * @param[out] p_event_handler Pointer to event handler function pointer. |
rgrover1 | 103:138bdc859cc9 | 137 | * |
rgrover1 | 103:138bdc859cc9 | 138 | * @return NRF_SUCCESS if new event, NRF_ERROR_NOT_FOUND if event queue is empty. |
rgrover1 | 103:138bdc859cc9 | 139 | */ |
rgrover1 | 103:138bdc859cc9 | 140 | static uint32_t app_sched_event_get(void ** pp_event_data, |
rgrover1 | 103:138bdc859cc9 | 141 | uint16_t * p_event_data_size, |
rgrover1 | 103:138bdc859cc9 | 142 | app_sched_event_handler_t * p_event_handler) |
rgrover1 | 103:138bdc859cc9 | 143 | { |
rgrover1 | 103:138bdc859cc9 | 144 | uint32_t err_code = NRF_ERROR_NOT_FOUND; |
rgrover1 | 103:138bdc859cc9 | 145 | |
rgrover1 | 103:138bdc859cc9 | 146 | if (!APP_SCHED_QUEUE_EMPTY()) |
rgrover1 | 103:138bdc859cc9 | 147 | { |
rgrover1 | 103:138bdc859cc9 | 148 | uint16_t event_index; |
rgrover1 | 103:138bdc859cc9 | 149 | |
rgrover1 | 103:138bdc859cc9 | 150 | // NOTE: There is no need for a critical region here, as this function will only be called |
rgrover1 | 103:138bdc859cc9 | 151 | // from app_sched_execute() from inside the main loop, so it will never interrupt |
rgrover1 | 103:138bdc859cc9 | 152 | // app_sched_event_put(). Also, updating of (i.e. writing to) the start index will be |
rgrover1 | 103:138bdc859cc9 | 153 | // an atomic operation. |
rgrover1 | 103:138bdc859cc9 | 154 | event_index = m_queue_start_index; |
rgrover1 | 103:138bdc859cc9 | 155 | m_queue_start_index = next_index(m_queue_start_index); |
rgrover1 | 103:138bdc859cc9 | 156 | |
rgrover1 | 103:138bdc859cc9 | 157 | *pp_event_data = &m_queue_event_data[event_index * m_queue_event_size]; |
rgrover1 | 103:138bdc859cc9 | 158 | *p_event_data_size = m_queue_event_headers[event_index].event_data_size; |
rgrover1 | 103:138bdc859cc9 | 159 | *p_event_handler = m_queue_event_headers[event_index].handler; |
rgrover1 | 103:138bdc859cc9 | 160 | |
rgrover1 | 103:138bdc859cc9 | 161 | err_code = NRF_SUCCESS; |
rgrover1 | 103:138bdc859cc9 | 162 | } |
rgrover1 | 103:138bdc859cc9 | 163 | |
rgrover1 | 103:138bdc859cc9 | 164 | return err_code; |
rgrover1 | 103:138bdc859cc9 | 165 | } |
rgrover1 | 103:138bdc859cc9 | 166 | |
rgrover1 | 103:138bdc859cc9 | 167 | |
rgrover1 | 103:138bdc859cc9 | 168 | void app_sched_execute(void) |
rgrover1 | 103:138bdc859cc9 | 169 | { |
rgrover1 | 103:138bdc859cc9 | 170 | void * p_event_data; |
rgrover1 | 103:138bdc859cc9 | 171 | uint16_t event_data_size; |
rgrover1 | 103:138bdc859cc9 | 172 | app_sched_event_handler_t event_handler; |
rgrover1 | 103:138bdc859cc9 | 173 | |
rgrover1 | 103:138bdc859cc9 | 174 | // Get next event (if any), and execute handler |
rgrover1 | 103:138bdc859cc9 | 175 | while ((app_sched_event_get(&p_event_data, &event_data_size, &event_handler) == NRF_SUCCESS)) |
rgrover1 | 103:138bdc859cc9 | 176 | { |
rgrover1 | 103:138bdc859cc9 | 177 | event_handler(p_event_data, event_data_size); |
rgrover1 | 103:138bdc859cc9 | 178 | } |
rgrover1 | 103:138bdc859cc9 | 179 | } |