001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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