~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-emisar-2ch.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 2-channel generic w/ tint ramping
 
2
// Copyright (C) 2021-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
/*
 
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
 
12
 *   5    PA2   button LED
 
13
 *   6    PA1   Opamp 2 enable (channel 2 LEDs)
 
14
 *   7    PA0   Opamp 1 enable (channel 1 LEDs)
 
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   [unused: ch1 LED PWM (FET) (PWM0A, 8-bit)]
 
23
 *  16    PB3   ch1 LED PWM (linear) (PWM1A)
 
24
 *  17    PB2   MISO
 
25
 *  18    PB1   MOSI / battery voltage (ADC6)
 
26
 *  19    PB0   (none)
 
27
 *  20    PA7   e-switch (PCINT7)
 
28
 *      ADC12   thermal sensor
 
29
 *
 
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
 */
 
33
 
 
34
#define ATTINY 1634
 
35
#include <avr/io.h>
 
36
 
 
37
#define HWDEF_C_FILE hwdef-emisar-2ch.c
 
38
 
 
39
// allow using aux LEDs as extra channel modes
 
40
#include "chan-rgbaux.h"
 
41
 
 
42
// channel modes:
 
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 {
 
50
    CM_CH1 = 0,
 
51
    CM_CH2,
 
52
    CM_BOTH,
 
53
    CM_BLEND,
 
54
    CM_AUTO,
 
55
    RGB_AUX_ENUMS
 
56
};
 
57
 
 
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
 
63
 
 
64
// can use some of the common handlers
 
65
#define USE_CALC_2CH_BLEND
 
66
 
 
67
 
 
68
#define PWM_CHANNELS 2  // old, remove this
 
69
 
 
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)
 
77
 
 
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
 
82
 
 
83
// main LEDs, linear
 
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
 
88
 
 
89
// 2nd LEDs, linear
 
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
 
94
 
 
95
// main LEDs, DD FET
 
96
//#define CH3_PIN  PC0            // pin 15, DD FET PWM
 
97
//#define CH3_PWM  OCR0A          // OCR0A is the output compare register for PC0
 
98
 
 
99
// e-switch
 
100
#ifndef SWITCH_PIN
 
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]
 
108
#endif
 
109
 
 
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
 
120
// divided by ...
 
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
 
125
 
 
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
 
131
#ifndef ADC_44
 
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
 
134
#endif
 
135
#ifndef ADC_22
 
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
 
138
#endif
 
139
 
 
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
 
147
 
 
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
 
152
 
 
153
 
 
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)
 
165
         ;
 
166
 
 
167
    // configure PWM
 
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)
 
178
            ;
 
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)
 
181
            ;
 
182
 
 
183
    // unused on this driver
 
184
    #if 0
 
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)
 
193
            ;
 
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)
 
196
            ;
 
197
    CH3_PWM = 0;  // ensure this channel is off, if it exists
 
198
    #endif
 
199
 
 
200
    // set PWM resolution
 
201
    PWM_TOP = PWM_TOP_INIT;
 
202
 
 
203
    // set up e-switch
 
204
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
205
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
206
}
 
207
 
 
208
 
 
209
#define LAYOUT_DEFINED
 
210