Skip to content

Commit 20149df

Browse files
committed
[HAL] Ad primitives for long lived timestamp to the ticker API.
- A new 64 timestamp type has been added: us_timestamp_t. - Changed type of timestamp in ticker_events iinto us_timestamp_t. - Event queue now have a to store a current, absolute timestamp. - Add alternative versions of ticker_insert_event and ticker_read which accept in input us_timestamp_t. - Add documentation explaining the limitation and behavior of ticker_read and ticker_insert_event.
1 parent 0a3b256 commit 20149df

File tree

1 file changed

+66
-8
lines changed

1 file changed

+66
-8
lines changed

hal/ticker_api.h

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,32 @@
2020
#define MBED_TICKER_API_H
2121

2222
#include <stdint.h>
23+
#include <stdbool.h>
2324
#include "device.h"
2425

26+
/**
27+
* Maximum delta (in us) between too interrupts.
28+
*/
29+
#define MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA 0x70000000ULL
30+
31+
/**
32+
* Legacy format representing a timestamp in us.
33+
* Given it is modeled as a 32 bit integer, this type can represent timestamp
34+
* up to 4294 seconds (71 minutes).
35+
* Prefer using us_timestamp_t which store timestamp as 64 bits integer.
36+
*/
2537
typedef uint32_t timestamp_t;
2638

39+
/**
40+
* A us timestamp stored in a 64 bit integer.
41+
* Can store timestamp up to 584810 years.
42+
*/
43+
typedef uint64_t us_timestamp_t;
44+
2745
/** Ticker's event structure
2846
*/
2947
typedef struct ticker_event_s {
30-
timestamp_t timestamp; /**< Event's timestamp */
48+
us_timestamp_t timestamp; /**< Event's timestamp */
3149
uint32_t id; /**< TimerEvent object */
3250
struct ticker_event_s *next; /**< Next event in the queue */
3351
} ticker_event_t;
@@ -49,6 +67,8 @@ typedef struct {
4967
typedef struct {
5068
ticker_event_handler event_handler; /**< Event handler */
5169
ticker_event_t *head; /**< A pointer to head */
70+
us_timestamp_t timestamp; /**< Store the last timestamp used */
71+
bool initialized; /**< Indicate if the instance is initialized */
5272
} ticker_event_queue_t;
5373

5474
/** Ticker's data structure
@@ -72,43 +92,81 @@ extern "C" {
7292
* @param data The ticker's data
7393
* @param handler A handler to be set
7494
*/
75-
void ticker_set_handler(const ticker_data_t *const data, ticker_event_handler handler);
95+
void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
7696

7797
/** IRQ handler that goes through the events to trigger overdue events.
7898
*
7999
* @param data The ticker's data
80100
*/
81-
void ticker_irq_handler(const ticker_data_t *const data);
101+
void ticker_irq_handler(const ticker_data_t *const ticker);
82102

83103
/** Remove an event from the queue
84104
*
85105
* @param data The ticker's data
86106
* @param obj The event object to be removed from the queue
87107
*/
88-
void ticker_remove_event(const ticker_data_t *const data, ticker_event_t *obj);
108+
void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
109+
110+
/** Insert an event to the queue
111+
*
112+
* The event will be executed in timestamp - ticker_read().
113+
*
114+
* @warning This function does not consider timestamp in the past. If an event
115+
* is inserted with a timestamp less than the current timestamp then the event
116+
* will be executed in timestamp - ticker_read() us.
117+
* The internal counter wrap very quickly it is hard to decide weither an
118+
* event is in the past or in 1 hour.
119+
*
120+
* @note prefer the use of ticker_insert_event_us which allows registration of
121+
* absolute timestamp.
122+
*
123+
* @param data The ticker's data
124+
* @param obj The event object to be inserted to the queue
125+
* @param timestamp The event's timestamp
126+
* @param id The event object
127+
*/
128+
void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
89129

90130
/** Insert an event to the queue
131+
*
132+
* The event will be executed in timestamp - ticker_read_us() us.
133+
*
134+
* @warning If an event is inserted with a timestamp less than the current
135+
* timestamp then the event will **not** be inserted.
91136
*
92137
* @param data The ticker's data
93138
* @param obj The event object to be inserted to the queue
94139
* @param timestamp The event's timestamp
95140
* @param id The event object
96141
*/
97-
void ticker_insert_event(const ticker_data_t *const data, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
142+
void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
98143

99-
/** Read the current ticker's timestamp
144+
/** Read the current (relative) ticker's timestamp
145+
*
146+
* @warning Return a relative timestamp because the counter wrap every 4294
147+
* seconds.
148+
*
149+
* @param data The ticker's data
150+
* @return The current timestamp
151+
*/
152+
timestamp_t ticker_read(const ticker_data_t *const ticker);
153+
154+
/** Read the current (absolute) ticker's timestamp
155+
*
156+
* @warning Return an absolute timestamp counting from the initialization of the
157+
* ticker.
100158
*
101159
* @param data The ticker's data
102160
* @return The current timestamp
103161
*/
104-
timestamp_t ticker_read(const ticker_data_t *const data);
162+
us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
105163

106164
/** Read the next event's timestamp
107165
*
108166
* @param data The ticker's data
109167
* @return 1 if timestamp is pending event, 0 if there's no event pending
110168
*/
111-
int ticker_get_next_timestamp(const ticker_data_t *const data, timestamp_t *timestamp);
169+
int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
112170

113171
/**@}*/
114172

0 commit comments

Comments
 (0)