46
46
#define USE_CALC_2CH_BLEND
49
#define PWM_CHANNELS 1 // old, remove this
53
#define PWM_GET PWM_GET8
54
#define PWM_DATATYPE uint8_t
55
#define PWM_DATATYPE2 uint16_t // only needs 32-bit if ramp values go over 255
56
#define PWM1_DATATYPE uint8_t //
49
#define PWM_CHANNELS 1 // old, remove this
51
#define PWM_BITS 16 // 0 to 32640 (0 to 255 PWM + 0 to 127 DSM) at constant kHz
52
#define PWM_GET PWM_GET16
53
#define PWM_DATATYPE uint16_t
54
#define PWM_DATATYPE2 uint32_t // only needs 32-bit if ramp values go over 255
55
#define PWM1_DATATYPE uint16_t // 15-bit PWM+DSM ramp
58
57
// PWM parameters of both channels are tied together because they share a counter
59
#define PWM_TOP_INIT 255 // highest value used in the top half of the ramp
60
#define PWM_TOP TCA0.SINGLE.PERBUF // holds the TOP value for for variable-resolution PWM
61
#define PWM_CNT TCA0.SINGLE.CNT // for resetting phase after each TOP adjustment
62
// TODO: implement DSM
59
#define PWM_TOP TCA0.SINGLE.PERBUF // holds the TOP value for for variable-resolution PWM
60
#define PWM_TOP_INIT 255
61
#define PWM_CNT TCA0.SINGLE.CNT // for resetting phase after each TOP adjustment
62
// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM)
63
63
#define DSM_TOP (255<<7) // 15-bit resolution leaves 1 bit for carry
65
// timer interrupt for DSM
66
#define DSM_vect TCA0_OVF_vect
67
#define DSM_INTCTRL TCA0.SINGLE.INTCTRL
68
#define DSM_OVF_bm TCA_SINGLE_OVF_bm
72
uint8_t ch1_pwm, ch1_dsm;
66
73
#define CH1_PIN PB1
67
#define CH1_PWM TCA0.SINGLE.CMP1 // CMP1 is the output compare register for PB1
74
#define CH1_PWM TCA0.SINGLE.CMP1 // CMP1 is the output compare register for PB1
71
#define CH2_PWM TCA0.SINGLE.CMP0 // CMP0 is the output compare register for PB0
78
uint8_t ch2_pwm, ch2_dsm;
80
#define CH2_PWM TCA0.SINGLE.CMP0 // CMP0 is the output compare register for PB0
74
83
#define AUXLED_PIN PIN5_bp
75
84
#define AUXLED_PORT PORTB
78
#define SWITCH_PIN PIN5_bp
79
#define SWITCH_PORT VPORTA.IN
80
#define SWITCH_ISC_REG PORTA.PIN2CTRL
81
#define SWITCH_VECT PORTA_PORT_vect
82
#define SWITCH_INTFLG VPORTA.INTFLAGS
87
#define SWITCH_PIN PIN5_bp
88
#define SWITCH_PORT VPORTA.IN
89
#define SWITCH_ISC_REG PORTA.PIN2CTRL
90
#define SWITCH_VECT PORTA_PORT_vect
91
#define SWITCH_INTFLG VPORTA.INTFLAGS
84
93
// average drop across diode on this hardware
85
94
#ifndef VOLTAGE_FUDGE_FACTOR
90
99
inline void hwdef_setup() {
92
// set up the system clock to run at 5 MHz instead of the default 3.33 MHz
93
_PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm );
101
// set up the system clock to run at 10 MHz instead of the default 3.33 MHz
102
_PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
103
CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
95
105
//VPORTA.DIR = ...;
97
107
VPORTB.DIR = PIN0_bm // cool white
98
108
| PIN1_bm // warm white
99
109
| PIN5_bm; // aux LED
130
140
// TODO: add references to MCU documentation
131
141
TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
132
142
| TCA_SINGLE_CMP1EN_bm
133
| TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
134
PWM_TOP = PWM_TOP_INIT;
143
| TCA_SINGLE_WGMODE_DSBOTTOM_gc;
135
144
TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
136
145
| TCA_SINGLE_ENABLE_bm;
147
PWM_TOP = PWM_TOP_INIT;
149
// set up interrupt for delta-sigma modulation
150
// (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
151
//DSM_INTCTRL |= DSM_OVF_bm; // interrupt once for each timer cycle