1
// hwdef for Noctigon M44 2-channel light
2
// Copyright (C) 2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
7
* Pin / Name / Function
8
* 1 PA6 ch2 LED PWM (boost) (PWM1B)
9
* 2 PA5 R: red aux LED (PWM0B)
10
* 3 PA4 G: green aux LED
11
* 4 PA3 B: blue aux LED
14
* 7 PA0 Opamp 2 enable (channel 2 LEDs)
23
* 16 PB3 ch1 LED PWM (linear) (PWM1A)
25
* 18 PB1 MOSI / battery voltage (ADC6)
26
* 19 PB0 Opamp 1 enable (channel 1 LEDs)
27
* 20 PA7 e-switch (PCINT7)
28
* ADC12 thermal sensor
34
#define HWDEF_C_FILE hwdef-noctigon-m44.c
36
// allow using aux LEDs as extra channel modes
37
#include "chan-rgbaux.h"
40
// * 0. channel 1 only
41
// * 1. channel 2 only
42
// * 2. both channels, tied together, max "200%" power
43
// * 3. both channels, manual blend, max "100%" power
44
// * 4? both channels, manual blend, max 200% power
45
// * 4. both channels, auto blend, reversible
46
#define NUM_CHANNEL_MODES (5 + NUM_RGB_AUX_CHANNEL_MODES)
47
enum channel_modes_e {
56
// right-most bit first, modes are in fedcba9876543210 order
57
#define CHANNEL_MODES_ENABLED 0b0000000000011111
58
#define USE_CHANNEL_MODE_ARGS
59
// _, _, _, 128=middle CCT, 0=warm-to-cool
60
#define CHANNEL_MODE_ARGS 0,0,0,128,0,RGB_AUX_CM_ARGS
62
// can use some of the common handlers
63
#define USE_CALC_2CH_BLEND
66
#define PWM_CHANNELS 1 // old, remove this
68
#define PWM_BITS 16 // 0 to 32640 (0 to 255 PWM + 0 to 127 DSM) at constant kHz
69
#define PWM_GET PWM_GET16
70
#define PWM_DATATYPE uint16_t
71
#define PWM_DATATYPE2 uint32_t // only needs 32-bit if ramp values go over 255
72
#define PWM1_DATATYPE uint16_t // 15-bit PWM+DSM ramp
73
//#define PWM2_DATATYPE uint16_t // max "200% power" ramp table
75
// PWM parameters of both channels are tied together because they share a counter
77
#define PWM_TOP ICR1 // holds the TOP value for for variable-resolution PWM
78
#define PWM_TOP_INIT 255
79
#define PWM_CNT TCNT1 // for checking / resetting phase
80
// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM)
81
#define DSM_TOP (255<<7) // 15-bit resolution leaves 1 bit for carry
83
// timer interrupt for DSM
84
#define DSM_vect TIMER1_OVF_vect
85
#define DSM_INTCTRL TIMSK
86
#define DSM_OVF_bm (1<<TOIE1)
88
#define DELAY_FACTOR 90 // less time in delay() because more time spent in interrupts
90
// 1st channel (8 LEDs)
92
uint8_t ch1_pwm, ch1_dsm;
93
#define CH1_PIN PB3 // pin 16, Opamp reference
94
#define CH1_PWM OCR1A // OCR1A is the output compare register for PB3
95
#define CH1_ENABLE_PIN PB0 // pin 19, Opamp power
96
#define CH1_ENABLE_PORT PORTB // control port for PB0
98
// 2nd channel (8 LEDs)
100
uint8_t ch2_pwm, ch2_dsm;
101
#define CH2_PIN PA6 // pin 1, 2nd LED Opamp reference
102
#define CH2_PWM OCR1B // OCR1B is the output compare register for PA6
103
#define CH2_ENABLE_PIN PA0 // pin 7, Opamp power
104
#define CH2_ENABLE_PORT PORTA // control port for PA0
108
#define SWITCH_PIN PA7 // pin 20
109
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
110
#define SWITCH_PCIE PCIE0 // PCIE0 is for PCINT[7:0]
111
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
112
#define SWITCH_PORT PINA // PINA or PINB or PINC
113
#define SWITCH_PUE PUEA // pullup group A
114
#define PCINT_vect PCINT0_vect // ISR for PCINT[7:0]
116
#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
117
#define VOLTAGE_PIN PB1 // Pin 18 / PB1 / ADC6
118
// pin to ADC mappings are in DS table 19-4
119
#define VOLTAGE_ADC ADC6D // digital input disable pin for PB1
120
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
121
#define VOLTAGE_ADC_DIDR DIDR1 // DIDR channel for ADC6D
122
// DS tables 19-3, 19-4
123
// Bit 7 6 5 4 3 2 1 0
124
// REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
125
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
127
// REFS[1:0] = 1, 0 for internal 1.1V reference
128
// other bits reserved
129
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
130
#define ADC_PRSCL 0x07 // clk/128
132
// Raw ADC readings at 4.4V and 2.2V
133
// calibrate the voltage readout here
134
// estimated / calculated values are:
135
// (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
136
// D1, R1, R2 = 0, 330, 100
138
//#define ADC_44 981 // raw value at 4.40V
139
#define ADC_44 967 // manually tweaked so 4.16V will blink out 4.2
142
//#define ADC_22 489 // raw value at 2.20V
143
#define ADC_22 482 // manually tweaked so 4.16V will blink out 4.2
146
// this light has aux LEDs under the optic
147
#define AUXLED_R_PIN PA5 // pin 2
148
#define AUXLED_G_PIN PA4 // pin 3
149
#define AUXLED_B_PIN PA3 // pin 4
150
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
151
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC
152
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC
154
#define BUTTON_LED_PIN PA2 // pin 5
155
#define BUTTON_LED_PORT PORTA // for all "PA" pins
156
#define BUTTON_LED_DDR DDRA // for all "PA" pins
157
#define BUTTON_LED_PUE PUEA // for all "PA" pins
160
inline void hwdef_setup() {
161
// enable output ports
162
//DDRC = (1 << CH3_PIN);
163
DDRB = (1 << CH1_PIN)
164
| (1 << CH1_ENABLE_PIN)
166
DDRA = (1 << CH2_PIN)
167
| (1 << CH2_ENABLE_PIN)
168
| (1 << AUXLED_R_PIN)
169
| (1 << AUXLED_G_PIN)
170
| (1 << AUXLED_B_PIN)
171
| (1 << BUTTON_LED_PIN)
175
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
176
// pre-scale for timer: N = 1
177
// PWM for both channels
178
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
179
// WGM1[3:0]: 1,1,1,0: PWM, Fast, adjustable (DS table 12-5)
180
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
181
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
182
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
183
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5)
184
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4)
185
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4)
187
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6)
188
//| (1<<WGM13) | (1<<WGM12) // fast adjustable PWM (DS table 12-5)
189
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5)
192
// set PWM resolution
193
PWM_TOP = PWM_TOP_INIT;
195
// set up interrupt for delta-sigma modulation
196
// (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
197
//DSM_INTCTRL |= DSM_OVF_bm; // interrupt once for each timer cycle
200
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch
201
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt
205
#define LAYOUT_DEFINED