20
20
#define MBED_TICKER_API_H
21
21
22
22
#include <stdint.h>
23
+ #include <stdbool.h>
23
24
#include "device.h"
24
25
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
+ */
25
37
typedef uint32_t timestamp_t ;
26
38
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
+
27
45
/** Ticker's event structure
28
46
*/
29
47
typedef struct ticker_event_s {
30
- timestamp_t timestamp ; /**< Event's timestamp */
48
+ us_timestamp_t timestamp ; /**< Event's timestamp */
31
49
uint32_t id ; /**< TimerEvent object */
32
50
struct ticker_event_s * next ; /**< Next event in the queue */
33
51
} ticker_event_t ;
@@ -49,6 +67,8 @@ typedef struct {
49
67
typedef struct {
50
68
ticker_event_handler event_handler ; /**< Event handler */
51
69
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 */
52
72
} ticker_event_queue_t ;
53
73
54
74
/** Ticker's data structure
@@ -72,43 +92,81 @@ extern "C" {
72
92
* @param data The ticker's data
73
93
* @param handler A handler to be set
74
94
*/
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 );
76
96
77
97
/** IRQ handler that goes through the events to trigger overdue events.
78
98
*
79
99
* @param data The ticker's data
80
100
*/
81
- void ticker_irq_handler (const ticker_data_t * const data );
101
+ void ticker_irq_handler (const ticker_data_t * const ticker );
82
102
83
103
/** Remove an event from the queue
84
104
*
85
105
* @param data The ticker's data
86
106
* @param obj The event object to be removed from the queue
87
107
*/
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 );
89
129
90
130
/** 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.
91
136
*
92
137
* @param data The ticker's data
93
138
* @param obj The event object to be inserted to the queue
94
139
* @param timestamp The event's timestamp
95
140
* @param id The event object
96
141
*/
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 );
98
143
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.
100
158
*
101
159
* @param data The ticker's data
102
160
* @return The current timestamp
103
161
*/
104
- timestamp_t ticker_read (const ticker_data_t * const data );
162
+ us_timestamp_t ticker_read_us (const ticker_data_t * const ticker );
105
163
106
164
/** Read the next event's timestamp
107
165
*
108
166
* @param data The ticker's data
109
167
* @return 1 if timestamp is pending event, 0 if there's no event pending
110
168
*/
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 );
112
170
113
171
/**@}*/
114
172
0 commit comments