Forked.

Fork of mbed-dev by mbed official

Committer:
Dollyparton
Date:
Tue Dec 19 12:50:13 2017 +0000
Revision:
174:ed647f63e28d
Parent:
167:e84263d55307
Added RAW socket.

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