fremoclock  1_3A01
FREMO Clock - Software for UTG
SysHwTimer.c
Go to the documentation of this file.
1 
44 #include <stdint.h> // uint8_t, uint32_t
45 #include <avr/io.h> // SREG
46 #include <avr/interrupt.h> // #define ISR()
47 #include <util/atomic.h> // #define ATOMIC_BLOCK(), ATOMIC_RESTORESTATE
48 #include "ClockKernel.h" // SysTimerUserInterrupt(),
49  // SysTimerUserInterrupt10(),
50  // SysTimerUserInterrupt100()
51 #include "PushButton.h" // PushButtonInterrupt()
52 #include "sysdef.h" // BOARD_PROTO_48, BOARD_PROTO_128
53 #include "SysHwTimer.h" // Prototypes of this module
54 
55 #define USE_TIMER_0
56 //#define USE_TIMER_2 //!!not tested in the first step!!
57 
58 /* Atmel hates me. Every name is changed for every new device.
59 Only a little bit, but enough to get mad.
60 And sometimes a new register:-((
61 
62 With the latest changes it seems to be imossible to get a
63 general naming scheme for the registers an bits. Therefore I
64 don't use the defs for boards, but put them at the direct place.
65 */
66 
67 #if defined BOARD_PROTO_48 //ProtoBoardMega48
68 
69  #ifdef USE_TIMER_0
70  #define TIMER_INT_VECT_COMP TIMER0_COMPA_vect
71  #elif USE_TIMER_2
72  #define TIMER_INT_VECT_COMP TIMER2_COMPA_vect
73  #endif
74 
75 #else
76 
77  #ifdef USE_TIMER_0
78  #define TIMER_INT_VECT_COMP TIMER0_COMP_vect
79  #elif USE_TIMER_2
80  #define TIMER_INT_VECT_COMP TIMER2_COMP_vect
81  #endif
82 
83 #endif //Boardtype
84 
89 static uint32_t ulGlobalTimer = 0;
90 
91 /* The Compare value und the Prescaler ratio have to be selected carefully
92 for every new F_CPU value, to get as close as possible to exact 1ms.
93 
94 COMPARE_VALUE = (F_CPU * TIMER_RESOLUTION_MS) / (1000 * PRESCALER)
95 
96 Be carefull not to get values greater then 255!!
97 */
98 
99 #if defined BOARD_PROTO_128 //ProtoBoardMega128
100  // F_CPU = 11059200
101  #define COMPARE_VALUE 172
102  #define PRESCALER_VALUE 4
104 #elif defined BOARD_PROTO_48 //ProtoBoardMega48
105  // F_CPU = 16000000
106  #define COMPARE_VALUE 249
107  #define PRESCALER_VALUE 3
109 #endif //Boardtype
110 
115 void HwTimerInit(void)
116 {
117 #ifdef USE_TIMER_0
118 
119  #if defined BOARD_PROTO_48 //Mega48/88/168
120 
121  // TCCR0A
122  // Bit# BitName Val. Description
123  // 7:6 COM0A1:0 00 Normal Port Operation
124  // 5:4 COM0B1:0 00 Normal Port Operation
125  // 3:2 -- 00 unused
126  // 2:0 WGM01:0 10 CTC Mode
127  TCCR0A |= 1<<WGM01;
128 
129  // TCCR0B
130  // Bit# BitName Val. Description
131  // 7 FOC0A 0 Not used
132  // 6 FOC0B 0 Not used
133  // 5:4 -- 00 unused
134  // 3 WGM02 0 CTC Mode. The Atmel designers hate me!!!
135  // 2:0 CS02:0 PRESCALER_VALUE
136  TCCR0B = PRESCALER_VALUE;
137 
138  OCR0A = COMPARE_VALUE; // see above
139 
140  TIMSK0 |= 1<<OCIE0A; // enable timer compare a interrupt, invokes
141  // ISR(TIMER_INT_VECT_COMP)
142 
143  #else //Mega128 etc.
144 
145  // Bit# BitName Val. Description
146  // 7 FOC0 0 Not used
147  // 6 WGM00 0 Clear Timer on Compare. DANGER: Bits of WGM2 are numbered in oposite sequence.
148  // 5:4 COM01:0 00 Normal Port Operation
149  // 3 WGM01 1 Clear Timer on Compare. The Atmel designers hate me!!!
150  // 2:0 CS02:0 PRESCALER_VALUE
151  TCCR0 = PRESCALER_VALUE;
152  TCCR0 |= 1<<WGM01;
153 
154  OCR0 = COMPARE_VALUE; // see above
155 
156  TIMSK |= 1<<OCIE0; // enable timer compare a interrupt, invokes
157  // ISR(TIMER_INT_VECT_COMP)
158  #endif
159 
160 #elif USE_TIMER_2
161 
162  #if defined BOARD_PROTO_48 //Mega48/88/168
163 
164  // TCCR2A
165  // Bit# BitName Val.Description
166  // 7:6 COM2A1:0 00 Normal Port Operation
167  // 5:4 COM2B1:0 00 Normal Port Operation
168  // 3:2 -- 00 unused
169  // 2:0 WGM21:0 10 CTC Mode
170  TCCR2A |= 1<<WGM21;
171 
172  // TCCR0B
173  // Bit# BitName Val.Description
174  // 7 FOC2A 0 Not used
175  // 6 FOC2B 0 Not used
176  // 5:4 -- 00 unused
177  // 3 WGM22 0 CTC Mode. The Atmel designers hate me!!!
178  // 2:0 CS22:0 PRESCALER_VALUE
179  TCCR2B = PRESCALER_VALUE;
180 
181  OCR2A = COMPARE_VALUE; // see above
182 
183  TIMSK2 |= 1<<OCIE2A; // enable timer compare a interrupt, invokes ISR(TIMER_INT_VECT_COMP)
184 
185  #else //Mega128 etc.
186 
187  // Bit# BitName Val.Description
188  // 7 FOC2 0 Not used
189  // 6 WGM20 0 Clear Timer on Compare. DANGER: Bits of WGM2 are numbered in oposite sequence.
190  // 5:4 COM21:0 00 Normal Port Operation
191  // 3 WGM21 1 Clear Timer on Compare. The Atmel designers hate me!!!
192  // 2:0 CS22:0 PRESCALER_VALUE
193  TCCR2 = PRESCALER_VALUE;
194  TCCR2 |= 1<<WGM21;
195 
196  OCR2 = COMPARE_VALUE; // see above
197 
198  TIMSK |= 1<<OCIE2; // enable timer compare a interrupt, invokes ISR(TIMER_INT_VECT_COMP)
199  #endif
200 
201 #endif
202 }
203 
208 #pragma GCC diagnostic push // avoid false positive on gcc 4.9.2
209 #pragma GCC diagnostic ignored "-Wreturn-type"
210 uint32_t HwTimerGetValue(void)
211 {
212  ATOMIC_BLOCK(ATOMIC_RESTORESTATE) // save SREG, cli() and restore SREG
213  {
214  return ulGlobalTimer;
215  }
216 }
217 #pragma GCC diagnostic pop
218 
224 {
225  static uint8_t ucGlobalTick10 = 10;
226  static uint8_t ucGlobalTick100 = 10;
227 
228  // unconditionally increment for use in Stefan's Timer
229  ulGlobalTimer++;
230 
231  // call (user)functions every 1mS
233 
234  // Call (user)functions every 10mS
235  ucGlobalTick10--;
236  if (ucGlobalTick10 == 0)
237  {
238  ucGlobalTick10 = 10;
239 
242 
243  ucGlobalTick100--;
244  if (ucGlobalTick100 == 0)
245  {
246  ucGlobalTick100 = 10;
248  }
249  }
250 }
uint32_t HwTimerGetValue(void)
Get the value of globalTimer variable guarded by disable/enable interrupts.
Definition: SysHwTimer.c:210
static uint32_t ulGlobalTimer
Gets incremented every TIMER_RESOLUTION_MS milliseconds by interrupt.
Definition: SysHwTimer.c:89
void PushButtonInterrupt(void)
Called by timer interrupt every 10ms.
Definition: PushButton.c:292
ISR(TIMER_INT_VECT_COMP)
Interrupt service routine for timer interrupt.
Definition: SysHwTimer.c:223
#define TIMER_INT_VECT_COMP
Definition: SysHwTimer.c:78
Push button debouncing module for Atmel AVR.
void SysTimerUserInterrupt10(void)
This function is called by SysHWTimer every 10ms.
Definition: ClockKernel.c:145
System wide Definitions for FREMO Clock.
void SysTimerUserInterrupt(void)
This function is called by SysHWTimer every 1ms.
Definition: ClockKernel.c:135
Hardware dependent timer module for Atmel AVR.
User function called by timer ISR.
void SysTimerUserInterrupt100(void)
This function is called by SysHWTimer every 100ms.
Definition: ClockKernel.c:155
void HwTimerInit(void)
Initialize the hardware time.
Definition: SysHwTimer.c:115