Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 537 | andfri | 1 | /** |
| 2 | * Timer software module. Utilizes a hardware timer to provide a number of |
||
| 3 | * software timers to the application. Each timer can be set to either one-shot |
||
| 4 | * or free-running mode. |
||
| 5 | * |
||
| 6 | * @author Andreas Fritiofson |
||
| 7 | * |
||
| 8 | * @date 2007-07-13 |
||
| 9 | */ |
||
| 10 | |||
| 11 | #ifndef TIMER_H_ |
||
| 12 | #define TIMER_H_ |
||
| 13 | |||
| 14 | |||
| 15 | /*----------------------------------------------------------------------------- |
||
| 16 | * Includes |
||
| 17 | *---------------------------------------------------------------------------*/ |
||
| 18 | #include <inttypes.h> |
||
| 19 | |||
| 20 | /*----------------------------------------------------------------------------- |
||
| 21 | * Defines |
||
| 22 | *---------------------------------------------------------------------------*/ |
||
| 23 | |||
| 24 | /** |
||
| 25 | * When passed as the type argument to timerSetTimeout, the timer will trigger |
||
| 26 | * only once and then disable itself. |
||
| 27 | */ |
||
| 28 | #define TimerTypeOneShot 0 |
||
| 29 | /** |
||
| 30 | * When passed as the type argument to timerSetTimeout, the timer will reload |
||
| 31 | * itself at every expiration and trigger periodically. |
||
| 32 | */ |
||
| 33 | #define TimerTypeFreeRunning 1 |
||
| 34 | |||
| 35 | /*----------------------------------------------------------------------------- |
||
| 36 | * Public Types |
||
| 37 | *---------------------------------------------------------------------------*/ |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The type of the callback function pointer. |
||
| 41 | */ |
||
| 42 | typedef void (*timerCallback_t)(uint8_t); |
||
| 43 | |||
| 44 | /*----------------------------------------------------------------------------- |
||
| 45 | * Public Function Prototypes |
||
| 46 | *---------------------------------------------------------------------------*/ |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Initializes the timer engine. The hardware timer interrupt source will be |
||
| 50 | * enabled, but global interrupts need to be enabled by the application. |
||
| 51 | */ |
||
| 52 | void timerInit(void); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Reads the current time. |
||
| 56 | * |
||
| 57 | * @return |
||
| 58 | * The current time in ticks. The tick count wraps around after 2^32 |
||
| 59 | * ticks without any notification. |
||
| 60 | */ |
||
| 61 | uint32_t timerGetTicks(void); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Initializes a software timer and starts it. |
||
| 65 | * |
||
| 66 | * @param timer |
||
| 67 | * The timer. |
||
| 68 | * @param timeout |
||
| 69 | * The timeout in ticks. Passing zero disables a running timer. A timeout |
||
| 70 | * value of X ticks gives an actual timeout of between (X-1) and X ticks, |
||
| 71 | * not including the time it takes for the application to check the |
||
| 72 | * expiration status of the timer. |
||
| 73 | * @param type |
||
| 74 | * TimerTypeOneShot or TimerTypeFreeRunning. |
||
| 75 | * @param callback |
||
| 76 | * Pointer to optional callback function on the form |
||
| 77 | * void callback(uint8_t timer). The callback function is called with the |
||
| 78 | * timer number as the argument whenever the timer expires. It is called |
||
| 79 | * from the interrupt handler and must be very short. Null if not used. |
||
| 80 | */ |
||
| 81 | void timerSetTimeout(uint8_t timer, uint16_t timeout, uint8_t type, timerCallback_t callback); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Checks if a timer has expired. The timer will only signal its expiration at |
||
| 85 | * the first call to this function after a timeout period, even if multiple |
||
| 86 | * timeout periods have elapsed since the previous call. |
||
| 87 | * |
||
| 88 | * @param timer |
||
| 89 | * The timer (0<=timer<TIMER_NUM_TIMERS). |
||
| 90 | * @return |
||
| 91 | * Non-zero if timer has expired since the previous check. |
||
| 92 | */ |
||
| 93 | uint8_t timerExpired(uint8_t timer); |
||
| 94 | |||
| 95 | |||
| 96 | #endif /*TIMER_H_*/ |