28
30
* ADC12 thermal sensor
34
33
#define ATTINY 1634
35
34
#include <avr/io.h>
36
#define HWDEF_C_FILE hwdef-emisar-d4sv2.c
38
// allow using aux LEDs as extra channel modes
39
#include "chan-rgbaux.h"
42
// * 0. FET+3+1 stacked
44
#define NUM_CHANNEL_MODES (1 + NUM_RGB_AUX_CHANNEL_MODES)
50
#define DEFAULT_CHANNEL_MODE CM_MAIN
52
// right-most bit first, modes are in fedcba9876543210 order
53
#define CHANNEL_MODES_ENABLED 0b0000000000000001
55
//#define USE_CHANNEL_MODE_ARGS
56
//#define CHANNEL_MODE_ARGS 0,0,0,0,0,0,0,0
59
#define PWM_CHANNELS 3 // old, remove this
61
#define PWM_BITS 16 // dynamic 16-bit, but never goes over 255
62
#define PWM_GET PWM_GET8
63
#define PWM_DATATYPE uint16_t // is used for PWM_TOPS (which goes way over 255)
64
#define PWM_DATATYPE2 uint16_t // only needs 32-bit if ramp values go over 255
65
#define PWM1_DATATYPE uint8_t // 1x7135 ramp (16-bit)
66
#define PWM2_DATATYPE uint8_t // 3x7135 ramp (8-bit)
67
#define PWM3_DATATYPE uint8_t // DD FET ramp (16-bit)
69
// PWM parameters of FET and 1x7135 channels are tied together because they share a counter
70
#define PWM_TOP ICR1 // holds the TOP value for for variable-resolution PWM
71
#define PWM_TOP_INIT 255 // highest value used in top half of ramp
72
#define PWM_CNT TCNT1 // for dynamic PWM, reset phase
75
#define CH1_PIN PB3 // pin 16, 1x7135 PWM
76
#define CH1_PWM OCR1A // OCR1A is the output compare register for PB3
79
#define CH2_PIN PC0 // pin 15, 3x7135 PWM
80
#define CH2_PWM OCR0A // OCR0A is the output compare register for PC0
83
#define CH3_PIN PA6 // pin 1, DD FET PWM
84
#define CH3_PWM OCR1B // OCR1B is the output compare register for PB1
37
87
#define SWITCH_PIN PA2 // pin 5
38
88
#define SWITCH_PCINT PCINT2 // pin 5 pin change interrupt
39
89
#define SWITCH_PCIE PCIE0 // PCIE0 is for PCINT[7:0]
40
90
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
41
91
#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
92
#define SWITCH_PUE PUEA // pullup group A
93
#define PCINT_vect PCINT0_vect // ISR for PCINT[7:0]
55
96
#define ADC_PRSCL 0x07 // clk/128
62
103
// 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
104
#define AUXLED_R_PIN PA5 // pin 2
105
#define AUXLED_G_PIN PA4 // pin 3
106
#define AUXLED_B_PIN PA3 // pin 4
107
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
108
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
109
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
111
#define BUTTON_LED_PIN PA1 // pin 6
112
#define BUTTON_LED_PORT PORTA // for all "PA" pins
113
#define BUTTON_LED_DDR DDRA // for all "PA" pins
114
#define BUTTON_LED_PUE PUEA // for all "PA" pins
116
// this light has three aux LED channels: R, G, B
117
#define USE_AUX_RGB_LEDS
118
// it also has an independent LED in the button
119
#define USE_BUTTON_LED
120
// the aux LEDs are front-facing, so turn them off while main LEDs are on
121
#ifdef USE_INDICATOR_LED_WHILE_RAMPING
122
#undef USE_INDICATOR_LED_WHILE_RAMPING
72
126
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
127
// enable output ports
129
DDRC = (1 << CH2_PIN);
131
DDRB = (1 << CH1_PIN);
133
DDRA = (1 << CH3_PIN)
134
| (1 << AUXLED_R_PIN)
135
| (1 << AUXLED_G_PIN)
136
| (1 << AUXLED_B_PIN)
137
| (1 << BUTTON_LED_PIN)
141
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
142
// pre-scale for timer: N = 1
143
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
144
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
145
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
146
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
147
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
148
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
149
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
151
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
152
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
155
// WGM0[2:0]: 0,0,1: PWM, Phase Correct (DS table 11-8)
156
// CS0[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 11-9)
157
// COM0A[1:0]: 1,0: PWM OC0A in the normal direction (DS table 11-4)
158
// COM0B[1:0]: 0,0: OC0B disabled (DS table 11-7)
159
// TCCR0A: COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00
160
TCCR0A = (0<<WGM01) | (1<<WGM00) // PWM, Phase Correct, TOP=0xFF (DS table 11-5)
161
| (1<<COM1A1) | (0<<COM1A0) // PWM 0A in normal direction (DS table 11-4)
162
| (0<<COM1B1) | (0<<COM1B0) // PWM 0B disabled (DS table 11-7)
164
// TCCR0B: FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00
165
TCCR0B = (0<<CS02) | (0<<CS01) | (1<<CS00) // clk/1 (no prescaling) (DS table 11-9)
166
| (0<<WGM02) // PWM, Phase Correct, TOP=0xFF (DS table 11-8)
169
// set PWM resolution
170
PWM_TOP = PWM_TOP_INIT;
173
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch
174
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
120
178
#define LAYOUT_DEFINED