amiro-os / core / inc / aos_timer.h @ 8ba3c06b
History | View | Annotate | Download (9.064 KB)
| 1 | e545e620 | Thomas Schöpping | /*
|
|---|---|---|---|
| 2 | AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
||
| 3 | 96621a83 | Thomas Schöpping | Copyright (C) 2016..2020 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 | 23ec8223 | Thomas Schöpping | * @brief Timer structure.
|
| 70 | e545e620 | Thomas Schöpping | */
|
| 71 | typedef struct aos_timer { |
||
| 72 | /**
|
||
| 73 | * @brief ChibiOS virtual timer.
|
||
| 74 | */
|
||
| 75 | virtual_timer_t vt; |
||
| 76 | |||
| 77 | /**
|
||
| 78 | 23ec8223 | Thomas Schöpping | * @brief Absolute time to trigger.
|
| 79 | e545e620 | Thomas Schöpping | */
|
| 80 | 23ec8223 | Thomas Schöpping | aos_timestamp_t triggertime; |
| 81 | e545e620 | Thomas Schöpping | |
| 82 | /**
|
||
| 83 | 23ec8223 | Thomas Schöpping | * @brief Relative interval for periodic timers.
|
| 84 | * @note A value of 0 indicates single shot timing.
|
||
| 85 | e545e620 | Thomas Schöpping | */
|
| 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 | 23ec8223 | Thomas Schöpping | } aos_timer_t; |
| 98 | e545e620 | Thomas Schöpping | |
| 99 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
| 100 | /* MACROS */
|
||
| 101 | /******************************************************************************/
|
||
| 102 | |||
| 103 | /******************************************************************************/
|
||
| 104 | /* EXTERN DECLARATIONS */
|
||
| 105 | /******************************************************************************/
|
||
| 106 | |||
| 107 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
| 108 | e545e620 | Thomas Schöpping | extern "C" { |
| 109 | 7de0cc90 | Thomas Schöpping | #endif /* defined(__cplusplus) */ |
| 110 | e545e620 | Thomas Schöpping | void aosTimerInit(aos_timer_t* timer);
|
| 111 | 23ec8223 | Thomas Schöpping | 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 | 7de0cc90 | Thomas Schöpping | #if defined(__cplusplus)
|
| 117 | e545e620 | Thomas Schöpping | } |
| 118 | 7de0cc90 | Thomas Schöpping | #endif /* defined(__cplusplus) */ |
| 119 | e545e620 | Thomas Schöpping | |
| 120 | f3ac1c96 | Thomas Schöpping | /******************************************************************************/
|
| 121 | /* INLINE FUNCTIONS */
|
||
| 122 | /******************************************************************************/
|
||
| 123 | |||
| 124 | e545e620 | Thomas Schöpping | /**
|
| 125 | 23ec8223 | Thomas Schöpping | * @brief Set timer to trigger at an absolute system time.
|
| 126 | e545e620 | Thomas Schöpping | *
|
| 127 | * @param[in] timer Pointer to the timer to set.
|
||
| 128 | 23ec8223 | Thomas Schöpping | * @param[in] uptime Absolute uptime for the timer to trigger.
|
| 129 | e545e620 | Thomas Schöpping | * @param[in] cb Pointer to a callback function to be called.
|
| 130 | 23ec8223 | Thomas Schöpping | * @param[in] par Pointer to a parameter for the callback function (may be NULL).
|
| 131 | e545e620 | Thomas Schöpping | */
|
| 132 | 23ec8223 | Thomas Schöpping | static inline void aosTimerSetAbsolute(aos_timer_t* timer, const aos_timestamp_t uptime, vtfunc_t cb, void* par) |
| 133 | e545e620 | Thomas Schöpping | {
|
| 134 | chSysLock(); |
||
| 135 | aosTimerSetAbsoluteI(timer, uptime, cb, par); |
||
| 136 | chSysUnlock(); |
||
| 137 | |||
| 138 | return;
|
||
| 139 | } |
||
| 140 | |||
| 141 | /**
|
||
| 142 | 23ec8223 | Thomas Schöpping | * @brief Set timer to trigger after a relative interval.
|
| 143 | e545e620 | Thomas Schöpping | *
|
| 144 | * @param[in] timer Pointer to the timer to set.
|
||
| 145 | 23ec8223 | Thomas Schöpping | * @param[in] offset Relative interval to set for the timer to trigger.
|
| 146 | e545e620 | Thomas Schöpping | * @param[in] cb Pointer to a callback function to be called.
|
| 147 | 23ec8223 | Thomas Schöpping | * 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 | e545e620 | Thomas Schöpping | */
|
| 151 | 23ec8223 | Thomas Schöpping | static inline void aosTimerSetInterval(aos_timer_t* timer, const aos_interval_t offset, vtfunc_t cb, void* par) |
| 152 | e545e620 | Thomas Schöpping | {
|
| 153 | chSysLock(); |
||
| 154 | aosTimerSetIntervalI(timer, offset, cb, par); |
||
| 155 | chSysUnlock(); |
||
| 156 | } |
||
| 157 | |||
| 158 | /**
|
||
| 159 | 23ec8223 | Thomas Schöpping | * @brief Set timer to trigger after a long relative interval.
|
| 160 | e545e620 | Thomas Schöpping | *
|
| 161 | * @param[in] timer Pointer to the timer to set.
|
||
| 162 | 23ec8223 | Thomas Schöpping | * @param[in] offset Long interval value to set for the timer to trigger.
|
| 163 | e545e620 | Thomas Schöpping | * @param[in] cb Pointer to a callback function to be called.
|
| 164 | 23ec8223 | Thomas Schöpping | * 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 | e545e620 | Thomas Schöpping | */
|
| 168 | 23ec8223 | Thomas Schöpping | static inline void aosTimerSetLongInterval(aos_timer_t* timer, const aos_longinterval_t offset, vtfunc_t cb, void* par) |
| 169 | e545e620 | Thomas Schöpping | {
|
| 170 | chSysLock(); |
||
| 171 | aosTimerSetLongIntervalI(timer, offset, cb, par); |
||
| 172 | chSysUnlock(); |
||
| 173 | } |
||
| 174 | |||
| 175 | /**
|
||
| 176 | 23ec8223 | Thomas Schöpping | * @brief Set timer to trigger periodically in the specified interval.
|
| 177 | e545e620 | Thomas Schöpping | *
|
| 178 | 23ec8223 | Thomas Schöpping | * @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 | e545e620 | Thomas Schöpping | */
|
| 185 | 23ec8223 | Thomas Schöpping | static inline void aosTimerPeriodicInterval(aos_timer_t *timer, const aos_interval_t interval, vtfunc_t cb, void *par) |
| 186 | e545e620 | Thomas Schöpping | {
|
| 187 | 23ec8223 | Thomas Schöpping | chSysLock(); |
| 188 | aosTimerPeriodicIntervalI(timer, interval, cb, par); |
||
| 189 | chSysUnlock(); |
||
| 190 | e545e620 | Thomas Schöpping | |
| 191 | return;
|
||
| 192 | } |
||
| 193 | |||
| 194 | /**
|
||
| 195 | 23ec8223 | Thomas Schöpping | * @brief Set timer to trigger periodically in the specified interval.
|
| 196 | e545e620 | Thomas Schöpping | *
|
| 197 | 23ec8223 | Thomas Schöpping | * @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 | e545e620 | Thomas Schöpping | */
|
| 204 | 23ec8223 | Thomas Schöpping | static inline void aosTimerPeriodicLongInterval(aos_timer_t* timer, const aos_longinterval_t interval, vtfunc_t cb, void* par) |
| 205 | e545e620 | Thomas Schöpping | {
|
| 206 | chSysLock(); |
||
| 207 | 23ec8223 | Thomas Schöpping | aosTimerPeriodicLongIntervalI(timer, interval, cb, par); |
| 208 | e545e620 | Thomas Schöpping | 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 | 23ec8223 | Thomas Schöpping | * @brief Reset a timer.
|
| 245 | e545e620 | Thomas Schöpping | *
|
| 246 | 23ec8223 | Thomas Schöpping | * @param[in] timer Pointer to the timer to reset.
|
| 247 | e545e620 | Thomas Schöpping | */
|
| 248 | 23ec8223 | Thomas Schöpping | static inline void aosTimerResetI(aos_timer_t* timer) |
| 249 | e545e620 | Thomas Schöpping | {
|
| 250 | 23ec8223 | Thomas Schöpping | chVTResetI(&(timer->vt)); |
| 251 | timer->interval = 0;
|
||
| 252 | e545e620 | Thomas Schöpping | |
| 253 | return;
|
||
| 254 | } |
||
| 255 | |||
| 256 | /**
|
||
| 257 | 23ec8223 | Thomas Schöpping | * @brief Reset a timer.
|
| 258 | e545e620 | Thomas Schöpping | *
|
| 259 | 23ec8223 | Thomas Schöpping | * @param[in] timer Pointer to the timer to reset.
|
| 260 | e545e620 | Thomas Schöpping | */
|
| 261 | 23ec8223 | Thomas Schöpping | static inline void aosTimerReset(aos_timer_t* timer) |
| 262 | e545e620 | Thomas Schöpping | {
|
| 263 | chSysLock(); |
||
| 264 | 23ec8223 | Thomas Schöpping | aosTimerResetI(timer); |
| 265 | e545e620 | Thomas Schöpping | chSysUnlock(); |
| 266 | |||
| 267 | return;
|
||
| 268 | } |
||
| 269 | |||
| 270 | 6ff06bbf | Thomas Schöpping | #endif /* AMIROOS_TIMER_H */ |
| 271 | 53710ca3 | Marc Rothmann | |
| 272 | /** @} */ |