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
ganlikun 0:20e0c61e0684 2 /** \addtogroup hal */
ganlikun 0:20e0c61e0684 3 /** @{*/
ganlikun 0:20e0c61e0684 4 /* mbed Microcontroller Library
ganlikun 0:20e0c61e0684 5 * Copyright (c) 2015 ARM Limited
ganlikun 0:20e0c61e0684 6 *
ganlikun 0:20e0c61e0684 7 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:20e0c61e0684 8 * you may not use this file except in compliance with the License.
ganlikun 0:20e0c61e0684 9 * You may obtain a copy of the License at
ganlikun 0:20e0c61e0684 10 *
ganlikun 0:20e0c61e0684 11 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:20e0c61e0684 12 *
ganlikun 0:20e0c61e0684 13 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:20e0c61e0684 14 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:20e0c61e0684 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:20e0c61e0684 16 * See the License for the specific language governing permissions and
ganlikun 0:20e0c61e0684 17 * limitations under the License.
ganlikun 0:20e0c61e0684 18 */
ganlikun 0:20e0c61e0684 19 #ifndef MBED_TICKER_API_H
ganlikun 0:20e0c61e0684 20 #define MBED_TICKER_API_H
ganlikun 0:20e0c61e0684 21
ganlikun 0:20e0c61e0684 22 #include <stdint.h>
ganlikun 0:20e0c61e0684 23 #include <stdbool.h>
ganlikun 0:20e0c61e0684 24 #include "device.h"
ganlikun 0:20e0c61e0684 25
ganlikun 0:20e0c61e0684 26 /**
ganlikun 0:20e0c61e0684 27 * Maximum delta (in us) between too interrupts.
ganlikun 0:20e0c61e0684 28 */
ganlikun 0:20e0c61e0684 29 #define MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA 0x70000000ULL
ganlikun 0:20e0c61e0684 30
ganlikun 0:20e0c61e0684 31 /**
ganlikun 0:20e0c61e0684 32 * Legacy format representing a timestamp in us.
ganlikun 0:20e0c61e0684 33 * Given it is modeled as a 32 bit integer, this type can represent timestamp
ganlikun 0:20e0c61e0684 34 * up to 4294 seconds (71 minutes).
ganlikun 0:20e0c61e0684 35 * Prefer using us_timestamp_t which store timestamp as 64 bits integer.
ganlikun 0:20e0c61e0684 36 */
ganlikun 0:20e0c61e0684 37 typedef uint32_t timestamp_t;
ganlikun 0:20e0c61e0684 38
ganlikun 0:20e0c61e0684 39 /**
ganlikun 0:20e0c61e0684 40 * A us timestamp stored in a 64 bit integer.
ganlikun 0:20e0c61e0684 41 * Can store timestamp up to 584810 years.
ganlikun 0:20e0c61e0684 42 */
ganlikun 0:20e0c61e0684 43 typedef uint64_t us_timestamp_t;
ganlikun 0:20e0c61e0684 44
ganlikun 0:20e0c61e0684 45 /** Ticker's event structure
ganlikun 0:20e0c61e0684 46 */
ganlikun 0:20e0c61e0684 47 typedef struct ticker_event_s {
ganlikun 0:20e0c61e0684 48 us_timestamp_t timestamp; /**< Event's timestamp */
ganlikun 0:20e0c61e0684 49 uint32_t id; /**< TimerEvent object */
ganlikun 0:20e0c61e0684 50 struct ticker_event_s *next; /**< Next event in the queue */
ganlikun 0:20e0c61e0684 51 } ticker_event_t;
ganlikun 0:20e0c61e0684 52
ganlikun 0:20e0c61e0684 53 typedef void (*ticker_event_handler)(uint32_t id);
ganlikun 0:20e0c61e0684 54
ganlikun 0:20e0c61e0684 55 /** Ticker's interface structure - required API for a ticker
ganlikun 0:20e0c61e0684 56 */
ganlikun 0:20e0c61e0684 57 typedef struct {
ganlikun 0:20e0c61e0684 58 void (*init)(void); /**< Init function */
ganlikun 0:20e0c61e0684 59 uint32_t (*read)(void); /**< Read function */
ganlikun 0:20e0c61e0684 60 void (*disable_interrupt)(void); /**< Disable interrupt function */
ganlikun 0:20e0c61e0684 61 void (*clear_interrupt)(void); /**< Clear interrupt function */
ganlikun 0:20e0c61e0684 62 void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
ganlikun 0:20e0c61e0684 63 void (*fire_interrupt)(void); /**< Fire interrupt right-away */
ganlikun 0:20e0c61e0684 64 } ticker_interface_t;
ganlikun 0:20e0c61e0684 65
ganlikun 0:20e0c61e0684 66 /** Ticker's event queue structure
ganlikun 0:20e0c61e0684 67 */
ganlikun 0:20e0c61e0684 68 typedef struct {
ganlikun 0:20e0c61e0684 69 ticker_event_handler event_handler; /**< Event handler */
ganlikun 0:20e0c61e0684 70 ticker_event_t *head; /**< A pointer to head */
ganlikun 0:20e0c61e0684 71 us_timestamp_t present_time; /**< Store the timestamp used for present time */
ganlikun 0:20e0c61e0684 72 bool initialized; /**< Indicate if the instance is initialized */
ganlikun 0:20e0c61e0684 73 } ticker_event_queue_t;
ganlikun 0:20e0c61e0684 74
ganlikun 0:20e0c61e0684 75 /** Ticker's data structure
ganlikun 0:20e0c61e0684 76 */
ganlikun 0:20e0c61e0684 77 typedef struct {
ganlikun 0:20e0c61e0684 78 const ticker_interface_t *interface; /**< Ticker's interface */
ganlikun 0:20e0c61e0684 79 ticker_event_queue_t *queue; /**< Ticker's event queue */
ganlikun 0:20e0c61e0684 80 } ticker_data_t;
ganlikun 0:20e0c61e0684 81
ganlikun 0:20e0c61e0684 82 #ifdef __cplusplus
ganlikun 0:20e0c61e0684 83 extern "C" {
ganlikun 0:20e0c61e0684 84 #endif
ganlikun 0:20e0c61e0684 85
ganlikun 0:20e0c61e0684 86 /**
ganlikun 0:20e0c61e0684 87 * \defgroup hal_ticker Ticker HAL functions
ganlikun 0:20e0c61e0684 88 * @{
ganlikun 0:20e0c61e0684 89 */
ganlikun 0:20e0c61e0684 90
ganlikun 0:20e0c61e0684 91 /** Initialize a ticker and set the event handler
ganlikun 0:20e0c61e0684 92 *
ganlikun 0:20e0c61e0684 93 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 94 * @param handler A handler to be set
ganlikun 0:20e0c61e0684 95 */
ganlikun 0:20e0c61e0684 96 void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
ganlikun 0:20e0c61e0684 97
ganlikun 0:20e0c61e0684 98 /** IRQ handler that goes through the events to trigger overdue events.
ganlikun 0:20e0c61e0684 99 *
ganlikun 0:20e0c61e0684 100 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 101 */
ganlikun 0:20e0c61e0684 102 void ticker_irq_handler(const ticker_data_t *const ticker);
ganlikun 0:20e0c61e0684 103
ganlikun 0:20e0c61e0684 104 /** Remove an event from the queue
ganlikun 0:20e0c61e0684 105 *
ganlikun 0:20e0c61e0684 106 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 107 * @param obj The event object to be removed from the queue
ganlikun 0:20e0c61e0684 108 */
ganlikun 0:20e0c61e0684 109 void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
ganlikun 0:20e0c61e0684 110
ganlikun 0:20e0c61e0684 111 /** Insert an event to the queue
ganlikun 0:20e0c61e0684 112 *
ganlikun 0:20e0c61e0684 113 * The event will be executed in timestamp - ticker_read().
ganlikun 0:20e0c61e0684 114 *
ganlikun 0:20e0c61e0684 115 * @warning This function does not consider timestamp in the past. If an event
ganlikun 0:20e0c61e0684 116 * is inserted with a timestamp less than the current timestamp then the event
ganlikun 0:20e0c61e0684 117 * will be executed in timestamp - ticker_read() us.
ganlikun 0:20e0c61e0684 118 * The internal counter wrap very quickly it is hard to decide weither an
ganlikun 0:20e0c61e0684 119 * event is in the past or in 1 hour.
ganlikun 0:20e0c61e0684 120 *
ganlikun 0:20e0c61e0684 121 * @note prefer the use of ticker_insert_event_us which allows registration of
ganlikun 0:20e0c61e0684 122 * absolute timestamp.
ganlikun 0:20e0c61e0684 123 *
ganlikun 0:20e0c61e0684 124 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 125 * @param obj The event object to be inserted to the queue
ganlikun 0:20e0c61e0684 126 * @param timestamp The event's timestamp
ganlikun 0:20e0c61e0684 127 * @param id The event object
ganlikun 0:20e0c61e0684 128 */
ganlikun 0:20e0c61e0684 129 void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
ganlikun 0:20e0c61e0684 130
ganlikun 0:20e0c61e0684 131 /** Insert an event to the queue
ganlikun 0:20e0c61e0684 132 *
ganlikun 0:20e0c61e0684 133 * The event will be executed in timestamp - ticker_read_us() us.
ganlikun 0:20e0c61e0684 134 *
ganlikun 0:20e0c61e0684 135 * @warning If an event is inserted with a timestamp less than the current
ganlikun 0:20e0c61e0684 136 * timestamp then the event will **not** be inserted.
ganlikun 0:20e0c61e0684 137 *
ganlikun 0:20e0c61e0684 138 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 139 * @param obj The event object to be inserted to the queue
ganlikun 0:20e0c61e0684 140 * @param timestamp The event's timestamp
ganlikun 0:20e0c61e0684 141 * @param id The event object
ganlikun 0:20e0c61e0684 142 */
ganlikun 0:20e0c61e0684 143 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 144
ganlikun 0:20e0c61e0684 145 /** Read the current (relative) ticker's timestamp
ganlikun 0:20e0c61e0684 146 *
ganlikun 0:20e0c61e0684 147 * @warning Return a relative timestamp because the counter wrap every 4294
ganlikun 0:20e0c61e0684 148 * seconds.
ganlikun 0:20e0c61e0684 149 *
ganlikun 0:20e0c61e0684 150 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 151 * @return The current timestamp
ganlikun 0:20e0c61e0684 152 */
ganlikun 0:20e0c61e0684 153 timestamp_t ticker_read(const ticker_data_t *const ticker);
ganlikun 0:20e0c61e0684 154
ganlikun 0:20e0c61e0684 155 /** Read the current (absolute) ticker's timestamp
ganlikun 0:20e0c61e0684 156 *
ganlikun 0:20e0c61e0684 157 * @warning Return an absolute timestamp counting from the initialization of the
ganlikun 0:20e0c61e0684 158 * ticker.
ganlikun 0:20e0c61e0684 159 *
ganlikun 0:20e0c61e0684 160 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 161 * @return The current timestamp
ganlikun 0:20e0c61e0684 162 */
ganlikun 0:20e0c61e0684 163 us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
ganlikun 0:20e0c61e0684 164
ganlikun 0:20e0c61e0684 165 /** Read the next event's timestamp
ganlikun 0:20e0c61e0684 166 *
ganlikun 0:20e0c61e0684 167 * @param ticker The ticker object.
ganlikun 0:20e0c61e0684 168 * @param timestamp The timestamp object.
ganlikun 0:20e0c61e0684 169 * @return 1 if timestamp is pending event, 0 if there's no event pending
ganlikun 0:20e0c61e0684 170 */
ganlikun 0:20e0c61e0684 171 int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
ganlikun 0:20e0c61e0684 172
ganlikun 0:20e0c61e0684 173 /**@}*/
ganlikun 0:20e0c61e0684 174
ganlikun 0:20e0c61e0684 175 #ifdef __cplusplus
ganlikun 0:20e0c61e0684 176 }
ganlikun 0:20e0c61e0684 177 #endif
ganlikun 0:20e0c61e0684 178
ganlikun 0:20e0c61e0684 179 #endif
ganlikun 0:20e0c61e0684 180
ganlikun 0:20e0c61e0684 181 /** @}*/
ganlikun 0:20e0c61e0684 182