~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-thefreeman-boost-fwaa-mp3432-hdr-dac-rgb.h

  • Committer: Selene ToyKeeper
  • Date: 2023-11-02 17:05:02 UTC
  • mfrom: (483.12.159 multi-channel)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20231102170502-sinkm18qjxlorsxa
merged multi-channel branch with a major refactor and half a year of updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// hwdef for thefreeman's boost FWAA driver 1.1 w/ MP3432, HDR DAC, RGB aux
 
2
// Copyright (C) 2023 TBD (thefreeman), Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
/* thefreeman’s FWAA AA/li-ion Boost driver based on MP3432 and attiny1616
 
7
 * with high dynamic range and DAC control, AUX : RGB
 
8
 * hardware versions : 1.0, 1.1
 
9
 *
 
10
 * Pin / Name / Function
 
11
 *   1    PA2   BattLVL (ADC0 - AIN2)
 
12
 *   2    PA3   
 
13
 *   3    GND   GND
 
14
 *   4    VCC   VCC
 
15
 *   5    PA4   BST EN: boost enable
 
16
 *   6    PA5   HDR
 
17
 *   7    PA6   DAC
 
18
 *   8    PA7   
 
19
 *   9    PB5   B: blue aux LED
 
20
 *  10    PB4   G: green aux LED
 
21
 *  11    PB3   R: red aux LED
 
22
 *  12    PB2   IN- NFET
 
23
 *  13    PB1   
 
24
 *  14    PB0   
 
25
 *  15    PC0   
 
26
 *  16    PC1   
 
27
 *  17    PC2   
 
28
 *  18    PC3   e-switch
 
29
 *  19    PA0   UDPI
 
30
 *  20    PA1   
 
31
 *
 
32
 * BST EN enable the boost regulator and Op-Amp
 
33
 * DAC sets the current, max current depends on Vset voltage divider and Rsense
 
34
 * HDR FET switches between high value Rsense (low current range, pin low),
 
35
 *         and low value Rsense (high current range, pin high)
 
36
 * IN- NFET : pull up after BST enable to eliminate startup flash, pull down otherwise
 
37
 */
 
38
 
 
39
#define ATTINY 1616
 
40
#include <avr/io.h>
 
41
 
 
42
#define HWDEF_C_FILE hwdef-thefreeman-boost21-mp3431-hdr-dac-argb.c
 
43
 
 
44
// allow using aux LEDs as extra channel modes
 
45
#include "chan-rgbaux.h"
 
46
 
 
47
// channel modes:
 
48
// * 0. main LEDs
 
49
// * 1+. aux RGB
 
50
#define NUM_CHANNEL_MODES   (1 + NUM_RGB_AUX_CHANNEL_MODES)
 
51
enum CHANNEL_MODES {
 
52
    CM_MAIN = 0,
 
53
    RGB_AUX_ENUMS
 
54
};
 
55
 
 
56
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
57
 
 
58
// right-most bit first, modes are in fedcba9876543210 order
 
59
#define CHANNEL_MODES_ENABLED 0b0000000000000001
 
60
 
 
61
 
 
62
#define PWM_CHANNELS  1  // old, remove this
 
63
 
 
64
#define PWM_BITS      8         // 8-bit DAC
 
65
#define PWM_GET       PWM_GET8
 
66
#define PWM_DATATYPE  uint8_t
 
67
#define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
 
68
#define PWM1_DATATYPE uint8_t   // main LED ramp
 
69
 
 
70
// main LED outputs
 
71
#define DAC_LVL   DAC0.DATA    // 0 to 255, for 0V to Vref
 
72
#define DAC_VREF  VREF.CTRLA   // 0.55V or 2.5V
 
73
#define PWM_TOP_INIT  255      // highest value used in top half of ramp (unused?)
 
74
// Vref values
 
75
#define V055  16
 
76
#define V11   17
 
77
#define V25   18
 
78
#define V43   19
 
79
#define V15   20
 
80
 
 
81
// BST enable
 
82
#define BST_ENABLE_PIN   PIN4_bp
 
83
#define BST_ENABLE_PORT  PORTA_OUT
 
84
 
 
85
// HDR
 
86
// turns on HDR FET for the high current range
 
87
#define HDR_ENABLE_PIN   PIN5_bp
 
88
#define HDR_ENABLE_PORT  PORTA_OUT
 
89
 
 
90
// IN- NFET
 
91
// pull high to force output to zero to eliminate the startup flash
 
92
#define IN_NFET_DELAY_TIME   4  // (ms)
 
93
#define IN_NFET_ENABLE_PIN   PIN2_bp
 
94
#define IN_NFET_ENABLE_PORT  PORTB_OUT
 
95
 
 
96
// e-switch
 
97
#ifndef SWITCH_PIN
 
98
#define SWITCH_PIN      PIN3_bp
 
