Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_timer.h @ 6d56ad8d

History | View | Annotate | Download (8.906 KB)

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

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 53710ca3 Marc Rothmann
/**
20
 * @file    aos_timer.h
21
 * @brief   Timer macros and structures.
22
 *
23
 * @addtogroup aos_timers
24
 * @{
25
 */
26
27 6ff06bbf Thomas Schöpping
#ifndef AMIROOS_TIMER_H
28
#define AMIROOS_TIMER_H
29 e545e620 Thomas Schöpping
30 3940ba8a Thomas Schöpping
#include <amiroos.h>
31
32 f3ac1c96 Thomas Schöpping
/******************************************************************************/
33
/* CONSTANTS                                                                  */
34
/******************************************************************************/
35 e545e620 Thomas Schöpping
36
/**
37
 * @brief   Maximum timer interval that can be set in system ticks.
38
 */
39 0128be0f Marc Rothmann
#define AOS_TIMER_MAX_INTERVAL_ST     TIME_MAX_INTERVAL
40 e545e620 Thomas Schöpping
41
/**
42
 * @brief   Maximum timer interval that can be set in seconds.
43
 */
44 1e5f7648 Thomas Schöpping
#define AOS_TIMER_MAX_INTERVAL_S      (chTimeI2S(AOS_TIMER_MAX_INTERVAL_ST) - 1)
45 e545e620 Thomas Schöpping
46
/**
47
 * @brief   Maximum timer interval that can be set in milliseconds.
48
 */
49 1e5f7648 Thomas Schöpping
#define AOS_TIMER_MAX_INTERVAL_MS     (chTimeI2MS(AOS_TIMER_MAX_INTERVAL_ST) - 1)
50 e545e620 Thomas Schöpping
51
/**
52
 * @brief   Maximum timer interval that can be set in microseconds.
53
 */
54 1e5f7648 Thomas Schöpping
#define AOS_TIMER_MAX_INTERVAL_US     (chTimeI2US(AOS_TIMER_MAX_INTERVAL_ST) - 1)
55 e545e620 Thomas Schöpping
56 f3ac1c96 Thomas Schöpping
/******************************************************************************/
57
/* SETTINGS                                                                   */
58
/******************************************************************************/
59
60
/******************************************************************************/
61
/* CHECKS                                                                     */
62
/******************************************************************************/
63
64
/******************************************************************************/
65
/* DATA STRUCTURES AND TYPES                                                  */
66
/******************************************************************************/
67
68 e545e620 Thomas Schöpping
/**
69
 * @brief   Timer stucture.
70
 */
71
typedef struct aos_timer {
72
  /**
73
   * @brief   ChibiOS virtual timer.
74
   */
75
  virtual_timer_t vt;
76
77
  /**
78
   * @brief   Time to wake up.
79
   */
80
  aos_timestamp_t wkuptime;
81
82
  /**
83
   * @brief   Pointer to a callback function.
84
   */
85
  vtfunc_t callback;
86
87
  /**
88
   * @brief   Pointer to a parameter for the callback function.
89
   */
90
  void* cbparam;
91
} aos_timer_t;
92
93
/**
94
 * @brief   Periodic timer structure.
95
 */
96
typedef struct aos_periodictimer {
97
  /**
98
   * @brief   AMiRo-OS timer.
99
   */
100
  aos_timer_t timer;
101
102
  /**
103
   * @brief   Period interval.
104
   */
105
  aos_longinterval_t interval;
106
107
  /**
108
   * @brief   Pointer to a callback function.
109
   */
110
  vtfunc_t callback;
111
112
  /**
113
   * @brief   Pointer to a parameter for the callback function.
114
   */
115
  void* cbparam;
116
} aos_periodictimer_t;
117
118 f3ac1c96 Thomas Schöpping
/******************************************************************************/
119
/* MACROS                                                                     */
120
/******************************************************************************/
121
122
/******************************************************************************/
123
/* EXTERN DECLARATIONS                                                        */
124
/******************************************************************************/
125
126 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
127 e545e620 Thomas Schöpping
extern "C" {
128 7de0cc90 Thomas Schöpping
#endif /* defined(__cplusplus) */
129 e545e620 Thomas Schöpping
  void aosTimerInit(aos_timer_t* timer);
130
  void aosTimerSetAbsoluteI(aos_timer_t* timer, aos_timestamp_t* uptime, vtfunc_t cb, void* par);
131
  void aosTimerSetIntervalI(aos_timer_t* timer, aos_interval_t offset, vtfunc_t cb, void* par);
132
  void aosTimerSetLongIntervalI(aos_timer_t* timer, aos_longinterval_t* offset, vtfunc_t cb, void* par);
133
  void aosPeriodicTimerInit(aos_periodictimer_t* ptimer);
134
  void aosPeriodicTimerSetI(aos_periodictimer_t* ptimer, aos_interval_t interval, vtfunc_t cb, void* par);
135
  void aosPeriodicTimerSetLongI(aos_periodictimer_t* ptimer, aos_longinterval_t* interval, vtfunc_t cb, void* par);
136 7de0cc90 Thomas Schöpping
#if defined(__cplusplus)
137 e545e620 Thomas Schöpping
}
138 7de0cc90 Thomas Schöpping
#endif /* defined(__cplusplus) */
139 e545e620 Thomas Schöpping
140 f3ac1c96 Thomas Schöpping
/******************************************************************************/
141
/* INLINE FUNCTIONS                                                           */
142
/******************************************************************************/
143
144 e545e620 Thomas Schöpping
/**
145
 * @brief   Set timer to fire at an absolute system time.
146
 *
147
 * @param[in] timer   Pointer to the timer to set.
148
 * @param[in] uptime  Pointer to the absolute uptime for the timer to fire.
149
 * @param[in] cb      Pointer to a callback function to be called.
150
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
151
 */
