Statistics
| Branch: | Tag: | Revision:

amiro-os / core / inc / aos_timer.h @ 1678f270

History | View | Annotate | Download (7.2 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  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 <ch.h>
31
#include <aos_time.h>
32

    
33
/**
34
 * @brief   Maximum timer interval that can be set in system ticks.
35
 */
36
#define AOS_TIMER_MAX_INTERVAL_ST     TIME_MAX_INTERVAL
37

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

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

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

    
53
/**
54
 * @brief   Timer stucture.
55
 */
56
typedef struct aos_timer {
57
  /**
58
   * @brief   ChibiOS virtual timer.
59
   */
60
  virtual_timer_t vt;
61

    
62
  /**
63
   * @brief   Time to wake up.
64
   */
65
  aos_timestamp_t wkuptime;
66

    
67
  /**
68
   * @brief   Pointer to a callback function.
69
   */
70
  vtfunc_t callback;
71

    
72
  /**
73
   * @brief   Pointer to a parameter for the callback function.
74
   */
75
  void* cbparam;
76
} aos_timer_t;
77

    
78
/**
79
 * @brief   Periodic timer structure.
80
 */
81
typedef struct aos_periodictimer {
82
  /**
83
   * @brief   AMiRo-OS timer.
84
   */
85
  aos_timer_t timer;
86

    
87
  /**
88
   * @brief   Period interval.
89
   */
90
  aos_longinterval_t interval;
91

    
92
  /**
93
   * @brief   Pointer to a callback function.
94
   */
95
  vtfunc_t callback;
96

    
97
  /**
98
   * @brief   Pointer to a parameter for the callback function.
99
   */
100
  void* cbparam;
101
} aos_periodictimer_t;
102

    
103
#ifdef __cplusplus
104
extern "C" {
105
#endif
106
  void aosTimerInit(aos_timer_t* timer);
107
  void aosTimerSetAbsoluteI(aos_timer_t* timer, aos_timestamp_t* uptime, vtfunc_t cb, void* par);
108
  void aosTimerSetIntervalI(aos_timer_t* timer, aos_interval_t offset, vtfunc_t cb, void* par);
109
  void aosTimerSetLongIntervalI(aos_timer_t* timer, aos_longinterval_t* offset, vtfunc_t cb, void* par);
110
  void aosPeriodicTimerInit(aos_periodictimer_t* ptimer);
111
  void aosPeriodicTimerSetI(aos_periodictimer_t* ptimer, aos_interval_t interval, vtfunc_t cb, void* par);
112
  void aosPeriodicTimerSetLongI(aos_periodictimer_t* ptimer, aos_longinterval_t* interval, vtfunc_t cb, void* par);
113
#ifdef __cplusplus
114
}
115
#endif
116

    
117
/**
118
 * @brief   Set timer to fire at an absolute system time.
119
 *
120
 * @param[in] timer   Pointer to the timer to set.
121
 * @param[in] uptime  Pointer to the absolute uptime for the timer to fire.
122
 * @param[in] cb      Pointer to a callback function to be called.
123
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
124
 */
125
static inline void aosTimerSetAbsolute(aos_timer_t* timer, aos_timestamp_t* uptime, vtfunc_t cb, void* par)
126
{
127
  chSysLock();
128
  aosTimerSetAbsoluteI(timer, uptime, cb, par);
129
  chSysUnlock();
130

    
131
  return;
132
}
133

    
134
/**
135
 * @brief   Set timer to fire after a relative interval.
136
 *
137
 * @param[in] timer   Pointer to the timer to set.
138
 * @param[in] offset  Relative interval to set for the timer to fire.
139
 * @param[in] cb      Pointer to a callback function to be called.
140
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
141
 */
142
static inline void aosTimerSetInterval(aos_timer_t* timer, aos_interval_t offset, vtfunc_t cb, void* par)
143
{
144
  chSysLock();
145
  aosTimerSetIntervalI(timer, offset, cb, par);
146
  chSysUnlock();
147
}
148

    
149
/**
150
 * @brief   Set timer to fire after a long relative interval.
151
 *
152
 * @param[in] timer   Pointer to the timer to set.
153
 * @param[in] offset  Pointer to a long interval value to set for the timer to fire.
154
 * @param[in] cb      Pointer to a callback function to be called.
155
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
156
 */
157
static inline void aosTimerSetLongInterval(aos_timer_t* timer, aos_longinterval_t* offset, vtfunc_t cb, void* par)
158
{
159
  chSysLock();
160
  aosTimerSetLongIntervalI(timer, offset, cb, par);
161
  chSysUnlock();
162
}
163

    
164
/**
165
 * @brief   Reset a timer.
166
 *
167
 * @param[in] timer   Pointer to the timer to reset.
168
 */
169
static inline void aosTimerResetI(aos_timer_t* timer)
170
{
171
  chVTResetI(&(timer->vt));
172

    
173
  return;
174
}
175

    
176
/**
177
 * @brief   Reset a timer.
178
 *
179
 * @param[in] timer   Pointer to the timer to reset.
180
 */
181
static inline void aosTimerReset(aos_timer_t* timer)
182
{
183
  chSysLock();
184
  aosTimerResetI(timer);
185
  chSysUnlock();
186

    
187
  return;
188
}
189

    
190
/**
191
 * @brief   Determine whether a timer is armed.
192
 *
193
 * @param[in] timer   Pointer to the timer to check.
194
 *
195
 * @return    true if the timer is armed, false otherwise.
196
 */
197
static inline bool aosTimerIsArmedI(aos_timer_t* timer)
198
{
199
  return chVTIsArmedI(&(timer->vt));
200
}
201

    
202
/**
203
 * @brief   Determine whether a timer is armed.
204
 *
205
 * @param[in] timer   Pointer to the timer to check.
206
 *
207
 * @return    true if the timer is armed, false otherwise.
208
 */
209
static inline bool aosTimerIsArmed(aos_timer_t* timer)
210
{
211
  bool b;
212

    
213
  chSysLock();
214
  b = aosTimerIsArmedI(timer);
215
  chSysUnlock();
216

    
217
  return b;
218
}
219

    
220
/**
221
 * @brief   Set a periodic timer to fire in the specified interval.
222
 *
223
 * @param[in] ptimer    Pointer to the periodic timer to set.
224
 * @param[in] interval  Interval for the periodic timer to fire,
225
 * @param[in] cb        Pointer to a callback function to be called.
226
 *                      In contrast to other callback functions, this get called from an already ISR locked context.
227
 *                      This it may not contain any chSysLockX() or chSysUnlockX() calls.
228
 * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
229
 */
230
static inline void aosPeriodicTimerSet(aos_periodictimer_t *ptimer, aos_interval_t interval, vtfunc_t cb, void *par)
231
{
232
  chSysLock();
233
  aosPeriodicTimerSetI(ptimer, interval, cb, par);
234
  chSysUnlock();
235

    
236
  return;
237
}
238

    
239
/**
240
 * @brief   Set a periodic timer to fire in the specified interval.
241
 *
242
 * @param[in] ptimer    Pointer to the periodic timer to set.
243
 * @param[in] interval  Pointer to a long interval value for the periodic timer to fire,
244
 * @param[in] cb        Pointer to a callback function to be called.
245
 *                      In contrast to other callback functions, this get called from an already ISR locked context.
246
 *                      This it may not contain any chSysLockX() or chSysUnlockX() calls.
247
 * @param[in] par       Pointer to a parameter for the callback function (may be NULL).
248
 */
249
static inline void aosPeriodicTimerSetLong(aos_periodictimer_t* ptimer, aos_longinterval_t* interval, vtfunc_t cb, void* par)
250
{
251
  chSysLock();
252
  aosPeriodicTimerSetLongI(ptimer, interval, cb, par);
253
  chSysUnlock();
254

    
255
  return;
256
}
257

    
258
#endif /* _AMIROOS_TIMER_H_ */
259

    
260
/** @} */