inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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