diff options
Diffstat (limited to 'tmk_core/common/avr/timer.c')
-rw-r--r-- | tmk_core/common/avr/timer.c | 61 |
1 files changed, 18 insertions, 43 deletions
diff --git a/tmk_core/common/avr/timer.c b/tmk_core/common/avr/timer.c index b7d4f060e..63ec549df 100644 --- a/tmk_core/common/avr/timer.c +++ b/tmk_core/common/avr/timer.c | |||
@@ -22,7 +22,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
22 | #include "timer_avr.h" | 22 | #include "timer_avr.h" |
23 | #include "timer.h" | 23 | #include "timer.h" |
24 | 24 | ||
25 | |||
26 | // counter resolution 1ms | 25 | // counter resolution 1ms |
27 | // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} | 26 | // NOTE: union { uint32_t timer32; struct { uint16_t dummy; uint16_t timer16; }} |
28 | volatile uint32_t timer_count; | 27 | volatile uint32_t timer_count; |
@@ -31,8 +30,7 @@ volatile uint32_t timer_count; | |||
31 | * | 30 | * |
32 | * FIXME: needs doc | 31 | * FIXME: needs doc |
33 | */ | 32 | */ |
34 | void timer_init(void) | 33 | void timer_init(void) { |
35 | { | ||
36 | #if TIMER_PRESCALER == 1 | 34 | #if TIMER_PRESCALER == 1 |
37 | uint8_t prescaler = 0x01; | 35 | uint8_t prescaler = 0x01; |
38 | #elif TIMER_PRESCALER == 8 | 36 | #elif TIMER_PRESCALER == 8 |
@@ -44,7 +42,7 @@ void timer_init(void) | |||
44 | #elif TIMER_PRESCALER == 1024 | 42 | #elif TIMER_PRESCALER == 1024 |
45 | uint8_t prescaler = 0x05; | 43 | uint8_t prescaler = 0x05; |
46 | #else | 44 | #else |
47 | # error "Timer prescaler value is NOT vaild." | 45 | # error "Timer prescaler value is NOT vaild." |
48 | #endif | 46 | #endif |
49 | 47 | ||
50 | #ifndef __AVR_ATmega32A__ | 48 | #ifndef __AVR_ATmega32A__ |
@@ -53,13 +51,13 @@ void timer_init(void) | |||
53 | 51 | ||
54 | TCCR0B = prescaler; | 52 | TCCR0B = prescaler; |
55 | 53 | ||
56 | OCR0A = TIMER_RAW_TOP; | 54 | OCR0A = TIMER_RAW_TOP; |
57 | TIMSK0 = (1<<OCIE0A); | 55 | TIMSK0 = (1 << OCIE0A); |
58 | #else | 56 | #else |
59 | // Timer0 CTC mode | 57 | // Timer0 CTC mode |
60 | TCCR0 = (1 << WGM01) | prescaler; | 58 | TCCR0 = (1 << WGM01) | prescaler; |
61 | 59 | ||
62 | OCR0 = TIMER_RAW_TOP; | 60 | OCR0 = TIMER_RAW_TOP; |
63 | TIMSK = (1 << OCIE0); | 61 | TIMSK = (1 << OCIE0); |
64 | #endif | 62 | #endif |
65 | } | 63 | } |
@@ -68,26 +66,18 @@ void timer_init(void) | |||
68 | * | 66 | * |
69 | * FIXME: needs doc | 67 | * FIXME: needs doc |
70 | */ | 68 | */ |
71 | inline | 69 | inline void timer_clear(void) { |
72 | void timer_clear(void) | 70 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { timer_count = 0; } |
73 | { | ||
74 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | ||
75 | timer_count = 0; | ||
76 | } | ||
77 | } | 71 | } |
78 | 72 | ||
79 | /** \brief timer read | 73 | /** \brief timer read |
80 | * | 74 | * |
81 | * FIXME: needs doc | 75 | * FIXME: needs doc |
82 | */ | 76 | */ |
83 | inline | 77 | inline uint16_t timer_read(void) { |
84 | uint16_t timer_read(void) | ||
85 | { | ||
86 | uint32_t t; | 78 | uint32_t t; |
87 | 79 | ||
88 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | 80 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } |
89 | t = timer_count; | ||
90 | } | ||
91 | 81 | ||
92 | return (t & 0xFFFF); | 82 | return (t & 0xFFFF); |
93 | } | 83 | } |
@@ -96,14 +86,10 @@ uint16_t timer_read(void) | |||
96 | * | 86 | * |
97 | * FIXME: needs doc | 87 | * FIXME: needs doc |
98 | */ | 88 | */ |
99 | inline | 89 | inline uint32_t timer_read32(void) { |
100 | uint32_t timer_read32(void) | ||
101 | { | ||
102 | uint32_t t; | 90 | uint32_t t; |
103 | 91 | ||
104 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | 92 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } |
105 | t = timer_count; | ||
106 | } | ||
107 | 93 | ||
108 | return t; | 94 | return t; |
109 | } | 95 | } |
@@ -112,14 +98,10 @@ uint32_t timer_read32(void) | |||
112 | * | 98 | * |
113 | * FIXME: needs doc | 99 | * FIXME: needs doc |
114 | */ | 100 | */ |
115 | inline | 101 | inline uint16_t timer_elapsed(uint16_t last) { |
116 | uint16_t timer_elapsed(uint16_t last) | ||
117 | { | ||
118 | uint32_t t; | 102 | uint32_t t; |
119 | 103 | ||
120 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | 104 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } |
121 | t = timer_count; | ||
122 | } | ||
123 | 105 | ||
124 | return TIMER_DIFF_16((t & 0xFFFF), last); | 106 | return TIMER_DIFF_16((t & 0xFFFF), last); |
125 | } | 107 | } |
@@ -128,25 +110,18 @@ uint16_t timer_elapsed(uint16_t last) | |||
128 | * | 110 | * |
129 | * FIXME: needs doc | 111 | * FIXME: needs doc |
130 | */ | 112 | */ |
131 | inline | 113 | inline uint32_t timer_elapsed32(uint32_t last) { |
132 | uint32_t timer_elapsed32(uint32_t last) | ||
133 | { | ||
134 | uint32_t t; | 114 | uint32_t t; |
135 | 115 | ||
136 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | 116 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { t = timer_count; } |
137 | t = timer_count; | ||
138 | } | ||
139 | 117 | ||
140 | return TIMER_DIFF_32(t, last); | 118 | return TIMER_DIFF_32(t, last); |
141 | } | 119 | } |
142 | 120 | ||
143 | // excecuted once per 1ms.(excess for just timer count?) | 121 | // excecuted once per 1ms.(excess for just timer count?) |
144 | #ifndef __AVR_ATmega32A__ | 122 | #ifndef __AVR_ATmega32A__ |
145 | #define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect | 123 | # define TIMER_INTERRUPT_VECTOR TIMER0_COMPA_vect |
146 | #else | 124 | #else |
147 | #define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect | 125 | # define TIMER_INTERRUPT_VECTOR TIMER0_COMP_vect |
148 | #endif | 126 | #endif |
149 | ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) | 127 | ISR(TIMER_INTERRUPT_VECTOR, ISR_NOBLOCK) { timer_count++; } |
150 | { | ||
151 | timer_count++; | ||
152 | } | ||