dpl_cputime.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Inria
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  */
8 
20 #ifndef DPL_DPL_CPUTIME_H
21 #define DPL_DPL_CPUTIME_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <stdint.h>
28 
29 #include "xtimer.h"
30 #include "hal/hal_timer.h"
31 
37 static inline uint32_t dpl_cputime_get32(void)
38 {
39  return xtimer_now().ticks32;
40 }
41 
49 static inline uint32_t dpl_cputime_usecs_to_ticks(uint32_t usecs)
50 {
51  return xtimer_ticks_from_usec(usecs).ticks32;
52 }
53 
61 static inline uint32_t dpl_cputime_ticks_to_usecs(uint32_t ticks)
62 {
63  xtimer_ticks32_t val = {.ticks32 = ticks};
64  return xtimer_usec_from_ticks(val);
65 }
66 
72 static inline void dpl_cputime_delay_ticks(uint32_t ticks)
73 {
74  xtimer_ticks32_t val = {.ticks32 = ticks};
76 }
77 
83 static inline void dpl_cputime_delay_usecs(uint32_t usecs)
84 {
85  xtimer_usleep(usecs);
86 }
87 
95 static inline void dpl_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp,
96  void *arg)
97 {
98  timer->timer.callback = fp;
99  timer->timer.arg = arg;
100 }
101 
115 static inline int dpl_cputime_timer_start(struct hal_timer *timer, uint32_t cputime)
116 {
117  xtimer_set(&timer->timer, xtimer_now_usec() + cputime);
118  return 0;
119 }
120 
133 static inline int dpl_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs)
134 {
135  uint32_t now = xtimer_now_usec();
136  if (now > usecs) {
137  xtimer_set(&timer->timer, now);
138  } else {
139  xtimer_set(&timer->timer, 0);
140  }
141  return 0;
142 }
143 
153 static inline void dpl_cputime_timer_stop(struct hal_timer *timer)
154 {
155  xtimer_remove(&timer->timer);
156 }
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
162 #endif /* DPL_DPL_CPUTIME_H */
hal_timer_cb
xtimer_callback_t hal_timer_cb
HAL timer callback.
Definition: hal_timer.h:32
xtimer::arg
void * arg
argument to pass to callback function
Definition: xtimer.h:89
dpl_cputime_timer_start
static int dpl_cputime_timer_start(struct hal_timer *timer, uint32_t cputime)
Start a cputimer that will expire at 'cputime'.
Definition: dpl_cputime.h:115
dpl_cputime_usecs_to_ticks
static uint32_t dpl_cputime_usecs_to_ticks(uint32_t usecs)
Converts the given number of microseconds into cputime ticks.
Definition: dpl_cputime.h:49
xtimer_ticks32_t::ticks32
uint32_t ticks32
Tick count.
Definition: xtimer.h:70
dpl_cputime_ticks_to_usecs
static uint32_t dpl_cputime_ticks_to_usecs(uint32_t ticks)
Convert the given number of ticks into microseconds.
Definition: dpl_cputime.h:61
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_tsleep32
static void xtimer_tsleep32(xtimer_ticks32_t ticks)
Stop execution of a thread for some time, 32bit version.
dpl_cputime_timer_init
static void dpl_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp, void *arg)
Initialize a CPU timer, using the given HAL timer.
Definition: dpl_cputime.h:95
dpl_cputime_timer_stop
static void dpl_cputime_timer_stop(struct hal_timer *timer)
Stops a cputimer from running.
Definition: dpl_cputime.h:153
xtimer_ticks_from_usec
static xtimer_ticks32_t xtimer_ticks_from_usec(uint32_t usec)
Convert microseconds to xtimer ticks.
dpl_cputime_get32
static uint32_t dpl_cputime_get32(void)
Returns the low 32 bits of cputime.
Definition: dpl_cputime.h:37
hal_timer::timer
xtimer_t timer
the timer
Definition: hal_timer.h:38
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.
hal_timer.h
Timer abstraction layer RIOT adaption.
xtimer_usleep
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
dpl_cputime_delay_usecs
static void dpl_cputime_delay_usecs(uint32_t usecs)
Wait until 'usecs' microseconds has elapsed.
Definition: dpl_cputime.h:83
xtimer_ticks32_t
xtimer timestamp (32 bit)
Definition: xtimer.h:69
dpl_cputime_delay_ticks
static void dpl_cputime_delay_ticks(uint32_t ticks)
Wait until the number of ticks has elapsed.
Definition: dpl_cputime.h:72
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.
dpl_cputime_timer_relative
static int dpl_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs)
Sets a cpu timer that will expire 'usecs' microseconds from the current cputime.
Definition: dpl_cputime.h:133
hal_timer
The HAL timer structure.
Definition: hal_timer.h:37
xtimer_remove
void xtimer_remove(xtimer_t *timer)
remove a timer
xtimer.h
xtimer interface definitions