001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2015 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16 #include <stdio.h>
ganlikun 0:13413ea9a877 17 #include <stddef.h>
ganlikun 0:13413ea9a877 18 #include "hal/ticker_api.h"
ganlikun 0:13413ea9a877 19 #include "platform/mbed_critical.h"
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 static void schedule_interrupt(const ticker_data_t *const ticker);
ganlikun 0:13413ea9a877 22 static void update_present_time(const ticker_data_t *const ticker);
ganlikun 0:13413ea9a877 23
ganlikun 0:13413ea9a877 24 /*
ganlikun 0:13413ea9a877 25 * Initialize a ticker instance.
ganlikun 0:13413ea9a877 26 */
ganlikun 0:13413ea9a877 27 static void initialize(const ticker_data_t *ticker)
ganlikun 0:13413ea9a877 28 {
ganlikun 0:13413ea9a877 29 // return if the queue has already been initialized, in that case the
ganlikun 0:13413ea9a877 30 // interface used by the queue is already initialized.
ganlikun 0:13413ea9a877 31 if (ticker->queue->initialized) {
ganlikun 0:13413ea9a877 32 return;
ganlikun 0:13413ea9a877 33 }
ganlikun 0:13413ea9a877 34
ganlikun 0:13413ea9a877 35 ticker->interface->init();
ganlikun 0:13413ea9a877 36
ganlikun 0:13413ea9a877 37 ticker->queue->event_handler = NULL;
ganlikun 0:13413ea9a877 38 ticker->queue->head = NULL;
ganlikun 0:13413ea9a877 39 ticker->queue->present_time = 0;
ganlikun 0:13413ea9a877 40 ticker->queue->initialized = true;
ganlikun 0:13413ea9a877 41
ganlikun 0:13413ea9a877 42 update_present_time(ticker);
ganlikun 0:13413ea9a877 43 schedule_interrupt(ticker);
ganlikun 0:13413ea9a877 44 }
ganlikun 0:13413ea9a877 45
ganlikun 0:13413ea9a877 46 /**
ganlikun 0:13413ea9a877 47 * Set the event handler function of a ticker instance.
ganlikun 0:13413ea9a877 48 */
ganlikun 0:13413ea9a877 49 static void set_handler(const ticker_data_t *const ticker, ticker_event_handler handler)
ganlikun 0:13413ea9a877 50 {
ganlikun 0:13413ea9a877 51 ticker->queue->event_handler = handler;
ganlikun 0:13413ea9a877 52 }
ganlikun 0:13413ea9a877 53
ganlikun 0:13413ea9a877 54 /*
ganlikun 0:13413ea9a877 55 * Convert a 32 bit timestamp into a 64 bit timestamp.
ganlikun 0:13413ea9a877 56 *
ganlikun 0:13413ea9a877 57 * A 64 bit timestamp is used as the point of time of reference while the
ganlikun 0:13413ea9a877 58 * timestamp to convert is relative to this point of time.
ganlikun 0:13413ea9a877 59 *
ganlikun 0:13413ea9a877 60 * The lower 32 bits of the timestamp returned will be equal to the timestamp to
ganlikun 0:13413ea9a877 61 * convert.
ganlikun 0:13413ea9a877 62 *
ganlikun 0:13413ea9a877 63 * If the timestamp to convert is less than the lower 32 bits of the time
ganlikun 0:13413ea9a877 64 * reference then the timestamp to convert is seen as an overflowed value and
ganlikun 0:13413ea9a877 65 * the upper 32 bit of the timestamp returned will be equal to the upper 32 bit
ganlikun 0:13413ea9a877 66 * of the reference point + 1.
ganlikun 0:13413ea9a877 67 * Otherwise, the upper 32 bit returned will be equal to the upper 32 bit of the
ganlikun 0:13413ea9a877 68 * reference point.
ganlikun 0:13413ea9a877 69 *
ganlikun 0:13413ea9a877 70 * @param ref: The 64 bit timestamp of reference.
ganlikun 0:13413ea9a877 71 * @param timestamp: The timestamp to convert.
ganlikun 0:13413ea9a877 72 */
ganlikun 0:13413ea9a877 73 static us_timestamp_t convert_timestamp(us_timestamp_t ref, timestamp_t timestamp)
ganlikun 0:13413ea9a877 74 {
ganlikun 0:13413ea9a877 75 bool overflow = timestamp < ((timestamp_t) ref) ? true : false;
ganlikun 0:13413ea9a877 76
ganlikun 0:13413ea9a877 77 us_timestamp_t result = (ref & ~((us_timestamp_t)UINT32_MAX)) | timestamp;
ganlikun 0:13413ea9a877 78 if (overflow) {
ganlikun 0:13413ea9a877 79 result += (1ULL<<32);
ganlikun 0:13413ea9a877 80 }
ganlikun 0:13413ea9a877 81
ganlikun 0:13413ea9a877 82 return result;
ganlikun 0:13413ea9a877 83 }
ganlikun 0:13413ea9a877 84
ganlikun 0:13413ea9a877 85 /**
ganlikun 0:13413ea9a877 86 * Update the present timestamp value of a ticker.
ganlikun 0:13413ea9a877 87 */
ganlikun 0:13413ea9a877 88 static void update_present_time(const ticker_data_t *const ticker)
ganlikun 0:13413ea9a877 89 {
ganlikun 0:13413ea9a877 90 ticker->queue->present_time = convert_timestamp(
ganlikun 0:13413ea9a877 91 ticker->queue->present_time,
ganlikun 0:13413ea9a877 92 ticker->interface->read()
ganlikun 0:13413ea9a877 93 );
ganlikun 0:13413ea9a877 94 }
ganlikun 0:13413ea9a877 95
ganlikun 0:13413ea9a877 96 /**
ganlikun 0:13413ea9a877 97 * Compute the time when the interrupt has to be triggered and schedule it.
ganlikun 0:13413ea9a877 98 *
ganlikun 0:13413ea9a877 99 * If there is no event in the queue or the next event to execute is in more
ganlikun 0:13413ea9a877 100 * than MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA us from now then the ticker
ganlikun 0:13413ea9a877 101 * irq will be scheduled in MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA us.
ganlikun 0:13413ea9a877 102 * Otherwise the irq will be scheduled to happen when the running counter reach
ganlikun 0:13413ea9a877 103 * the timestamp of the first event in the queue.
ganlikun 0:13413ea9a877 104 *
ganlikun 0:13413ea9a877 105 * @note If there is no event in the queue then the interrupt is scheduled to
ganlikun 0:13413ea9a877 106 * in MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA. This is necessary to keep track
ganlikun 0:13413ea9a877 107 * of the timer overflow.
ganlikun 0:13413ea9a877 108 */
ganlikun 0:13413ea9a877 109 static void schedule_interrupt(const ticker_data_t *const ticker)
ganlikun 0:13413ea9a877 110 {
ganlikun 0:13413ea9a877 111 update_present_time(ticker);
ganlikun 0:13413ea9a877 112 uint32_t relative_timeout = MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA;
ganlikun 0:13413ea9a877 113
ganlikun 0:13413ea9a877 114 if (ticker->queue->head) {
ganlikun 0:13413ea9a877 115 us_timestamp_t present = ticker->queue->present_time;
ganlikun 0:13413ea9a877 116 us_timestamp_t next_event_timestamp = ticker->queue->head->timestamp;
ganlikun 0:13413ea9a877 117
ganlikun 0:13413ea9a877 118 // if the event at the head of the queue is in the past then schedule
ganlikun 0:13413ea9a877 119 // it immediately.
ganlikun 0:13413ea9a877 120 if (next_event_timestamp <= present) {
ganlikun 0:13413ea9a877 121 ticker->interface->fire_interrupt();
ganlikun 0:13413ea9a877 122 return;
ganlikun 0:13413ea9a877 123 } else if ((next_event_timestamp - present) < MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA) {
ganlikun 0:13413ea9a877 124 relative_timeout = next_event_timestamp - present;
ganlikun 0:13413ea9a877 125 }
ganlikun 0:13413ea9a877 126 }
ganlikun 0:13413ea9a877 127
ganlikun 0:13413ea9a877 128 us_timestamp_t new_match_time = ticker->queue->present_time + relative_timeout;
ganlikun 0:13413ea9a877 129 ticker->interface->set_interrupt(new_match_time);
ganlikun 0:13413ea9a877 130 // there could be a delay, reread the time, check if it was set in the past
ganlikun 0:13413ea9a877 131 // As result, if it is already in the past, we fire it immediately
ganlikun 0:13413ea9a877 132 update_present_time(ticker);
ganlikun 0:13413ea9a877 133 us_timestamp_t present = ticker->queue->present_time;
ganlikun 0:13413ea9a877 134 if (present >= new_match_time) {
ganlikun 0:13413ea9a877 135 ticker->interface->fire_interrupt();
ganlikun 0:13413ea9a877 136 }
ganlikun 0:13413ea9a877 137 }
ganlikun 0:13413ea9a877 138
ganlikun 0:13413ea9a877 139 void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler)
ganlikun 0:13413ea9a877 140 {
ganlikun 0:13413ea9a877 141 initialize(ticker);
ganlikun 0:13413ea9a877 142 set_handler(ticker, handler);
ganlikun 0:13413ea9a877 143 }
ganlikun 0:13413ea9a877 144
ganlikun 0:13413ea9a877 145 void ticker_irq_handler(const ticker_data_t *const ticker)
ganlikun 0:13413ea9a877 146 {
ganlikun 0:13413ea9a877 147 ticker->interface->clear_interrupt();
ganlikun 0:13413ea9a877 148
ganlikun 0:13413ea9a877 149 /* Go through all the pending TimerEvents */
ganlikun 0:13413ea9a877 150 while (1) {
ganlikun 0:13413ea9a877 151 if (ticker->queue->head == NULL) {
ganlikun 0:13413ea9a877 152 break;
ganlikun 0:13413ea9a877 153 }
ganlikun 0:13413ea9a877 154
ganlikun 0:13413ea9a877 155 // update the current timestamp used by the queue
ganlikun 0:13413ea9a877 156 update_present_time(ticker);
ganlikun 0:13413ea9a877 157
ganlikun 0:13413ea9a877 158 if (ticker->queue->head->timestamp <= ticker->queue->present_time) {
ganlikun 0:13413ea9a877 159 // This event was in the past:
ganlikun 0:13413ea9a877 160 // point to the following one and execute its handler
ganlikun 0:13413ea9a877 161 ticker_event_t *p = ticker->queue->head;
ganlikun 0:13413ea9a877 162 ticker->queue->head = ticker->queue->head->next;
ganlikun 0:13413ea9a877 163 if (ticker->queue->event_handler != NULL) {
ganlikun 0:13413ea9a877 164 (*ticker->queue->event_handler)(p->id); // NOTE: the handler can set new events
ganlikun 0:13413ea9a877 165 }
ganlikun 0:13413ea9a877 166 /* Note: We continue back to examining the head because calling the
ganlikun 0:13413ea9a877 167 * event handler may have altered the chain of pending events. */
ganlikun 0:13413ea9a877 168 } else {
ganlikun 0:13413ea9a877 169 break;
ganlikun 0:13413ea9a877 170 }
ganlikun 0:13413ea9a877 171 }
ganlikun 0:13413ea9a877 172
ganlikun 0:13413ea9a877 173 schedule_interrupt(ticker);
ganlikun 0:13413ea9a877 174 }
ganlikun 0:13413ea9a877 175
ganlikun 0:13413ea9a877 176 void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id)
ganlikun 0:13413ea9a877 177 {
ganlikun 0:13413ea9a877 178 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 179
ganlikun 0:13413ea9a877 180 // update the current timestamp
ganlikun 0:13413ea9a877 181 update_present_time(ticker);
ganlikun 0:13413ea9a877 182 us_timestamp_t absolute_timestamp = convert_timestamp(
ganlikun 0:13413ea9a877 183 ticker->queue->present_time,
ganlikun 0:13413ea9a877 184 timestamp
ganlikun 0:13413ea9a877 185 );
ganlikun 0:13413ea9a877 186 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 187
ganlikun 0:13413ea9a877 188 // defer to ticker_insert_event_us
ganlikun 0:13413ea9a877 189 ticker_insert_event_us(
ganlikun 0:13413ea9a877 190 ticker,
ganlikun 0:13413ea9a877 191 obj, absolute_timestamp, id
ganlikun 0:13413ea9a877 192 );
ganlikun 0:13413ea9a877 193 }
ganlikun 0:13413ea9a877 194
ganlikun 0:13413ea9a877 195 void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id)
ganlikun 0:13413ea9a877 196 {
ganlikun 0:13413ea9a877 197 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 198
ganlikun 0:13413ea9a877 199 // update the current timestamp
ganlikun 0:13413ea9a877 200 update_present_time(ticker);
ganlikun 0:13413ea9a877 201
ganlikun 0:13413ea9a877 202 // initialise our data
ganlikun 0:13413ea9a877 203 obj->timestamp = timestamp;
ganlikun 0:13413ea9a877 204 obj->id = id;
ganlikun 0:13413ea9a877 205
ganlikun 0:13413ea9a877 206 /* Go through the list until we either reach the end, or find
ganlikun 0:13413ea9a877 207 an element this should come before (which is possibly the
ganlikun 0:13413ea9a877 208 head). */
ganlikun 0:13413ea9a877 209 ticker_event_t *prev = NULL, *p = ticker->queue->head;
ganlikun 0:13413ea9a877 210 while (p != NULL) {
ganlikun 0:13413ea9a877 211 /* check if we come before p */
ganlikun 0:13413ea9a877 212 if (timestamp < p->timestamp) {
ganlikun 0:13413ea9a877 213 break;
ganlikun 0:13413ea9a877 214 }
ganlikun 0:13413ea9a877 215 /* go to the next element */
ganlikun 0:13413ea9a877 216 prev = p;
ganlikun 0:13413ea9a877 217 p = p->next;
ganlikun 0:13413ea9a877 218 }
ganlikun 0:13413ea9a877 219
ganlikun 0:13413ea9a877 220 /* if we're at the end p will be NULL, which is correct */
ganlikun 0:13413ea9a877 221 obj->next = p;
ganlikun 0:13413ea9a877 222
ganlikun 0:13413ea9a877 223 /* if prev is NULL we're at the head */
ganlikun 0:13413ea9a877 224 if (prev == NULL) {
ganlikun 0:13413ea9a877 225 ticker->queue->head = obj;
ganlikun 0:13413ea9a877 226 } else {
ganlikun 0:13413ea9a877 227 prev->next = obj;
ganlikun 0:13413ea9a877 228 }
ganlikun 0:13413ea9a877 229
ganlikun 0:13413ea9a877 230 schedule_interrupt(ticker);
ganlikun 0:13413ea9a877 231
ganlikun 0:13413ea9a877 232 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 233 }
ganlikun 0:13413ea9a877 234
ganlikun 0:13413ea9a877 235 void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj)
ganlikun 0:13413ea9a877 236 {
ganlikun 0:13413ea9a877 237 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 238
ganlikun 0:13413ea9a877 239 // remove this object from the list
ganlikun 0:13413ea9a877 240 if (ticker->queue->head == obj) {
ganlikun 0:13413ea9a877 241 // first in the list, so just drop me
ganlikun 0:13413ea9a877 242 ticker->queue->head = obj->next;
ganlikun 0:13413ea9a877 243 schedule_interrupt(ticker);
ganlikun 0:13413ea9a877 244 } else {
ganlikun 0:13413ea9a877 245 // find the object before me, then drop me
ganlikun 0:13413ea9a877 246 ticker_event_t* p = ticker->queue->head;
ganlikun 0:13413ea9a877 247 while (p != NULL) {
ganlikun 0:13413ea9a877 248 if (p->next == obj) {
ganlikun 0:13413ea9a877 249 p->next = obj->next;
ganlikun 0:13413ea9a877 250 break;
ganlikun 0:13413ea9a877 251 }
ganlikun 0:13413ea9a877 252 p = p->next;
ganlikun 0:13413ea9a877 253 }
ganlikun 0:13413ea9a877 254 }
ganlikun 0:13413ea9a877 255
ganlikun 0:13413ea9a877 256 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 257 }
ganlikun 0:13413ea9a877 258
ganlikun 0:13413ea9a877 259 timestamp_t ticker_read(const ticker_data_t *const ticker)
ganlikun 0:13413ea9a877 260 {
ganlikun 0:13413ea9a877 261 return ticker_read_us(ticker);
ganlikun 0:13413ea9a877 262 }
ganlikun 0:13413ea9a877 263
ganlikun 0:13413ea9a877 264 us_timestamp_t ticker_read_us(const ticker_data_t *const ticker)
ganlikun 0:13413ea9a877 265 {
ganlikun 0:13413ea9a877 266 initialize(ticker);
ganlikun 0:13413ea9a877 267 update_present_time(ticker);
ganlikun 0:13413ea9a877 268 return ticker->queue->present_time;
ganlikun 0:13413ea9a877 269 }
ganlikun 0:13413ea9a877 270
ganlikun 0:13413ea9a877 271 int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp)
ganlikun 0:13413ea9a877 272 {
ganlikun 0:13413ea9a877 273 int ret = 0;
ganlikun 0:13413ea9a877 274
ganlikun 0:13413ea9a877 275 /* if head is NULL, there are no pending events */
ganlikun 0:13413ea9a877 276 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 277 if (data->queue->head != NULL) {
ganlikun 0:13413ea9a877 278 *timestamp = data->queue->head->timestamp;
ganlikun 0:13413ea9a877 279 ret = 1;
ganlikun 0:13413ea9a877 280 }
ganlikun 0:13413ea9a877 281 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 282
ganlikun 0:13413ea9a877 283 return ret;
ganlikun 0:13413ea9a877 284 }
ganlikun 0:13413ea9a877 285