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