30
32
* Main LED power uses one pin to turn the Opamp on/off,
31
33
* and one pin to control Opamp power level.
32
* Main brightness control uses the power level pin, with 4 kHz 10-bit PWM.
34
* Main brightness control uses the power level pin, with dynamic PWM.
33
35
* The on/off pin is only used to turn the main LED on and off,
34
36
* not to change brightness.
35
37
* Also has a direct-drive FET for turbo.
41
40
#define ATTINY 1634
42
41
#include <avr/io.h>
44
#define PWM_CHANNELS 2
45
#define PWM_BITS 10 // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
43
#define HWDEF_C_FILE hwdef-noctigon-kr4.c
45
// allow using aux LEDs as extra channel modes
46
#include "chan-rgbaux.h"
49
// * 0. linear + DD FET stacked
51
#define NUM_CHANNEL_MODES (1 + NUM_RGB_AUX_CHANNEL_MODES)
57
#define DEFAULT_CHANNEL_MODE CM_MAIN
59
// right-most bit first, modes are in fedcba9876543210 order
60
#define CHANNEL_MODES_ENABLED 0b0000000000000001
62
//#define USE_CHANNEL_MODE_ARGS
63
//#define CHANNEL_MODE_ARGS 0,0,0,0,0,0,0,0
66
#define PWM_CHANNELS 2 // old, remove this
68
#define PWM_BITS 16 // dynamic 16-bit, but never goes over 255
69
#define PWM_GET PWM_GET8
70
#define PWM_DATATYPE uint16_t // is used for PWM_TOPS (which goes way over 255)
71
#define PWM_DATATYPE2 uint16_t // only needs 32-bit if ramp values go over 255
72
#define PWM1_DATATYPE uint8_t // linear ramp
73
#define PWM2_DATATYPE uint8_t // DD FET ramp
75
// PWM parameters of both channels are tied together because they share a counter
76
#define PWM_TOP ICR1 // holds the TOP value for variable-resolution PWM
77
#define PWM_TOP_INIT 255 // highest value used in top half of ramp
78
#define PWM_CNT TCNT1 // for dynamic PWM, reset phase
81
#define CH1_PIN PB3 // pin 16, Opamp reference
82
#define CH1_PWM OCR1A // OCR1A is the output compare register for PB3
83
#define CH1_ENABLE_PIN PB0 // pin 19, Opamp power
84
#define CH1_ENABLE_PORT PORTB // control port for PB0
87
#define CH2_PIN PA6 // pin 1, DD FET PWM
88
#define CH2_PWM OCR1B // OCR1B is the output compare register for PA6
48
91
#define SWITCH_PIN PB2 // pin 17
49
92
#define SWITCH_PCINT PCINT10 // pin 17 pin change interrupt
50
93
#define SWITCH_PCIE PCIE1 // PCIE1 is for PCINT[11:8]
51
94
#define SWITCH_PCMSK PCMSK1 // PCMSK1 is for PCINT[11:8]
52
95
#define SWITCH_PORT PINB // PINA or PINB or PINC
96
#define SWITCH_PUE PUEB // pullup group B
53
97
#define PCINT_vect PCINT1_vect // ISR for PCINT[11:8]
55
#define PWM1_PIN PB3 // pin 16, Opamp reference
56
#define PWM1_LVL OCR1A // OCR1A is the output compare register for PB3
58
#define PWM2_PIN PA6 // pin 1, DD FET PWM
59
#define PWM2_LVL OCR1B // OCR1B is the output compare register for PA6
61
#define LED_ENABLE_PIN PB0 // pin 19, Opamp power
62
#define LED_ENABLE_PORT PORTB // control port for PB0
65
99
#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
66
100
#define VOLTAGE_PIN PB1 // Pin 18 / PB1 / ADC6
67
101
// pin to ADC mappings are in DS table 19-4
103
137
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
104
138
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
106
// with so many pins, doing this all with #ifdefs gets awkward...
107
// ... so just hardcode it in each hwdef file instead
108
140
inline void hwdef_setup() {
109
// enable output ports
110
// Opamp level and Opamp on/off
111
DDRB = (1 << PWM1_PIN)
112
| (1 << LED_ENABLE_PIN);
113
// DD FET PWM, aux R/G/B
114
DDRA = (1 << PWM2_PIN)
115
| (1 << AUXLED_R_PIN)
116
| (1 << AUXLED_G_PIN)
117
| (1 << AUXLED_B_PIN)
121
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
122
// pre-scale for timer: N = 1
123
// WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
124
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
125
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
126
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
127
TCCR1A = (1<<WGM11) | (1<<WGM10) // 10-bit (TOP=0x03FF) (DS table 12-5)
128
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
129
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
131
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
132
| (0<<WGM13) | (0<<WGM12) // phase-correct PWM (DS table 12-5)
136
//PORTB = (1 << SWITCH_PIN); // TODO: configure PORTA / PORTB / PORTC?
137
PUEB = (1 << SWITCH_PIN); // pull-up for e-switch
138
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
141
// enable output ports
142
// Opamp level and Opamp on/off
143
DDRB = (1 << CH1_PIN)
144
| (1 << CH1_ENABLE_PIN);
145
// DD FET PWM, aux R/G/B
146
DDRA = (1 << CH2_PIN)
147
| (1 << AUXLED_R_PIN)
148
| (1 << AUXLED_G_PIN)
149
| (1 << AUXLED_B_PIN)
153
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
154
// pre-scale for timer: N = 1
155
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
156
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
157
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
158
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
159
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
160
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
161
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
163
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
164
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
167
// set PWM resolution
168
PWM_TOP = PWM_TOP_INIT;
171
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch
172
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
141
176
#define LAYOUT_DEFINED