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