~toykeeper/flashlight-firmware/fsm

483.12.14 by Selene ToyKeeper
switched the rest of FSM + Anduril to use SPDX license headers
1
// Noctigon DM11 (12V) driver layout (attiny1634)
2
// Copyright (C) 2021-2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
4
#pragma once
483.9.6 by Selene ToyKeeper
added early initial attempt at a boost/12V/6V version of the MD11
5
483.12.14 by Selene ToyKeeper
switched the rest of FSM + Anduril to use SPDX license headers
6
/*
483.9.6 by Selene ToyKeeper
added early initial attempt at a boost/12V/6V version of the MD11
7
 * (based on Noctigon K1)
8
 *
9
 * Pin / Name / Function
10
 *   1    PA6   (none) (PWM1B) (reserved for DD drivers)
11
 *   2    PA5   R: red aux LED (PWM0B)
12
 *   3    PA4   G: green aux LED
13
 *   4    PA3   B: blue aux LED
14
 *   5    PA2   L: button LED
15
 *   6    PA1   (none)
16
 *   7    PA0   (none)
17
 *   8    GND   GND
18
 *   9    VCC   VCC
19
 *  10    PC5   (none)
20
 *  11    PC4   (none)
21
 *  12    PC3   RESET
22
 *  13    PC2   (none)
23
 *  14    PC1   SCK
24
 *  15    PC0   boost PMIC enable (PWM0A not used)
25
 *  16    PB3   main LED PWM (PWM1A)
26
 *  17    PB2   MISO / e-switch? (PCINT10)
27
 *  18    PB1   MOSI / battery voltage (ADC6)
28
 *  19    PB0   Opamp power
29
 *  20    PA7   e-switch?  (PCINT7)
30
 *      ADC12   thermal sensor
31
 *
32
 * Main LED power uses one pin to turn the Opamp on/off,
33
 * and one pin to control Opamp power level.
34
 * Linear brightness control uses the power level pin, with dynamic PWM.
35
 * The on/off pin is only used to turn the main LED on and off,
36
 * not to change brightness.
37
 */
38
39
#ifdef ATTINY
40
#undef ATTINY
41
#endif
42
#define ATTINY 1634
43
#include <avr/io.h>
44
45
#define PWM_CHANNELS 1  // can't use DD FET on boost drivers
46
#define PWM_BITS 16  // data type needs 16 bits, not 8
47
#define PWM_TOP  255 // highest value used in top half of ramp
48
#define USE_DYN_PWM  // dynamic frequency and speed
49
50
//#define SWITCH_PIN   PB2     // pin 17
51
//#define SWITCH_PCINT PCINT10 // pin 17 pin change interrupt
52
//#define SWITCH_PCIE  PCIE1   // PCIE1 is for PCINT[11:8]
53
//#define SWITCH_PCMSK PCMSK1  // PCMSK1 is for PCINT[11:8]
54
//#define SWITCH_PORT  PINB    // PINA or PINB or PINC
55
//#define PCINT_vect   PCINT1_vect  // ISR for PCINT[11:8]
56
57
#define SWITCH_PIN   PA7    // pin 20
58
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
59
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
60
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
61
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
62
63
#define PWM1_PIN PB3        // pin 16, Opamp reference
64
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
65
#define PWM1_CNT TCNT1      // for dynamic PWM, reset phase
483.11.5 by Selene ToyKeeper
applied new phase-hack flags to other builds where relevant
66
#define PWM1_PHASE_RESET_OFF  // force reset while shutting off
67
#define PWM1_PHASE_RESET_ON   // force reset while turning on
68
#define PWM1_PHASE_SYNC       // manual sync while changing level
483.9.6 by Selene ToyKeeper
added early initial attempt at a boost/12V/6V version of the MD11
69
70
// PWM parameters of both channels are tied together because they share a counter
71
#define PWM1_TOP ICR1       // holds the TOP value for for variable-resolution PWM
72
73
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
74
#define LED_ENABLE_PORT PORTB  // control port for PB0
75
76
#define LED2_ENABLE_PIN  PC0    // pin 15, boost PMIC enable
77
#define LED2_ENABLE_PORT PORTC  // control port for PC0
78
79
80
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
81
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
82
// pin to ADC mappings are in DS table 19-4
83
#define VOLTAGE_ADC      ADC6D  // digital input disable pin for PB1
84
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
85
#define VOLTAGE_ADC_DIDR DIDR1  // DIDR channel for ADC6D
86
// DS tables 19-3, 19-4
87
// Bit   7     6     5      4     3    2    1    0
88
//     REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
89
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
90
// divided by ...
91
// REFS[1:0] = 1, 0 for internal 1.1V reference
92
// other bits reserved
93
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
94
#define ADC_PRSCL   0x07    // clk/128
95
96
// Raw ADC readings at 4.4V and 2.2V
97
// calibrate the voltage readout here
98
// estimated / calculated values are:
99
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
100
// D1, R1, R2 = 0, 330, 100
101
#ifndef ADC_44
102
//#define ADC_44 981  // raw value at 4.40V
103
#define ADC_44 967  // manually tweaked so 4.16V will blink out 4.2
104
#endif
105
#ifndef ADC_22
106
//#define ADC_22 489  // raw value at 2.20V
107
#define ADC_22 482  // manually tweaked so 4.16V will blink out 4.2
108
#endif
109
110
// this light has aux LEDs under the optic
111
#define AUXLED_R_PIN    PA5    // pin 2
112
#define AUXLED_G_PIN    PA4    // pin 3
113
#define AUXLED_B_PIN    PA3    // pin 4
114
#define AUXLED_RGB_PORT PORTA  // PORTA or PORTB or PORTC
115
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
116
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
117
118
#define BUTTON_LED_PIN  PA2    // pin 5
119
#define BUTTON_LED_PORT PORTA  // for all "PA" pins
120
#define BUTTON_LED_DDR  DDRA   // for all "PA" pins
121
#define BUTTON_LED_PUE  PUEA   // for all "PA" pins
122
123
// with so many pins, doing this all with #ifdefs gets awkward...
124
// ... so just hardcode it in each hwdef file instead
125
inline void hwdef_setup() {
126
  // enable output ports
127
  // boost PMIC on/off
128
  DDRC = (1 << LED2_ENABLE_PIN);
129
  // Opamp level and Opamp on/off
130
  DDRB = (1 << PWM1_PIN)
131
       | (1 << LED_ENABLE_PIN);
132
  // aux R/G/B, button LED
133
  DDRA = (1 << AUXLED_R_PIN)
134
       | (1 << AUXLED_G_PIN)
135
       | (1 << AUXLED_B_PIN)
136
       | (1 << BUTTON_LED_PIN)
137
       ;
138
139
  // configure PWM
140
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
141
  // pre-scale for timer: N = 1
142
  // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
143
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
144
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
145
  // COM1B[1:0]:    0,0: PWM OC1B disabled (DS table 12-4)
146
  TCCR1A  = (1<<WGM11)  | (0<<WGM10)   // adjustable PWM (TOP=ICR1) (DS table 12-5)
147
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
148
          | (0<<COM1B1) | (0<<COM1B0)  // PWM 1B disabled (DS table 12-4)
149
          ;
150
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
151
          | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
152
          ;
153
154
  // set PWM resolution
155
  PWM1_TOP = PWM_TOP;
156
157
  // set up e-switch
158
  ////PORTB = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
159
  //PUEB = (1 << SWITCH_PIN);  // pull-up for e-switch
160
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
161
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
162
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
163
}
164
165
#define LAYOUT_DEFINED
166