Skip to content

Commit 26cb7b2

Browse files
committed
Move timers initialization to variant
1 parent 9d96f63 commit 26cb7b2

File tree

5 files changed

+92
-86
lines changed

5 files changed

+92
-86
lines changed

cores/arduino/Arduino.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
117117
#define TIMERB2 4
118118
#define TIMERB3 5
119119

120-
#define PWM_TIMER_PERIOD 0xFF /* For frequency */
121-
#define PWM_TIMER_COMPARE 0x80 /* For duty cycle */
120+
void setup_timers() __attribute__((weak));
122121

123122
#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
124123
#define digitalPinToBitPosition(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_bit_position + pin) : NOT_A_PIN )

cores/arduino/wiring.c

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@
2424

2525
// the prescaler is set so that timerb3 ticks every 64 clock cycles, and the
2626
// the overflow handler is called every 256 ticks.
27-
uint16_t microseconds_per_timerb3_overflow;
27+
volatile uint16_t microseconds_per_timerb3_overflow;
28+
volatile uint16_t microseconds_per_timerb3_tick;
2829

2930
uint32_t F_CPU_CORRECTED = F_CPU;
3031

31-
#define TIME_TRACKING_TIMER_PERIOD 0xFF
32-
#define TIME_TRACKING_TICKS_PER_OVF (TIME_TRACKING_TIMER_PERIOD + 1) /* Timer ticks per overflow of TCB3 */
33-
#define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCB3 */
34-
#define TIME_TRACKING_CYCLES_PER_OVF (TIME_TRACKING_TICKS_PER_OVF * TIME_TRACKING_TIMER_DIVIDER)
35-
3632
// the whole number of milliseconds per timerb3 overflow
3733
uint16_t millis_inc;
3834

@@ -41,7 +37,6 @@ uint16_t fract_inc;
4137
#define FRACT_MAX (1000)
4238

4339
// whole number of microseconds per timerb3 tick
44-
uint16_t microseconds_per_timerb3_tick;
4540

4641
volatile uint32_t timerb3_overflow_count = 0;
4742
volatile uint32_t timerb3_millis = 0;
@@ -329,83 +324,6 @@ void init()
329324
/* Apply calculated value to F_CPU_CORRECTED */
330325
F_CPU_CORRECTED = (uint32_t)cpu_freq;
331326

332-
/***************************** TIMERS FOR PWM *********************************/
333-
334-
335-
336-
/* TYPE A TIMER */
337-
338-
/* PORTMUX setting for TCA -> all outputs [0:2] point to PORTB pins [0:2] */
339-
PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTB_gc;
340-
341-
/* Setup timers for single slope PWM, but do not enable, will do in analogWrite() */
342-
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
343-
344-
/* Period setting, 16 bit register but val resolution is 8 bit */
345-
TCA0.SINGLE.PER = PWM_TIMER_PERIOD;
346-
347-
/* Default duty 50%, will re-assign in analogWrite() */
348-
TCA0.SINGLE.CMP0BUF = PWM_TIMER_COMPARE;
349-
TCA0.SINGLE.CMP1BUF = PWM_TIMER_COMPARE;
350-
TCA0.SINGLE.CMP2BUF = PWM_TIMER_COMPARE;
351-
352-
/* Use DIV64 prescaler (giving 250kHz clock), enable TCA timer */
353-
TCA0.SINGLE.CTRLA = (TCA_SINGLE_CLKSEL_DIV64_gc) | (TCA_SINGLE_ENABLE_bm);
354-
355-
356-
/* TYPE B TIMERS */
357-
358-
/* PORTMUX alternate location needed for TCB0 & 1, TCB2 is default location */
359-
PORTMUX.TCBROUTEA |= (PORTMUX_TCB0_bm | PORTMUX_TCB1_bm);
360-
361-
/* Start with TCB0 */
362-
TCB_t *timer_B = (TCB_t *)&TCB0;
363-
364-
/* Timer B Setup loop for TCB[0:2] */
365-
do{
366-
/* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
367-
timer_B->CTRLB = (TCB_CNTMODE_PWM8_gc);
368-
369-
/* Assign 8-bit period */
370-
timer_B->CCMPL = PWM_TIMER_PERIOD;
371-
372-
/* default duty 50%, set when output enabled */
373-
timer_B->CCMPH = PWM_TIMER_COMPARE;
374-
375-
/* Use TCA clock (250kHz) and enable */
376-
/* (sync update commented out, might try to synchronize later */
377-
timer_B->CTRLA = (TCB_CLKSEL_CLKTCA_gc)
378-
//|(TCB_SYNCUPD_bm)
379-
|(TCB_ENABLE_bm);
380-
381-
/* Increment pointer to next TCB instance */
382-
timer_B++;
383-
384-
/* Stop when pointing to TCB3 */
385-
} while (timer_B < (TCB_t *)&TCB3);
386-
387-
388-
389-
/* Stuff for synchronizing PWM timers */
390-
// /* Restart TCA to sync TCBs */
391-
// /* should not be needed */
392-
// TCA0.SINGLE.CTRLESET = TCA_SINGLE_CMD_RESTART_gc;
393-
// TCA0.SINGLE.CTRLECLR = TCA_SINGLE_CMD_RESTART_gc;
394-
//
395-
// timer_B = (TCB_t *)&TCB0;
396-
//
397-
// /* TCB are sync to TCA, remove setting */
398-
// for (uint8_t digitial_pin_timer = (TIMERB0 - TIMERB0);
399-
// digitial_pin_timer < (TIMERB3 - TIMERB0);
400-
// digitial_pin_timer++)
401-
// {
402-
// /* disable sync with tca */
403-
// timer_B->CTRLA &= ~ (TCB_SYNCUPD_bm);
404-
//
405-
// /* Add offset to register */
406-
// timer_B++;
407-
//
408-
// }
409327

