1
// Emisar 2-channel generic w/ tint ramping
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 [unused: 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.
37
#define HWDEF_C_FILE hwdef-emisar-2ch.c
39
// allow using aux LEDs as extra channel modes
40
#include "chan-rgbaux.h"
43
// * 0. channel 1 only
44
// * 1. channel 2 only
45
// * 2. both channels, tied together, max "200%" power
46
// * 3. both channels, manual blend, max "100%" power
47
// * 4. both channels, auto blend, reversible
48
#define NUM_CHANNEL_MODES (5 + NUM_RGB_AUX_CHANNEL_MODES)
49
enum channel_modes_e {
58
// right-most bit first, modes are in fedcba9876543210 order
59
#define CHANNEL_MODES_ENABLED 0b0000000000011111
60
#define USE_CHANNEL_MODE_ARGS
61
// _, _, _, 128=middle CCT, 0=warm-to-cool
62
#define CHANNEL_MODE_ARGS 0,0,0,128,0,RGB_AUX_CM_ARGS
64
// can use some of the common handlers
65
#define USE_CALC_2CH_BLEND
68
#define PWM_CHANNELS 2 // old, remove this
70
#define PWM_BITS 16 // 0 to 16383 at variable Hz, not 0 to 255 at 16 kHz
71
#define PWM_GET PWM_GET16
72
#define PWM_DATATYPE uint16_t
73
#define PWM_DATATYPE2 uint32_t // only needs 32-bit if ramp values go over 255
74
#define PWM1_DATATYPE uint16_t // regular ramp table
75
#define PWM2_DATATYPE uint16_t // max "200% power" ramp table
76
//#define PWM3_DATATYPE uint8_t // DD FET ramp table (8-bit only)
78
// PWM parameters of both channels are tied together because they share a counter
79
#define PWM_TOP ICR1 // holds the TOP value for for variable-resolution PWM
80
#define PWM_TOP_INIT 511 // highest value used in top half of ramp
81
#define PWM_CNT TCNT1 // for dynamic PWM, reset phase
84
#define CH1_PIN PB3 // pin 16, Opamp reference
85
#define CH1_PWM OCR1A // OCR1A is the output compare register for PB3
86
#define CH1_ENABLE_PIN PA0 // pin 7, Opamp power
87
#define CH1_ENABLE_PORT PORTA // control port for PA0
90
#define CH2_PIN PA6 // pin 1, 2nd LED Opamp reference
91
#define CH2_PWM OCR1B // OCR1B is the output compare register for PA6
92
#define CH2_ENABLE_PIN PA1 // pin 6, Opamp power
93
#define CH2_ENABLE_PORT PORTA // control port for PA1
96
//#define CH3_PIN PC0 // pin 15, DD FET PWM
97
//#define CH3_PWM OCR0A // OCR0A is the output compare register for PC0
101
#define SWITCH_PIN PA7 // pin 20
102
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
103
#define SWITCH_PCIE PCIE0 // PCIE1 is for PCINT[7:0]
104
#define SWITCH_PCMSK PCMSK0 // PCMSK1 is for PCINT[7:0]
105
#define SWITCH_PORT PINA // PINA or PINB or PINC
106
#define SWITCH_PUE PUEA // pullup group A
107
#define PCINT_vect PCINT0_vect // ISR for PCINT[7:0]
110
#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
111
#define VOLTAGE_PIN PB1 // Pin 18 / PB1 / ADC6
112
// pin to ADC mappings are in DS table 19-4
113
#define VOLTAGE_ADC ADC6D // digital input disable pin for PB1
114
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
115
#define VOLTAGE_ADC_DIDR DIDR1 // DIDR channel for ADC6D
116
// DS tables 19-3, 19-4
117
// Bit 7 6 5 4 3 2 1 0
118
// REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
119
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
121
// REFS[1:0] = 1, 0 for internal 1.1V reference
122
// other bits reserved
123
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
124
#define ADC_PRSCL 0x07 // clk/128
126
// Raw ADC readings at 4.4V and 2.2V
127
// calibrate the voltage readout here
128
// estimated / calculated values are:
129
// (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
130
// D1, R1, R2 = 0, 330, 100
132
//#define ADC_44 981 // raw value at 4.40V
133
#define ADC_44 967 // manually tweaked so 4.16V will blink out 4.2
136
//#define ADC_22 489 // raw value at 2.20V
137
#define ADC_22 482 // manually tweaked so 4.16V will blink out 4.2
140
// this light has aux LEDs under the optic
141
#define AUXLED_R_PIN PA5 // pin 2
142
#define AUXLED_G_PIN PA4 // pin 3
143
#define AUXLED_B_PIN PA3 // pin 4
144
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
145
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
146
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
148
#define BUTTON_LED_PIN PA2 // pin 5
149
#define BUTTON_LED_PORT PORTA // for all "PA" pins
150
#define BUTTON_LED_DDR DDRA // for all "PA" pins
151
#define BUTTON_LED_PUE PUEA // for all "PA" pins
154
inline void hwdef_setup() {
155
// enable output ports
156
//DDRC = (1 << CH3_PIN);
157
DDRB = (1 << CH1_PIN);
158
DDRA = (1 << CH2_PIN)
159
| (1 << AUXLED_R_PIN)
160
| (1 << AUXLED_G_PIN)
161
| (1 << AUXLED_B_PIN)
162
| (1 << BUTTON_LED_PIN)
163
| (1 << CH1_ENABLE_PIN)
164
| (1 << CH2_ENABLE_PIN)
168
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
169
// pre-scale for timer: N = 1
170
// Linear opamp PWM for both main and 2nd LEDs (10-bit)
171
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
172
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
173
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
174
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
175
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
176
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
177
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
179
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
180
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
183
// unused on this driver
185
// FET PWM (8-bit; this channel can't do 10-bit)
186
// WGM0[2:0]: 0,0,1: PWM, Phase Correct, 8-bit (DS table 11-8)
187
// CS0[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 11-9)
188
// COM0A[1:0]: 1,0: PWM OC0A in the normal direction (DS table 11-4)
189
// COM0B[1:0]: 1,0: PWM OC0B in the normal direction (DS table 11-7)
190
TCCR0A = (0<<WGM01) | (1<<WGM00) // 8-bit (TOP=0xFF) (DS table 11-8)
191
| (1<<COM0A1) | (0<<COM0A0) // PWM 0A in normal direction (DS table 11-4)
192
//| (1<<COM0B1) | (0<<COM0B0) // PWM 0B in normal direction (DS table 11-7)
194
TCCR0B = (0<<CS02) | (0<<CS01) | (1<<CS00) // clk/1 (no prescaling) (DS table 11-9)
195
| (0<<WGM02) // phase-correct PWM (DS table 11-8)
197
CH3_PWM = 0; // ensure this channel is off, if it exists
200
// set PWM resolution
201
PWM_TOP = PWM_TOP_INIT;
204
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch
205
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
209
#define LAYOUT_DEFINED