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
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
10
* Pin / Name / Function
11
* 1 PA2 BattLVL (ADC0 - AIN2)
15
* 5 PA4 BST EN: boost enable
19
* 9 PB5 B: blue aux LED
20
* 10 PB4 G: green aux LED
21
* 11 PB3 R: red aux LED
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
42
#define HWDEF_C_FILE hwdef-thefreeman-boost21-mp3431-hdr-dac-argb.c
44
// allow using aux LEDs as extra channel modes
45
#include "chan-rgbaux.h"
50
#define NUM_CHANNEL_MODES (1 + NUM_RGB_AUX_CHANNEL_MODES)
56
#define DEFAULT_CHANNEL_MODE CM_MAIN
58
// right-most bit first, modes are in fedcba9876543210 order
59
#define CHANNEL_MODES_ENABLED 0b0000000000000001
62
#define PWM_CHANNELS 1 // old, remove this
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
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?)
82
#define BST_ENABLE_PIN PIN4_bp
83
#define BST_ENABLE_PORT PORTA_OUT
86
// turns on HDR FET for the high current range
87
#define HDR_ENABLE_PIN PIN5_bp
88
#define HDR_ENABLE_PORT PORTA_OUT
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
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
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
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
119
#define ADC_44 951 // raw value at 4.40V
122
#define ADC_22 476 // raw value at 2.20V
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
131
// this light has three aux LED channels: R, G, B
132
#define USE_AUX_RGB_LEDS
135
inline void hwdef_setup() {
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 );
142
VPORTA.DIR = PIN4_bm // BST EN
145
VPORTB.DIR = PIN2_bm // IN- NFET
149
//VPORTC.DIR = PIN0_bm | PIN1_bm;
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;
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
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
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
186
#define LAYOUT_DEFINED