xtimer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 Kaspar Schleiser <kaspar@schleiser.de>
3  * SPDX-FileCopyrightText: 2016 Eistec AB
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
46 #define XTIMER_H
47 
48 #include <stdbool.h>
49 #include <stdint.h>
50 
51 #include "modules.h"
52 #include "timex.h"
53 #ifdef MODULE_CORE_MSG
54 #include "msg.h"
55 #endif /* MODULE_CORE_MSG */
56 #include "mutex.h"
57 #include "sched.h"
58 #include "rmutex.h"
59 
60 #if IS_USED(MODULE_ZTIMER64_XTIMER_COMPAT)
61 #include "ztimer64/xtimer_compat.h" /* IWYU pragma: export */
62 #elif IS_USED(MODULE_ZTIMER_XTIMER_COMPAT)
63 #include "ztimer/xtimer_compat.h" /* IWYU pragma: export */
64 #else
65 #include "board.h"
66 #if !IS_USED(MODULE_XTIMER_ON_ZTIMER)
67 #include "periph_conf.h"
68 #endif
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
79 typedef struct {
80  uint64_t ticks64;
82 
88 typedef struct {
89  uint32_t ticks32;
91 
95 typedef void (*xtimer_callback_t)(void*);
96 
100 typedef struct xtimer {
101  struct xtimer *next;
102  uint32_t offset;
103  uint32_t long_offset;
104  uint32_t start_time;
105  uint32_t long_start_time;
108  void *arg;
110 
119 static inline xtimer_ticks32_t xtimer_now(void);
120 
126 static inline xtimer_ticks64_t xtimer_now64(void);
127 
134 
140 static inline uint32_t xtimer_now_usec(void);
141 
147 static inline uint64_t xtimer_now_usec64(void);
148 
155 void xtimer_init(void);
156 
166 static inline void xtimer_sleep(uint32_t seconds);
167 
173 static inline void xtimer_msleep(uint32_t milliseconds);
174 
184 static inline void xtimer_usleep(uint32_t microseconds);
185 
193 static inline void xtimer_usleep64(uint64_t microseconds);
194 
210 static inline void xtimer_nanosleep(uint32_t nanoseconds);
211 
221 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks);
222 
232 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks);
233 
241 static inline void xtimer_spin(xtimer_ticks32_t ticks);
242 
260 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period);
261 
272 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
273 
284 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
285 
302 static inline void xtimer_set(xtimer_t *timer, uint32_t offset);
303 
321 static inline void xtimer_set64(xtimer_t *timer, uint64_t offset_us);
322 
330 void xtimer_remove(xtimer_t *timer);
331 
337 static inline bool xtimer_is_set(const xtimer_t *timer);
338 
346 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec);
347 
355 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec);
356 
364 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks);
365 
373 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks);
374 
382 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks);
383 
391 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks);
392 
402 
412 
422 
432 
442 
452 int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);
453 
463 int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us);
464 
465 #if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
466 
476 void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout);
477 
486 void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout);
487 #endif
488 
497 uint64_t xtimer_left_usec(const xtimer_t *timer);
498 
499 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
514 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
515 
530 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
531 
541 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout);
542 
552 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout);
553 #endif
554 
563 #ifndef XTIMER_BACKOFF
564 #define XTIMER_BACKOFF 30
565 #endif
566 
567 #ifndef XTIMER_ISR_BACKOFF
576 #define XTIMER_ISR_BACKOFF 20
577 #endif
578 
579 /*
580  * Default xtimer configuration
581  */
582 #ifndef XTIMER_DEV
586 #define XTIMER_DEV TIMER_DEV(0)
590 #define XTIMER_CHAN (0)
591 
592 #if (TIMER_0_MAX_VALUE) == 0xfffffful
593 #define XTIMER_WIDTH (24)
594 #elif (TIMER_0_MAX_VALUE) == 0xffff
595 #define XTIMER_WIDTH (16)
596 #endif
597 
598 #endif
599 
600 #ifndef XTIMER_WIDTH
607 #define XTIMER_WIDTH (32)
608 #endif
609 
610 #if (XTIMER_WIDTH != 32) || DOXYGEN
620 #define XTIMER_MASK ((0xffffffff >> XTIMER_WIDTH) << XTIMER_WIDTH)
621 #else
622 #define XTIMER_MASK (0)
623 #endif
624 
628 #define XTIMER_HZ_BASE (1000000ul)
629 
630 #if !defined(XTIMER_HZ) && !defined(MODULE_XTIMER_ON_ZTIMER)
634 #define XTIMER_HZ XTIMER_HZ_BASE
635 #endif
636 
637 #if !defined(XTIMER_SHIFT) && !defined(MODULE_XTIMER_ON_ZTIMER)
638 #if (XTIMER_HZ == 32768ul)
639 /* No shift necessary, the conversion is not a power of two and is handled by
640  * functions in tick_conversion.h */
641 #define XTIMER_SHIFT (0)
642 #elif (XTIMER_HZ == XTIMER_HZ_BASE)
658 #define XTIMER_SHIFT (0)
659 #elif (XTIMER_HZ >> 1 == XTIMER_HZ_BASE) || (XTIMER_HZ << 1 == XTIMER_HZ_BASE)
660 #define XTIMER_SHIFT (1)
661 #elif (XTIMER_HZ >> 2 == XTIMER_HZ_BASE) || (XTIMER_HZ << 2 == XTIMER_HZ_BASE)
662 #define XTIMER_SHIFT (2)
663 #elif (XTIMER_HZ >> 3 == XTIMER_HZ_BASE) || (XTIMER_HZ << 3 == XTIMER_HZ_BASE)
664 #define XTIMER_SHIFT (3)
665 #elif (XTIMER_HZ >> 4 == XTIMER_HZ_BASE) || (XTIMER_HZ << 4 == XTIMER_HZ_BASE)
666 #define XTIMER_SHIFT (4)
667 #elif (XTIMER_HZ >> 5 == XTIMER_HZ_BASE) || (XTIMER_HZ << 5 == XTIMER_HZ_BASE)
668 #define XTIMER_SHIFT (5)
669 #elif (XTIMER_HZ >> 6 == XTIMER_HZ_BASE) || (XTIMER_HZ << 6 == XTIMER_HZ_BASE)
670 #define XTIMER_SHIFT (6)
671 #else
672 #error "XTIMER_SHIFT cannot be derived for given XTIMER_HZ, verify settings!"
673 #endif
674 #elif !defined(MODULE_XTIMER_ON_ZTIMER)
675 #error "XTIMER_SHIFT is set relative to XTIMER_HZ, no manual define required!"
676 #endif
677 
678 #include "xtimer/tick_conversion.h"
679 
680 #include "xtimer/implementation.h"
681 
682 #ifdef __cplusplus
683 }
684 #endif
685 
686 #endif /* MODULE_ZTIMER_XTIMER_COMPAT */
687 
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
Set timeout thread flag after timeout.
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
static xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
static void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
Set a timer that sends a message, 64bit version.
void xtimer_remove(xtimer_t *timer)
remove a timer
static void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread, 64bit version.
static bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compare two xtimer time stamps, 64 bit version.
static xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
static void xtimer_set64(xtimer_t *timer, uint64_t offset_us)
Set a timer to execute a callback at some time in the future, 64bit version.
void(* xtimer_callback_t)(void *)
xtimer callback type
Definition: xtimer.h:95
static void xtimer_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
static void xtimer_msleep(uint32_t milliseconds)
Pause the execution of a thread for some milliseconds.
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
static xtimer_ticks32_t xtimer_diff32_64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compute 32 bit difference between two 64 bit xtimer time stamps.
int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us)
lock a rmutex but with timeout
static xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
Create an xtimer time stamp, 64 bit version.
static void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period)
will cause the calling thread to be suspended until the absolute time (last_wakeup + period).
static void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
Set a timer that sends a message.
static bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compare two xtimer time stamps.
static uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
Convert xtimer ticks to microseconds.
static xtimer_ticks64_t xtimer_diff64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compute difference between two xtimer time stamps, 64 bit version.
int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
lock a mutex but with timeout
static void xtimer_usleep64(uint64_t microseconds)
Pause the execution of a thread for some microseconds.
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
void xtimer_now_timex(timex_t *out)
get the current system time into a timex_t
void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
Set timeout thread flag after timeout.
static void xtimer_tsleep64(xtimer_ticks64_t ticks)
Stop execution of a thread for some time, 64bit version.
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
static void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
void xtimer_init(void)
xtimer initialization function
static void xtimer_spin(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, blocking.
static uint64_t xtimer_now_usec64(void)
get the current system time in microseconds since start
static void xtimer_set(xtimer_t *timer, uint32_t offset)
Set a timer to execute a callback at some time in the future.
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
struct xtimer xtimer_t
xtimer timer structure
static xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
Create an xtimer time stamp.
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value
static bool xtimer_is_set(const xtimer_t *timer)
state if an xtimer is currently set (waiting to be expired)
static uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
Convert xtimer ticks to microseconds, 64 bit version.
static int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
uint64_t xtimer_left_usec(const xtimer_t *timer)
Get remaining time of timer.
xtimer implementation
Common macros and compiler attributes/pragmas configuration.
Mutex for thread synchronization.
Recursive Mutex for thread synchronization.
Scheduler API definition.
Describes a message object which can be sent between threads.
Definition: msg.h:192
Mutex structure.
Definition: mutex.h:36
Mutex structure.
Definition: rmutex.h:34
A timex timestamp.
Definition: timex.h:45
xtimer timestamp (32 bit)
Definition: xtimer.h:88
uint32_t ticks32
Tick count.
Definition: xtimer.h:89
xtimer timestamp (64 bit)
Definition: xtimer.h:79
uint64_t ticks64
Tick count.
Definition: xtimer.h:80
xtimer timer structure
Definition: xtimer.h:100
uint32_t long_offset
upper 32bit offset time
Definition: xtimer.h:103
struct xtimer * next
reference to next timer in timer lists
Definition: xtimer.h:101
void * arg
argument to pass to callback function
Definition: xtimer.h:108
uint32_t start_time
lower 32bit absolute start time
Definition: xtimer.h:104
uint32_t long_start_time
upper 32bit absolute start time
Definition: xtimer.h:105
xtimer_callback_t callback
callback function to call when timer expires
Definition: xtimer.h:106
uint32_t offset
lower 32bit offset time
Definition: xtimer.h:102
xtimer tick <-> seconds conversions for different values of XTIMER_HZ
Utility library for comparing and computing timestamps.
ztimer64 xtimer wrapper interface
ztimer xtimer wrapper interface