1
// Emisar 2-channel generic w/ tint ramping + DD FET
2
// Copyright (C) 2021-2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
7
* Pin / Name / Function
8
* 1 PA6 ch2 LED PWM (linear) (PWM1B)
9
* 2 PA5 R: red aux LED (PWM0B)
10
* 3 PA4 G: green aux LED
11
* 4 PA3 B: blue aux LED
13
* 6 PA1 Opamp 2 enable (channel 2 LEDs)
14
* 7 PA0 Opamp 1 enable (channel 1 LEDs)
22
* 15 PC0 ch1 LED PWM (FET) (PWM0A, 8-bit)
23
* 16 PB3 ch1 LED PWM (linear) (PWM1A)
25
* 18 PB1 MOSI / battery voltage (ADC6)
27
* 20 PA7 e-switch (PCINT7)
28
* ADC12 thermal sensor
30
* Both sets of LEDs use one pin to turn the Opamp on/off,
31
* and one pin to control the Opamp power level.
32
* The first channel also has a direct-drive FET for turbo.
38
#define HWDEF_C_FILE hwdef-emisar-2ch-fet.c
40
// allow using aux LEDs as extra channel modes
41
#include "chan-rgbaux.h"
44
// * 0. channel 1 only (linear + DD FET)
45
// * 1. channel 2 only (linear)
46
// * 2. both channels, tied together, max "200%" power + DD FET at top of ramp
47
// * 3. both channels, manual blend, max "100%" power + "200%" and DD FET at top of ramp
48
// * 4. both channels, auto blend, reversible (linear only)
49
#define NUM_CHANNEL_MODES (5 + NUM_RGB_AUX_CHANNEL_MODES)
50
enum channel_modes_e {
59
// right-most bit first, modes are in fedcba9876543210 order
60
#define CHANNEL_MODES_ENABLED 0b0000000000011111
61
#define USE_CHANNEL_MODE_ARGS
62
// _, _, _, 128=middle CCT, 0=warm-to-cool
63
#define CHANNEL_MODE_ARGS 0,0,0,128,0,RGB_AUX_CM_ARGS
65
// can use some of the common handlers
66
#define USE_CALC_2CH_BLEND
69
#define PWM_CHANNELS 3 // old, remove this
71
#define PWM_BITS 16 // 0 to 16383 at variable Hz, not 0 to 255 at 16 kHz
72
#define PWM_GET PWM_GET8
73
#define PWM_DATATYPE uint16_t
74
#define PWM_DATATYPE2 uint16_t // only needs 32-bit if ramp values go over 255
75
#define PWM1_DATATYPE uint8_t // linear part of linear+FET ramp
76
#define PWM2_DATATYPE uint8_t // DD FET part of linear+FET ramp
77
#define PWM3_DATATYPE uint16_t // linear+FET ramp tops
78
#define PWM4_DATATYPE uint8_t // linear-only ramp
79
#define PWM5_DATATYPE uint16_t // linear-only ramp tops
81
// PWM parameters of both channels are tied together because they share a counter
82
#define PWM_TOP ICR1 // holds the TOP value for for variable-resolution PWM
83
#define PWM_TOP_INIT 255 // highest value used in top half of ramp
84
#define PWM_CNT TCNT1 // for dynamic PWM, reset phase
87
#define CH1_PIN PB3 // pin 16, Opamp reference
88
#define CH1_PWM OCR1A // OCR1A is the output compare register for PB3
89
#define CH1_ENABLE_PIN PA0 // pin 7, Opamp power
90
#define CH1_ENABLE_PORT PORTA // control port for PA0
93
#define CH2_PIN PA6 // pin 1, 2nd LED Opamp reference
94
#define CH2_PWM OCR1B // OCR1B is the output compare register for PA6
95
#define CH2_ENABLE_PIN PA1 // pin 6, Opamp power
96
#define CH2_ENABLE_PORT PORTA // control port for PA1
99
#define CH3_PIN PC0 // pin 15, DD FET PWM
100
#define CH3_PWM OCR0A // OCR0A is the output compare register for PC0
104
#define SWITCH_PIN PA7 // pin 20
105
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
106
#define SWITCH_PCIE PCIE0 // PCIE1 is for PCINT[7:0]
107
#define SWITCH_PCMSK PCMSK0 // PCMSK1 is for PCINT[7:0]
108
#define SWITCH_PORT PINA // PINA or PINB or PINC
109
#define SWITCH_PUE PUEA // pullup group A
110
#define PCINT_vect PCINT0_vect // ISR for PCINT[7:0]
113
#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
114
#define VOLTAGE_PIN PB1 // Pin 18 / PB1 / ADC6
115
// pin to ADC mappings are in DS table 19-4
116
#define VOLTAGE_ADC ADC6D // digital input disable pin for PB1
117
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
118
#define VOLTAGE_ADC_DIDR DIDR1 // DIDR channel for ADC6D
119
// DS tables 19-3, 19-4
120
// Bit 7 6 5 4 3 2 1 0
121
// REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
122
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
124
// REFS[1:0] = 1, 0 for internal 1.1V reference
125
// other bits reserved
126
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
127
#define ADC_PRSCL 0x07 // clk/128
129
// Raw ADC readings at 4.4V and 2.2V
130
// calibrate the voltage readout here
131
// estimated / calculated values are:
132
// (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
133
// D1, R1, R2 = 0, 330, 100
135
//#define ADC_44 981 // raw value at 4.40V
136
#define ADC_44 967 // manually tweaked so 4.16V will blink out 4.2
139
//#define ADC_22 489 // raw value at 2.20V
140
#define ADC_22 482 // manually tweaked so 4.16V will blink out 4.2
143
// this light has aux LEDs under the optic
144
#define AUXLED_R_PIN PA5 // pin 2
145
#define AUXLED_G_PIN PA4 // pin 3
146
#define AUXLED_B_PIN PA3 // pin 4
147
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
148
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
149
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
151
#define BUTTON_LED_PIN PA2 // pin 5
152
#define BUTTON_LED_PORT PORTA // for all "PA" pins
153
#define BUTTON_LED_DDR DDRA // for all "PA" pins
154
#define BUTTON_LED_PUE PUEA // for all "PA" pins
157
inline void hwdef_setup() {
158
// enable output ports
159
DDRC = (1 << CH3_PIN);
160
DDRB = (1 << CH1_PIN);
161
DDRA = (1 << CH2_PIN)
162
| (1 << AUXLED_R_PIN)
163
| (1 << AUXLED_G_PIN)
164
| (1 << AUXLED_B_PIN)
165
| (1 << BUTTON_LED_PIN)
166
| (1 << CH1_ENABLE_PIN)
167
| (1 << CH2_ENABLE_PIN)
171
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
172
// pre-scale for timer: N = 1
173
// Linear opamp PWM for both main and 2nd LEDs (10-bit)
174
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
175
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
176
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
177
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
178
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
179
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
180
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
182
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
183
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
186
// FET PWM (8-bit; this channel can't do 10-bit)
187
// WGM0[2:0]: 0,0,1: PWM, Phase Correct, 8-bit (DS table 11-8)
188
// CS0[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 11-9)
189
// COM0A[1:0]: 1,0: PWM OC0A in the normal direction (DS table 11-4)
190
// COM0B[1:0]: 1,0: PWM OC0B in the normal direction (DS table 11-7)
191
TCCR0A = (0<<WGM01) | (1<<WGM00) // 8-bit (TOP=0xFF) (DS table 11-8)
192
| (1<<COM0A1) | (0<<COM0A0) // PWM 0A in normal direction (DS table 11-4)
193
//| (1<<COM0B1) | (0<<COM0B0) // PWM 0B in normal direction (DS table 11-7)
195
TCCR0B = (0<<CS02) | (0<<CS01) | (1<<CS00) // clk/1 (no prescaling) (DS table 11-9)
196
| (0<<WGM02) // phase-correct PWM (DS table 11-8)
199
// set PWM resolution
200
PWM_TOP = PWM_TOP_INIT;
203
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch
204
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
208
#define LAYOUT_DEFINED