ARMEBS4  revision-26.06.2015
time.h
Go to the documentation of this file.
1 /************************************************************************//**
2  * \file heivs/time.h
3  * \brief simple time abstraction
4  * \author marc dot pignat at hevs dot ch
5  ***************************************************************************/
6 
7 /************************************************************************//**
8  * \defgroup timeout Timeout functions
9  * @{
10  * \ingroup time
11  * \brief Provides a simple way to manage time and timeouts
12  ***************************************************************************/
13 
14 #ifndef HEIVS_TIME_H
15 #define HEIVS_TIME_H
16 
17 #include "heivs/config.h"
18 #include <stdint.h>
19 
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 /**
26  * \brief Timeout structure
27  *
28  * This structure is used to hold a timeout value. This structure only holds a
29  * single integer, this trick is used to prevent erroneous operations that can
30  * be done on integers, but not on this value.
31  *
32  * This protection comes at no CPU or memory cost.
33  */
34 typedef struct
35 {
36  uint32_t value;
37 } timeout_t;
38 
39 /**
40  * \brief Get current time in obscure format from boot
41  *
42  * \return the current time
43  *
44  * \warning will overlap
45  */
46 static inline timeout_t time_get(void)
47 {
48  /**
49  * There is no need for locking, there is only one cpu in the system
50  * and it will update the gTimeCounter in an atomic manner.
51  */
52  extern volatile uint32_t gTimeCounter;
53  timeout_t time =
54  {
55  .value = gTimeCounter,
56  };
57 
58  return time;
59 }
60 
61 static inline uint32_t time_to_ms(timeout_t t)
62 {
63  return t.value;
64 }
65 
66 /**
67  * \brief Is this time passed?
68  * \param timeout the timeout value
69  * \return zero if this time has not elapsed
70  */
71 static inline uint32_t time_elapsed(timeout_t timeout)
72 {
73  timeout_t now = time_get();
74  return (timeout.value - now.value) > INT32_MAX;
75 }
76 
77 /**
78  * \brief Set an obscure time at least ms milliseconds in the future
79  *
80  * \param ms the minimum delay
81  * \return the timeout obscure variable
82  *
83  * \see time_elapsed
84  *
85  * time_elapsed will return true when *more* than ms have passed
86  */
87 timeout_t time_set_timeout_ms(uint32_t ms);
88 
89 uint32_t time_diff_ms(timeout_t now, timeout_t before);
90 
91 void time_init(void);
92 void time_suspend(void);
93 void time_resume(void);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 /************************************************************************//**
100  * @}
101  ***************************************************************************/
102 
103 #endif /* HEIVS_TIME_H */
libheivs configuration file
static uint32_t time_elapsed(timeout_t timeout)
Is this time passed?
Definition: time.h:71
static timeout_t time_get(void)
Get current time in obscure format from boot.
Definition: time.h:46
Timeout structure.
Definition: time.h:34
timeout_t time_set_timeout_ms(uint32_t ms)
Set an obscure time at least ms milliseconds in the future.
Definition: time.c:15