410328
/********************************* ADC ****************************************/
411329

variants/uno2018/pins_arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define Pins_Arduino_h
2525

2626
#include <avr/pgmspace.h>
27+
#include "timers.h"
2728

2829
#define NUM_DIGITAL_PINS 20
2930
#define NUM_ANALOG_INPUTS 6

variants/uno2018/timers.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __TIMERS_H__
2+
#define __TIMERS_H__
3+
4+
#define TIME_TRACKING_TIMER_PERIOD 0xFF
5+
#define TIME_TRACKING_TICKS_PER_OVF (TIME_TRACKING_TIMER_PERIOD + 1) /* Timer ticks per overflow of TCB3 */
6+
#define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCB3 */
7+
#define TIME_TRACKING_CYCLES_PER_OVF (TIME_TRACKING_TICKS_PER_OVF * TIME_TRACKING_TIMER_DIVIDER)
8+
9+
#define PWM_TIMER_PERIOD 0xFF /* For frequency */
10+
#define PWM_TIMER_COMPARE 0x80 /* For duty cycle */
11+
12+
#endif

variants/uno2018/variant.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "pins_arduino.h"
2+
3+
void setup_timers() {
4+
5+
/* TYPE A TIMER */
6+
7+
/* PORTMUX setting for TCA -> all outputs [0:2] point to PORTB pins [0:2] */
8+
PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTB_gc;
9+
10+
/* Setup timers for single slope PWM, but do not enable, will do in analogWrite() */
11+
TCA0.SINGLE.CTRLB = TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
12+
13+
/* Period setting, 16 bit register but val resolution is 8 bit */
14+
TCA0.SINGLE.PER = PWM_TIMER_PERIOD;
15+
16+
/* Default duty 50%, will re-assign in analogWrite() */
17+
TCA0.SINGLE.CMP0BUF = PWM_TIMER_COMPARE;
18+
TCA0.SINGLE.CMP1BUF = PWM_TIMER_COMPARE;
19+
TCA0.SINGLE.CMP2BUF = PWM_TIMER_COMPARE;
20+
21+
/* Use DIV64 prescaler (giving 250kHz clock), enable TCA timer */
22+
TCA0.SINGLE.CTRLA = (TCA_SINGLE_CLKSEL_DIV64_gc) | (TCA_SINGLE_ENABLE_bm);
23+
24+
25+
/* TYPE B TIMERS */
26+
27+
/* PORTMUX alternate location needed for TCB0 & 1, TCB2 is default location */
28+
PORTMUX.TCBROUTEA |= (PORTMUX_TCB0_bm | PORTMUX_TCB1_bm);
29+
30+
/* Start with TCB0 */
31+
TCB_t *timer_B = (TCB_t *)&TCB0;
32+
33+
/* Timer B Setup loop for TCB[0:2] */
34+
do{
35+
/* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
36+
timer_B->CTRLB = (TCB_CNTMODE_PWM8_gc);
37+
38+
/* Assign 8-bit period */
39+
timer_B->CCMPL = PWM_TIMER_PERIOD;
40+
41+
/* default duty 50%, set when output enabled */
42+
timer_B->CCMPH = PWM_TIMER_COMPARE;
43+
44+
/* Use TCA clock (250kHz) and enable */
45+
/* (sync update commented out, might try to synchronize later */
46+
timer_B->CTRLA = (TCB_CLKSEL_CLKTCA_gc)
47+
//|(TCB_SYNCUPD_bm)
48+
|(TCB_ENABLE_bm);
49+
50+
/* Increment pointer to next TCB instance */
51+
timer_B++;
52+
53+
/* Stop when pointing to TCB3 */
54+
} while (timer_B < (TCB_t *)&TCB3);
55+
56+
/* Stuff for synchronizing PWM timers */
57+
// /* Restart TCA to sync TCBs */
58+
// /* should not be needed */
59+
// TCA0.SINGLE.CTRLESET = TCA_SINGLE_CMD_RESTART_gc;
60+
// TCA0.SINGLE.CTRLECLR = TCA_SINGLE_CMD_RESTART_gc;
61+
//
62+
// timer_B = (TCB_t *)&TCB0;
63+
//
64+
// /* TCB are sync to TCA, remove setting */
65+
// for (uint8_t digitial_pin_timer = (TIMERB0 - TIMERB0);
66+
// digitial_pin_timer < (TIMERB3 - TIMERB0);
67+
// digitial_pin_timer++)
68+
// {
69+
// /* disable sync with tca */
70+
// timer_B->CTRLA &= ~ (TCB_SYNCUPD_bm);
71+
//
72+
// /* Add offset to register */
73+
// timer_B++;
74+
//
75+
// }
76+
}

0 commit comments

Comments
 (0)