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