Statistics
| Branch: | Tag: | Revision:

amiro-os / os / core / inc / aos_timer.h @ e545e620

History | View | Annotate | Download (7.05 KB)

1
/*
2
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  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
#ifndef _AMIROOS_TIMER_H_
20
#define _AMIROOS_TIMER_H_
21

    
22
#include <ch.h>
23
#include <aos_time.h>
24

    
25
/**
26
 * @brief   Maximum timer interval that can be set in system ticks.
27
 */
28
#define TIMER_MAX_INTERVAL_ST         TIME_MAXIMUM
29

    
30
/**
31
 * @brief   Maximum timer interval that can be set in seconds.
32
 */
33
#define TIMER_MAX_INTERVAL_S          (ST2S(TIMER_MAX_INTERVAL_ST) - 1)
34

    
35
/**
36
 * @brief   Maximum timer interval that can be set in milliseconds.
37
 */
38
#define TIMER_MAX_INTERVAL_MS         (ST2MS(TIMER_MAX_INTERVAL_ST) - 1)
39

    
40
/**
41
 * @brief   Maximum timer interval that can be set in microseconds.
42
 */
43
#define TIMER_MAX_INTERVAL_US         (ST2US(TIMER_MAX_INTERVAL_ST) - 1)
44

    
45
/**
46
 * @brief   Timer stucture.
47
 */
48
typedef struct aos_timer {
49
  /**
50
   * @brief   ChibiOS virtual timer.
51
   */
52
  virtual_timer_t vt;
53

    
54
  /**
55
   * @brief   Time to wake up.
56
   */
57
  aos_timestamp_t wkuptime;
58

    
59
  /**
60
   * @brief   Pointer to a callback function.
61
   */
62
  vtfunc_t callback;
63

    
64
  /**
65
   * @brief   Pointer to a parameter for the callback function.
66
   */
67
  void* cbparam;
68
} aos_timer_t;
69

    
70
/**
71
 * @brief   Periodic timer structure.
72
 */
73
typedef struct aos_periodictimer {
74
  /**
75
   * @brief   AMiRo-OS timer.
76
   */
77
  aos_timer_t timer;
78

    
79
  /**
80
   * @brief   Period interval.
81
   */
82
  aos_longinterval_t interval;
83

    
84
  /**
85
   * @brief   Pointer to a callback function.
86
   */
87
  vtfunc_t callback;
88

    
89
  /**
90
   * @brief   Pointer to a parameter for the callback function.
91
   */
92
  void* cbparam;
93
} aos_periodictimer_t;
94

    
95
#ifdef __cplusplus
96
extern "C" {
97
#endif
98
  void aosTimerInit(aos_timer_t* timer);
99
  void aosTimerSetAbsoluteI(aos_timer_t* timer, aos_timestamp_t* uptime, vtfunc_t cb, void* par);
100
  void aosTimerSetIntervalI(aos_timer_t* timer, aos_interval_t offset, vtfunc_t cb, void* par);
101
  void aosTimerSetLongIntervalI(aos_timer_t* timer, aos_longinterval_t* offset, vtfunc_t cb, void* par);
102
  void aosPeriodicTimerInit(aos_periodictimer_t* ptimer);
103
  void aosPeriodicTimerSetI(aos_periodictimer_t* ptimer, aos_interval_t interval, vtfunc_t cb, void* par);
104
  void aosPeriodicTimerSetLongI(aos_periodictimer_t* ptimer, aos_longinterval_t* interval, vtfunc_t cb, void* par);
105
#ifdef __cplusplus
106
}
107
#endif
108

    
109
/**
110
 * @brief   Set timer to fire at an absolute system time.
111
 *
112
 * @param[in] timer   Pointer to the timer to set.
113
 * @param[in] uptime  Pointer to the absolute uptime for the timer to fire.
114
 * @param[in] cb      Pointer to a callback function to be called.
115
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
116
 */
117
static inline void aosTimerSetAbsolute(aos_timer_t* timer, aos_timestamp_t* uptime, vtfunc_t cb, void* par)
118
{
119
  chSysLock();
120
  aosTimerSetAbsoluteI(timer, uptime, cb, par);
121
  chSysUnlock();
122

    
123
  return;
124
}
125

    
126
/**
127
 * @brief   Set timer to fire after a relative interval.
128
 *
129
 * @param[in] timer   Pointer to the timer to set.
130
 * @param[in] offset  Relative interval to set for the timer to fire.
131
 * @param[in] cb      Pointer to a callback function to be called.
132
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
133
 */
134
static inline void aosTimerSetInterval(aos_timer_t* timer, aos_interval_t offset, vtfunc_t cb, void* par)
135
{
136
  chSysLock();
137
  aosTimerSetIntervalI(timer, offset, cb, par);
138
  chSysUnlock();
139
}
140

    
141
/**
142
 * @brief   Set timer to fire after a long relative interval.
143
 *
144
 * @param[in] timer   Pointer to the timer to set.
145
 * @param[in] offset  Pointer to a long interval value to set for the timer to fire.
146
 * @param[in] cb      Pointer to a callback function to be called.
147
 * @param[in] par     Pointer to a parameter fpr the callback function (may be NULL).
148
 */
149
static inline void aosTimerSetLongInterval(aos_timer_t* timer, aos_longinterval_t* offset, vtfunc_t cb, void* par)
150
{
151
  chSysLock();
152
  aosTimerSetLongIntervalI(timer, offset, cb, par);
153
  chSysUnlock();
154
}
155

    
156
/**
157
 * @brief   Reset a timer.
158
 *
159
 * @param[in] timer   Pointer to the timer to reset.
160
 */
161
static inline void aosTimerResetI(aos_timer_t* timer)
162
{
163
  chVTResetI(&(timer->vt));
164

    
165
  return;
166
}
167

    
168
/**
169
 * @brief   Reset a timer.
170
 *
171
 * @param[in] timer   Pointer to the timer to reset.
172
 */
173
static inline void aosTimerReset(aos_timer_t* timer)
174
{
175
  chSysLock();
176
  aosTimerResetI(timer);
177
  chSysUnlock();
178

    
179
  return;
180
}
181

    
182
/**
183
 * @brief   Determine whether a timer is armed.
184
 *
185
 * @param[in] timer   Pointer to the timer to check.
186
 *
187
 * @return    true if the timer is armed, false otherwise.
188
 */
189
static inline bool aosTimerIsArmedI(aos_timer_t* timer)
190
{
191
  return chVTIsArmedI(&(timer->vt));
192
}
193

    
194
/**
195
 * @brief   Determine whether a timer is armed.
196
 *
197
 * @param[in] timer   Pointer to the timer to check.
198
 *
199
 * @return    true if the timer is armed, false otherwise.
200
 */
201
static inline bool aosTimerIsArmed(aos_timer_t* timer)
202
{
203
  bool b;
204

    
205
  chSysLock();
206
  b = aosTimerIsArmedI(timer);
207
  chSysUnlock();
208

    
209
  return b;
210
}
211

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

    
228
  return;
229
}
230

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

    
247
  return;
248
}
249

    
250
#endif /* _AMIROOS_TIMER_H_ */