483.12.14
by Selene ToyKeeper
switched the rest of FSM + Anduril to use SPDX license headers |
1 |
// Noctigon KR4 / D4V2.5 driver layout (attiny1634)
|
2 |
// Copyright (C) 2020-2023 Selene ToyKeeper
|
|
3 |
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
4 |
#pragma once
|
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
5 |
|
483.12.14
by Selene ToyKeeper
switched the rest of FSM + Anduril to use SPDX license headers |
6 |
/*
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
7 |
* Pin / Name / Function
|
483.12.86
by Selene ToyKeeper
converted noctigon-dm11-* builds to multi-channel |
8 |
* 1 PA6 FET PWM (direct drive) (PWM1B) (on some models)
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
9 |
* 2 PA5 R: red aux LED (PWM0B)
|
10 |
* 3 PA4 G: green aux LED
|
|
11 |
* 4 PA3 B: blue aux LED
|
|
483.12.86
by Selene ToyKeeper
converted noctigon-dm11-* builds to multi-channel |
12 |
* 5 PA2 L: button LED (on some models)
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
13 |
* 6 PA1 (none)
|
14 |
* 7 PA0 (none)
|
|
15 |
* 8 GND GND
|
|
16 |
* 9 VCC VCC
|
|
17 |
* 10 PC5 (none)
|
|
18 |
* 11 PC4 (none)
|
|
19 |
* 12 PC3 RESET
|
|
20 |
* 13 PC2 (none)
|
|
21 |
* 14 PC1 SCK
|
|
22 |
* 15 PC0 (none) PWM0A
|
|
23 |
* 16 PB3 main LED PWM (linear) (PWM1A)
|
|
24 |
* 17 PB2 MISO / e-switch (PCINT10)
|
|
25 |
* 18 PB1 MOSI / battery voltage (ADC6)
|
|
26 |
* 19 PB0 Opamp power
|
|
27 |
* 20 PA7 (none)
|
|
28 |
* ADC12 thermal sensor
|
|
29 |
*
|
|
30 |
* Main LED power uses one pin to turn the Opamp on/off,
|
|
31 |
* and one pin to control Opamp power level.
|
|
483.12.86
by Selene ToyKeeper
converted noctigon-dm11-* builds to multi-channel |
32 |
* Linear brightness control uses the power level pin, with dynamic PWM.
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
33 |
* The on/off pin is only used to turn the main LED on and off,
|
34 |
* not to change brightness.
|
|
35 |
* Some models also have a direct-drive FET for turbo.
|
|
36 |
*/
|
|
37 |
||
38 |
#define ATTINY 1634
|
|
39 |
#include <avr/io.h> |
|
40 |
||
483.12.38
by Selene ToyKeeper
converted KR4-nofet build |
41 |
#ifndef HWDEF_C_FILE
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
42 |
#define HWDEF_C_FILE hwdef-noctigon-kr4.c
|
483.12.38
by Selene ToyKeeper
converted KR4-nofet build |
43 |
#endif
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
44 |
|
45 |
// allow using aux LEDs as extra channel modes
|
|
46 |
#include "chan-rgbaux.h" |
|
47 |
||
48 |
// channel modes:
|
|
49 |
// * 0. linear + DD FET stacked
|
|
483.12.72
by Selene ToyKeeper
fixed d4v2, kr4, m44, emisar-2ch (using new refactor), |
50 |
// * 1+. aux RGB
|
51 |
#define NUM_CHANNEL_MODES (1 + NUM_RGB_AUX_CHANNEL_MODES)
|
|
483.12.57
by Selene ToyKeeper
synced configs between D4v2 and KR4 |
52 |
enum CHANNEL_MODES { |
53 |
CM_MAIN = 0, |
|
483.12.72
by Selene ToyKeeper
fixed d4v2, kr4, m44, emisar-2ch (using new refactor), |
54 |
RGB_AUX_ENUMS
|
483.12.57
by Selene ToyKeeper
synced configs between D4v2 and KR4 |
55 |
};
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
56 |
|
483.12.57
by Selene ToyKeeper
synced configs between D4v2 and KR4 |
57 |
#define DEFAULT_CHANNEL_MODE CM_MAIN
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
58 |
|
483.12.72
by Selene ToyKeeper
fixed d4v2, kr4, m44, emisar-2ch (using new refactor), |
59 |
// right-most bit first, modes are in fedcba9876543210 order
|
60 |
#define CHANNEL_MODES_ENABLED 0b0000000000000001
|
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
61 |
// no args
|
62 |
//#define USE_CHANNEL_MODE_ARGS
|
|
483.12.57
by Selene ToyKeeper
synced configs between D4v2 and KR4 |
63 |
//#define CHANNEL_MODE_ARGS 0,0,0,0,0,0,0,0
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
64 |
|
65 |
||
483.12.157
by Selene ToyKeeper
converted fw3x-lume1 to new API, I think |
66 |
#define PWM_CHANNELS 2 // old, remove this |
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
67 |
|
483.12.36
by Selene ToyKeeper
KR4: ramp can use 8 bits, since values never go over 255 |
68 |
#define PWM_BITS 16 // dynamic 16-bit, but never goes over 255 |
69 |
#define PWM_GET PWM_GET8
|
|
483.12.37
by Selene ToyKeeper
D4v2 can use 8-bit ramp tables too |
70 |
#define PWM_DATATYPE uint16_t // is used for PWM_TOPS (which goes way over 255) |
483.12.36
by Selene ToyKeeper
KR4: ramp can use 8 bits, since values never go 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 |
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
74 |
|
75 |
// PWM parameters of both channels are tied together because they share a counter
|
|
483.12.85
by Selene ToyKeeper
converted noctigon-k1 to multi-channel |
76 |
#define PWM_TOP ICR1 // holds the TOP value for variable-resolution PWM |
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
77 |
#define PWM_TOP_INIT 255 // highest value used in top half of ramp |
78 |
#define PWM_CNT TCNT1 // for dynamic PWM, reset phase |
|
79 |
||
80 |
// linear channel
|
|
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 |
|
85 |
||
86 |
// DD FET channel
|
|
87 |
#define CH2_PIN PA6 // pin 1, DD FET PWM |
|
88 |
#define CH2_PWM OCR1B // OCR1B is the output compare register for PA6 |
|
89 |
||
90 |
// e-switch
|
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
91 |
#define SWITCH_PIN PB2 // pin 17 |
92 |
#define SWITCH_PCINT PCINT10 // pin 17 pin change interrupt |
|
93 |
#define SWITCH_PCIE PCIE1 // PCIE1 is for PCINT[11:8] |
|
94 |
#define SWITCH_PCMSK PCMSK1 // PCMSK1 is for PCINT[11:8] |
|
95 |
#define SWITCH_PORT PINB // PINA or PINB or PINC |
|
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
96 |
#define SWITCH_PUE PUEB // pullup group B |
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
97 |
#define PCINT_vect PCINT1_vect // ISR for PCINT[11:8] |
98 |
||
99 |
// the button tends to short out the voltage divider,
|
|
100 |
// so ignore voltage while the button is being held
|
|
433.3.4
by Selene Scriven
fixed the ADC channel for voltage measurement |
101 |
//#define NO_LVP_WHILE_BUTTON_PRESSED
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
102 |
|
103 |
#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened |
|
104 |
#define VOLTAGE_PIN PB1 // Pin 18 / PB1 / ADC6 |
|
105 |
// pin to ADC mappings are in DS table 19-4
|
|
106 |
#define VOLTAGE_ADC ADC6D // digital input disable pin for PB1 |
|
107 |
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
|
|
108 |
#define VOLTAGE_ADC_DIDR DIDR1 // DIDR channel for ADC6D |
|
109 |
// DS tables 19-3, 19-4
|
|
110 |
// Bit 7 6 5 4 3 2 1 0
|
|
111 |
// REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
|
|
112 |
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
|
|
113 |
// divided by ...
|
|
114 |
// REFS[1:0] = 1, 0 for internal 1.1V reference
|
|
115 |
// other bits reserved
|
|
433.3.4
by Selene Scriven
fixed the ADC channel for voltage measurement |
116 |
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
|
433.2.6
by Selene Scriven
applied ADC rewrite updates to most recent build targets |
117 |
#define ADC_PRSCL 0x07 // clk/128 |
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
118 |
|
119 |
// Raw ADC readings at 4.4V and 2.2V
|
|
120 |
// calibrate the voltage readout here
|
|
121 |
// estimated / calculated values are:
|
|
481.1.2
by Selene Scriven
minor formatting cleanup |
122 |
// (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
123 |
// D1, R1, R2 = 0, 330, 100
|
124 |
#ifndef ADC_44
|
|
125 |
//#define ADC_44 981 // raw value at 4.40V
|
|
126 |
#define ADC_44 967 // manually tweaked so 4.16V will blink out 4.2 |
|
127 |
#endif
|
|
128 |
#ifndef ADC_22
|
|
129 |
//#define ADC_22 489 // raw value at 2.20V
|
|
130 |
#define ADC_22 482 // manually tweaked so 4.16V will blink out 4.2 |
|
131 |
#endif
|
|
132 |
||
133 |
// this light has aux LEDs under the optic
|
|
134 |
#define AUXLED_R_PIN PA5 // pin 2 |
|
135 |
#define AUXLED_G_PIN PA4 // pin 3 |
|
136 |
#define AUXLED_B_PIN PA3 // pin 4 |
|
137 |
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC |
|
138 |
#define AUXLED_RGB_DDR DDRA // DDRA or DDRB or DDRC |
|
139 |
#define AUXLED_RGB_PUE PUEA // PUEA or PUEB or PUEC |
|
140 |
||
481.1.1
by Selene Scriven
added Emisar D4v2.5 support (D4v2 with a KR4 driver, same as KR4 but with lighted button indicator LED support) |
141 |
#define BUTTON_LED_PIN PA2 // pin 5 |
142 |
#define BUTTON_LED_PORT PORTA // for all "PA" pins |
|
143 |
#define BUTTON_LED_DDR DDRA // for all "PA" pins |
|
144 |
#define BUTTON_LED_PUE PUEA // for all "PA" pins |
|
145 |
||
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
146 |
// this light has three aux LED channels: R, G, B
|
147 |
#define USE_AUX_RGB_LEDS
|
|
148 |
// some variants also have an independent LED in the button
|
|
149 |
#define USE_BUTTON_LED
|
|
150 |
// the aux LEDs are front-facing, so turn them off while main LEDs are on
|
|
151 |
#ifdef USE_INDICATOR_LED_WHILE_RAMPING
|
|
152 |
#undef USE_INDICATOR_LED_WHILE_RAMPING
|
|
153 |
#endif
|
|
154 |
||
155 |
||
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
156 |
inline void hwdef_setup() { |
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
157 |
// enable output ports
|
158 |
// Opamp level and Opamp on/off
|
|
159 |
DDRB = (1 << CH1_PIN) |
|
160 |
| (1 << CH1_ENABLE_PIN); |
|
161 |
// DD FET PWM, aux R/G/B, button LED
|
|
162 |
DDRA = (1 << CH2_PIN) |
|
163 |
| (1 << AUXLED_R_PIN) |
|
164 |
| (1 << AUXLED_G_PIN) |
|
165 |
| (1 << AUXLED_B_PIN) |
|
166 |
| (1 << BUTTON_LED_PIN) |
|
167 |
;
|
|
168 |
||
169 |
// configure PWM
|
|
170 |
// Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
|
|
171 |
// pre-scale for timer: N = 1
|
|
172 |
// WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
|
|
173 |
// CS1[2:0]: 0,0,1: clk/1 (No prescaling) (DS table 12-6)
|
|
174 |
// COM1A[1:0]: 1,0: PWM OC1A in the normal direction (DS table 12-4)
|
|
175 |
// COM1B[1:0]: 1,0: PWM OC1B in the normal direction (DS table 12-4)
|
|
176 |
TCCR1A = (1<<WGM11) | (0<<WGM10) // adjustable PWM (TOP=ICR1) (DS table 12-5) |
|
177 |
| (1<<COM1A1) | (0<<COM1A0) // PWM 1A in normal direction (DS table 12-4) |
|
178 |
| (1<<COM1B1) | (0<<COM1B0) // PWM 1B in normal direction (DS table 12-4) |
|
179 |
;
|
|
180 |
TCCR1B = (0<<CS12) | (0<<CS11) | (1<<CS10) // clk/1 (no prescaling) (DS table 12-6) |
|
181 |
| (1<<WGM13) | (0<<WGM12) // phase-correct adjustable PWM (DS table 12-5) |
|
182 |
;
|
|
183 |
||
184 |
// set PWM resolution
|
|
185 |
PWM_TOP = PWM_TOP_INIT; |
|
186 |
||
187 |
// set up e-switch
|
|
188 |
SWITCH_PUE = (1 << SWITCH_PIN); // pull-up for e-switch |
|
189 |
SWITCH_PCMSK = (1 << SWITCH_PCINT); // enable pin change interrupt |
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
190 |
}
|
191 |
||
483.12.34
by Selene ToyKeeper
Noctigon KR4: updated to use new channel system |
192 |
|
433.3.1
by Selene Scriven
initial support for Noctigon KR4 |
193 |
#define LAYOUT_DEFINED
|
194 |