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