~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-emisar-d4k-3ch.h

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Emisar D4K 3-channel hwdef
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
/*
 
7
 * Pin / Name / Function
 
8
 *   1    PA6   LED 4 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
 
12
 *   5    PA2   button LED
 
13
 *   6    PA1   3rd LED opamp enable
 
14
 *   7    PA0   4th LED opamp enable
 
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   main 2 LEDs PWM (linear) (PWM0A) (8-bit only)
 
23
 *  16    PB3   3rd LED PWM (linear) (PWM1A)
 
24
 *  17    PB2   MISO
 
25
 *  18    PB1   MOSI / battery voltage (ADC6)
 
26
 *  19    PB0   main 2 LEDs opamp enable
 
27
 *  20    PA7   e-switch (PCINT7)
 
28
 *      ADC12   thermal sensor
 
29
 *
 
30
 * PWM speed + resolution is dynamic on 2 channels,
 
31
 * and 8-bit 16 kHz on 1 channel.
 
32
 *
 
33
 * Note: Some hardware might swap the wires at build time!
 
34
 * It might be wired 8/16/16 (8-bit LEDs 1+2) or 16/16/8 (8-bit LED 4).
 
35
 * So this code should support both wire layouts.
 
36
 */
 
37
 
 
38
#define ATTINY 1634
 
39
#include <avr/io.h>
 
40
 
 
41
#define HWDEF_C_FILE hwdef-emisar-d4k-3ch.c
 
42
 
 
43
// allow using aux LEDs as extra channel modes
 
44
#include "chan-rgbaux.h"
 
45
 
 
46
// channel modes:
 
47
// - 1. main 2 LEDs only (8/16/16 wiring) or LED 4 only (16/16/8)
 
48
// - 2. 3rd LED only
 
49
// - 3. 4th LED only (8/16/16 wiring) or main 2 LEDs only (16/16/8)
 
50
// - 4. all 3 channels (equal amounts)
 
51
// - 5. 2ch blend (3rd + 4th LEDs, 8/16/16 wiring)
 
52
// - 6. 2ch blend (3rd + 4th LEDs, 16/16/8 wiring)
 
53
// - 7. 3ch blend (HSV style)
 
54
// - 8. 3ch auto blend (red-warm-cool style, led4-led3-main2)
 
55
// - 9+. RGB aux (hidden)
 
56
#define NUM_CHANNEL_MODES   (8 + NUM_RGB_AUX_CHANNEL_MODES)
 
57
enum channel_modes_e {
 
58
    CM_MAIN2 = 0,
 
59
    CM_LED3,
 
60
    CM_LED4,
 
61
    CM_ALL,
 
62
    CM_BLEND34A,  // 8 / [16+16]
 
63
    CM_BLEND34B,  // 16 / [16+8]
 
64
    CM_HSV,
 
65
    CM_AUTO3,
 
66
    RGB_AUX_ENUMS
 
67
};
 
68
 
 
69
#define CHANNEL_MODES_ENABLED 0b0000000000001111
 
70
#define USE_CHANNEL_MODE_ARGS
 
71
// _, _, _, _, 128=middle CCT, 128=middle CCT, 213=purple, _
 
72
#define CHANNEL_MODE_ARGS 0,0,0,0,128,128,213,0,RGB_AUX_CM_ARGS
 
73
#define USE_CUSTOM_CHANNEL_3H_MODES
 
74
#define USE_CIRCULAR_TINT_3H
 
75
 
 
76
// can use some of the common handlers
 
77
#define USE_CALC_2CH_BLEND
 
78
#define USE_HSV2RGB
 
79
 
 
80
 
 
81
#define PWM_CHANNELS 1  // old, remove this
 
82
 
 
83
#define PWM_BITS      16     // 0 to 16383 at variable Hz, not 0 to 255 at 16 kHz
 
84
#define PWM_GET       PWM_GET16
 
85
#define PWM_DATATYPE  uint16_t
 
86
#define PWM_DATATYPE2 uint32_t  // only needs 32-bit if ramp values go over 255
 
87
#define PWM1_DATATYPE uint16_t  // 15-bit PWM+DSM ramp
 
88
 
 
89
// dynamic PWM
 
90
#define PWM_TOP       ICR1   // holds the TOP value for for variable-resolution PWM
 
