t

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Oct 11 12:45:49 2017 +0100
Revision:
176:af195413fb11
Parent:
175:b96e65c34a4d
This updates the lib to the mbed lib v 153

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