mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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