mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
hal/ticker_api.h@187:fa51feb62426, 2018-10-09 (annotated)
- Committer:
- pmcorreia
- Date:
- Tue Oct 09 14:42:37 2018 +0000
- Revision:
- 187:fa51feb62426
- Parent:
- 186:707f6e361f3e
Updated version to work with F446RE
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 */ |
Anna Bridge |
186:707f6e361f3e | 83 | uint8_t frequency_shifts; /**< If frequency is a value of 2^n, this is n, otherwise 0 */ |
<> | 144:ef7eb2e8f9f7 | 84 | } ticker_event_queue_t; |
<> | 144:ef7eb2e8f9f7 | 85 | |
<> | 144:ef7eb2e8f9f7 | 86 | /** Ticker's data structure |
<> | 144:ef7eb2e8f9f7 | 87 | */ |
<> | 144:ef7eb2e8f9f7 | 88 | typedef struct { |
<> | 144:ef7eb2e8f9f7 | 89 | const ticker_interface_t *interface; /**< Ticker's interface */ |
<> | 144:ef7eb2e8f9f7 | 90 | ticker_event_queue_t *queue; /**< Ticker's event queue */ |
<> | 144:ef7eb2e8f9f7 | 91 | } ticker_data_t; |
<> | 144:ef7eb2e8f9f7 | 92 | |
<> | 144:ef7eb2e8f9f7 | 93 | #ifdef __cplusplus |
<> | 144:ef7eb2e8f9f7 | 94 | extern "C" { |
<> | 144:ef7eb2e8f9f7 | 95 | #endif |
<> | 144:ef7eb2e8f9f7 | 96 | |
<> | 144:ef7eb2e8f9f7 | 97 | /** |
<> | 144:ef7eb2e8f9f7 | 98 | * \defgroup hal_ticker Ticker HAL functions |
<> | 144:ef7eb2e8f9f7 | 99 | * @{ |
<> | 144:ef7eb2e8f9f7 | 100 | */ |
<> | 144:ef7eb2e8f9f7 | 101 | |
<> | 144:ef7eb2e8f9f7 | 102 | /** Initialize a ticker and set the event handler |
<> | 144:ef7eb2e8f9f7 | 103 | * |
AnnaBridge | 167:e84263d55307 | 104 | * @param ticker The ticker object. |
<> | 144:ef7eb2e8f9f7 | 105 | * @param handler A handler to be set |
<> | 144:ef7eb2e8f9f7 | 106 | */ |
AnnaBridge | 167:e84263d55307 | 107 | void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler); |
<> | 144:ef7eb2e8f9f7 | 108 | |
<> | 144:ef7eb2e8f9f7 | 109 | /** IRQ handler that goes through the events to trigger overdue events. |
<> | 144:ef7eb2e8f9f7 | 110 | * |
AnnaBridge | 167:e84263d55307 | 111 | * @param ticker The ticker object. |
<> | 144:ef7eb2e8f9f7 | 112 | */ |
AnnaBridge | 167:e84263d55307 | 113 | void ticker_irq_handler(const ticker_data_t *const ticker); |
<> | 144:ef7eb2e8f9f7 | 114 | |
<> | 144:ef7eb2e8f9f7 | 115 | /** Remove an event from the queue |
<> | 144:ef7eb2e8f9f7 | 116 | * |
AnnaBridge | 167:e84263d55307 | 117 | * @param ticker The ticker object. |
<> | 144:ef7eb2e8f9f7 | 118 | * @param obj The event object to be removed from the queue |
<> | 144:ef7eb2e8f9f7 | 119 | */ |
AnnaBridge | 167:e84263d55307 | 120 | void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj); |
<> | 144:ef7eb2e8f9f7 | 121 | |
<> | 144:ef7eb2e8f9f7 | 122 | /** Insert an event to the queue |
<> | 144:ef7eb2e8f9f7 | 123 | * |
AnnaBridge | 167:e84263d55307 | 124 | * The event will be executed in timestamp - ticker_read(). |
AnnaBridge | 167:e84263d55307 | 125 | * |
AnnaBridge | 167:e84263d55307 | 126 | * @warning This function does not consider timestamp in the past. If an event |
AnnaBridge | 167:e84263d55307 | 127 | * is inserted with a timestamp less than the current timestamp then the event |
AnnaBridge | 167:e84263d55307 | 128 | * will be executed in timestamp - ticker_read() us. |
AnnaBridge | 167:e84263d55307 | 129 | * The internal counter wrap very quickly it is hard to decide weither an |
AnnaBridge | 167:e84263d55307 | 130 | * event is in the past or in 1 hour. |
AnnaBridge | 167:e84263d55307 | 131 | * |
AnnaBridge | 167:e84263d55307 | 132 | * @note prefer the use of ticker_insert_event_us which allows registration of |
AnnaBridge | 167:e84263d55307 | 133 | * absolute timestamp. |
AnnaBridge | 167:e84263d55307 | 134 | * |
AnnaBridge | 167:e84263d55307 | 135 | * @param ticker The ticker object. |
<> | 144:ef7eb2e8f9f7 | 136 | * @param obj The event object to be inserted to the queue |
<> | 144:ef7eb2e8f9f7 | 137 | * @param timestamp The event's timestamp |
<> | 144:ef7eb2e8f9f7 | 138 | * @param id The event object |
<> | 144:ef7eb2e8f9f7 | 139 | */ |
AnnaBridge | 167:e84263d55307 | 140 | void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id); |
<> | 144:ef7eb2e8f9f7 | 141 | |
AnnaBridge | 167:e84263d55307 | 142 | /** Insert an event to the queue |
AnnaBridge | 167:e84263d55307 | 143 | * |
AnnaBridge | 167:e84263d55307 | 144 | * The event will be executed in timestamp - ticker_read_us() us. |
AnnaBridge | 167:e84263d55307 | 145 | * |
AnnaBridge | 176:447f873cad2f | 146 | * @note If an event is inserted with a timestamp less than the current |
AnnaBridge | 176:447f873cad2f | 147 | * timestamp then the event will be scheduled immediately resulting in |
AnnaBridge | 176:447f873cad2f | 148 | * an instant call to event handler. |
<> | 144:ef7eb2e8f9f7 | 149 | * |
AnnaBridge | 167:e84263d55307 | 150 | * @param ticker The ticker object. |
AnnaBridge | 167:e84263d55307 | 151 | * @param obj The event object to be inserted to the queue |
AnnaBridge | 167:e84263d55307 | 152 | * @param timestamp The event's timestamp |
AnnaBridge | 167:e84263d55307 | 153 | * @param id The event object |
AnnaBridge | 167:e84263d55307 | 154 | */ |
AnnaBridge | 167:e84263d55307 | 155 | 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 | 156 | |
AnnaBridge | 167:e84263d55307 | 157 | /** Read the current (relative) ticker's timestamp |
AnnaBridge | 167:e84263d55307 | 158 | * |
AnnaBridge | 167:e84263d55307 | 159 | * @warning Return a relative timestamp because the counter wrap every 4294 |
AnnaBridge | 167:e84263d55307 | 160 | * seconds. |
AnnaBridge | 167:e84263d55307 | 161 | * |
AnnaBridge | 167:e84263d55307 | 162 | * @param ticker The ticker object. |
<> | 144:ef7eb2e8f9f7 | 163 | * @return The current timestamp |
<> | 144:ef7eb2e8f9f7 | 164 | */ |
AnnaBridge | 167:e84263d55307 | 165 | timestamp_t ticker_read(const ticker_data_t *const ticker); |
AnnaBridge | 167:e84263d55307 | 166 | |
AnnaBridge | 167:e84263d55307 | 167 | /** Read the current (absolute) ticker's timestamp |
AnnaBridge | 167:e84263d55307 | 168 | * |
AnnaBridge | 167:e84263d55307 | 169 | * @warning Return an absolute timestamp counting from the initialization of the |
AnnaBridge | 167:e84263d55307 | 170 | * ticker. |
AnnaBridge | 167:e84263d55307 | 171 | * |
AnnaBridge | 167:e84263d55307 | 172 | * @param ticker The ticker object. |
AnnaBridge | 167:e84263d55307 | 173 | * @return The current timestamp |
AnnaBridge | 167:e84263d55307 | 174 | */ |
AnnaBridge | 167:e84263d55307 | 175 | us_timestamp_t ticker_read_us(const ticker_data_t *const ticker); |
<> | 144:ef7eb2e8f9f7 | 176 | |
<> | 144:ef7eb2e8f9f7 | 177 | /** Read the next event's timestamp |
<> | 144:ef7eb2e8f9f7 | 178 | * |
AnnaBridge | 167:e84263d55307 | 179 | * @param ticker The ticker object. |
AnnaBridge | 167:e84263d55307 | 180 | * @param timestamp The timestamp object. |
<> | 144:ef7eb2e8f9f7 | 181 | * @return 1 if timestamp is pending event, 0 if there's no event pending |
<> | 144:ef7eb2e8f9f7 | 182 | */ |
AnnaBridge | 167:e84263d55307 | 183 | int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp); |
<> | 144:ef7eb2e8f9f7 | 184 | |
Anna Bridge |
180:96ed750bd169 | 185 | /* Private functions |
Anna Bridge |
180:96ed750bd169 | 186 | * |
Anna Bridge |
180:96ed750bd169 | 187 | * @cond PRIVATE |
Anna Bridge |
180:96ed750bd169 | 188 | * |
Anna Bridge |
180:96ed750bd169 | 189 | */ |
Anna Bridge |
180:96ed750bd169 | 190 | |
Anna Bridge |
180:96ed750bd169 | 191 | int _ticker_match_interval_passed(timestamp_t prev_tick, timestamp_t cur_tick, timestamp_t match_tick); |
Anna Bridge |
180:96ed750bd169 | 192 | |
Anna Bridge |
180:96ed750bd169 | 193 | /* |
Anna Bridge |
180:96ed750bd169 | 194 | * @endcond PRIVATE |
Anna Bridge |
180:96ed750bd169 | 195 | * |
Anna Bridge |
180:96ed750bd169 | 196 | */ |
Anna Bridge |
180:96ed750bd169 | 197 | |
<> | 144:ef7eb2e8f9f7 | 198 | /**@}*/ |
<> | 144:ef7eb2e8f9f7 | 199 | |
<> | 144:ef7eb2e8f9f7 | 200 | #ifdef __cplusplus |
<> | 144:ef7eb2e8f9f7 | 201 | } |
<> | 144:ef7eb2e8f9f7 | 202 | #endif |
<> | 144:ef7eb2e8f9f7 | 203 | |
<> | 144:ef7eb2e8f9f7 | 204 | #endif |
<> | 149:156823d33999 | 205 | |
<> | 149:156823d33999 | 206 | /** @}*/ |