ztimer64.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2021 Inria
3  * SPDX-FileCopyrightText: 2021 Freie Universität Berlin
4  * SPDX-FileCopyrightText: 2021 Kaspar Schleiser <kaspar@schleiser.de>
5  * SPDX-License-Identifier: LGPL-2.1-only
6  */
7 
8 #pragma once
9 
63 #include <stdint.h>
64 
65 #include "irq.h"
66 #include "mutex.h"
67 #include "msg.h"
68 #include "ztimer.h"
69 
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
77 typedef struct ztimer64_base ztimer64_base_t;
81 typedef struct ztimer64_clock ztimer64_clock_t;
82 
86 struct ztimer64_base {
88  uint64_t target;
89 };
90 
97 typedef struct {
99  void (*callback)(void *arg);
100  void *arg;
101 } ztimer64_t;
102 
110  uint64_t checkpoint;
111  uint16_t adjust_set;
112  uint16_t adjust_sleep;
114 #if MODULE_PM_LAYERED || DOXYGEN
115  uint8_t block_pm_mode;
116 #endif
117 };
118 
127 
136 static inline uint64_t ztimer64_offset2absolute(ztimer64_clock_t *clock,
137  uint64_t offset)
138 {
139  unsigned state = irq_disable();
140  uint64_t result = ztimer64_now(clock) + offset;
141 
142  irq_restore(state);
143  return result;
144 }
145 
160  uint64_t target);
161 
175 static inline void ztimer64_set(ztimer64_clock_t *clock, ztimer64_t *timer,
176  uint64_t offset)
177 {
178  ztimer64_set_at(clock, timer,
179  ztimer64_offset2absolute(clock, offset));
180 }
181 
190 unsigned ztimer64_is_set(const ztimer64_t *timer);
191 
204 
220  uint64_t target,
221  msg_t *msg, kernel_pid_t target_pid);
222 
238 static inline void ztimer64_set_msg(ztimer64_clock_t *clock, ztimer64_t *timer,
239  uint64_t offset,
240  msg_t *msg, kernel_pid_t target_pid)
241 {
242  ztimer64_set_msg_at(clock, timer,
243  ztimer64_offset2absolute(clock, offset),
244  msg, target_pid);
245 }
246 
265  uint64_t target);
266 
285  msg_t *msg,
286  uint64_t timeout)
287 {
288  return ztimer64_msg_receive_until(clock, msg,
289  ztimer64_offset2absolute(clock, timeout));
290 }
291 
292 #define MSG_ZTIMER64 0xc83f
314 void ztimer64_periodic_wakeup(ztimer64_clock_t *clock, uint64_t *last_wakeup,
315  uint64_t period);
316 
323 void ztimer64_sleep_until(ztimer64_clock_t *clock, uint64_t target);
324 
331 static inline void ztimer64_sleep(ztimer64_clock_t *clock, uint64_t duration)
332 {
333  ztimer64_sleep_until(clock, ztimer64_offset2absolute(clock, duration));
334 }
335 
344 static inline void ztimer64_spin_until(ztimer64_clock_t *clock, uint64_t target)
345 {
346  while (ztimer64_now(clock) <= target) {}
347 }
348 
361  uint64_t target,
362  kernel_pid_t pid);
363 
375 static inline void ztimer64_set_wakeup(ztimer64_clock_t *clock,
376  ztimer64_t *timer, uint64_t offset,
377  kernel_pid_t pid)
378 {
379  ztimer64_set_wakeup_at(clock, timer,
380  ztimer64_offset2absolute(clock, offset), pid);
381 }
382 
394  uint64_t target);
395 
406 static inline void ztimer64_set_timeout_flag(ztimer64_clock_t *clock,
407  ztimer64_t *timer,
408  uint64_t timeout)
409 {
410  ztimer64_set_timeout_flag_at(clock, timer,
411  ztimer64_offset2absolute(clock, timeout));
412 }
413 
425  uint64_t target);
426 
438  mutex_t *mutex,
439  uint64_t timeout)
440 {
441  return ztimer64_mutex_lock_until(clock, mutex,
442  ztimer64_offset2absolute(clock, timeout));
443 }
444 
456  uint64_t target);
457 
469  rmutex_t *rmutex,
470  uint64_t timeout)
471 {
472  return ztimer64_rmutex_lock_until(clock, rmutex,
473  ztimer64_offset2absolute(clock, timeout));
474 }
475 
484 
488 void ztimer64_init(void);
489 
496 
497 /* default ztimer virtual devices */
501 extern ztimer64_clock_t *const ZTIMER64_USEC;
502 
506 extern ztimer64_clock_t *const ZTIMER64_MSEC;
507 
511 extern ztimer64_clock_t *const ZTIMER64_SEC;
512 
524 int64_t ztimer64_overhead(ztimer64_clock_t *clock, uint64_t base);
525 
526 #ifdef __cplusplus
527 }
528 #endif
529 
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:135
void ztimer64_init(void)
Initialize the board-specific default ztimer configuration.
void ztimer64_set_wakeup_at(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t target, kernel_pid_t pid)
Set a timer that wakes up a thread (absolute version)
void ztimer64_clock_init(ztimer64_clock_t *clock, ztimer_clock_t *base_clock)
Initialize clock to be run from base_clock.
unsigned ztimer64_is_set(const ztimer64_t *timer)
Check if a timer is currently active.
static void ztimer64_set_wakeup(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread (relative version)
Definition: ztimer64.h:375
void ztimer64_remove(ztimer64_clock_t *clock, ztimer64_t *timer)
Remove a timer from a clock.
static void ztimer64_set_timeout_flag(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t timeout)
Set timeout thread flag after timeout.
Definition: ztimer64.h:406
int ztimer64_mutex_lock_until(ztimer64_clock_t *clock, mutex_t *mutex, uint64_t target)
Try to lock the given mutex, but give up at target time.
void ztimer64_sleep_until(ztimer64_clock_t *clock, uint64_t target)
Put the calling thread to sleep until the specified time.
void ztimer64_update_head_offset(ztimer64_clock_t *clock)
Update ztimer clock head list offset.
uint64_t ztimer64_now(ztimer64_clock_t *clock)
Get the current time from a clock.
static void ztimer64_set_msg(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t offset, msg_t *msg, kernel_pid_t target_pid)
Post a message after a delay (relative version)
Definition: ztimer64.h:238
static int ztimer64_msg_receive_timeout(ztimer64_clock_t *clock, msg_t *msg, uint64_t timeout)
receive a message (blocking, with relative timeout)
Definition: ztimer64.h:284
static int ztimer64_mutex_lock_timeout(ztimer64_clock_t *clock, mutex_t *mutex, uint64_t timeout)
Try to lock the given mutex, but give up after timeout.
Definition: ztimer64.h:437
static void ztimer64_set(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t offset)
Set a timer on a clock (relative version)
Definition: ztimer64.h:175
void ztimer64_periodic_wakeup(ztimer64_clock_t *clock, uint64_t *last_wakeup, uint64_t period)
Suspend the calling thread until the time (last_wakeup + period)
void ztimer64_set_timeout_flag_at(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t target)
Set timeout thread flag at target time.
static uint64_t ztimer64_offset2absolute(ztimer64_clock_t *clock, uint64_t offset)
Get absolute target time for a clock given offset.
Definition: ztimer64.h:136
void ztimer64_set_at(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t target)
Set a timer on a clock (absolute version)
static void ztimer64_sleep(ztimer64_clock_t *clock, uint64_t duration)
Put the calling thread to sleep for the specified number of ticks.
Definition: ztimer64.h:331
int ztimer64_msg_receive_until(ztimer64_clock_t *clock, msg_t *msg, uint64_t target)
receive a message (blocking, with absolute timeout)
static int ztimer64_rmutex_lock_timeout(ztimer64_clock_t *clock, rmutex_t *rmutex, uint64_t timeout)
Try to lock the given rmutex, but give up after timeout.
Definition: ztimer64.h:468
ztimer64_clock_t *const ZTIMER64_USEC
Default ztimer microsecond clock.
int64_t ztimer64_overhead(ztimer64_clock_t *clock, uint64_t base)
Measure ztimer64 overhead.
void ztimer64_set_msg_at(ztimer64_clock_t *clock, ztimer64_t *timer, uint64_t target, msg_t *msg, kernel_pid_t target_pid)
Post a message at a specified time.
ztimer64_clock_t *const ZTIMER64_MSEC
Default ztimer millisecond clock.
ztimer64_clock_t *const ZTIMER64_SEC
Default ztimer second clock.
static void ztimer64_spin_until(ztimer64_clock_t *clock, uint64_t target)
Busy-wait until specified target time.
Definition: ztimer64.h:344
int ztimer64_rmutex_lock_until(ztimer64_clock_t *clock, rmutex_t *rmutex, uint64_t target)
Try to lock the given rmutex, but give up at time.
IRQ driver interface.
Mutex for thread synchronization.
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
Minimum information for each timer.
Definition: ztimer64.h:86
ztimer64_base_t * next
next timer in list
Definition: ztimer64.h:87
uint64_t target
absolute target time
Definition: ztimer64.h:88
ztimer64 clock structure
Definition: ztimer64.h:106
ztimer_t base_timer
32bit backend timer
Definition: ztimer64.h:109
uint16_t adjust_sleep
will be subtracted on every sleep(), in addition to adjust_set
Definition: ztimer64.h:112
uint8_t block_pm_mode
min.
Definition: ztimer64.h:115
uint64_t checkpoint
lower timer checkpoint offset
Definition: ztimer64.h:110
uint16_t adjust_set
will be subtracted on every set()
Definition: ztimer64.h:111
ztimer64_base_t * first
list of active timers
Definition: ztimer64.h:107
ztimer_clock_t * base_clock
32bit clock backend
Definition: ztimer64.h:108
ztimer64 structure
Definition: ztimer64.h:97
ztimer64_base_t base
clock list entry
Definition: ztimer64.h:98
void * arg
timer callback argument
Definition: ztimer64.h:100
ztimer device structure
Definition: ztimer.h:367
ztimer structure
Definition: ztimer.h:316
ztimer API