1
#ifndef HWDEF_EMISAR_D4SV2_H
2
#define HWDEF_EMISAR_D4SV2_H
4
/* Emisar D4Sv2 driver layout (attiny1634)
5
* (same layout as D4v2, except it's a FET+3+1 instead of FET+1)
7
* Pin / Name / Function
8
* 1 PA6 FET PWM (PWM1B)
9
* 2 PA5 red aux LED (PWM0B)
22
* 15 PC0 3x7135 PWM (PWM0A)
23
* 16 PB3 1x7135 PWM (PWM1A)
28
* ADC12 thermal sensor
37
#define SWITCH_PIN PA2 // pin 5
38
#define SWITCH_PCINT PCINT2 // pin 5 pin change interrupt
39
#define SWITCH_PCIE PCIE0 // PCIE0 is for PCINT[7:0]
40
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
41
#define SWITCH_PORT PINA // PINA or PINB or PINC
43
#define PWM_CHANNELS 3
45
#define PWM1_PIN PB3 // pin 16, 1x7135 PWM
46
#define PWM1_LVL OCR1A // OCR1A is the output compare register for PB3
48
#define PWM2_PIN PC0 // pin 15, 3x7135 PWM
49
#define PWM2_LVL OCR0A // OCR0A is the output compare register for PC0
51
#define PWM3_PIN PA6 // pin 1, FET PWM
52
#define PWM3_LVL OCR1B // OCR1B is the output compare register for PB1
55
#define ADC_PRSCL 0x07 // clk/128
57
// average drop across diode on this hardware
58
#ifndef VOLTAGE_FUDGE_FACTOR
59
#define VOLTAGE_FUDGE_FACTOR 4 // add 0.20V (measured 0.22V)
62
// this light has aux LEDs under the optic
63
#define AUXLED_R_PIN PA5 // pin 2
64
#define AUXLED_G_PIN PA4 // pin 3
65
#define AUXLED_B_PIN PA3 // pin 4
66
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
67
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
68
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
70
// with so many pins, doing this all with #ifdefs gets awkward...
71
// ... so just hardcode it in each hwdef file instead
72
inline void hwdef_setup() {
73
// enable output ports
75
DDRA = (1 << PWM3_PIN)
81
DDRB = (1 << PWM1_PIN);
83
DDRC = (1 << PWM2_PIN);
86
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
87
// pre-scale for timer: N = 1
88
// WGM1[3:0]: 0,0,0,1: PWM, Phase Correct, 8-bit (DS table 12-5)
89
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
90
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
91
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
92
TCCR1A = (0<<WGM11) | (1<<WGM10) // 8-bit (TOP=0xFF) (DS table 12-5)
93
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
94
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
96
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
97
| (0<<WGM13) | (0<<WGM12) // phase-correct PWM (DS table 12-5)
100
// WGM0[2:0]: 0,0,1: PWM, Phase Correct (DS table 11-8)
101
// CS0[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 11-9)
102
// COM0A[1:0]: 1,0: PWM OC0A in the normal direction (DS table 11-4)
103
// COM0B[1:0]: 0,0: OC0B disabled (DS table 11-7)
104
// TCCR0A: COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00
105
TCCR0A = (0<<WGM01) | (1<<WGM00) // PWM, Phase Correct, TOP=0xFF (DS table 11-5)
106
| (1<<COM1A1) | (0<<COM1A0) // PWM 0A in normal direction (DS table 11-4)
107
| (0<<COM1B1) | (0<<COM1B0) // PWM 0B disabled (DS table 11-7)
109
// TCCR0B: FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00
110
TCCR0B = (0<<CS02) | (0<<CS01) | (1<<CS00) // clk/1 (no prescaling) (DS table 11-9)
111
| (0<<WGM02) // PWM, Phase Correct, TOP=0xFF (DS table 11-8)
115
//PORTA = (1 << SWITCH_PIN); // TODO: configure PORTA / PORTB / PORTC?
116
PUEA = (1 << SWITCH_PIN); // pull-up for e-switch
117
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
120
#define LAYOUT_DEFINED