22
22
23
23
#include "wiring_private.h"
24
24
25
- // the prescaler is set so that timerb3 ticks every 64 clock cycles, and the
25
+ // the prescaler is set so that timerb0 ticks every 64 clock cycles, and the
26
26
// the overflow handler is called every 256 ticks.
27
- volatile uint16_t microseconds_per_timerb3_overflow ;
28
- volatile uint16_t microseconds_per_timerb3_tick ;
27
+ volatile uint16_t microseconds_per_timerb0_overflow ;
28
+ volatile uint16_t microseconds_per_timerb0_tick ;
29
29
30
30
uint32_t F_CPU_CORRECTED = F_CPU ;
31
31
32
- // the whole number of milliseconds per timerb3 overflow
32
+ // the whole number of milliseconds per timerb0 overflow
33
33
uint16_t millis_inc ;
34
34
35
- // the fractional number of milliseconds per timerb3 overflow
35
+ // the fractional number of milliseconds per timerb0 overflow
36
36
uint16_t fract_inc ;
37
37
#define FRACT_MAX (1000)
38
38
39
- // whole number of microseconds per timerb3 tick
39
+ // whole number of microseconds per timerb0 tick
40
40
41
- volatile uint32_t timerb3_overflow_count = 0 ;
42
- volatile uint32_t timerb3_millis = 0 ;
43
- static uint16_t timerb3_fract = 0 ;
41
+ volatile uint32_t timerb0_overflow_count = 0 ;
42
+ volatile uint32_t timerb0_millis = 0 ;
43
+ static uint16_t timerb0_fract = 0 ;
44
44
45
45
inline uint16_t clockCyclesPerMicrosecondComp (uint32_t clk ){
46
46
return ( (clk ) / 1000000L );
@@ -58,12 +58,12 @@ inline unsigned long microsecondsToClockCycles(unsigned long microseconds){
58
58
return ( microseconds * clockCyclesPerMicrosecond () );
59
59
}
60
60
61
- ISR (TCB3_INT_vect )
61
+ ISR (TCB0_INT_vect )
62
62
{
63
63
// copy these to local variables so they can be stored in registers
64
64
// (volatile variables must be read from memory on every access)
65
- uint32_t m = timerb3_millis ;
66
- uint16_t f = timerb3_fract ;
65
+ uint32_t m = timerb0_millis ;
66
+ uint16_t f = timerb0_fract ;
67
67
68
68
m += millis_inc ;
69
69
f += fract_inc ;
@@ -73,12 +73,12 @@ ISR(TCB3_INT_vect)
73
73
m += 1 ;
74
74
}
75
75
76
- timerb3_fract = f ;
77
- timerb3_millis = m ;
78
- timerb3_overflow_count ++ ;
76
+ timerb0_fract = f ;
77
+ timerb0_millis = m ;
78
+ timerb0_overflow_count ++ ;
79
79
80
80
/* Clear flag */
81
- TCB3 .INTFLAGS = TCB_CAPT_bm ;
81
+ TCB0 .INTFLAGS = TCB_CAPT_bm ;
82
82
}
83
83
84
84
unsigned long millis ()
@@ -89,7 +89,7 @@ unsigned long millis()
89
89
// inconsistent value (e.g. in the middle of a write to timer0_millis)
90
90
uint8_t status = SREG ;
91
91
cli ();
92
- m = timerb3_millis ;
92
+ m = timerb0_millis ;
93
93
94
94
SREG = status ;
95
95
@@ -105,22 +105,22 @@ unsigned long micros() {
105
105
cli ();
106
106
107
107
/* Get current number of overflows and timer count */
108
- overflows = timerb3_overflow_count ;
109
- ticks = TCB3 .CNTL ;
108
+ overflows = timerb0_overflow_count ;
109
+ ticks = TCB0 .CNTL ;
110
110
111
111
/* If the timer overflow flag is raised, we just missed it,
112
112
increment to account for it, & read new ticks */
113
- if (TCB3 .INTFLAGS & TCB_CAPT_bm ){
113
+ if (TCB0 .INTFLAGS & TCB_CAPT_bm ){
114
114
overflows ++ ;
115
- ticks = TCB3 .CNTL ;
115
+ ticks = TCB0 .CNTL ;
116
116
}
117
117
118
118
/* Restore state */
119
119
SREG = status ;
120
120
121
121
/* Return microseconds of up time (resets every ~70mins) */
122
- microseconds = ((overflows * microseconds_per_timerb3_overflow )
123
- + (ticks * microseconds_per_timerb3_tick ));
122
+ microseconds = ((overflows * microseconds_per_timerb0_overflow )
123
+ + (ticks * microseconds_per_timerb0_tick ));
124
124
return microseconds ;
125
125
}
126
126
@@ -355,31 +355,31 @@ void init()
355
355
356
356
setup_timers ();
357
357
358
- /********************* TCB3 for system time tracking **************************/
358
+ /********************* TCB0 for system time tracking **************************/
359
359
360
360
/* Calculate relevant time tracking values */
361
- microseconds_per_timerb3_overflow = clockCyclesToMicroseconds (TIME_TRACKING_CYCLES_PER_OVF );
362
- microseconds_per_timerb3_tick = microseconds_per_timerb3_overflow /TIME_TRACKING_TIMER_PERIOD ;
361
+ microseconds_per_timerb0_overflow = clockCyclesToMicroseconds (TIME_TRACKING_CYCLES_PER_OVF );
362
+ microseconds_per_timerb0_tick = microseconds_per_timerb0_overflow /TIME_TRACKING_TIMER_PERIOD ;
363
363
364
- millis_inc = microseconds_per_timerb3_overflow / 1000 ;
365
- fract_inc = ((microseconds_per_timerb3_overflow % 1000 ));
364
+ millis_inc = microseconds_per_timerb0_overflow / 1000 ;
365
+ fract_inc = ((microseconds_per_timerb0_overflow % 1000 ));
366
366
367
367
/* Default Periodic Interrupt Mode */
368
368
/* TOP value for overflow every 1024 clock cycles */
369
- TCB3 .CCMP = TIME_TRACKING_TIMER_PERIOD ;
369
+ TCB0 .CCMP = TIME_TRACKING_TIMER_PERIOD ;
370
370
371
- /* Enable TCB3 interrupt */
372
- TCB3 .INTCTRL |= TCB_CAPT_bm ;
371
+ /* Enable TCB0 interrupt */
372
+ TCB0 .INTCTRL |= TCB_CAPT_bm ;
373
373
374
374
/* Clock selection -> same as TCA (F_CPU/64 -- 250kHz) */
375
- TCB3 .CTRLA = TCB_CLKSEL_CLKTCA_gc ;
375
+ TCB0 .CTRLA = TCB_CLKSEL_CLKTCA_gc ;
376
376
377
377
/* Enable & start */
378
- TCB3 .CTRLA |= TCB_ENABLE_bm ; /* Keep this last before enabling interrupts to ensure tracking as accurate as possible */
378
+ TCB0 .CTRLA |= TCB_ENABLE_bm ; /* Keep this last before enabling interrupts to ensure tracking as accurate as possible */
379
379
380
380
/*************************** ENABLE GLOBAL INTERRUPTS *************************/
381
381
382
382
sei ();
383
383
}
384
384
385
- void setup_timers (void ) __attribute__((weak ));
385
+ void setup_timers (void ) __attribute__((weak ));
0 commit comments