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