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