Rev 537 | Details | Compare with Previous | 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 | /*----------------------------------------------------------------------------- |
||
| 12 | * Includes |
||
| 13 | *---------------------------------------------------------------------------*/ |
||
| 14 | #include <inttypes.h> |
||
| 15 | #include <avr/io.h> |
||
| 16 | #include <avr/interrupt.h> |
||
| 17 | |||
| 18 | #include <drivers/timer/timer.h> |
||
| 19 | #include "timer_cfg.h" |
||
| 20 | |||
| 21 | |||
| 22 | /*----------------------------------------------------------------------------- |
||
| 23 | * Globals |
||
| 24 | *---------------------------------------------------------------------------*/ |
||
| 25 | volatile uint32_t ticks; |
||
| 26 | |||
| 27 | struct { |
||
| 28 | uint16_t timeout; |
||
| 29 | uint16_t reload; |
||
| 30 | unsigned char expired; |
||
| 31 | timerCallback_t callback; |
||
| 32 | } timers[TIMER_NUM_TIMERS]; |
||
| 33 | |||
| 34 | /*----------------------------------------------------------------------------- |
||
| 35 | * Interrupt Service Routines |
||
| 36 | *---------------------------------------------------------------------------*/ |
||
| 37 | ISR(TIMER_VECTOR) |
||
| 38 | { |
||
| 39 | uint8_t t; |
||
| 40 | #if defined(TIMER_RELOAD) |
||
| 41 | /* The hw timer does not support auto-reload so set the count manually. */ |
||
| 42 | TIMER_RELOAD(TIMER_COUNTS_PER_TICK); |
||
| 43 | #endif |
||
| 44 | |||
| 45 | /* Increase the global tick count. */ |
||
| 46 | ticks++; |
||
| 47 | |||
| 48 | /* Decrease the timeout of all active timers and check if any have |
||
| 49 | * expired. */ |
||
| 50 | for (t = 0; t < TIMER_NUM_TIMERS; t++) |
||
| 51 | { |
||
| 52 | if ((timers[t].timeout > 0) && (--timers[t].timeout == 0)) |
||
| 53 | { |
||
| 54 | timers[t].expired = 1; |
||
| 55 | timers[t].timeout = timers[t].reload; |
||
| 56 | /* Call the callback, if any. */ |
||
| 57 | if (timers[t].callback) timers[t].callback(t); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | |||
| 63 | /*----------------------------------------------------------------------------- |
||
| 64 | * Public Functions |
||
| 65 | *---------------------------------------------------------------------------*/ |
||
| 66 | |||
| 544 | andfri | 67 | void Timer_Init() |
| 537 | andfri | 68 | { |
| 69 | /* Do basic setup of the timer hw, including interrupts. */ |
||
| 70 | TIMER_INIT(); |
||
| 71 | #if defined(TIMER_RELOAD) |
||
| 72 | /* The hw timer does not support auto-reload so set the count manually. */ |
||
| 73 | TIMER_RELOAD(TIMER_COUNTS_PER_TICK); |
||
| 74 | #endif |
||
| 75 | #if defined(TIMER_COMPARE_REG) |
||
| 76 | /* The hw timer has a compare match module so use it for auto-reload. */ |
||
| 77 | TIMER_COMPARE_REG = TIMER_COUNTS_PER_TICK; |
||
| 78 | #endif |
||
| 79 | /* Finalize hw setup and start the timer by setting its prescaler. */ |
||
| 80 | TIMER_SET_PRESCALER(); |
||
| 81 | } |
||
| 82 | |||
| 83 | /*---------------------------------------------------------------------------*/ |
||
| 84 | |||
| 544 | andfri | 85 | uint32_t Timer_GetTicks() |
| 537 | andfri | 86 | { |
| 87 | uint32_t res; |
||
| 88 | uint8_t sreg = SREG; |
||
| 89 | cli(); |
||
| 90 | res = ticks; |
||
| 91 | SREG = sreg; |
||
| 92 | return res; |
||
| 93 | } |
||
| 94 | |||
| 95 | /*---------------------------------------------------------------------------*/ |
||
| 96 | |||
| 544 | andfri | 97 | void Timer_SetTimeout(uint8_t timer, uint16_t timeout, uint8_t type, timerCallback_t callback) |
| 537 | andfri | 98 | { |
| 99 | if (timer<TIMER_NUM_TIMERS) |
||
| 100 | { |
||
| 101 | uint8_t sreg = SREG; |
||
| 102 | cli(); |
||
| 103 | timers[timer].expired = 0; |
||
| 104 | timers[timer].timeout = timeout; |
||
| 105 | if (type == TimerTypeFreeRunning) |
||
| 106 | timers[timer].reload = timeout; |
||
| 107 | else |
||
| 108 | timers[timer].reload = 0; |
||
| 109 | timers[timer].callback = callback; |
||
| 110 | SREG = sreg; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /*---------------------------------------------------------------------------*/ |
||
| 115 | |||
| 544 | andfri | 116 | uint8_t Timer_Expired(uint8_t timer) |
| 537 | andfri | 117 | { |
| 118 | if ((timer<TIMER_NUM_TIMERS) && (timers[timer].expired)) |
||
| 119 | { |
||
| 120 | timers[timer].expired = 0; |
||
| 121 | return 1; |
||
| 122 | } |
||
| 123 | return 0; |
||
| 124 | } |