152
static inline void aosTimerSetAbsolute(aos_timer_t* timer, aos_timestamp_t* uptime, vtfunc_t cb, void* par)
153
{
154
  chSysLock();
155
  aosTimerSetAbsoluteI(timer, uptime, cb, par);
156
  chSysUnlock();
157
158
  return;
159
}
160
161
/**
162
 * @brief   Set timer to fire after a relative interval.
163
 *
164
 * @param[in] timer   Pointer to the timer to set.
165
 * @param[in] offset  Relative interval to set for the timer to fire.
166
 * @param[in] cb      Pointer to a callback function to be called.
167
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
168
 */
169
static inline void aosTimerSetInterval(aos_timer_t* timer, aos_interval_t offset, vtfunc_t cb, void* par)
170
{
171
  chSysLock();
172
  aosTimerSetIntervalI(timer, offset, cb, par);
173
  chSysUnlock();
174
}
175
176
/**
177
 * @brief   Set timer to fire after a long relative interval.
178
 *
179
 * @param[in] timer   Pointer to the timer to set.
180
 * @param[in] offset  Pointer to a long interval value to set for the timer to fire.
181
 * @param[in] cb      Pointer to a callback function to be called.
182
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
183
 */
184
static inline void aosTimerSetLongInterval(aos_timer_t* timer, aos_longinterval_t* offset, vtfunc_t cb, void* par)
185
{
186
  chSysLock();
187
  aosTimerSetLongIntervalI(timer, offset, cb, par);
188
  chSysUnlock();
189
}
190
191
/**
192
 * @brief   Reset a timer.
193
 *
194
 * @param[in] timer   Pointer to the timer to reset.
195
 */
196
static inline void aosTimerResetI(aos_timer_t* timer)
197
{
198
  chVTResetI(&(timer->vt));
199
200
  return;
201
}
202
203
/**
204
 * @brief   Reset a timer.
205
 *
206
 * @param[in] timer   Pointer to the timer to reset.
207
 */
208
static inline void aosTimerReset(aos_timer_t* timer)
209
{
210
  chSysLock();
211
  aosTimerResetI(timer);
212
  chSysUnlock();
213
214
  return;
215
}
216
217
/**
218
 * @brief   Determine whether a timer is armed.
219
 *
220
 * @param[in] timer   Pointer to the timer to check.
221
 *
222
 * @return    true if the timer is armed, false otherwise.
223
 */
224
static inline bool aosTimerIsArmedI(aos_timer_t* timer)
225
{
226
  return chVTIsArmedI(&(timer->vt));
227
}
228
229
/**
230
 * @brief   Determine whether a timer is armed.
231
 *
232
 * @param[in] timer   Pointer to the timer to check.
233
 *
234
 * @return    true if the timer is armed, false otherwise.
235
 */
236
static inline bool aosTimerIsArmed(aos_timer_t* timer)
237
{
238
  bool b;
239
240
  chSysLock();
241
  b = aosTimerIsArmedI(timer);
242
  chSysUnlock();
243
244
  return b;
245
}
246
247
/**
248
 * @brief   Set a periodic timer to fire in the specified interval.
249
 *
250
 * @param[in] ptimer    Pointer to the periodic timer to set.
251
 * @param[in] interval  Interval for the periodic timer to fire,
252
 * @param[in] cb        Pointer to a callback function to be called.
253
 *                      In contrast to other callback functions, this get called from an already ISR locked context.
254
 *                      This it may not contain any chSysLockX() or chSysUnlockX() calls.
255
 * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
256
 */
257
static inline void aosPeriodicTimerSet(aos_periodictimer_t *ptimer, aos_interval_t interval, vtfunc_t cb, void *par)
258
{
259
  chSysLock();
260
  aosPeriodicTimerSetI(ptimer, interval, cb, par);
261
  chSysUnlock();
262
263
  return;
264
}
265
266
/**
267
 * @brief   Set a periodic timer to fire in the specified interval.
268
 *
269
 * @param[in] ptimer    Pointer to the periodic timer to set.
270
 * @param[in] interval  Pointer to a long interval value for the periodic timer to fire,
271
 * @param[in] cb        Pointer to a callback function to be called.
272
 *                      In contrast to other callback functions, this get called from an already ISR locked context.
273
 *                      This it may not contain any chSysLockX() or chSysUnlockX() calls.
274
 * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
275
 */
276
static inline void aosPeriodicTimerSetLong(aos_periodictimer_t* ptimer, aos_longinterval_t* interval, vtfunc_t cb, void* par)
277
{
278
  chSysLock();
279
  aosPeriodicTimerSetLongI(ptimer, interval, cb, par);
280
  chSysUnlock();
281
282
  return;
283
}
284
285 6ff06bbf Thomas Schöpping
#endif /* AMIROOS_TIMER_H */
286 53710ca3 Marc Rothmann
287
/** @} */