Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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