KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tm.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : time.h
11  * Created : 8 mrt. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 
16 #ifndef TM_H_
17 #define TM_H_
18 
19 /**
20  * @file
21  *
22  * @ingroup kernel
23  *
24  * Simple timer functions.
25  */
26 
27 #include "drv/wb/ticks.h"
28 #include "util/macro.h"
29 #include "kernel/err.h"
30 #include "errorcode.h"
31 
32 
33 #include <stdbool.h>
34 
35 /**
36  * Initializes a timeout with the specified no of msecs.
37  *
38  * Use together with timeOutCheck, e.g:
39  *
40  * @code
41  *
42  * uint32_t to = timeOutInit(3000); // 3 second timeout.
43  * while (!hasData()) {
44  * if (timeOutExpired(to)) return -1;
45  * }
46  *
47  * @endcode
48  *
49  * @param msec No of msecs before timeout.
50  *
51  * return Time out value.
52  */
53 static inline uint32_t timeOutInit(uint32_t msec)
54 {
55  // overflows, not a problem.
56  return ticks() + msec;
57 }
58 
59 /**
60  * Updates the original timeout with the new timeout. Used for fixed interval timeout.
61  *
62  * The timeout is at least guaranteed to wait for the specied time, but may expire later
63  *
64  * @param Timeout value (returned from timeOutInit).
65  * @param msec No of msecs before timeout.
66  */
67 static inline uint32_t timeOutUpdate(uint32_t to, uint32_t msec) {
68  return to + msec;
69 }
70 
71 
72 /**
73  * Checks whether or not the timeout has expired.
74  *
75  * @param Timeout value (returned from timeOutInit).
76  */
77 static inline bool timeOut(uint32_t to)
78 {
79  return CGT(ticks(), to, 32);
80 }
81 
82 
83 
84 /**
85  * Function to wait for a specific flag to be set or cleared.
86  *
87  * @param flags A pointer to a 32 bit value
88  * @param mask The mask to apply
89  * @param result The result to wait for
90  * @param timeout The maximum timeout in milliseconds
91  *
92  * @retval true Condition met
93  * @retval false Condition not met, error set to E_TIMEOUT.
94  */
95 static inline bool timeOutWaitFor(uint32_t * flags, uint32_t mask, uint32_t result, uint32_t msec) {
96 
97  uint32_t to = timeOutInit(msec);
98 
99  while (!timeOut(to)) {
100  if ((*flags & mask) == result) return true;
101  }
102 
103  return errSet(ERROR(E_TIMEOUT));
104 }
105 
106 
107 /**
108  * Simple busy-wait delay.
109  *
110  * The routine guarantees to at least wait for the specified time, but may wait longer.
111  *
112  * Currently its avr = msec + 0.5, with a std dev. of 1 / sqrt(12).
113  */
114 void timeDelay(uint32_t msec);
115 
116 #endif /* TM_H_ */
White Rabbit simple timer &#39;Ticks&#39; driver.
void timeDelay(uint32_t msec)
Simple busy-wait delay.
Definition: tm.c:18
static uint32_t ticks()
Nr of ticks since device start up.
Definition: ticks.h:37
static uint32_t timeOutInit(uint32_t msec)
Initializes a timeout with the specified no of msecs.
Definition: tm.h:53
static bool timeOutWaitFor(uint32_t *flags, uint32_t mask, uint32_t result, uint32_t msec)
Function to wait for a specific flag to be set or cleared.
Definition: tm.h:95
static bool timeOut(uint32_t to)
Checks whether or not the timeout has expired.
Definition: tm.h:77
Manages the global system error.
This module is responsible for distributing error codes.
#define E_TIMEOUT
Generic error: Timeout error.
Definition: errorcode.h:100
#define CGT(A, B, BITS)
Cyclic comparison function Greater Than.
Definition: macro.h:66
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
Provides common macros.
static uint32_t timeOutUpdate(uint32_t to, uint32_t msec)
Updates the original timeout with the new timeout.
Definition: tm.h:67