forked from espressif/arduino-esp32
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxos_timer.h
executable file
·592 lines (519 loc) · 22.3 KB
/
xos_timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/** @file */
// xos_timer.h - XOS Timer API interface and data structures.
// Copyright (c) 2003-2015 Cadence Design Systems, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// NOTE: Do not include this file directly in your application. Including
// xos.h will automatically include this file.
#ifndef __XOS_TIMER_H__
#define __XOS_TIMER_H__
#include "xos_types.h"
#ifdef __cplusplus
extern "C" {
#endif
//-----------------------------------------------------------------------------
///
/// Function pointer type for timer callbacks.
///
//-----------------------------------------------------------------------------
typedef void (XosTimerFunc)(void * arg);
//-----------------------------------------------------------------------------
///
/// Timer event structure. Used to track pending timer events.
///
//-----------------------------------------------------------------------------
typedef struct XosTimer {
struct XosTimer * next; ///< Pointer to next event in list.
uint64_t when; ///< Time (clock cycles) at which to trigger.
uint64_t delta; ///< Delta for next re-trigger, 0 if none.
XosTimerFunc * fn; ///< Function to call when timer expires.
void * arg; ///< Argument to pass to called function.
bool active; ///< Set if active (in some list of events).
#if XOS_OPT_TIMER_WAIT
XosThreadQueue waitq; ///< Queue of threads waiting on this timer.
#endif
#if XOS_TIMER_DEBUG
uint32_t signature;
#endif
} XosTimer;
//-----------------------------------------------------------------------------
// Extern declarations.
//-----------------------------------------------------------------------------
// System clock frequency in cycles per second.
extern uint32_t xos_clock_freq;
///@{
//-----------------------------------------------------------------------------
// Functions to convert from clock cycles to time units and vice versa.
//
// Note that these are integer conversions so for example a cycle count of less
// than one second will convert to zero seconds.
//-----------------------------------------------------------------------------
/// Converts CPU cycles to time in seconds.
///
/// \param cycles Number of CPU cycles.
///
/// \return Equivalent number of seconds (truncated to integer).
static inline uint64_t
xos_cycles_to_secs(uint64_t cycles)
{
return cycles / xos_clock_freq;
}
/// Converts CPU cycles to time in milliseconds.
///
/// \param cycles Number of CPU cycles.
///
/// \return Equivalent number of milliseconds (truncated to integer).
static inline uint64_t
xos_cycles_to_msecs(uint64_t cycles)
{
return (cycles * 1000) / xos_clock_freq;
}
/// Converts CPU cycles to time in microseconds.
///
/// \param cycles Number of CPU cycles.
///
/// \return Equivalent number of microseconds (truncated to integer).
static inline uint64_t
xos_cycles_to_usecs(uint64_t cycles)
{
return (cycles * 1000000) / xos_clock_freq;
}
/// Converts time in seconds to CPU cycle count.
///
/// \param secs Number of seconds.
///
/// \return Equivalent number of CPU cycles.
static inline uint64_t
xos_secs_to_cycles(uint64_t secs)
{
return secs * xos_clock_freq;
}
/// Converts time in milliseconds to CPU cycle count.
///
/// \param msecs Number of milliseconds.
///
/// \return Equivalent number of CPU cycles.
static inline uint64_t
xos_msecs_to_cycles(uint64_t msecs)
{
return (msecs * xos_clock_freq) / 1000;
}
/// Converts time in microseconds to CPU cycle count.
///
/// \param usecs Number of microseconds.
///
/// \return Equivalent number of CPU cycles.
static inline uint64_t
xos_usecs_to_cycles(uint64_t usecs)
{
return (usecs * xos_clock_freq) / 1000000;
}
///@}
//-----------------------------------------------------------------------------
///
/// Set system clock frequency. This is expected to be set only once, and only
/// if the clock frequency is not known at compile time.
///
/// \param freq Frequency in cycles per second.
///
/// \return Returns nothing.
///
//-----------------------------------------------------------------------------
static inline void
xos_set_clock_freq(uint32_t freq)
{
xos_clock_freq = freq;
}
//-----------------------------------------------------------------------------
///
/// Get current system clock frequency.
///
/// \return Returns current system clock frequency in cycles per second.
///
//-----------------------------------------------------------------------------
static inline uint32_t
xos_get_clock_freq()
{
return xos_clock_freq;
}
//-----------------------------------------------------------------------------
///
/// Initialize timer support and start the system timer.
/// This function must be called before calling any other timer function.
///
/// NOTE: The smaller the tick period, the more precisely delays can be
/// specified using timers. However, we also need to make the tick period
/// large enough to allow time both to execute the tick timer interrupt handler
/// and for the application to make reasonable forward progress. If tick_period
/// is too small, the timer interrupt may re-trigger before the timer interrupt
/// handler has returned to the application, thus keeping the processor busy in
/// constantly executing the timer interrupt handler without leaving any cycles
/// for the application. Or, the application might get some cycles but only a
/// fraction of what is spent in the timer interrupt handler, thus severely
/// impacting application performance.
///
/// The exact number of cycles needed to execute the timer interrupt handler
/// is not specified here. It depends on many factors (e.g. use of caches,
/// various processor configuration options, etc) and can vary by orders of
/// magnitude. Also note that the time to execute this handler is variable:
/// when timers expire upon a given tick timer interrupt, their respective
/// timer handler functions are called from within the interrupt handler.
///
/// \param timer_num Which Xtensa timer to use (0..2). This timer
/// must exist and be configured at level 1 or at a
/// medium-priority interrupt level (<=EXCM_LEVEL).
/// If 'timer_num' is -1, then this function will
/// automatically choose the highest priority timer
/// that is suitable for use. This value will be
/// passed to xos_system_timer_select().
///
/// \param tick_period Number of clock (CCOUNT) cycles between ticks.
/// Must range between 0 and UINT32_MAX.
/// Zero is used to specify dynamic tick (tickless)
/// mode.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_start_system_timer(int32_t timer_num, uint32_t tick_period);
//-----------------------------------------------------------------------------
///
/// Get the timer number of the system timer. Useful mainly when XOS has been
/// allowed to choose its own timer via xos_start_system_timer(). Not valid if
/// called before the system timer has been started.
///
/// \return Returns one of XOS_SYS_TIMER_0, XOS_SYS_TIMER_1, XOS_SYS_TIMER_2
/// or XOS_SYS_TIMER_EXTERNAL, or XOS_SYS_TIMER_NONE.
///
//-----------------------------------------------------------------------------
int32_t
xos_get_system_timer_num(void);
//-----------------------------------------------------------------------------
///
/// Initialize timer object.
///
/// \param timer Pointer to timer event structure.
///
/// \return Returns nothing.
///
/// NOTE: This function should not be called on a timer object once it has
/// been activated.
///
//-----------------------------------------------------------------------------
void xos_timer_init(XosTimer * timer);
//-----------------------------------------------------------------------------
// Flags for xos_timer_start().
//-----------------------------------------------------------------------------
#define XOS_TIMER_DELTA 0x0000
#define XOS_TIMER_PERIODIC 0x0001
#define XOS_TIMER_ABSOLUTE 0x0002
#define XOS_TIMER_FROM_NOW 0x0000
#define XOS_TIMER_FROM_LAST 0x0010
//-----------------------------------------------------------------------------
///
/// Start the timer, and when the timer expires, call the specified function
/// (invoke (*fn)(arg)). If the timer is periodic, it will be automatically
/// restarted when it expires.
///
/// The specified timer event structure must have been initialized before
/// first use by calling xos_timer_init().
///
/// The callback function will be called in an interrupt context. Hence it is
/// NOT safe to use any coprocessors in the function, including the FPU. If a
/// coprocessor must be used, then its state must be saved and restored across
/// its use.
///
/// NOTE: If you are using the timer only to wait on (via xos_timer_wait())
/// then it is not necessary to specify a callback function. You should pass
/// NULL for the callback function and zero for the callback argument.
///
/// \param timer Pointer to timer event structure. Must have been
/// initialized. May be active or not.
///
/// \param when When to call the function (see flags).
///
/// \param flags Set of option flags XOS_TIMER_* \n
/// The following flags are mutually exclusive:
/// - XOS_TIMER_DELTA -- when is number of cycles from
/// [see below] (default)
/// - XOS_TIMER_PERIODIC -- when is number of cycles
/// from [see below], and timer continually
/// re-triggers at that interval
/// - XOS_TIMER_ABSOLUTE -- when is absolute value of
/// cycle count \n
/// \n
/// The following flags are mutually exclusive:
/// - XOS_TIMER_FROM_NOW -- *DELTA and *PERIODIC are
/// relative to now (default)
/// - XOS_TIMER_FROM_LAST -- *DELTA and *PERIODIC are
/// relative to the timer event's last specified expiry
/// time (usually in the future if active, in the past
/// if not, absolute 0 if was never activated).
///
/// \param func Function to call (called in timer interrupt context).
/// This argument is optional. Specify NULL if no function
/// is to be called.
///
/// \param arg Argument passed to callback function. Only relevant if
/// 'func' is not NULL.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_timer_start(XosTimer * timer,
uint64_t when,
uint32_t flags,
XosTimerFunc * func,
void * arg);
//-----------------------------------------------------------------------------
///
/// Stop the timer and remove it from the list of active timers. Has no effect
/// if the timer is not active. Any waiting threads are woken up.
///
/// The timer structure must have been initialized at least once, else its
/// contents are undefined and can lead to unpredictable results.
///
/// \param timer Pointer to timer object.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_timer_stop(XosTimer * timer);
//-----------------------------------------------------------------------------
///
/// Reset and restart the timer.
///
/// The timer is reset to go off at time "when" from now. If the timer was not
/// active, it will be activated. If the timer was active, it will be restarted.
/// If the timer is periodic, the period will be set to "when".
/// The timer object must have been initialized at some point before this call.
///
/// \param timer Pointer to timer object.
///
/// \param when Number of cycles from now that the timer will expire.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_timer_restart(XosTimer * timer, uint64_t when);
//-----------------------------------------------------------------------------
///
/// Check if the timer is active. The timer is active if it has been started
/// and not yet expired or canceled.
///
/// \param timer Pointer to timer object.
///
/// \return Returns non-zero if the timer is active, else zero.
///
//-----------------------------------------------------------------------------
static inline int32_t
xos_timer_is_active(XosTimer * timer)
{
return timer ? timer->active : 0;
}
//-----------------------------------------------------------------------------
///
/// Get the repeat period for a periodic timer. For a one-shot timer this will
/// return zero. The period is reported in system clock cycles.
///
/// \param timer Pointer to timer object.
///
/// \return Returns period in cycles, or zero for non-periodic timers.
///
//-----------------------------------------------------------------------------
static inline uint64_t
xos_timer_get_period(XosTimer * timer)
{
return timer ? timer->delta : 0;
}
//-----------------------------------------------------------------------------
///
/// Set the repeat period for a periodic timer. The period must be specified
/// in system clock cycles.
///
/// If the timer is active, the change in period does not take effect until
/// the timer expires at least once after this call.
/// Note that setting a period of zero will effectively turn a periodic timer
/// into a one-shot timer. Similarly, a one-shot timer can be turned into a
/// periodic timer.
///
/// \param timer Pointer to timer object.
///
/// \param period Repeat period in system clock cycles.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_timer_set_period(XosTimer * timer, uint64_t period);
//-----------------------------------------------------------------------------
///
/// Get the current system cycle count. This accounts for the periodic rollover
/// of the 32-bit CCOUNT cycle counter and returns a 64-bit value.
///
/// \return Returns the current system cycle count.
///
//-----------------------------------------------------------------------------
static inline uint64_t
xos_get_system_cycles(void)
{
extern uint64_t xos_system_cycles;
extern uint32_t xos_last_ccount;
// xos_last_ccount was updated when xos_system_cycles was last updated.
// We need to add in the number of cycles elapsed since then.
return xos_system_cycles + (xos_get_ccount() - xos_last_ccount);
}
//-----------------------------------------------------------------------------
///
/// Put calling thread to sleep for at least the specified number of cycles.
/// The actual number of cycles spent sleeping may be larger depending upon
/// the granularity of the system timer. Once the specified time has elapsed
/// the thread will be woken and made ready.
///
/// \param cycles Number of system clock cycles to sleep.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_thread_sleep(uint64_t cycles);
//-----------------------------------------------------------------------------
///
/// Put calling thread to sleep for at least the specified number of msec.
/// The actual amount of time spent sleeping may be larger depending upon
/// the granularity of the system timer. Once the specified time has elapsed
/// the thread will be woken and made ready.
///
/// \return msecs The number of milliseconds to sleep.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
static inline int32_t
xos_thread_sleep_msec(uint64_t msecs)
{
return xos_thread_sleep(xos_msecs_to_cycles(msecs));
}
//-----------------------------------------------------------------------------
///
/// Put calling thread to sleep for at least the specified number of usec.
/// The actual amount of time spent sleeping may be larger depending upon
/// the granularity of the system timer. Once the specified time has elapsed
/// the thread will be woken and made ready.
///
/// \return usecs The number of microseconds to sleep.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
static inline int32_t
xos_thread_sleep_usec(uint64_t usecs)
{
return xos_thread_sleep(xos_usecs_to_cycles(usecs));
}
//-----------------------------------------------------------------------------
///
/// Wait on a timer until it expires or is cancelled. The calling thread will
/// be blocked. The timer must be active.
/// NOTE: This operation is only available if XOS_OPT_TIMER_WAIT is set
/// to 1 in the configuration options.
///
/// \param timer Pointer to timer object.
///
/// \return Returns XOS_OK on normal timeout, else an error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_timer_wait(XosTimer * timer);
//-----------------------------------------------------------------------------
// System timer control interface.
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Defines for system timer ID.
//-----------------------------------------------------------------------------
#define XOS_SYS_TIMER_0 0 ///< Internal timer 0
#define XOS_SYS_TIMER_1 1 ///< Internal timer 1
#define XOS_SYS_TIMER_2 2 ///< Internal timer 2
#define XOS_SYS_TIMER_EXTERNAL -2 ///< External timer
#define XOS_SYS_TIMER_NONE -1 ///< No system timer selected
//-----------------------------------------------------------------------------
///
/// This function handles XOS timer tick processing. It must be called by the
/// timer interrupt handler on every timer interrupt. This function computes
/// the time to the next tick and sets it up by calling xos_system_timer_set().
///
//-----------------------------------------------------------------------------
void
xos_tick_handler(void);
//-----------------------------------------------------------------------------
///
/// Selects the timer to use. The selection may be one of the internal timers
/// or an external timer. The default implementation selects an internal timer.
/// This function can be overridden to provide custom timer processing or to
/// support an external timer.
///
/// \param timer_num The internal timer number to select (0-2) or
/// -1 to auto-select a timer. This parameter can
/// be ignored by custom implementations that use
/// an external timer.
///
/// \param psel Pointer to a location where the selected timer
/// ID must be returned. The timer ID must be one
/// of XOS_SYS_TIMER_0, XOS_SYS_TIMER_1, XOS_SYS_TIMER_2
/// or XOS_SYS_TIMER_EXTERNAL.
///
/// \return Returns XOS_OK on success, else error code.
///
//-----------------------------------------------------------------------------
int32_t
xos_system_timer_select(int32_t timer_num, int32_t *psel);
//-----------------------------------------------------------------------------
///
/// Starts the system timer and sets up the first interrupt. This function can
/// be overridden to provide custom timer processing or to support an external
/// timer.
///
/// \param cycles The number of CPU cycles from now when the
/// first interrupt must occur.
///
//-----------------------------------------------------------------------------
void
xos_system_timer_init(uint32_t cycles);
//-----------------------------------------------------------------------------
///
/// Sets the next trigger value of the system timer. The parameter 'cycles' is
/// the number of CPU cycles from now when the interrupt must occur.
/// This function can be overridden to provide custom timer processing or to
/// support an external timer.
///
/// \param cycles The number of CPU cycles from now when the
/// next interrupt must occur.
///
//-----------------------------------------------------------------------------
void
xos_system_timer_set(uint32_t cycles);
#ifdef __cplusplus
}
#endif
#endif // __XOS_TIMER_H__