The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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