[352] | 1 |
|
---|
| 2 | /** \addtogroup hal */
|
---|
| 3 | /** @{*/
|
---|
| 4 | /* mbed Microcontroller Library
|
---|
| 5 | * Copyright (c) 2015 ARM Limited
|
---|
| 6 | *
|
---|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 8 | * you may not use this file except in compliance with the License.
|
---|
| 9 | * You may obtain a copy of the License at
|
---|
| 10 | *
|
---|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 12 | *
|
---|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 16 | * See the License for the specific language governing permissions and
|
---|
| 17 | * limitations under the License.
|
---|
| 18 | */
|
---|
| 19 | #ifndef MBED_TICKER_API_H
|
---|
| 20 | #define MBED_TICKER_API_H
|
---|
| 21 |
|
---|
| 22 | #include <stdint.h>
|
---|
[374] | 23 | #include <stdbool.h>
|
---|
[352] | 24 | #include "device.h"
|
---|
| 25 |
|
---|
[374] | 26 | /**
|
---|
| 27 | * Legacy format representing a timestamp in us.
|
---|
| 28 | * Given it is modeled as a 32 bit integer, this type can represent timestamp
|
---|
| 29 | * up to 4294 seconds (71 minutes).
|
---|
| 30 | * Prefer using us_timestamp_t which store timestamp as 64 bits integer.
|
---|
| 31 | */
|
---|
[352] | 32 | typedef uint32_t timestamp_t;
|
---|
| 33 |
|
---|
[374] | 34 | /**
|
---|
| 35 | * A us timestamp stored in a 64 bit integer.
|
---|
| 36 | * Can store timestamp up to 584810 years.
|
---|
| 37 | */
|
---|
| 38 | typedef uint64_t us_timestamp_t;
|
---|
| 39 |
|
---|
[352] | 40 | /** Ticker's event structure
|
---|
| 41 | */
|
---|
| 42 | typedef struct ticker_event_s {
|
---|
[374] | 43 | us_timestamp_t timestamp; /**< Event's timestamp */
|
---|
[352] | 44 | uint32_t id; /**< TimerEvent object */
|
---|
| 45 | struct ticker_event_s *next; /**< Next event in the queue */
|
---|
| 46 | } ticker_event_t;
|
---|
| 47 |
|
---|
| 48 | typedef void (*ticker_event_handler)(uint32_t id);
|
---|
| 49 |
|
---|
[374] | 50 | /** Information about the ticker implementation
|
---|
| 51 | */
|
---|
| 52 | typedef struct {
|
---|
| 53 | uint32_t frequency; /**< Frequency in Hz this ticker runs at */
|
---|
| 54 | uint32_t bits; /**< Number of bits this ticker supports */
|
---|
| 55 | } ticker_info_t;
|
---|
| 56 |
|
---|
| 57 |
|
---|
[352] | 58 | /** Ticker's interface structure - required API for a ticker
|
---|
| 59 | */
|
---|
| 60 | typedef struct {
|
---|
| 61 | void (*init)(void); /**< Init function */
|
---|
| 62 | uint32_t (*read)(void); /**< Read function */
|
---|
| 63 | void (*disable_interrupt)(void); /**< Disable interrupt function */
|
---|
| 64 | void (*clear_interrupt)(void); /**< Clear interrupt function */
|
---|
| 65 | void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
|
---|
[374] | 66 | void (*fire_interrupt)(void); /**< Fire interrupt right-away */
|
---|
| 67 | void (*free)(void); /**< Disable function */
|
---|
| 68 | const ticker_info_t *(*get_info)(void); /**< Return info about this ticker's implementation */
|
---|
[352] | 69 | } ticker_interface_t;
|
---|
| 70 |
|
---|
| 71 | /** Ticker's event queue structure
|
---|
| 72 | */
|
---|
| 73 | typedef struct {
|
---|
| 74 | ticker_event_handler event_handler; /**< Event handler */
|
---|
| 75 | ticker_event_t *head; /**< A pointer to head */
|
---|
[374] | 76 | uint32_t frequency; /**< Frequency of the timer in Hz */
|
---|
| 77 | uint32_t bitmask; /**< Mask to be applied to time values read */
|
---|
| 78 | uint32_t max_delta; /**< Largest delta in ticks that can be used when scheduling */
|
---|
| 79 | uint64_t max_delta_us; /**< Largest delta in us that can be used when scheduling */
|
---|
| 80 | uint32_t tick_last_read; /**< Last tick read */
|
---|
| 81 | uint64_t tick_remainder; /**< Ticks that have not been added to base_time */
|
---|
| 82 | us_timestamp_t present_time; /**< Store the timestamp used for present time */
|
---|
| 83 | bool initialized; /**< Indicate if the instance is initialized */
|
---|
| 84 | bool dispatching; /**< The function ticker_irq_handler is dispatching */
|
---|
| 85 | bool suspended; /**< Indicate if the instance is suspended */
|
---|
| 86 | uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */
|
---|
[352] | 87 | } ticker_event_queue_t;
|
---|
| 88 |
|
---|
| 89 | /** Ticker's data structure
|
---|
| 90 | */
|
---|
| 91 | typedef struct {
|
---|
| 92 | const ticker_interface_t *interface; /**< Ticker's interface */
|
---|
| 93 | ticker_event_queue_t *queue; /**< Ticker's event queue */
|
---|
| 94 | } ticker_data_t;
|
---|
| 95 |
|
---|
| 96 | #ifdef __cplusplus
|
---|
| 97 | extern "C" {
|
---|
| 98 | #endif
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * \defgroup hal_ticker Ticker HAL functions
|
---|
| 102 | * @{
|
---|
| 103 | */
|
---|
| 104 |
|
---|
| 105 | /** Initialize a ticker and set the event handler
|
---|
| 106 | *
|
---|
[374] | 107 | * @param ticker The ticker object.
|
---|
[352] | 108 | * @param handler A handler to be set
|
---|
| 109 | */
|
---|
[374] | 110 | void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
|
---|
[352] | 111 |
|
---|
| 112 | /** IRQ handler that goes through the events to trigger overdue events.
|
---|
| 113 | *
|
---|
[374] | 114 | * @param ticker The ticker object.
|
---|
[352] | 115 | */
|
---|
[374] | 116 | void ticker_irq_handler(const ticker_data_t *const ticker);
|
---|
[352] | 117 |
|
---|
| 118 | /** Remove an event from the queue
|
---|
| 119 | *
|
---|
[374] | 120 | * @param ticker The ticker object.
|
---|
[352] | 121 | * @param obj The event object to be removed from the queue
|
---|
| 122 | */
|
---|
[374] | 123 | void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
|
---|
[352] | 124 |
|
---|
| 125 | /** Insert an event to the queue
|
---|
| 126 | *
|
---|
[374] | 127 | * The event will be executed in timestamp - ticker_read().
|
---|
| 128 | *
|
---|
| 129 | * @warning This function does not consider timestamp in the past. If an event
|
---|
| 130 | * is inserted with a timestamp less than the current timestamp then the event
|
---|
| 131 | * will be executed in timestamp - ticker_read() us.
|
---|
| 132 | * The internal counter wrap very quickly it is hard to decide weither an
|
---|
| 133 | * event is in the past or in 1 hour.
|
---|
| 134 | *
|
---|
| 135 | * @note prefer the use of ticker_insert_event_us which allows registration of
|
---|
| 136 | * absolute timestamp.
|
---|
| 137 | *
|
---|
| 138 | * @param ticker The ticker object.
|
---|
[352] | 139 | * @param obj The event object to be inserted to the queue
|
---|
| 140 | * @param timestamp The event's timestamp
|
---|
| 141 | * @param id The event object
|
---|
| 142 | */
|
---|
[374] | 143 | void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
|
---|
[352] | 144 |
|
---|
[374] | 145 | /** Insert an event to the queue
|
---|
[352] | 146 | *
|
---|
[374] | 147 | * The event will be executed in timestamp - ticker_read_us() us.
|
---|
| 148 | *
|
---|
| 149 | * @note If an event is inserted with a timestamp less than the current
|
---|
| 150 | * timestamp then the event will be scheduled immediately resulting in
|
---|
| 151 | * an instant call to event handler.
|
---|
| 152 | *
|
---|
| 153 | * @param ticker The ticker object.
|
---|
| 154 | * @param obj The event object to be inserted to the queue
|
---|
| 155 | * @param timestamp The event's timestamp
|
---|
| 156 | * @param id The event object
|
---|
| 157 | */
|
---|
| 158 | void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
|
---|
| 159 |
|
---|
| 160 | /** Read the current (relative) ticker's timestamp
|
---|
| 161 | *
|
---|
| 162 | * @warning Return a relative timestamp because the counter wrap every 4294
|
---|
| 163 | * seconds.
|
---|
| 164 | *
|
---|
| 165 | * @param ticker The ticker object.
|
---|
[352] | 166 | * @return The current timestamp
|
---|
| 167 | */
|
---|
[374] | 168 | timestamp_t ticker_read(const ticker_data_t *const ticker);
|
---|
[352] | 169 |
|
---|
[374] | 170 | /** Read the current (absolute) ticker's timestamp
|
---|
| 171 | *
|
---|
| 172 | * @warning Return an absolute timestamp counting from the initialization of the
|
---|
| 173 | * ticker.
|
---|
| 174 | *
|
---|
| 175 | * @param ticker The ticker object.
|
---|
| 176 | * @return The current timestamp
|
---|
| 177 | */
|
---|
| 178 | us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
|
---|
| 179 |
|
---|
[352] | 180 | /** Read the next event's timestamp
|
---|
| 181 | *
|
---|
[374] | 182 | * @param ticker The ticker object.
|
---|
| 183 | * @param timestamp The timestamp object.
|
---|
[352] | 184 | * @return 1 if timestamp is pending event, 0 if there's no event pending
|
---|
| 185 | */
|
---|
[374] | 186 | int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
|
---|
[352] | 187 |
|
---|
[374] | 188 | /** Suspend this ticker
|
---|
| 189 | *
|
---|
| 190 | * When suspended reads will always return the same time and no
|
---|
| 191 | * events will be dispatched. When suspended the common layer
|
---|
| 192 | * will only ever call the interface function clear_interrupt()
|
---|
| 193 | * and that is only if ticker_irq_handler is called.
|
---|
| 194 | *
|
---|
| 195 | *
|
---|
| 196 | * @param ticker The ticker object.
|
---|
| 197 | */
|
---|
| 198 | void ticker_suspend(const ticker_data_t *const ticker);
|
---|
| 199 |
|
---|
| 200 | /** Resume this ticker
|
---|
| 201 | *
|
---|
| 202 | * When resumed the ticker will ignore any time that has passed
|
---|
| 203 | * and continue counting up where it left off.
|
---|
| 204 | *
|
---|
| 205 | * @param ticker The ticker object.
|
---|
| 206 | */
|
---|
| 207 | void ticker_resume(const ticker_data_t *const ticker);
|
---|
| 208 |
|
---|
| 209 | /* Private functions
|
---|
| 210 | *
|
---|
| 211 | * @cond PRIVATE
|
---|
| 212 | *
|
---|
| 213 | */
|
---|
| 214 |
|
---|
| 215 | int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, timestamp_t match_tick);
|
---|
| 216 |
|
---|
| 217 | /*
|
---|
| 218 | * @endcond PRIVATE
|
---|
| 219 | *
|
---|
| 220 | */
|
---|
| 221 |
|
---|
[352] | 222 | /**@}*/
|
---|
| 223 |
|
---|
| 224 | #ifdef __cplusplus
|
---|
| 225 | }
|
---|
| 226 | #endif
|
---|
| 227 |
|
---|
| 228 | #endif
|
---|
| 229 |
|
---|
| 230 | /** @}*/
|
---|