30
30
* Main LED power uses one pin to turn the Opamp on/off,
31
31
* and one pin to control Opamp power level.
32
* All brightness control uses the power level pin, with 4 kHz 10-bit PWM.
32
* Regulated brightness control uses the power level pin, with PWM+DSM.
33
33
* The on/off pin is only used to turn the main LED on and off,
34
34
* not to change brightness.
40
37
#define ATTINY 1634
41
38
#include <avr/io.h>
43
#define PWM_CHANNELS 1
44
#define PWM_BITS 10 // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
47
#define SWITCH_PIN PA7 // pin 20
48
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
49
#define SWITCH_PCIE PCIE0 // PCIE0 is for PCINT[7:0]
50
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
51
#define SWITCH_PORT PINA // PINA or PINB or PINC
53
#define PWM1_PIN PB3 // pin 16, Opamp reference
54
#define PWM1_LVL OCR1A // OCR1A is the output compare register for PB3
56
#define LED_ENABLE_PIN PB0 // pin 19, Opamp power
57
#define LED_ENABLE_PORT PORTB // control port for PB0
59
#define LED_ENABLE2_PIN PC0 // pin 15, boost PMIC enable
60
#define LED_ENABLE2_PORT PORTC // control port for PC0
40
#define HWDEF_C_FILE hwdef-noctigon-dm11-boost.c
42
// allow using aux LEDs as extra channel modes
43
#include "chan-rgbaux.h"
48
#define NUM_CHANNEL_MODES (1 + NUM_RGB_AUX_CHANNEL_MODES)
54
#define DEFAULT_CHANNEL_MODE CM_MAIN
56
// right-most bit first, modes are in fedcba9876543210 order
57
#define CHANNEL_MODES_ENABLED 0b0000000000000001
59
//#define USE_CHANNEL_MODE_ARGS
60
//#define CHANNEL_MODE_ARGS 0,0,0,0,0,0,0,0
63
#define PWM_CHANNELS 1 // old, remove this
65
#define PWM_BITS 16 // 0 to 32640 (0 to 255 PWM + 0 to 127 DSM) at constant kHz
66
#define PWM_GET PWM_GET16
67
#define PWM_DATATYPE uint16_t
68
#define PWM_DATATYPE2 uint32_t // only needs 32-bit if ramp values go over 255
69
#define PWM1_DATATYPE uint16_t // 15-bit PWM+DSM ramp
71
#define PWM_TOP ICR1 // holds the TOP value for variable-resolution PWM
72
#define PWM_TOP_INIT 255
73
#define PWM_CNT TCNT1 // for checking / resetting phase
74
// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM)
75
#define DSM_TOP (255<<7) // 15-bit resolution leaves 1 bit for carry
77
// timer interrupt for DSM
78
#define DSM_vect TIMER1_OVF_vect
79
#define DSM_INTCTRL TIMSK
80
#define DSM_OVF_bm (1<<TOIE1)
82
#define DELAY_FACTOR 90 // less time in delay() because more time spent in interrupts
86
uint8_t ch1_pwm, ch1_dsm;
87
#define CH1_PIN PB3 // pin 16, Opamp reference
88
#define CH1_PWM OCR1A // OCR1A is the output compare register for PB3
90
#define CH1_ENABLE_PIN PB0 // pin 19, Opamp power
91
#define CH1_ENABLE_PORT PORTB // control port for PB0
93
#define CH1_ENABLE_PIN2 PC0 // pin 15, boost PMIC enable
94
#define CH1_ENABLE_PORT2 PORTC // control port for PC0
97
#define SWITCH_PIN PA7 // pin 20
98
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
99
#define SWITCH_PCIE PCIE0 // PCIE0 is for PCINT[7:0]
100
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
101
#define SWITCH_PORT PINA // PINA or PINB or PINC
102
#define SWITCH_PUE PUEA // pullup group A
103
#define PCINT_vect PCINT0_vect // ISR for PCINT[7:0]
63
105
#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
64
106
#define VOLTAGE_PIN PB1 // Pin 18 / PB1 / ADC6
98
140
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
99
141
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
101
// with so many pins, doing this all with #ifdefs gets awkward...
102
// ... so just hardcode it in each hwdef file instead
103
143
inline void hwdef_setup() {
104
// enable output ports
106
DDRC = (1 << LED_ENABLE2_PIN);
107
// Opamp level and Opamp on/off
108
DDRB = (1 << PWM1_PIN)
109
| (1 << LED_ENABLE_PIN);
111
DDRA = (1 << AUXLED_R_PIN)
112
| (1 << AUXLED_G_PIN)
113
| (1 << AUXLED_B_PIN)
117
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
118
// pre-scale for timer: N = 1
119
// WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
120
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
121
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
122
// COM1B[1:0]: 0,0: PWM OC1B disabled (DS table 12-4)
123
TCCR1A = (1<<WGM11) | (1<<WGM10) // 10-bit (TOP=0x03FF) (DS table 12-5)
124
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
125
| (0<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
127
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
128
| (0<<WGM13) | (0<<WGM12) // phase-correct PWM (DS table 12-5)
132
PUEA = (1 << SWITCH_PIN); // pull-up for e-switch
133
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
144
// enable output ports
146
DDRC = (1 << CH1_ENABLE_PIN2);
147
// Opamp level and Opamp on/off
148
DDRB = (1 << CH1_PIN)
149
| (1 << CH1_ENABLE_PIN);
151
DDRA = (1 << AUXLED_R_PIN)
152
| (1 << AUXLED_G_PIN)
153
| (1 << AUXLED_B_PIN)
157
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
158
// pre-scale for timer: N = 1
159
// PWM for both channels
160
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
161
// WGM1[3:0]: 1,1,1,0: PWM, Fast, adjustable (DS table 12-5)
162
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
163
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
164
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
165
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
166
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
167
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
169
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
170
//| (1<<WGM13) | (1<<WGM12) // fast adjustable PWM (DS table 12-5)
171
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
174
// set PWM resolution
175
PWM_TOP = PWM_TOP_INIT;
177
// set up interrupt for delta-sigma modulation
178
// (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
179
//DSM_INTCTRL |= DSM_OVF_bm; // interrupt once for each timer cycle
182
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch
183
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
136
187
#define LAYOUT_DEFINED