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 "kernel_types.h"
40 
41 #include "board.h"
42 #include "periph_conf.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
53 typedef struct {
54  uint64_t ticks64;
56 
62 typedef struct {
63  uint32_t ticks32;
65 
69 typedef void (*xtimer_callback_t)(void*);
70 
74 typedef struct xtimer {
75  struct xtimer *next;
76  uint32_t offset;
77  uint32_t long_offset;
78  uint32_t start_time;
79  uint32_t long_start_time;
82  void *arg;
83 } xtimer_t;
84 
93 static inline xtimer_ticks32_t xtimer_now(void);
94 
100 static inline xtimer_ticks64_t xtimer_now64(void);
101 
107 void xtimer_now_timex(timex_t *out);
108 
114 static inline uint32_t xtimer_now_usec(void);
115 
121 static inline uint64_t xtimer_now_usec64(void);
122 
129 void xtimer_init(void);
130 
140 static inline void xtimer_sleep(uint32_t seconds);
141 
151 static inline void xtimer_usleep(uint32_t microseconds);
152 
164 static inline void xtimer_nanosleep(uint32_t nanoseconds);
165 
175 static inline void xtimer_tsleep32(xtimer_ticks32_t ticks);
176 
186 static inline void xtimer_tsleep64(xtimer_ticks64_t ticks);
187 
195 static inline void xtimer_spin(xtimer_ticks32_t ticks);
196 
214 static inline void xtimer_periodic_wakeup(xtimer_ticks32_t *last_wakeup, uint32_t period);
215 
226 static inline void xtimer_set_wakeup(xtimer_t *timer, uint32_t offset, kernel_pid_t pid);
227 
238 static inline void xtimer_set_wakeup64(xtimer_t *timer, uint64_t offset, kernel_pid_t pid);
239 
256 static inline void xtimer_set(xtimer_t *timer, uint32_t offset);
257 
275 static inline void xtimer_set64(xtimer_t *timer, uint64_t offset_us);
276 
284 void xtimer_remove(xtimer_t *timer);
285 
293 static inline xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec);
294 
302 static inline xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec);
303 
311 static inline uint32_t xtimer_usec_from_ticks(xtimer_ticks32_t ticks);
312 
320 static inline uint64_t xtimer_usec_from_ticks64(xtimer_ticks64_t ticks);
321 
329 static inline xtimer_ticks32_t xtimer_ticks(uint32_t ticks);
330 
338 static inline xtimer_ticks64_t xtimer_ticks64(uint64_t ticks);
339 
349 
359 
369 
378 static inline bool xtimer_less(xtimer_ticks32_t a, xtimer_ticks32_t b);
379 
388 static inline bool xtimer_less64(xtimer_ticks64_t a, xtimer_ticks64_t b);
389 
399 int xtimer_mutex_lock_timeout(mutex_t *mutex, uint64_t us);
400 
410 void xtimer_set_timeout_flag(xtimer_t *t, uint32_t timeout);
411 
412 #if defined(MODULE_CORE_MSG) || defined(DOXYGEN)
413 
427 static inline void xtimer_set_msg(xtimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid);
428 
443 static inline void xtimer_set_msg64(xtimer_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid);
444 
454 static inline int xtimer_msg_receive_timeout(msg_t *msg, uint32_t timeout);
455 
465 static inline int xtimer_msg_receive_timeout64(msg_t *msg, uint64_t timeout);
466 #endif
467 
476 #ifndef XTIMER_BACKOFF
477 #define XTIMER_BACKOFF 30
478 #endif
479 
480 #ifndef XTIMER_ISR_BACKOFF
481 
489 #define XTIMER_ISR_BACKOFF 20
490 #endif
491 
492 /*
493  * Default xtimer configuration
494  */
495 #ifndef XTIMER_DEV
496 
499 #define XTIMER_DEV TIMER_DEV(0)
500 
503 #define XTIMER_CHAN (0)
504 
505 #if (TIMER_0_MAX_VALUE) == 0xfffffful
506 #define XTIMER_WIDTH (24)
507 #elif (TIMER_0_MAX_VALUE) == 0xffff
508 #define XTIMER_WIDTH (16)
509 #endif
510 
511 #endif
512 
513 #ifndef XTIMER_WIDTH
514 
520 #define XTIMER_WIDTH (32)
521 #endif
522 
523 #if (XTIMER_WIDTH != 32) || DOXYGEN
524 
533 #define XTIMER_MASK ((0xffffffff >> XTIMER_WIDTH) << XTIMER_WIDTH)
534 #else
535 #define XTIMER_MASK (0)
536 #endif
537 
541 #define XTIMER_HZ_BASE (1000000ul)
542 
543 #ifndef XTIMER_HZ
544 
547 #define XTIMER_HZ XTIMER_HZ_BASE
548 #endif
549 
550 #ifndef XTIMER_SHIFT
551 #if (XTIMER_HZ == 32768ul)
552 /* No shift necessary, the conversion is not a power of two and is handled by
553  * functions in tick_conversion.h */
554 #define XTIMER_SHIFT (0)
555 #elif (XTIMER_HZ == XTIMER_HZ_BASE)
556 
571 #define XTIMER_SHIFT (0)
572 #elif (XTIMER_HZ >> 1 == XTIMER_HZ_BASE) || (XTIMER_HZ << 1 == XTIMER_HZ_BASE)
573 #define XTIMER_SHIFT (1)
574 #elif (XTIMER_HZ >> 2 == XTIMER_HZ_BASE) || (XTIMER_HZ << 2 == XTIMER_HZ_BASE)
575 #define XTIMER_SHIFT (2)
576 #elif (XTIMER_HZ >> 3 == XTIMER_HZ_BASE) || (XTIMER_HZ << 3 == XTIMER_HZ_BASE)
577 #define XTIMER_SHIFT (3)
578 #elif (XTIMER_HZ >> 4 == XTIMER_HZ_BASE) || (XTIMER_HZ << 4 == XTIMER_HZ_BASE)
579 #define XTIMER_SHIFT (4)
580 #elif (XTIMER_HZ >> 5 == XTIMER_HZ_BASE) || (XTIMER_HZ << 5 == XTIMER_HZ_BASE)
581 #define XTIMER_SHIFT (5)
582 #elif (XTIMER_HZ >> 6 == XTIMER_HZ_BASE) || (XTIMER_HZ << 6 == XTIMER_HZ_BASE)
583 #define XTIMER_SHIFT (6)
584 #else
585 #error "XTIMER_SHIFT cannot be derived for given XTIMER_HZ, verify settings!"
586 #endif
587 #else
588 #error "XTIMER_SHIFT is set relative to XTIMER_HZ, no manual define required!"
589 #endif
590 
591 #include "xtimer/tick_conversion.h"
592 
593 #include "xtimer/implementation.h"
594 
595 #ifdef __cplusplus
596 }
597 #endif
598 
600 #endif /* XTIMER_H */
Types used by the kernel.
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:84
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.
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
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
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.
uint32_t long_start_time
upper 32bit absolute start time
Definition: xtimer.h:79
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:54
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:63
void(* xtimer_callback_t)(void *)
xtimer callback type
Definition: xtimer.h:69
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 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.
static xtimer_ticks64_t xtimer_ticks_from_usec64(uint64_t usec)
Convert microseconds to xtimer ticks, 64 bit version.
uint32_t start_time
lower 32bit absolute start time
Definition: xtimer.h:78
static xtimer_ticks32_t xtimer_diff(xtimer_ticks32_t a, xtimer_ticks32_t b)
Compute difference between two xtimer time stamps.
Mutex for thread synchronization.
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.
uint32_t long_offset
upper 32bit offset time
Definition: xtimer.h:77
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.
xtimer timer structure
Definition: xtimer.h:74
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:82
uint32_t offset
lower 32bit offset time
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:80
xtimer timestamp (32 bit)
Definition: xtimer.h:62
struct xtimer * next
reference to next timer in timer lists
Definition: xtimer.h:75
xtimer timestamp (64 bit)
Definition: xtimer.h:53
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