Loading...
Searching...
No Matches
ztimer.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 Kaspar Schleiser <kaspar@schleiser.de>
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
262#ifndef ZTIMER_H
263#define ZTIMER_H
264
265#include <stdint.h>
266
267#include "sched.h"
268#include "msg.h"
269#include "mutex.h"
270#include "rmutex.h"
271
272#ifdef __cplusplus
273extern "C" {
274#endif
275
279#define ZTIMER_CLOCK_NO_REQUIRED_PM_MODE (UINT8_MAX)
280
285
290
294typedef void (*ztimer_callback_t)(void *arg);
295
301 uint32_t offset;
302};
303
310typedef uint32_t ztimer_now_t;
311
323
331typedef struct {
337 void (*set)(ztimer_clock_t *clock, uint32_t val);
338
343 uint32_t (*now)(ztimer_clock_t *clock);
344
349 void (*cancel)(ztimer_clock_t *clock);
350
351#if MODULE_ZTIMER_ONDEMAND || DOXYGEN
356 void (*start)(ztimer_clock_t *clock);
357
362 void (*stop)(ztimer_clock_t *clock);
363#endif
365
373 uint16_t adjust_set;
374 uint16_t adjust_sleep;
376#if MODULE_ZTIMER_ONDEMAND || DOXYGEN
380 uint16_t users;
381#endif
382#if MODULE_ZTIMER_EXTEND || DOXYGEN
383 /* values used for checkpointed intervals and 32bit extension */
384 uint32_t max_value;
385 uint32_t lower_last;
387#endif
388#if MODULE_PM_LAYERED && !MODULE_ZTIMER_ONDEMAND || DOXYGEN
391#endif
392};
393
401
402/* User API */
414#if MODULE_ZTIMER_ONDEMAND || DOXYGEN
416#else
417static inline bool ztimer_acquire(ztimer_clock_t *clock)
418{
419 (void)clock;
420 return false;
421}
422#endif
423
434#if MODULE_ZTIMER_ONDEMAND || DOXYGEN
436#else
437static inline bool ztimer_release(ztimer_clock_t *clock)
438{
439 (void)clock;
440 return false;
441}
442#endif
443
460uint32_t ztimer_set(ztimer_clock_t *clock, ztimer_t *timer, uint32_t val);
461
471unsigned ztimer_is_set(const ztimer_clock_t *clock, const ztimer_t *timer);
472
491
507void ztimer_set_msg(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset,
508 msg_t *msg, kernel_pid_t target_pid);
509
528 uint32_t timeout);
529
530/* created with dist/tools/define2u16.py */
531#define MSG_ZTIMER 0xc83e
542
553
667{
668#if MODULE_ZTIMER_ONDEMAND && DEVELHELP
670#endif
671
672#if MODULE_ZTIMER_EXTEND
673 if (clock->max_value < UINT32_MAX) {
674#else
675 if (0) {
676#endif
677 return _ztimer_now_extend(clock);
678 }
679 else {
680 return clock->ops->now(clock);
681 }
682}
683
706void ztimer_periodic_wakeup(ztimer_clock_t *clock, uint32_t *last_wakeup,
707 uint32_t period);
708
715void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration);
716
725static inline void ztimer_spin(ztimer_clock_t *clock, uint32_t duration)
726{
727 ztimer_acquire(clock);
728 uint32_t end = ztimer_now(clock) + duration;
729
730 /* Rely on integer overflow. `end - now` will be smaller than `duration`,
731 * counting down, until it underflows to UINT32_MAX. Loop ends then. */
732 while ((end - ztimer_now(clock)) <= duration) {}
733 ztimer_release(clock);
734}
735
747void ztimer_set_wakeup(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset,
748 kernel_pid_t pid);
749
761 uint32_t timeout);
762
774 uint32_t timeout, mutex_t *mutex);
775
787 uint32_t timeout);
788
800 uint32_t timeout);
801
805void ztimer_init(void);
806
807#if defined(MODULE_ZTIMER_EXTEND) || defined(DOXYGEN)
818static inline void ztimer_init_extend(ztimer_clock_t *clock)
819{
820 if (clock->max_value < UINT32_MAX) {
821 clock->ops->set(clock, clock->max_value >> 1);
822 }
823}
824#endif /* MODULE_ZTIMER_EXTEND */
825
826/* default ztimer virtual devices */
830extern ztimer_clock_t *const ZTIMER_USEC;
831
835extern ztimer_clock_t *const ZTIMER_MSEC;
836
840extern ztimer_clock_t *const ZTIMER_SEC;
841
856extern ztimer_clock_t *const ZTIMER_USEC_BASE;
857
875extern ztimer_clock_t *const ZTIMER_MSEC_BASE;
876
877#ifdef __cplusplus
878}
879#endif
880
881#endif /* ZTIMER_H */
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:139
void ztimer_init(void)
Initialize the board-specific default ztimer configuration.
bool ztimer_release(ztimer_clock_t *clock)
Release a clock.
void(* ztimer_callback_t)(void *arg)
Type of callbacks in timers.
Definition ztimer.h:294
void ztimer_periodic_wakeup(ztimer_clock_t *clock, uint32_t *last_wakeup, uint32_t period)
Suspend the calling thread until the time (last_wakeup + period)
void ztimer_set_wakeup(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset, kernel_pid_t pid)
Set a timer that wakes up a thread.
ztimer_clock_t *const ZTIMER_SEC
Default ztimer second clock.
void ztimer_set_msg(ztimer_clock_t *clock, ztimer_t *timer, uint32_t offset, msg_t *msg, kernel_pid_t target_pid)
Post a message after a delay.
ztimer_clock_t *const ZTIMER_MSEC_BASE
Base ztimer for the millisecond clock (ZTIMER_MSEC)
uint32_t ztimer_set(ztimer_clock_t *clock, ztimer_t *timer, uint32_t val)
Set a timer on a clock.
ztimer_clock_t *const ZTIMER_USEC
Default ztimer microsecond clock.
static void ztimer_spin(ztimer_clock_t *clock, uint32_t duration)
Busy-wait specified duration.
Definition ztimer.h:725
bool ztimer_acquire(ztimer_clock_t *clock)
Acquire a clock.
uint32_t ztimer_now_t
type for ztimer_now() result
Definition ztimer.h:310
static ztimer_now_t ztimer_now(ztimer_clock_t *clock)
Get the current time from a clock.
Definition ztimer.h:666
int ztimer_msg_receive_timeout(ztimer_clock_t *clock, msg_t *msg, uint32_t timeout)
receive a message (blocking, with timeout)
static void ztimer_init_extend(ztimer_clock_t *clock)
Initialize possible ztimer extension intermediate timer.
Definition ztimer.h:818
unsigned ztimer_is_set(const ztimer_clock_t *clock, const ztimer_t *timer)
Check if a timer is currently active.
int ztimer_rmutex_lock_timeout(ztimer_clock_t *clock, rmutex_t *rmutex, uint32_t timeout)
Try to lock the given rmutex, but give up after timeout.
void ztimer_sleep(ztimer_clock_t *clock, uint32_t duration)
Put the calling thread to sleep for the specified number of ticks.
bool ztimer_remove(ztimer_clock_t *clock, ztimer_t *timer)
Remove a timer from a clock.
void ztimer_handler(ztimer_clock_t *clock)
main ztimer callback handler
ztimer_now_t _ztimer_now_extend(ztimer_clock_t *clock)
ztimer_now() for extending timers
void ztimer_mutex_unlock(ztimer_clock_t *clock, ztimer_t *timer, uint32_t timeout, mutex_t *mutex)
Unlock mutex after timeout.
ztimer_clock_t *const ZTIMER_USEC_BASE
Base ztimer for the microsecond clock (ZTIMER_USEC)
void _ztimer_assert_clock_active(ztimer_clock_t *clock)
asserts the given clock to be active
void ztimer_set_timeout_flag(ztimer_clock_t *clock, ztimer_t *timer, uint32_t timeout)
Set timeout thread flag after timeout.
int ztimer_mutex_lock_timeout(ztimer_clock_t *clock, mutex_t *mutex, uint32_t timeout)
Try to lock the given mutex, but give up after timeout.
ztimer_clock_t *const ZTIMER_MSEC
Default ztimer millisecond clock.
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:196
Mutex structure.
Definition mutex.h:146
Mutex structure.
Definition rmutex.h:38
Minimum information for each timer.
Definition ztimer.h:299
uint32_t offset
offset from last timer in list
Definition ztimer.h:301
ztimer_base_t * next
next timer in list
Definition ztimer.h:300
ztimer device structure
Definition ztimer.h:369
uint16_t adjust_sleep
will be subtracted on every sleep(), in addition to adjust_set
Definition ztimer.h:374
uint32_t lower_last
timer value at last now() call
Definition ztimer.h:385
uint32_t max_value
maximum relative timer value
Definition ztimer.h:384
ztimer_now_t checkpoint
cumulated time at last now() call
Definition ztimer.h:386
ztimer_base_t * last
last timer in queue, for _is_set()
Definition ztimer.h:372
uint16_t users
user count of this clock
Definition ztimer.h:380
ztimer_base_t list
list of active timers
Definition ztimer.h:370
uint16_t adjust_clock_start
will be subtracted on every set(), if the underlying periph is in stopped state
Definition ztimer.h:377
uint16_t adjust_set
will be subtracted on every set()
Definition ztimer.h:373
const ztimer_ops_t * ops
pointer to methods structure
Definition ztimer.h:371
uint8_t block_pm_mode
min.
Definition ztimer.h:389
ztimer backend method structure
Definition ztimer.h:331
uint32_t(* now)(ztimer_clock_t *clock)
Get the current count of the timer.
Definition ztimer.h:343
void(* set)(ztimer_clock_t *clock, uint32_t val)
Set a new timer target.
Definition ztimer.h:337
ztimer structure
Definition ztimer.h:318
ztimer_callback_t callback
timer callback function pointer
Definition ztimer.h:320
void * arg
timer callback argument
Definition ztimer.h:321
ztimer_base_t base
clock list entry
Definition ztimer.h:319