99
#define SWITCH_PORT     VPORTC.IN
 
100
#define SWITCH_ISC_REG  PORTC.PIN3CTRL
 
101
#define SWITCH_VECT     PORTC_PORT_vect
 
102
#define SWITCH_INTFLG   VPORTC.INTFLAGS
 
103
#define SWITCH_PCINT    PCINT0
 
104
#define PCINT_vect      PCINT0_vect
 
105
#endif
 
106
 
 
107
// Voltage divider battLVL
 
108
#define USE_VOLTAGE_DIVIDER       // use a dedicated pin, not VCC, because VCC input is regulated
 
109
#define DUAL_VOLTAGE_FLOOR     21 // for AA/14500 boost drivers, don't indicate low voltage if below this level
 
110
#define DUAL_VOLTAGE_LOW_LOW   7  // the lower voltage range's danger zone 0.7 volts (NiMH)
 
111
#define ADMUX_VOLTAGE_DIVIDER  ADC_MUXPOS_AIN2_gc  // which ADC channel to read
 
112
 
 
113
// Raw ADC readings at 4.4V and 2.2V
 
114
// calibrate the voltage readout here
 
115
// estimated / calculated values are:
 
116
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
 
117
// Resistors are 330k and 100k
 
118
#ifndef ADC_44
 
119
#define ADC_44 951  // raw value at 4.40V
 
120
#endif
 
121
#ifndef ADC_22
 
122
#define ADC_22 476  // raw value at 2.20V
 
123
#endif
 
124
 
 
125
// this driver allows for aux LEDs under the optic
 
126
#define AUXLED_R_PIN  PIN3_bp
 
127
#define AUXLED_G_PIN  PIN4_bp
 
128
#define AUXLED_B_PIN  PIN5_bp
 
129
#define AUXLED_RGB_PORT PORTB  // PORTA or PORTB or PORTC
 
130
 
 
131
// this light has three aux LED channels: R, G, B
 
132
#define USE_AUX_RGB_LEDS
 
133
 
 
134
 
 
135
inline void hwdef_setup() {
 
136
 
 
137
    // TODO: for this DAC controlled-light, try to decrease the clock speed or use the ULP
 
138
    // set up the system clock to run at 10 MHz to match other attiny1616 lights
 
139
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
140
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
 
141
 
 
142
    VPORTA.DIR = PIN4_bm  // BST EN
 
143
               | PIN5_bm  // HDR
 
144
               | PIN6_bm; // DAC
 
145
    VPORTB.DIR = PIN2_bm  // IN- NFET
 
146
               | PIN3_bm  // R
 
147
               | PIN4_bm  // G
 
148
               | PIN5_bm; // B
 
149
    //VPORTC.DIR = PIN0_bm | PIN1_bm;
 
150
 
 
151
    // enable pullups on the input pins to reduce power
 
152
    PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
 
153
    PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
 
154
    //PORTA.PIN2CTRL = PORT_PULLUPEN_bm;  // BattLVL
 
155
    PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
 
156
    //PORTA.PIN4CTRL = PORT_PULLUPEN_bm;  // EN
 
157
    //PORTA.PIN5CTRL = PORT_PULLUPEN_bm;  // HDR
 
158
    //PORTA.PIN6CTRL = PORT_PULLUPEN_bm;  // DAC
 
159
    PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
 
160
 
 
161
    PORTB.PIN0CTRL = PORT_PULLUPEN_bm;
 
162
    PORTB.PIN1CTRL = PORT_PULLUPEN_bm;
 
163
    //PORTB.PIN2CTRL = PORT_PULLUPEN_bm;  // IN- NFET
 
164
    //PORTB.PIN3CTRL = PORT_PULLUPEN_bm;  // R
 
165
    //PORTB.PIN4CTRL = PORT_PULLUPEN_bm;  // G
 
166
    //PORTB.PIN5CTRL = PORT_PULLUPEN_bm;  // B
 
167
 
 
168
    PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
 
169
    PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
 
170
    PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
 
171
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm
 
172
                   | PORT_ISC_BOTHEDGES_gc;  // e-switch
 
173
 
 
174
    // set up the DAC
 
175
    // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
 
176
    // DAC ranges from 0V to (255 * Vref) / 256
 
177
    // also VREF_DAC0REFSEL_0V55_gc and VREF_DAC0REFSEL_1V1_gc and VREF_DAC0REFSEL_2V5_gc
 
178
    VREF.CTRLA |= VREF_DAC0REFSEL_2V5_gc;
 
179
    VREF.CTRLB |= VREF_DAC0REFEN_bm;
 
180
    DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm;
 
181
    DAC0.DATA = 255; // set the output voltage
 
182
 
 
183
}
 
184
 
 
185
 
 
186
#define LAYOUT_DEFINED
 
187