91
#define PWM_TOP_INIT  255    // highest value used in top half of ramp
 
92
#define PWM_CNT       TCNT1  // for checking / resetting phase
 
93
#define PWM_CNT2      TCNT0  // for checking / resetting phase
 
94
// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM)
 
95
#define DSM_TOP       (255<<7) // 15-bit resolution leaves 1 bit for carry
 
96
 
 
97
// timer interrupt for DSM
 
98
#define DSM_vect     TIMER0_OVF_vect
 
99
#define DSM_INTCTRL  TIMSK
 
100
#define DSM_OVF_bm   (1<<TOIE0)
 
101
 
 
102
#define DELAY_FACTOR 90  // less time in delay() because more time spent in interrupts
 
103
 
 
104
// main 2 LEDs / 1st channel (2 LEDs)
 
105
uint16_t main2_dsm_lvl;
 
106
uint8_t main2_pwm, main2_dsm;
 
107
#define MAIN2_PWM_PIN PC0
 
108
#define MAIN2_PWM_LVL OCR0A   // OCR0A is the output compare register for PC0
 
109
#define MAIN2_ENABLE_PIN  PB0    // Opamp power
 
110
#define MAIN2_ENABLE_PORT PORTB  // control port for PB0
 
111
 
 
112
// LED 3 / 2nd channel (1 LED)
 
113
uint16_t led3_dsm_lvl;
 
114
uint8_t led3_pwm, led3_dsm;
 
115
#define LED3_PWM_PIN PB3
 
116
#define LED3_PWM_LVL OCR1A   // OCR1A is the output compare register for PB3
 
117
#define LED3_ENABLE_PIN  PA1    // Opamp power
 
118
#define LED3_ENABLE_PORT PORTA  // control port for PA1
 
119
 
 
120
// LED 4 / 3rd channel (1 LED)
 
121
uint16_t led4_dsm_lvl;
 
122
uint8_t led4_pwm, led4_dsm;
 
123
#define LED4_PWM_PIN PA6
 
124
#define LED4_PWM_LVL OCR1B  // OCR1B is the output compare register for PA6
 
125
#define LED4_ENABLE_PIN  PA0    // Opamp power
 
126
#define LED4_ENABLE_PORT PORTA  // control port for PA0
 
127
 
 
128
// e-switch
 
129
#ifndef SWITCH_PIN
 
130
#define SWITCH_PIN   PA7     // pin 20
 
131
#define SWITCH_PCINT PCINT7  // pin 20 pin change interrupt
 
132
#define SWITCH_PCIE  PCIE0   // PCIE1 is for PCINT[7:0]
 
133
#define SWITCH_PCMSK PCMSK0  // PCMSK1 is for PCINT[7:0]
 
134
#define SWITCH_PORT  PINA    // PINA or PINB or PINC
 
135
#define SWITCH_PUE   PUEA    // pullup group A
 
136
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
 
137
#endif
 
138
 
 
139
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
 
140
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
 
141
// pin to ADC mappings are in DS table 19-4
 
142
#define VOLTAGE_ADC      ADC6D  // digital input disable pin for PB1
 
143
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
 
144
#define VOLTAGE_ADC_DIDR DIDR1  // DIDR channel for ADC6D
 
145
// DS tables 19-3, 19-4
 
146
// Bit   7     6     5      4     3    2    1    0
 
147
//     REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
 
148
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
 
149
// divided by ...
 
150
// REFS[1:0] = 1, 0 for internal 1.1V reference
 
151
// other bits reserved
 
152
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
 
153
#define ADC_PRSCL   0x07    // clk/128
 
154
 
 
155
// Raw ADC readings at 4.4V and 2.2V
 
156
// calibrate the voltage readout here
 
157
// estimated / calculated values are:
 
158
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
 
159
// D1, R1, R2 = 0, 330, 100
 
160
#ifndef ADC_44
 
161
//#define ADC_44 981  // raw value at 4.40V
 
162
#define ADC_44 967  // manually tweaked so 4.16V will blink out 4.2
 
163
#endif
 
164
#ifndef ADC_22
 
165
//#define ADC_22 489  // raw value at 2.20V
 
166
#define ADC_22 482  // manually tweaked so 4.16V will blink out 4.2
 
167
#endif
 
