BA
/
BaBoRo_test2
Backup 1
mbed-os/hal/mbed_ticker_api.c@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* mbed Microcontroller Library |
borlanic | 0:02dd72d1d465 | 2 | * Copyright (c) 2015 ARM Limited |
borlanic | 0:02dd72d1d465 | 3 | * |
borlanic | 0:02dd72d1d465 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 5 | * you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 6 | * You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 7 | * |
borlanic | 0:02dd72d1d465 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 9 | * |
borlanic | 0:02dd72d1d465 | 10 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 13 | * See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 14 | * limitations under the License. |
borlanic | 0:02dd72d1d465 | 15 | */ |
borlanic | 0:02dd72d1d465 | 16 | #include <stdio.h> |
borlanic | 0:02dd72d1d465 | 17 | #include <stddef.h> |
borlanic | 0:02dd72d1d465 | 18 | #include "hal/ticker_api.h" |
borlanic | 0:02dd72d1d465 | 19 | #include "platform/mbed_critical.h" |
borlanic | 0:02dd72d1d465 | 20 | #include "mbed_assert.h" |
borlanic | 0:02dd72d1d465 | 21 | |
borlanic | 0:02dd72d1d465 | 22 | static void schedule_interrupt(const ticker_data_t *const ticker); |
borlanic | 0:02dd72d1d465 | 23 | static void update_present_time(const ticker_data_t *const ticker); |
borlanic | 0:02dd72d1d465 | 24 | |
borlanic | 0:02dd72d1d465 | 25 | /* |
borlanic | 0:02dd72d1d465 | 26 | * Initialize a ticker instance. |
borlanic | 0:02dd72d1d465 | 27 | */ |
borlanic | 0:02dd72d1d465 | 28 | static void initialize(const ticker_data_t *ticker) |
borlanic | 0:02dd72d1d465 | 29 | { |
borlanic | 0:02dd72d1d465 | 30 | // return if the queue has already been initialized, in that case the |
borlanic | 0:02dd72d1d465 | 31 | // interface used by the queue is already initialized. |
borlanic | 0:02dd72d1d465 | 32 | if (ticker->queue->initialized) { |
borlanic | 0:02dd72d1d465 | 33 | return; |
borlanic | 0:02dd72d1d465 | 34 | } |
borlanic | 0:02dd72d1d465 | 35 | |
borlanic | 0:02dd72d1d465 | 36 | ticker->interface->init(); |
borlanic | 0:02dd72d1d465 | 37 | |
borlanic | 0:02dd72d1d465 | 38 | const ticker_info_t *info = ticker->interface->get_info(); |
borlanic | 0:02dd72d1d465 | 39 | uint32_t frequency = info->frequency; |
borlanic | 0:02dd72d1d465 | 40 | if (info->frequency == 0) { |
borlanic | 0:02dd72d1d465 | 41 | MBED_ASSERT(0); |
borlanic | 0:02dd72d1d465 | 42 | frequency = 1000000; |
borlanic | 0:02dd72d1d465 | 43 | } |
borlanic | 0:02dd72d1d465 | 44 | |
borlanic | 0:02dd72d1d465 | 45 | uint32_t bits = info->bits; |
borlanic | 0:02dd72d1d465 | 46 | if ((info->bits > 32) || (info->bits < 4)) { |
borlanic | 0:02dd72d1d465 | 47 | MBED_ASSERT(0); |
borlanic | 0:02dd72d1d465 | 48 | bits = 32; |
borlanic | 0:02dd72d1d465 | 49 | } |
borlanic | 0:02dd72d1d465 | 50 | uint32_t max_delta = 0x7 << (bits - 4); // 7/16th |
borlanic | 0:02dd72d1d465 | 51 | uint64_t max_delta_us = |
borlanic | 0:02dd72d1d465 | 52 | ((uint64_t)max_delta * 1000000 + frequency - 1) / frequency; |
borlanic | 0:02dd72d1d465 | 53 | |
borlanic | 0:02dd72d1d465 | 54 | ticker->queue->event_handler = NULL; |
borlanic | 0:02dd72d1d465 | 55 | ticker->queue->head = NULL; |
borlanic | 0:02dd72d1d465 | 56 | ticker->queue->tick_last_read = ticker->interface->read(); |
borlanic | 0:02dd72d1d465 | 57 | ticker->queue->tick_remainder = 0; |
borlanic | 0:02dd72d1d465 | 58 | ticker->queue->frequency = frequency; |
borlanic | 0:02dd72d1d465 | 59 | ticker->queue->bitmask = ((uint64_t)1 << bits) - 1; |
borlanic | 0:02dd72d1d465 | 60 | ticker->queue->max_delta = max_delta; |
borlanic | 0:02dd72d1d465 | 61 | ticker->queue->max_delta_us = max_delta_us; |
borlanic | 0:02dd72d1d465 | 62 | ticker->queue->present_time = 0; |
borlanic | 0:02dd72d1d465 | 63 | ticker->queue->initialized = true; |
borlanic | 0:02dd72d1d465 | 64 | |
borlanic | 0:02dd72d1d465 | 65 | update_present_time(ticker); |
borlanic | 0:02dd72d1d465 | 66 | schedule_interrupt(ticker); |
borlanic | 0:02dd72d1d465 | 67 | } |
borlanic | 0:02dd72d1d465 | 68 | |
borlanic | 0:02dd72d1d465 | 69 | /** |
borlanic | 0:02dd72d1d465 | 70 | * Set the event handler function of a ticker instance. |
borlanic | 0:02dd72d1d465 | 71 | */ |
borlanic | 0:02dd72d1d465 | 72 | static void set_handler(const ticker_data_t *const ticker, ticker_event_handler handler) |
borlanic | 0:02dd72d1d465 | 73 | { |
borlanic | 0:02dd72d1d465 | 74 | ticker->queue->event_handler = handler; |
borlanic | 0:02dd72d1d465 | 75 | } |
borlanic | 0:02dd72d1d465 | 76 | |
borlanic | 0:02dd72d1d465 | 77 | /* |
borlanic | 0:02dd72d1d465 | 78 | * Convert a 32 bit timestamp into a 64 bit timestamp. |
borlanic | 0:02dd72d1d465 | 79 | * |
borlanic | 0:02dd72d1d465 | 80 | * A 64 bit timestamp is used as the point of time of reference while the |
borlanic | 0:02dd72d1d465 | 81 | * timestamp to convert is relative to this point of time. |
borlanic | 0:02dd72d1d465 | 82 | * |
borlanic | 0:02dd72d1d465 | 83 | * The lower 32 bits of the timestamp returned will be equal to the timestamp to |
borlanic | 0:02dd72d1d465 | 84 | * convert. |
borlanic | 0:02dd72d1d465 | 85 | * |
borlanic | 0:02dd72d1d465 | 86 | * If the timestamp to convert is less than the lower 32 bits of the time |
borlanic | 0:02dd72d1d465 | 87 | * reference then the timestamp to convert is seen as an overflowed value and |
borlanic | 0:02dd72d1d465 | 88 | * the upper 32 bit of the timestamp returned will be equal to the upper 32 bit |
borlanic | 0:02dd72d1d465 | 89 | * of the reference point + 1. |
borlanic | 0:02dd72d1d465 | 90 | * Otherwise, the upper 32 bit returned will be equal to the upper 32 bit of the |
borlanic | 0:02dd72d1d465 | 91 | * reference point. |
borlanic | 0:02dd72d1d465 | 92 | * |
borlanic | 0:02dd72d1d465 | 93 | * @param ref: The 64 bit timestamp of reference. |
borlanic | 0:02dd72d1d465 | 94 | * @param timestamp: The timestamp to convert. |
borlanic | 0:02dd72d1d465 | 95 | */ |
borlanic | 0:02dd72d1d465 | 96 | static us_timestamp_t convert_timestamp(us_timestamp_t ref, timestamp_t timestamp) |
borlanic | 0:02dd72d1d465 | 97 | { |
borlanic | 0:02dd72d1d465 | 98 | bool overflow = timestamp < ((timestamp_t) ref) ? true : false; |
borlanic | 0:02dd72d1d465 | 99 | |
borlanic | 0:02dd72d1d465 | 100 | us_timestamp_t result = (ref & ~((us_timestamp_t)UINT32_MAX)) | timestamp; |
borlanic | 0:02dd72d1d465 | 101 | if (overflow) { |
borlanic | 0:02dd72d1d465 | 102 | result += (1ULL<<32); |
borlanic | 0:02dd72d1d465 | 103 | } |
borlanic | 0:02dd72d1d465 | 104 | |
borlanic | 0:02dd72d1d465 | 105 | return result; |
borlanic | 0:02dd72d1d465 | 106 | } |
borlanic | 0:02dd72d1d465 | 107 | |
borlanic | 0:02dd72d1d465 | 108 | /** |
borlanic | 0:02dd72d1d465 | 109 | * Update the present timestamp value of a ticker. |
borlanic | 0:02dd72d1d465 | 110 | */ |
borlanic | 0:02dd72d1d465 | 111 | static void update_present_time(const ticker_data_t *const ticker) |
borlanic | 0:02dd72d1d465 | 112 | { |
borlanic | 0:02dd72d1d465 | 113 | ticker_event_queue_t *queue = ticker->queue; |
borlanic | 0:02dd72d1d465 | 114 | uint32_t ticker_time = ticker->interface->read(); |
borlanic | 0:02dd72d1d465 | 115 | if (ticker_time == ticker->queue->tick_last_read) { |
borlanic | 0:02dd72d1d465 | 116 | // No work to do |
borlanic | 0:02dd72d1d465 | 117 | return; |
borlanic | 0:02dd72d1d465 | 118 | } |
borlanic | 0:02dd72d1d465 | 119 | |
borlanic | 0:02dd72d1d465 | 120 | uint64_t elapsed_ticks = (ticker_time - queue->tick_last_read) & queue->bitmask; |
borlanic | 0:02dd72d1d465 | 121 | queue->tick_last_read = ticker_time; |
borlanic | 0:02dd72d1d465 | 122 | |
borlanic | 0:02dd72d1d465 | 123 | uint64_t elapsed_us; |
borlanic | 0:02dd72d1d465 | 124 | if (1000000 == queue->frequency) { |
borlanic | 0:02dd72d1d465 | 125 | // Optimized for 1MHz |
borlanic | 0:02dd72d1d465 | 126 | |
borlanic | 0:02dd72d1d465 | 127 | elapsed_us = elapsed_ticks; |
borlanic | 0:02dd72d1d465 | 128 | } else if (32768 == queue->frequency) { |
borlanic | 0:02dd72d1d465 | 129 | // Optimized for 32KHz |
borlanic | 0:02dd72d1d465 | 130 | |
borlanic | 0:02dd72d1d465 | 131 | uint64_t us_x_ticks = elapsed_ticks * 1000000; |
borlanic | 0:02dd72d1d465 | 132 | elapsed_us = us_x_ticks >> 15; |
borlanic | 0:02dd72d1d465 | 133 | |
borlanic | 0:02dd72d1d465 | 134 | // Update remainder |
borlanic | 0:02dd72d1d465 | 135 | queue->tick_remainder += us_x_ticks - (elapsed_us << 15); |
borlanic | 0:02dd72d1d465 | 136 | if (queue->tick_remainder >= queue->frequency) { |
borlanic | 0:02dd72d1d465 | 137 | elapsed_us += 1; |
borlanic | 0:02dd72d1d465 | 138 | queue->tick_remainder -= queue->frequency; |
borlanic | 0:02dd72d1d465 | 139 | } |
borlanic | 0:02dd72d1d465 | 140 | } else { |
borlanic | 0:02dd72d1d465 | 141 | // General case |
borlanic | 0:02dd72d1d465 | 142 | |
borlanic | 0:02dd72d1d465 | 143 | uint64_t us_x_ticks = elapsed_ticks * 1000000; |
borlanic | 0:02dd72d1d465 | 144 | elapsed_us = us_x_ticks / queue->frequency; |
borlanic | 0:02dd72d1d465 | 145 | |
borlanic | 0:02dd72d1d465 | 146 | // Update remainder |
borlanic | 0:02dd72d1d465 | 147 | queue->tick_remainder += us_x_ticks - elapsed_us * queue->frequency; |
borlanic | 0:02dd72d1d465 | 148 | if (queue->tick_remainder >= queue->frequency) { |
borlanic | 0:02dd72d1d465 | 149 | elapsed_us += 1; |
borlanic | 0:02dd72d1d465 | 150 | queue->tick_remainder -= queue->frequency; |
borlanic | 0:02dd72d1d465 | 151 | } |
borlanic | 0:02dd72d1d465 | 152 | } |
borlanic | 0:02dd72d1d465 | 153 | |
borlanic | 0:02dd72d1d465 | 154 | // Update current time |
borlanic | 0:02dd72d1d465 | 155 | queue->present_time += elapsed_us; |
borlanic | 0:02dd72d1d465 | 156 | } |
borlanic | 0:02dd72d1d465 | 157 | |
borlanic | 0:02dd72d1d465 | 158 | /** |
borlanic | 0:02dd72d1d465 | 159 | * Given the absolute timestamp compute the hal tick timestamp. |
borlanic | 0:02dd72d1d465 | 160 | */ |
borlanic | 0:02dd72d1d465 | 161 | static timestamp_t compute_tick(const ticker_data_t *const ticker, us_timestamp_t timestamp) |
borlanic | 0:02dd72d1d465 | 162 | { |
borlanic | 0:02dd72d1d465 | 163 | ticker_event_queue_t *queue = ticker->queue; |
borlanic | 0:02dd72d1d465 | 164 | us_timestamp_t delta_us = timestamp - queue->present_time; |
borlanic | 0:02dd72d1d465 | 165 | |
borlanic | 0:02dd72d1d465 | 166 | timestamp_t delta = ticker->queue->max_delta; |
borlanic | 0:02dd72d1d465 | 167 | if (delta_us <= ticker->queue->max_delta_us) { |
borlanic | 0:02dd72d1d465 | 168 | // Checking max_delta_us ensures the operation will not overflow |
borlanic | 0:02dd72d1d465 | 169 | |
borlanic | 0:02dd72d1d465 | 170 | if (1000000 == queue->frequency) { |
borlanic | 0:02dd72d1d465 | 171 | // Optimized for 1MHz |
borlanic | 0:02dd72d1d465 | 172 | |
borlanic | 0:02dd72d1d465 | 173 | delta = delta_us; |
borlanic | 0:02dd72d1d465 | 174 | if (delta > ticker->queue->max_delta) { |
borlanic | 0:02dd72d1d465 | 175 | delta = ticker->queue->max_delta; |
borlanic | 0:02dd72d1d465 | 176 | } |
borlanic | 0:02dd72d1d465 | 177 | } else if (32768 == queue->frequency) { |
borlanic | 0:02dd72d1d465 | 178 | // Optimized for 32KHz |
borlanic | 0:02dd72d1d465 | 179 | |
borlanic | 0:02dd72d1d465 | 180 | delta = (delta_us << 15) / 1000000; |
borlanic | 0:02dd72d1d465 | 181 | if (delta > ticker->queue->max_delta) { |
borlanic | 0:02dd72d1d465 | 182 | delta = ticker->queue->max_delta; |
borlanic | 0:02dd72d1d465 | 183 | } |
borlanic | 0:02dd72d1d465 | 184 | } else { |
borlanic | 0:02dd72d1d465 | 185 | // General case |
borlanic | 0:02dd72d1d465 | 186 | |
borlanic | 0:02dd72d1d465 | 187 | delta = delta_us * queue->frequency / 1000000; |
borlanic | 0:02dd72d1d465 | 188 | if (delta > ticker->queue->max_delta) { |
borlanic | 0:02dd72d1d465 | 189 | delta = ticker->queue->max_delta; |
borlanic | 0:02dd72d1d465 | 190 | } |
borlanic | 0:02dd72d1d465 | 191 | } |
borlanic | 0:02dd72d1d465 | 192 | } |
borlanic | 0:02dd72d1d465 | 193 | return (queue->tick_last_read + delta) & queue->bitmask; |
borlanic | 0:02dd72d1d465 | 194 | } |
borlanic | 0:02dd72d1d465 | 195 | |
borlanic | 0:02dd72d1d465 | 196 | /** |
borlanic | 0:02dd72d1d465 | 197 | * Return 1 if the tick has incremented to or past match_tick, otherwise 0. |
borlanic | 0:02dd72d1d465 | 198 | */ |
borlanic | 0:02dd72d1d465 | 199 | int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, timestamp_t match_tick) |
borlanic | 0:02dd72d1d465 | 200 | { |
borlanic | 0:02dd72d1d465 | 201 | if (match_tick > prev_tick) { |
borlanic | 0:02dd72d1d465 | 202 | return (cur_tick >= match_tick) || (cur_tick < prev_tick); |
borlanic | 0:02dd72d1d465 | 203 | } else { |
borlanic | 0:02dd72d1d465 | 204 | return (cur_tick < prev_tick) && (cur_tick >= match_tick); |
borlanic | 0:02dd72d1d465 | 205 | } |
borlanic | 0:02dd72d1d465 | 206 | } |
borlanic | 0:02dd72d1d465 | 207 | |
borlanic | 0:02dd72d1d465 | 208 | /** |
borlanic | 0:02dd72d1d465 | 209 | * Compute the time when the interrupt has to be triggered and schedule it. |
borlanic | 0:02dd72d1d465 | 210 | * |
borlanic | 0:02dd72d1d465 | 211 | * If there is no event in the queue or the next event to execute is in more |
borlanic | 0:02dd72d1d465 | 212 | * than ticker.queue.max_delta ticks from now then the ticker irq will be |
borlanic | 0:02dd72d1d465 | 213 | * scheduled in ticker.queue.max_delta ticks. Otherwise the irq will be |
borlanic | 0:02dd72d1d465 | 214 | * scheduled to happen when the running counter reach the timestamp of the |
borlanic | 0:02dd72d1d465 | 215 | * first event in the queue. |
borlanic | 0:02dd72d1d465 | 216 | * |
borlanic | 0:02dd72d1d465 | 217 | * @note If there is no event in the queue then the interrupt is scheduled to |
borlanic | 0:02dd72d1d465 | 218 | * in ticker.queue.max_delta. This is necessary to keep track |
borlanic | 0:02dd72d1d465 | 219 | * of the timer overflow. |
borlanic | 0:02dd72d1d465 | 220 | */ |
borlanic | 0:02dd72d1d465 | 221 | static void schedule_interrupt(const ticker_data_t *const ticker) |
borlanic | 0:02dd72d1d465 | 222 | { |
borlanic | 0:02dd72d1d465 | 223 | ticker_event_queue_t *queue = ticker->queue; |
borlanic | 0:02dd72d1d465 | 224 | update_present_time(ticker); |
borlanic | 0:02dd72d1d465 | 225 | |
borlanic | 0:02dd72d1d465 | 226 | if (ticker->queue->head) { |
borlanic | 0:02dd72d1d465 | 227 | us_timestamp_t present = ticker->queue->present_time; |
borlanic | 0:02dd72d1d465 | 228 | us_timestamp_t match_time = ticker->queue->head->timestamp; |
borlanic | 0:02dd72d1d465 | 229 | |
borlanic | 0:02dd72d1d465 | 230 | // if the event at the head of the queue is in the past then schedule |
borlanic | 0:02dd72d1d465 | 231 | // it immediately. |
borlanic | 0:02dd72d1d465 | 232 | if (match_time <= present) { |
borlanic | 0:02dd72d1d465 | 233 | ticker->interface->fire_interrupt(); |
borlanic | 0:02dd72d1d465 | 234 | return; |
borlanic | 0:02dd72d1d465 | 235 | } |
borlanic | 0:02dd72d1d465 | 236 | |
borlanic | 0:02dd72d1d465 | 237 | timestamp_t match_tick = compute_tick(ticker, match_time); |
borlanic | 0:02dd72d1d465 | 238 | // The time has been checked to be future, but it could still round |
borlanic | 0:02dd72d1d465 | 239 | // to the last tick as a result of us to ticks conversion |
borlanic | 0:02dd72d1d465 | 240 | if (match_tick == queue->tick_last_read) { |
borlanic | 0:02dd72d1d465 | 241 | // Match time has already expired so fire immediately |
borlanic | 0:02dd72d1d465 | 242 | ticker->interface->fire_interrupt(); |
borlanic | 0:02dd72d1d465 | 243 | return; |
borlanic | 0:02dd72d1d465 | 244 | } |
borlanic | 0:02dd72d1d465 | 245 | |
borlanic | 0:02dd72d1d465 | 246 | ticker->interface->set_interrupt(match_tick); |
borlanic | 0:02dd72d1d465 | 247 | timestamp_t cur_tick = ticker->interface->read(); |
borlanic | 0:02dd72d1d465 | 248 | |
borlanic | 0:02dd72d1d465 | 249 | if (_ticker_match_interval_passed(queue->tick_last_read, cur_tick, match_tick)) { |
borlanic | 0:02dd72d1d465 | 250 | ticker->interface->fire_interrupt(); |
borlanic | 0:02dd72d1d465 | 251 | } |
borlanic | 0:02dd72d1d465 | 252 | } else { |
borlanic | 0:02dd72d1d465 | 253 | uint32_t match_tick = |
borlanic | 0:02dd72d1d465 | 254 | (queue->tick_last_read + queue->max_delta) & queue->bitmask; |
borlanic | 0:02dd72d1d465 | 255 | ticker->interface->set_interrupt(match_tick); |
borlanic | 0:02dd72d1d465 | 256 | } |
borlanic | 0:02dd72d1d465 | 257 | } |
borlanic | 0:02dd72d1d465 | 258 | |
borlanic | 0:02dd72d1d465 | 259 | void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler) |
borlanic | 0:02dd72d1d465 | 260 | { |
borlanic | 0:02dd72d1d465 | 261 | initialize(ticker); |
borlanic | 0:02dd72d1d465 | 262 | |
borlanic | 0:02dd72d1d465 | 263 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 264 | set_handler(ticker, handler); |
borlanic | 0:02dd72d1d465 | 265 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 266 | } |
borlanic | 0:02dd72d1d465 | 267 | |
borlanic | 0:02dd72d1d465 | 268 | void ticker_irq_handler(const ticker_data_t *const ticker) |
borlanic | 0:02dd72d1d465 | 269 | { |
borlanic | 0:02dd72d1d465 | 270 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 271 | |
borlanic | 0:02dd72d1d465 | 272 | ticker->interface->clear_interrupt(); |
borlanic | 0:02dd72d1d465 | 273 | |
borlanic | 0:02dd72d1d465 | 274 | /* Go through all the pending TimerEvents */ |
borlanic | 0:02dd72d1d465 | 275 | while (1) { |
borlanic | 0:02dd72d1d465 | 276 | if (ticker->queue->head == NULL) { |
borlanic | 0:02dd72d1d465 | 277 | break; |
borlanic | 0:02dd72d1d465 | 278 | } |
borlanic | 0:02dd72d1d465 | 279 | |
borlanic | 0:02dd72d1d465 | 280 | // update the current timestamp used by the queue |
borlanic | 0:02dd72d1d465 | 281 | update_present_time(ticker); |
borlanic | 0:02dd72d1d465 | 282 | |
borlanic | 0:02dd72d1d465 | 283 | if (ticker->queue->head->timestamp <= ticker->queue->present_time) { |
borlanic | 0:02dd72d1d465 | 284 | // This event was in the past: |
borlanic | 0:02dd72d1d465 | 285 | // point to the following one and execute its handler |
borlanic | 0:02dd72d1d465 | 286 | ticker_event_t *p = ticker->queue->head; |
borlanic | 0:02dd72d1d465 | 287 | ticker->queue->head = ticker->queue->head->next; |
borlanic | 0:02dd72d1d465 | 288 | if (ticker->queue->event_handler != NULL) { |
borlanic | 0:02dd72d1d465 | 289 | (*ticker->queue->event_handler)(p->id); // NOTE: the handler can set new events |
borlanic | 0:02dd72d1d465 | 290 | } |
borlanic | 0:02dd72d1d465 | 291 | /* Note: We continue back to examining the head because calling the |
borlanic | 0:02dd72d1d465 | 292 | * event handler may have altered the chain of pending events. */ |
borlanic | 0:02dd72d1d465 | 293 | } else { |
borlanic | 0:02dd72d1d465 | 294 | break; |
borlanic | 0:02dd72d1d465 | 295 | } |
borlanic | 0:02dd72d1d465 | 296 | } |
borlanic | 0:02dd72d1d465 | 297 | |
borlanic | 0:02dd72d1d465 | 298 | schedule_interrupt(ticker); |
borlanic | 0:02dd72d1d465 | 299 | |
borlanic | 0:02dd72d1d465 | 300 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 301 | } |
borlanic | 0:02dd72d1d465 | 302 | |
borlanic | 0:02dd72d1d465 | 303 | void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id) |
borlanic | 0:02dd72d1d465 | 304 | { |
borlanic | 0:02dd72d1d465 | 305 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 306 | |
borlanic | 0:02dd72d1d465 | 307 | // update the current timestamp |
borlanic | 0:02dd72d1d465 | 308 | update_present_time(ticker); |
borlanic | 0:02dd72d1d465 | 309 | us_timestamp_t absolute_timestamp = convert_timestamp( |
borlanic | 0:02dd72d1d465 | 310 | ticker->queue->present_time, |
borlanic | 0:02dd72d1d465 | 311 | timestamp |
borlanic | 0:02dd72d1d465 | 312 | ); |
borlanic | 0:02dd72d1d465 | 313 | |
borlanic | 0:02dd72d1d465 | 314 | // defer to ticker_insert_event_us |
borlanic | 0:02dd72d1d465 | 315 | ticker_insert_event_us( |
borlanic | 0:02dd72d1d465 | 316 | ticker, |
borlanic | 0:02dd72d1d465 | 317 | obj, absolute_timestamp, id |
borlanic | 0:02dd72d1d465 | 318 | ); |
borlanic | 0:02dd72d1d465 | 319 | |
borlanic | 0:02dd72d1d465 | 320 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 321 | } |
borlanic | 0:02dd72d1d465 | 322 | |
borlanic | 0:02dd72d1d465 | 323 | void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id) |
borlanic | 0:02dd72d1d465 | 324 | { |
borlanic | 0:02dd72d1d465 | 325 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 326 | |
borlanic | 0:02dd72d1d465 | 327 | // update the current timestamp |
borlanic | 0:02dd72d1d465 | 328 | update_present_time(ticker); |
borlanic | 0:02dd72d1d465 | 329 | |
borlanic | 0:02dd72d1d465 | 330 | // initialise our data |
borlanic | 0:02dd72d1d465 | 331 | obj->timestamp = timestamp; |
borlanic | 0:02dd72d1d465 | 332 | obj->id = id; |
borlanic | 0:02dd72d1d465 | 333 | |
borlanic | 0:02dd72d1d465 | 334 | /* Go through the list until we either reach the end, or find |
borlanic | 0:02dd72d1d465 | 335 | an element this should come before (which is possibly the |
borlanic | 0:02dd72d1d465 | 336 | head). */ |
borlanic | 0:02dd72d1d465 | 337 | ticker_event_t *prev = NULL, *p = ticker->queue->head; |
borlanic | 0:02dd72d1d465 | 338 | while (p != NULL) { |
borlanic | 0:02dd72d1d465 | 339 | /* check if we come before p */ |
borlanic | 0:02dd72d1d465 | 340 | if (timestamp < p->timestamp) { |
borlanic | 0:02dd72d1d465 | 341 | break; |
borlanic | 0:02dd72d1d465 | 342 | } |
borlanic | 0:02dd72d1d465 | 343 | /* go to the next element */ |
borlanic | 0:02dd72d1d465 | 344 | prev = p; |
borlanic | 0:02dd72d1d465 | 345 | p = p->next; |
borlanic | 0:02dd72d1d465 | 346 | } |
borlanic | 0:02dd72d1d465 | 347 | |
borlanic | 0:02dd72d1d465 | 348 | /* if we're at the end p will be NULL, which is correct */ |
borlanic | 0:02dd72d1d465 | 349 | obj->next = p; |
borlanic | 0:02dd72d1d465 | 350 | |
borlanic | 0:02dd72d1d465 | 351 | /* if prev is NULL we're at the head */ |
borlanic | 0:02dd72d1d465 | 352 | if (prev == NULL) { |
borlanic | 0:02dd72d1d465 | 353 | ticker->queue->head = obj; |
borlanic | 0:02dd72d1d465 | 354 | } else { |
borlanic | 0:02dd72d1d465 | 355 | prev->next = obj; |
borlanic | 0:02dd72d1d465 | 356 | } |
borlanic | 0:02dd72d1d465 | 357 | |
borlanic | 0:02dd72d1d465 | 358 | schedule_interrupt(ticker); |
borlanic | 0:02dd72d1d465 | 359 | |
borlanic | 0:02dd72d1d465 | 360 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 361 | |
borlanic | 0:02dd72d1d465 | 362 | } |
borlanic | 0:02dd72d1d465 | 363 | |
borlanic | 0:02dd72d1d465 | 364 | void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj) |
borlanic | 0:02dd72d1d465 | 365 | { |
borlanic | 0:02dd72d1d465 | 366 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 367 | |
borlanic | 0:02dd72d1d465 | 368 | // remove this object from the list |
borlanic | 0:02dd72d1d465 | 369 | if (ticker->queue->head == obj) { |
borlanic | 0:02dd72d1d465 | 370 | // first in the list, so just drop me |
borlanic | 0:02dd72d1d465 | 371 | ticker->queue->head = obj->next; |
borlanic | 0:02dd72d1d465 | 372 | schedule_interrupt(ticker); |
borlanic | 0:02dd72d1d465 | 373 | } else { |
borlanic | 0:02dd72d1d465 | 374 | // find the object before me, then drop me |
borlanic | 0:02dd72d1d465 | 375 | ticker_event_t* p = ticker->queue->head; |
borlanic | 0:02dd72d1d465 | 376 | while (p != NULL) { |
borlanic | 0:02dd72d1d465 | 377 | if (p->next == obj) { |
borlanic | 0:02dd72d1d465 | 378 | p->next = obj->next; |
borlanic | 0:02dd72d1d465 | 379 | break; |
borlanic | 0:02dd72d1d465 | 380 | } |
borlanic | 0:02dd72d1d465 | 381 | p = p->next; |
borlanic | 0:02dd72d1d465 | 382 | } |
borlanic | 0:02dd72d1d465 | 383 | } |
borlanic | 0:02dd72d1d465 | 384 | |
borlanic | 0:02dd72d1d465 | 385 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 386 | } |
borlanic | 0:02dd72d1d465 | 387 | |
borlanic | 0:02dd72d1d465 | 388 | timestamp_t ticker_read(const ticker_data_t *const ticker) |
borlanic | 0:02dd72d1d465 | 389 | { |
borlanic | 0:02dd72d1d465 | 390 | return ticker_read_us(ticker); |
borlanic | 0:02dd72d1d465 | 391 | } |
borlanic | 0:02dd72d1d465 | 392 | |
borlanic | 0:02dd72d1d465 | 393 | us_timestamp_t ticker_read_us(const ticker_data_t *const ticker) |
borlanic | 0:02dd72d1d465 | 394 | { |
borlanic | 0:02dd72d1d465 | 395 | initialize(ticker); |
borlanic | 0:02dd72d1d465 | 396 | |
borlanic | 0:02dd72d1d465 | 397 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 398 | update_present_time(ticker); |
borlanic | 0:02dd72d1d465 | 399 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 400 | |
borlanic | 0:02dd72d1d465 | 401 | return ticker->queue->present_time; |
borlanic | 0:02dd72d1d465 | 402 | } |
borlanic | 0:02dd72d1d465 | 403 | |
borlanic | 0:02dd72d1d465 | 404 | int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp) |
borlanic | 0:02dd72d1d465 | 405 | { |
borlanic | 0:02dd72d1d465 | 406 | int ret = 0; |
borlanic | 0:02dd72d1d465 | 407 | |
borlanic | 0:02dd72d1d465 | 408 | /* if head is NULL, there are no pending events */ |
borlanic | 0:02dd72d1d465 | 409 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 410 | if (data->queue->head != NULL) { |
borlanic | 0:02dd72d1d465 | 411 | *timestamp = data->queue->head->timestamp; |
borlanic | 0:02dd72d1d465 | 412 | ret = 1; |
borlanic | 0:02dd72d1d465 | 413 | } |
borlanic | 0:02dd72d1d465 | 414 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 415 | |
borlanic | 0:02dd72d1d465 | 416 | return ret; |
borlanic | 0:02dd72d1d465 | 417 | } |