xtimer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
3  * Copyright (C) 2016 Eistec AB
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
29 #ifndef XTIMER_H
30 #define XTIMER_H
31 
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include "timex.h"
35 #ifdef MODULE_CORE_MSG
36 #include "msg.h"
37 #endif /* MODULE_CORE_MSG */
38 #include "mutex.h"
39 #include "sched.h"
40 #include "rmutex.h"
41 
42 #ifdef MODULE_ZTIMER_XTIMER_COMPAT
43 #include "ztimer/xtimer_compat.h"
44 #else
45 
46 #include "board.h"
47 #ifndef MODULE_XTIMER_ON_ZTIMER
48 #include "periph_conf.h"
49 #endif
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
60 typedef struct {
61  uint64_t ticks64;
63 
69 typedef struct {
70  uint32_t ticks32;
72 
76 typedef void (*xtimer_callback_t)(void*);
77 
81 typedef struct xtimer {
82  struct xtimer *next;
83  uint32_t offset;
84  uint32_t long_offset;
85  uint32_t start_time;
86  uint32_t long_start_time;
89  void *arg;
90 } xtimer_t;
91 
100 static inline xtimer_ticks32_t xtimer_now(void);
101 
107 static inline xtimer_ticks64_t xtimer_now64(void);
108 
114 void xtimer_now_timex(timex_t *out);
115 
121 static inline uint32_t xtimer_now_usec(void);
122 
128 static inline uint64_t xtimer_now_usec64(void);
129 
136 void xtimer_init(void);
137 
147 static inline void xtimer_sleep(uint32_t seconds);
148 
154 static inline void xtimer_msleep(uint32_t milliseconds);
155 
165 static inline void xtimer_usleep(uint32_t microseconds);
166 
174 static inline void xtimer_usleep64(uint64_t microseconds);
175 
187 static inline void xtimer_nanosleep(uint32_t nanoseconds);
188 
198 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks);
199 
209 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks);
210 
218 static inline void xtimer_spin(xtimer_ticks32_t ticks);
219 
237 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period);
238 
249 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
250 
261 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
262 
279 static inline void xtimer_set(xtimer_t *timer, uint32_t offset);
280 
298 static inline void xtimer_set64(xtimer_t *timer, uint64_t offset_us);
299 
307 void xtimer_remove(xtimer_t *timer);
308 
316 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec);
317 
325 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec);
326 
334 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks);
335 
343 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks);
344 
352 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks);
353 
361 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks);
362 
372 
382 
392 
401 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b);
402 
411 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b);
412 
422 int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);
423 
433 int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us);
434 
435 #if defined(MODULE_CORE_THREAD_FLAGS) || defined(DOXYGEN)
436 
446 void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout);
447 
456 void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout);
457 #endif
458 
467 uint64_t xtimer_left_usec(const xtimer_t *timer);
468 
469 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
470 
484 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
485 
500 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
501 
511 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout);
512 
522 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout);
523 #endif
524 
533 #ifndef XTIMER_BACKOFF
534 #define XTIMER_BACKOFF 30
535 #endif
536 
537 #ifndef XTIMER_ISR_BACKOFF
538 
546 #define XTIMER_ISR_BACKOFF 20
547 #endif
548 
549 /*
550  * Default xtimer configuration
551  */
552 #ifndef XTIMER_DEV
553 
556 #define XTIMER_DEV TIMER_DEV(0)
557 
560 #define XTIMER_CHAN (0)
561 
562 #if (TIMER_0_MAX_VALUE) == 0xfffffful
563 #define XTIMER_WIDTH (24)
564 #elif (TIMER_0_MAX_VALUE) == 0xffff
565 #define XTIMER_WIDTH (16)
566 #endif
567 
568 #endif
569 
570 #ifndef XTIMER_WIDTH
571 
577 #define XTIMER_WIDTH (32)
578 #endif
579 
580 #if (XTIMER_WIDTH != 32) || DOXYGEN
581 
590 #define XTIMER_MASK ((0xffffffff >> XTIMER_WIDTH) << XTIMER_WIDTH)
591 #else
592 #define XTIMER_MASK (0)
593 #endif
594 
598 #define XTIMER_HZ_BASE (1000000ul)
599 
600 #if !defined(XTIMER_HZ) && !defined(MODULE_XTIMER_ON_ZTIMER)
601 
604 #define XTIMER_HZ XTIMER_HZ_BASE
605 #endif
606 
607 #if !defined(XTIMER_SHIFT) && !defined(MODULE_XTIMER_ON_ZTIMER)
608 #if (XTIMER_HZ == 32768ul)
609 /* No shift necessary, the conversion is not a power of two and is handled by
610  * functions in tick_conversion.h */
611 #define XTIMER_SHIFT (0)
612 #elif (XTIMER_HZ == XTIMER_HZ_BASE)
613 
628 #define XTIMER_SHIFT (0)
629 #elif (XTIMER_HZ >> 1 == XTIMER_HZ_BASE) || (XTIMER_HZ << 1 == XTIMER_HZ_BASE)
630 #define XTIMER_SHIFT (1)
631 #elif (XTIMER_HZ >> 2 == XTIMER_HZ_BASE) || (XTIMER_HZ << 2 == XTIMER_HZ_BASE)
632 #define XTIMER_SHIFT (2)
633 #elif (XTIMER_HZ >> 3 == XTIMER_HZ_BASE) || (XTIMER_HZ << 3 == XTIMER_HZ_BASE)
634 #define XTIMER_SHIFT (3)
635 #elif (XTIMER_HZ >> 4 == XTIMER_HZ_BASE) || (XTIMER_HZ << 4 == XTIMER_HZ_BASE)
636 #define XTIMER_SHIFT (4)
637 #elif (XTIMER_HZ >> 5 == XTIMER_HZ_BASE) || (XTIMER_HZ << 5 == XTIMER_HZ_BASE)
638 #define XTIMER_SHIFT (5)
639 #elif (XTIMER_HZ >> 6 == XTIMER_HZ_BASE) || (XTIMER_HZ << 6 == XTIMER_HZ_BASE)
640 #define XTIMER_SHIFT (6)
641 #else
642 #error "XTIMER_SHIFT cannot be derived for given XTIMER_HZ, verify settings!"
643 #endif
644 #elif !defined(MODULE_XTIMER_ON_ZTIMER)
645 #error "XTIMER_SHIFT is set relative to XTIMER_HZ, no manual define required!"
646 #endif
647 
648 #include "xtimer/tick_conversion.h"
649 
650 #include "xtimer/implementation.h"
651 
652 #ifdef __cplusplus
653 }
654 #endif
655 
656 #endif /* MODULE_ZTIMER_XTIMER_COMPAT */
657 
659 #endif /* XTIMER_H */
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
xtimer
xtimer timer structure
Definition: xtimer.h:81
xtimer_t
struct xtimer xtimer_t
xtimer timer structure
xtimer::arg
void * arg
argument to pass to callback function
Definition: xtimer.h:89
xtimer_periodic_wakeup
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).
tick_conversion.h
xtimer tick <-> seconds conversions for different values of XTIMER_HZ
xtimer_less
static bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compare two xtimer time stamps.
xtimer_now_timex
void xtimer_now_timex(timex_t *out)
get the current system time into a timex_t
xtimer_usleep64
static void xtimer_usleep64(uint64_t microseconds)
Pause the execution of a thread for some microseconds.
xtimer_init
void xtimer_init(void)
xtimer initialization function
xtimer_msg_receive_timeout64
static int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
xtimer_ticks64
static xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
Create an xtimer time stamp, 64 bit version.
xtimer_ticks32_t::ticks32
uint32_t ticks32
Tick count.
Definition: xtimer.h:70
xtimer_now64
static xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
xtimer_set_wakeup64
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.
timex_t
A timex timestamp.
Definition: timex.h:89
xtimer_callback_t
void(* xtimer_callback_t)(void *)
xtimer callback type
Definition: xtimer.h:76
xtimer_left_usec
uint64_t xtimer_left_usec(const xtimer_t *timer)
Get remaining time of timer.
xtimer_now
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value
xtimer_now_usec
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
xtimer_msleep
static void xtimer_msleep(uint32_t milliseconds)
Pause the execution of a thread for some milliseconds.
msg.h
Messaging API for inter process communication.
sched.h
Scheduler API definition.
xtimer_set_msg64
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.
xtimer_diff32_64
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.
xtimer_tsleep32
static void xtimer_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
xtimer_rmutex_lock_timeout
int xtimer_rmutex_lock_timeout(rmutex_t *rmutex, uint64_t us)
lock a rmutex but with timeout
implementation.h
xtimer implementation
xtimer_ticks_from_usec
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
xtimer_ticks_from_usec64
static xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
xtimer_diff64
static xtimer_ticks64_t xtimer_diff64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compute difference between two xtimer time stamps, 64 bit version.
timex.h
Utility library for comparing and computing timestamps.
xtimer::long_start_time
uint32_t long_start_time
upper 32bit absolute start time
Definition: xtimer.h:86
xtimer_set
static void xtimer_set(xtimer_t *timer, uint32_t offset)
Set a timer to execute a callback at some time in the future.
xtimer_compat.h
ztimer xtimer wrapper interface
xtimer_diff
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
xtimer_usleep
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
xtimer_ticks64_t::ticks64
uint64_t ticks64
Tick count.
Definition: xtimer.h:61
xtimer_ticks
static xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
Create an xtimer time stamp.
xtimer_less64
static bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compare two xtimer time stamps, 64 bit version.
xtimer_mutex_lock_timeout
int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us)
lock a mutex but with timeout
rmutex_t
Mutex structure.
Definition: rmutex.h:43
xtimer_set_msg
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.
xtimer::start_time
uint32_t start_time
lower 32bit absolute start time
Definition: xtimer.h:85
xtimer_msg_receive_timeout
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
xtimer_set_timeout_flag
void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
Set timeout thread flag after timeout.
rmutex.h
Recursive Mutex for thread synchronization.
xtimer_tsleep64
static void xtimer_tsleep64(xtimer_ticks64_t ticks)
Stop execution of a thread for some time, 64bit version.
xtimer_ticks32_t
xtimer timestamp (32 bit)
Definition: xtimer.h:69
xtimer_nanosleep
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
mutex.h
Mutex for thread synchronization.
xtimer_ticks64_t
xtimer timestamp (64 bit)
Definition: xtimer.h:60
xtimer::callback
xtimer_callback_t callback
callback function to call when timer expires
Definition: xtimer.h:87
xtimer_usec_from_ticks
static uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
Convert xtimer ticks to microseconds.
xtimer_set_wakeup
static void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
xtimer::next
struct xtimer * next
reference to next timer in timer lists
Definition: xtimer.h:82
xtimer_set64
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.
xtimer_sleep
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
xtimer::offset
uint32_t offset
lower 32bit offset time
Definition: xtimer.h:83
msg_t
Describes a message object which can be sent between threads.
Definition: msg.h:185
xtimer_usec_from_ticks64
static uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
Convert xtimer ticks to microseconds, 64 bit version.
xtimer_spin
static void xtimer_spin(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, blocking.
xtimer_now_usec64
static uint64_t xtimer_now_usec64(void)
get the current system time in microseconds since start
xtimer_remove
void xtimer_remove(xtimer_t *timer)
remove a timer
xtimer_set_timeout_flag64
void xtimer_set_timeout_flag64(xtimer_t *t, uint64_t timeout)
Set timeout thread flag after timeout.
xtimer::long_offset
uint32_t long_offset
upper 32bit offset time
Definition: xtimer.h:84
mutex_t
Mutex structure.
Definition: mutex.h:120