168
 
 
169
// this light has aux LEDs under the optic
 
170
#define AUXLED_R_PIN    PA5    // pin 2
 
171
#define AUXLED_G_PIN    PA4    // pin 3
 
172
#define AUXLED_B_PIN    PA3    // pin 4
 
173
#define AUXLED_RGB_PORT PORTA  // PORTA or PORTB or PORTC
 
174
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
 
175
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
 
176
 
 
177
#define BUTTON_LED_PIN  PA2    // pin 5
 
178
#define BUTTON_LED_PORT PORTA  // for all "PA" pins
 
179
#define BUTTON_LED_DDR  DDRA   // for all "PA" pins
 
180
#define BUTTON_LED_PUE  PUEA   // for all "PA" pins
 
181
 
 
182
// this light has three aux LED channels: R, G, B
 
183
#define USE_AUX_RGB_LEDS
 
184
// the aux LEDs are front-facing, so turn them off while main LEDs are on
 
185
// it also has an independent LED in the button
 
186
#define USE_BUTTON_LED
 
187
 
 
188
 
 
189
inline void hwdef_setup() {
 
190
    // enable output ports
 
191
    DDRC = (1 << MAIN2_PWM_PIN);
 
192
    DDRB = (1 << LED3_PWM_PIN)
 
193
         | (1 << MAIN2_ENABLE_PIN)
 
194
         ;
 
195
    DDRA = (1 << LED4_PWM_PIN)
 
196
         | (1 << LED4_ENABLE_PIN)
 
197
         | (1 << LED3_ENABLE_PIN)
 
198
         | (1 << AUXLED_R_PIN)
 
199
         | (1 << AUXLED_G_PIN)
 
200
         | (1 << AUXLED_B_PIN)
 
201
         | (1 << BUTTON_LED_PIN)
 
202
         ;
 
203
 
 
204
    // configure PWM
 
205
    // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
206
    // pre-scale for timer: N = 1
 
207
    // Linear opamp PWM for both 16-bit channels
 
208
    // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
 
209
    // WGM1[3:0]: 1,1,1,0: PWM, Fast, adjustable (DS table 12-5)
 
210
    // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
211
    // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
212
    // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
 
213
    TCCR1A  = (1<<WGM11)  | (0<<WGM10)   // adjustable PWM (TOP=ICR1) (DS table 12-5)
 
214
            | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
215
            | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
216
            ;
 
217
    TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
218
            //| (1<<WGM13)  | (1<<WGM12)  // fast adjustable PWM (DS table 12-5)
 
219
            | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
 
220
            ;
 
221
    // set PWM resolution
 
222
    PWM_TOP = PWM_TOP_INIT;
 
223
 
 
224
    // 8-bit PWM channel (LEDs 1+2 or LED 4)
 
225
    // WGM0[2:0]: 0,0,1: PWM, Phase Correct, 8-bit (DS table 11-8)
 
226
    // CS0[2:0]:  0,0,1: clk/1 (No prescaling) (DS table 11-9)
 
227
    // COM0A[1:0]:  1,0: PWM OC0A in the normal direction (DS table 11-4)
 
228
    // COM0B[1:0]:  1,0: PWM OC0B in the normal direction (DS table 11-7)
 
229
    TCCR0A  = (0<<WGM01)  | (1<<WGM00)   // 8-bit (TOP=0xFF) (DS table 11-8)
 
230
            | (1<<COM0A1) | (0<<COM0A0)  // PWM 0A in normal direction (DS table 11-4)
 
231
            //| (1<<COM0B1) | (0<<COM0B0)  // PWM 0B in normal direction (DS table 11-7)
 
232
            ;
 
233
    TCCR0B  = (0<<CS02)   | (0<<CS01) | (1<<CS00)  // clk/1 (no prescaling) (DS table 11-9)
 
234
            | (0<<WGM02)  // phase-correct PWM (DS table 11-8)
 
235
            ;
 
236
 
 
237
    // set up interrupt for delta-sigma modulation
 
238
    // (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
 
239
    //DSM_INTCTRL |= DSM_OVF_bm;  // interrupt once for each timer cycle
 
240
 
 
241
    // set up e-switch
 
242
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
243
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
244
}
 
245
 
 
246
 
 
247
#define LAYOUT_DEFINED
 
248