Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_timer.h @ 0339e6fd

History | View | Annotate | Download (9.064 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2020  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU General Public License for more details.
14

15
You should have received a copy of the GNU General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    aos_timer.h
21
 * @brief   Timer macros and structures.
22
 *
23
 * @addtogroup aos_timers
24
 * @{
25
 */
26

    
27
#ifndef AMIROOS_TIMER_H
28
#define AMIROOS_TIMER_H
29

    
30
#include <amiroos.h>
31

    
32
/******************************************************************************/
33
/* CONSTANTS                                                                  */
34
/******************************************************************************/
35

    
36
/**
37
 * @brief   Maximum timer interval that can be set in system ticks.
38
 */
39
#define AOS_TIMER_MAX_INTERVAL_ST     TIME_MAX_INTERVAL
40

    
41
/**
42
 * @brief   Maximum timer interval that can be set in seconds.
43
 */
44
#define AOS_TIMER_MAX_INTERVAL_S      (chTimeI2S(AOS_TIMER_MAX_INTERVAL_ST) - 1)
45

    
46
/**
47
 * @brief   Maximum timer interval that can be set in milliseconds.
48
 */
49
#define AOS_TIMER_MAX_INTERVAL_MS     (chTimeI2MS(AOS_TIMER_MAX_INTERVAL_ST) - 1)
50

    
51
/**
52
 * @brief   Maximum timer interval that can be set in microseconds.
53
 */
54
#define AOS_TIMER_MAX_INTERVAL_US     (chTimeI2US(AOS_TIMER_MAX_INTERVAL_ST) - 1)
55

    
56
/******************************************************************************/
57
/* SETTINGS                                                                   */
58
/******************************************************************************/
59

    
60
/******************************************************************************/
61
/* CHECKS                                                                     */
62
/******************************************************************************/
63

    
64
/******************************************************************************/
65
/* DATA STRUCTURES AND TYPES                                                  */
66
/******************************************************************************/
67

    
68
/**
69
 * @brief   Timer structure.
70
 */
71
typedef struct aos_timer {
72
  /**
73
   * @brief   ChibiOS virtual timer.
74
   */
75
  virtual_timer_t vt;
76

    
77
  /**
78
   * @brief   Absolute time to trigger.
79
   */
80
  aos_timestamp_t triggertime;
81

    
82
  /**
83
   * @brief   Relative interval for periodic timers.
84
   * @note    A value of 0 indicates single shot timing.
85
   */
86
  aos_longinterval_t interval;
87

    
88
  /**
89
   * @brief   Pointer to a callback function.
90
   */
91
  vtfunc_t callback;
92

    
93
  /**
94
   * @brief   Pointer to a parameter for the callback function.
95
   */
96
  void* cbparam;
97
} aos_timer_t;
98

    
99
/******************************************************************************/
100
/* MACROS                                                                     */
101
/******************************************************************************/
102

    
103
/******************************************************************************/
104
/* EXTERN DECLARATIONS                                                        */
105
/******************************************************************************/
106

    
107
#if defined(__cplusplus)
108
extern "C" {
109
#endif /* defined(__cplusplus) */
110
  void aosTimerInit(aos_timer_t* timer);
111
  void aosTimerSetAbsoluteI(aos_timer_t* timer, const aos_timestamp_t uptime, vtfunc_t cb, void* par);
112
  void aosTimerSetIntervalI(aos_timer_t* timer, const aos_interval_t offset, vtfunc_t cb, void* par);
113
  void aosTimerSetLongIntervalI(aos_timer_t* timer, const aos_longinterval_t offset, vtfunc_t cb, void* par);
114
  void aosTimerPeriodicIntervalI(aos_timer_t* timer, const aos_interval_t interval, vtfunc_t cb, void* par);
115
  void aosTimerPeriodicLongIntervalI(aos_timer_t* timer, const aos_longinterval_t interval, vtfunc_t cb, void* par);
116
#if defined(__cplusplus)
117
}
118
#endif /* defined(__cplusplus) */
119

    
120
/******************************************************************************/
121
/* INLINE FUNCTIONS                                                           */
122
/******************************************************************************/
123

    
124
/**
125
 * @brief   Set timer to trigger at an absolute system time.
126
 *
127
 * @param[in] timer   Pointer to the timer to set.
128
 * @param[in] uptime  Absolute uptime for the timer to trigger.
129
 * @param[in] cb      Pointer to a callback function to be called.
130
 * @param[in] par     Pointer to a parameter for the callback function (may be NULL).
131
 */
132
static inline void aosTimerSetAbsolute(aos_timer_t* timer, const aos_timestamp_t uptime, vtfunc_t cb, void* par)
133
{
134
  chSysLock();
135
  aosTimerSetAbsoluteI(timer, uptime, cb, par);
136
  chSysUnlock();
137

    
138
  return;
139
}
140

    
141
/**
142
 * @brief   Set timer to trigger after a relative interval.
143
 *
144
 * @param[in] timer   Pointer to the timer to set.
145
 * @param[in] offset  Relative interval to set for the timer to trigger.
146
 * @param[in] cb      Pointer to a callback function to be called.
147
 *                    In contrast to other callback functions, this gets called from an already ISR locked context.
148
 *                    Thus it may not contain any chSysLockX() or chSysUnlockX() calls.
149
 * @param[in] par     Pointer to a parameter for the callback function (may be NULL).
150
 */
151
static inline void aosTimerSetInterval(aos_timer_t* timer, const aos_interval_t offset, vtfunc_t cb, void* par)
152
{
153
  chSysLock();
154
  aosTimerSetIntervalI(timer, offset, cb, par);
155
  chSysUnlock();
156
}
157

    
158
/**
159
 * @brief   Set timer to trigger after a long relative interval.
160
 *
161
 * @param[in] timer   Pointer to the timer to set.
162
 * @param[in] offset  Long interval value to set for the timer to trigger.
163
 * @param[in] cb      Pointer to a callback function to be called.
164
 *                    In contrast to other callback functions, this gets called from an already ISR locked context.
165
 *                    Thus it may not contain any chSysLockX() or chSysUnlockX() calls.
166
 * @param[in] par     Pointer to a parameter for the callback function (may be NULL).
167
 */
168
static inline void aosTimerSetLongInterval(aos_timer_t* timer, const aos_longinterval_t offset, vtfunc_t cb, void* par)
169
{
170
  chSysLock();
171
  aosTimerSetLongIntervalI(timer, offset, cb, par);
172
  chSysUnlock();
173
}
174

    
175
/**
176
 * @brief   Set timer to trigger periodically in the specified interval.
177
 *
178
 * @param[in] timer     Pointer to the timer to set.
179
 * @param[in] interval  Interval for the timer to trigger periodically.
180
 * @param[in] cb        Pointer to a callback function to be called.
181
 *                      In contrast to other callback functions, this gets called from an already ISR locked context.
182
 *                      Thus it may not contain any chSysLockX() or chSysUnlockX() calls.
183
 * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
184
 */
185
static inline void aosTimerPeriodicInterval(aos_timer_t *timer, const aos_interval_t interval, vtfunc_t cb, void *par)
186
{
187
  chSysLock();
188
  aosTimerPeriodicIntervalI(timer, interval, cb, par);
189
  chSysUnlock();
190

    
191
  return;
192
}
193

    
194
/**
195
 * @brief   Set timer to trigger periodically in the specified interval.
196
 *
197
 * @param[in] timer     Pointer to the timer to set.
198
 * @param[in] interval  Long interval value for the periodic timer to trigger periodically.
199
 * @param[in] cb        Pointer to a callback function to be called.
200
 *                      In contrast to other callback functions, this get called from an already ISR locked context.
201
 *                      This it may not contain any chSysLockX() or chSysUnlockX() calls.
202
 * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
203
 */
204
static inline void aosTimerPeriodicLongInterval(aos_timer_t* timer, const aos_longinterval_t interval, vtfunc_t cb, void* par)
205
{
206
  chSysLock();
207
  aosTimerPeriodicLongIntervalI(timer, interval, cb, par);
208
  chSysUnlock();
209

    
210
  return;
211
}
212

    
213
/**
214
 * @brief   Determine whether a timer is armed.
215
 *
216
 * @param[in] timer   Pointer to the timer to check.
217
 *
218
 * @return    true if the timer is armed, false otherwise.
219
 */
220
static inline bool aosTimerIsArmedI(aos_timer_t* timer)
221
{
222
  return chVTIsArmedI(&(timer->vt));
223
}
224

    
225
/**
226
 * @brief   Determine whether a timer is armed.
227
 *
228
 * @param[in] timer   Pointer to the timer to check.
229
 *
230
 * @return    true if the timer is armed, false otherwise.
231
 */
232
static inline bool aosTimerIsArmed(aos_timer_t* timer)
233
{
234
  bool b;
235

    
236
  chSysLock();
237
  b = aosTimerIsArmedI(timer);
238
  chSysUnlock();
239

    
240
  return b;
241
}
242

    
243
/**
244
 * @brief   Reset a timer.
245
 *
246
 * @param[in] timer   Pointer to the timer to reset.
247
 */
248
static inline void aosTimerResetI(aos_timer_t* timer)
249
{
250
  chVTResetI(&(timer->vt));
251
  timer->interval = 0;
252

    
253
  return;
254
}
255

    
256
/**
257
 * @brief   Reset a timer.
258
 *
259
 * @param[in] timer   Pointer to the timer to reset.
260
 */
261
static inline void aosTimerReset(aos_timer_t* timer)
262
{
263
  chSysLock();
264
  aosTimerResetI(timer);
265
  chSysUnlock();
266

    
267
  return;
268
}
269

    
270
#endif /* AMIROOS_TIMER_H */
271

    
272
/** @} */