Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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