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 <stdint.h>
33 #include "timex.h"
34 #include "msg.h"
35 #include "mutex.h"
36 
37 #include "board.h"
38 #include "periph_conf.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
49 typedef struct {
50  uint64_t ticks64;
52 
58 typedef struct {
59  uint32_t ticks32;
61 
65 typedef void (*xtimer_callback_t)(void*);
66 
70 typedef struct xtimer {
71  struct xtimer *next;
72  uint32_t target;
73  uint32_t long_target;
76  void *arg;
77 } xtimer_t;
78 
87 static inline xtimer_ticks32_t xtimer_now(void);
88 
94 static inline xtimer_ticks64_t xtimer_now64(void);
95 
101 void xtimer_now_timex(timex_t *out);
102 
108 static inline uint32_t xtimer_now_usec(void);
109 
115 static inline uint64_t xtimer_now_usec64(void);
116 
123 void xtimer_init(void);
124 
134 static inline void xtimer_sleep(uint32_t seconds);
135 
145 static inline void xtimer_usleep(uint32_t microseconds);
146 
158 static inline void xtimer_nanosleep(uint32_t nanoseconds);
159 
169 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks);
170 
180 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks);
181 
189 static inline void xtimer_spin(xtimer_ticks32_t ticks);
190 
208 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period);
209 
226 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
227 
244 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
245 
258 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
259 
272 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
273 
292 static inline void xtimer_set(xtimer_t *timer, uint32_t offset);
293 
301 void xtimer_remove(xtimer_t *timer);
302 
312 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout);
313 
323 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout);
324 
332 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec);
333 
341 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec);
342 
350 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks);
351 
359 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks);
360 
368 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks);
369 
377 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks);
378 
388 
398 
408 
417 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b);
418 
427 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b);
428 
440 int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);
441 
451 void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout);
452 
461 #ifndef XTIMER_BACKOFF
462 #define XTIMER_BACKOFF 30
463 #endif
464 
484 #ifndef XTIMER_OVERHEAD
485 #define XTIMER_OVERHEAD 20
486 #endif
487 
488 #ifndef XTIMER_ISR_BACKOFF
489 
497 #define XTIMER_ISR_BACKOFF 20
498 #endif
499 
500 #ifndef XTIMER_PERIODIC_SPIN
501 
507 #define XTIMER_PERIODIC_SPIN (XTIMER_BACKOFF * 2)
508 #endif
509 
510 #ifndef XTIMER_PERIODIC_RELATIVE
511 
520 #define XTIMER_PERIODIC_RELATIVE (512)
521 #endif
522 
523 /*
524  * Default xtimer configuration
525  */
526 #ifndef XTIMER_DEV
527 
530 #define XTIMER_DEV TIMER_DEV(0)
531 
534 #define XTIMER_CHAN (0)
535 
536 #if (TIMER_0_MAX_VALUE) == 0xfffffful
537 #define XTIMER_WIDTH (24)
538 #elif (TIMER_0_MAX_VALUE) == 0xffff
539 #define XTIMER_WIDTH (16)
540 #endif
541 
542 #endif
543 
544 #ifndef XTIMER_WIDTH
545 
551 #define XTIMER_WIDTH (32)
552 #endif
553 
554 #if (XTIMER_WIDTH != 32) || DOXYGEN
555 
564 #define XTIMER_MASK ((0xffffffff >> XTIMER_WIDTH) << XTIMER_WIDTH)
565 #else
566 #define XTIMER_MASK (0)
567 #endif
568 
572 #define XTIMER_HZ_BASE (1000000ul)
573 
574 #ifndef XTIMER_HZ
575 
578 #define XTIMER_HZ XTIMER_HZ_BASE
579 #endif
580 
581 #ifndef XTIMER_SHIFT
582 #if (XTIMER_HZ == 32768ul)
583 /* No shift necessary, the conversion is not a power of two and is handled by
584  * functions in tick_conversion.h */
585 #define XTIMER_SHIFT (0)
586 #elif (XTIMER_HZ == XTIMER_HZ_BASE)
587 
602 #define XTIMER_SHIFT (0)
603 #elif (XTIMER_HZ >> 1 == XTIMER_HZ_BASE) || (XTIMER_HZ << 1 == XTIMER_HZ_BASE)
604 #define XTIMER_SHIFT (1)
605 #elif (XTIMER_HZ >> 2 == XTIMER_HZ_BASE) || (XTIMER_HZ << 2 == XTIMER_HZ_BASE)
606 #define XTIMER_SHIFT (2)
607 #elif (XTIMER_HZ >> 3 == XTIMER_HZ_BASE) || (XTIMER_HZ << 3 == XTIMER_HZ_BASE)
608 #define XTIMER_SHIFT (3)
609 #elif (XTIMER_HZ >> 4 == XTIMER_HZ_BASE) || (XTIMER_HZ << 4 == XTIMER_HZ_BASE)
610 #define XTIMER_SHIFT (4)
611 #elif (XTIMER_HZ >> 5 == XTIMER_HZ_BASE) || (XTIMER_HZ << 5 == XTIMER_HZ_BASE)
612 #define XTIMER_SHIFT (5)
613 #else
614 #error "XTIMER_SHIFT cannot be derived for given XTIMER_HZ, verify settings!"
615 #endif
616 #else
617 #error "XTIMER_SHIFT is set relative to XTIMER_HZ, no manual define required!"
618 #endif
619 
620 #include "xtimer/tick_conversion.h"
621 
622 #include "xtimer/implementation.h"
623 
624 #ifdef __cplusplus
625 }
626 #endif
627 
629 #endif /* XTIMER_H */
static xtimer_ticks64_t xtimer_ticks64(uint64_t ticks)
Create an xtimer time stamp, 64 bit version.
static uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks)
Convert xtimer ticks to microseconds, 64 bit version.
A timex timestamp.
Definition: timex.h:79
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.
static void xtimer_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
static uint64_t xtimer_now_usec64(void)
get the current system time in microseconds since start
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 uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
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 xtimer_ticks64_t xtimer_now64(void)
get the current system time as 64bit time stamp
void xtimer_now_timex(timex_t *out)
get the current system time into a timex_t
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.
xtimer tick <-> seconds conversions for different values of XTIMER_HZ
uint32_t target
lower 32bit absolute target time
Definition: xtimer.h:72
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
static void xtimer_nanosleep(uint32_t nanoseconds)
Stop execution of a thread for some time.
Utility library for comparing and computing timestamps.
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
struct xtimer xtimer_t
xtimer timer structure
void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout)
Set timeout thread flag after timeout.
uint64_t ticks64
Tick count.
Definition: xtimer.h:50
Messaging API for inter process communication.
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.
uint32_t ticks32
Tick count.
Definition: xtimer.h:59
void(* xtimer_callback_t)(void *)
xtimer callback type
Definition: xtimer.h:65
static void xtimer_spin(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, blocking.
Describes a message object which can be sent between threads.
Definition: msg.h:184
static void xtimer_tsleep64(xtimer_ticks64_t ticks)
Stop execution of a thread for some time, 64bit version.
static xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
RIOT synchronization API.
static void xtimer_set(xtimer_t *timer, uint32_t offset)
Set a timer to execute a callback at some time in the future.
static void xtimer_sleep(uint32_t seconds)
Pause the execution of a thread for some seconds.
static xtimer_ticks32_t xtimer_ticks(uint32_t ticks)
Create an xtimer time stamp.
static bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b)
Compare two xtimer time stamps, 64 bit version.
uint32_t long_target
upper 32bit absolute target time
Definition: xtimer.h:73
xtimer timer structure
Definition: xtimer.h:70
Mutex structure.
Definition: mutex.h:36
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.
void xtimer_init(void)
xtimer initialization function
void xtimer_remove(xtimer_t *timer)
remove a timer
static int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout)
receive a message blocking but with timeout, 64bit version
void * arg
argument to pass to callback function
Definition: xtimer.h:76
static bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compare two xtimer time stamps.
xtimer implementation
xtimer_callback_t callback
callback function to call when timer expires
Definition: xtimer.h:74
xtimer timestamp (32 bit)
Definition: xtimer.h:58
struct xtimer * next
reference to next timer in timer lists
Definition: xtimer.h:71
xtimer timestamp (64 bit)
Definition: xtimer.h:49
static uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks)
Convert xtimer ticks to microseconds.
static void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
static int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout)
receive a message blocking but with timeout
static xtimer_ticks32_t xtimer_now(void)
get the current system time as 32bit time stamp value