From 17027a533661d14082f6566848bfb9cd122d03c6 Mon Sep 17 00:00:00 2001 From: Gabriel Hart Date: Fri, 29 Oct 2021 12:32:30 -0500 Subject: Add thefreeman's DAC linear driver (beta); also update Sofirn bilds to use the new 2C_STYLE defines --- hwdef-thefreeman-lin16dac.h | 109 +++++++++++++++++++++ spaghetti-monster/anduril/cfg-blf-lantern.h | 4 +- spaghetti-monster/anduril/cfg-blf-q8-t1616.h | 4 +- spaghetti-monster/anduril/cfg-blf-q8.h | 4 +- .../anduril/cfg-thefreeman-lin16dac.h | 45 +++++++++ spaghetti-monster/anduril/version.h | 2 +- 6 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 hwdef-thefreeman-lin16dac.h create mode 100644 spaghetti-monster/anduril/cfg-thefreeman-lin16dac.h diff --git a/hwdef-thefreeman-lin16dac.h b/hwdef-thefreeman-lin16dac.h new file mode 100644 index 0000000..0999c4c --- /dev/null +++ b/hwdef-thefreeman-lin16dac.h @@ -0,0 +1,109 @@ +#ifndef HWDEF_THEFREEMAN_LIN18_H +#define HWDEF_THEFREEMAN_LIN18_H + +/* thefreeman's Linear 16 driver using DAC control + +PA6 - DAC for LED brightness control +PA7 - Op-amp enable pin +PB5 - Aux LED +PB4 - Switch pin, internal pullup +PB3 - HDR control, set High to enable the high power channel, set Low for low power +Read voltage from VCC pin, has PFET so no drop + +*/ + + +#define LAYOUT_DEFINED + +#ifdef ATTINY +#undef ATTINY +#endif +#define ATTINY 1616 +#include + +#define PWM_CHANNELS 1 +#define USE_DYN_PWM // dynamic frequency and speed + +#ifndef SWITCH_PIN +#define SWITCH_PIN PIN4_bp +#define SWITCH_PORT VPORTB.IN +#define SWITCH_ISC_REG PORTB.PIN2CTRL +#define SWITCH_VECT PORTB_PORT_vect +#define SWITCH_INTFLG VPORTB.INTFLAGS +#endif + + +#define PWM1_LVL DAC0.DATA // use this for DAC voltage output + +// PWM parameters of both channels are tied together because they share a counter +#define PWM1_TOP VREF.CTRLA // holds the TOP value for for variable-resolution PWM + +// For enabling / disabling the HDR high-range channel +#define LED_ENABLE_PIN PIN3_bp +#define LED_ENABLE_PORT PORTB_OUT +#define LED_ENABLE_PIN_LEVEL_MIN 35 +#define LED_ENABLE_PIN_LEVEL_MAX 150 + +// For turning on and off the op-amp +#define LED2_ENABLE_PIN PIN7_bp +#define LED2_ENABLE_PORT PORTA_OUT + + +// average drop across diode on this hardware +#ifndef VOLTAGE_FUDGE_FACTOR +#define VOLTAGE_FUDGE_FACTOR 0 // using a PFET so no appreciable drop +#endif + +// lighted button +#ifndef AUXLED_PIN +#define AUXLED_PIN PIN5_bp +#define AUXLED_PORT PORTB +#endif + + +// with so many pins, doing this all with #ifdefs gets awkward... +// ... so just hardcode it in each hwdef file instead +inline void hwdef_setup() { + + // set up the system clock to run at 10 MHz instead of the default 3.33 MHz + // TODO: for this DAC controlled-light, try to decrease the clock speed or use the ULP + _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm ); + + VPORTA.DIR = PIN6_bm | PIN7_bm; + VPORTB.DIR = PIN3_bm; + //VPORTC.DIR = 0b00000000; + + // enable pullups on the input pins to reduce power + PORTA.PIN0CTRL = PORT_PULLUPEN_bm; + PORTA.PIN1CTRL = PORT_PULLUPEN_bm; + PORTA.PIN2CTRL = PORT_PULLUPEN_bm; + PORTA.PIN3CTRL = PORT_PULLUPEN_bm; + PORTA.PIN4CTRL = PORT_PULLUPEN_bm; + PORTA.PIN5CTRL = PORT_PULLUPEN_bm; + //PORTA.PIN6CTRL = PORT_PULLUPEN_bm; // DAC ouput + //PORTA.PIN7CTRL = PORT_PULLUPEN_bm; // Op-amp enable pin + + PORTB.PIN0CTRL = PORT_PULLUPEN_bm; + PORTB.PIN1CTRL = PORT_PULLUPEN_bm; + PORTB.PIN2CTRL = PORT_PULLUPEN_bm; + //PORTB.PIN3CTRL = PORT_PULLUPEN_bm; // HDR channel selection + PORTB.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // switch + //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED + + PORTC.PIN0CTRL = PORT_PULLUPEN_bm; + PORTC.PIN1CTRL = PORT_PULLUPEN_bm; + PORTC.PIN2CTRL = PORT_PULLUPEN_bm; + PORTC.PIN3CTRL = PORT_PULLUPEN_bm; + + // set up the DAC + // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf + // DAC ranges from 0V to (255 * Vref) / 256 + VREF.CTRLA |= VREF_DAC0REFSEL_2V5_gc; // also VREF_DAC0REFSEL_0V55_gc and VREF_DAC0REFSEL_1V1_gc and VREF_DAC0REFSEL_2V5_gc + VREF.CTRLB |= VREF_DAC0REFEN_bm; + DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm; + DAC0.DATA = 255; // set the output voltage + +} + + +#endif diff --git a/spaghetti-monster/anduril/cfg-blf-lantern.h b/spaghetti-monster/anduril/cfg-blf-lantern.h index ff28a98..a989ca4 100644 --- a/spaghetti-monster/anduril/cfg-blf-lantern.h +++ b/spaghetti-monster/anduril/cfg-blf-lantern.h @@ -53,8 +53,8 @@ #define SIMPLE_UI_CEIL RAMP_DISCRETE_CEIL #define SIMPLE_UI_STEPS RAMP_DISCRETE_STEPS -// also at Sofirn's request, enable 2 click turbo -#define USE_2C_MAX_TURBO +// also at Sofirn's request, enable 2 click turbo (Anduril 1 style) +#define DEFAULT_2C_STYLE 1 #define USE_SOS_MODE #define USE_SOS_MODE_IN_BLINKY_GROUP diff --git a/spaghetti-monster/anduril/cfg-blf-q8-t1616.h b/spaghetti-monster/anduril/cfg-blf-q8-t1616.h index 340faa1..30a3368 100644 --- a/spaghetti-monster/anduril/cfg-blf-q8-t1616.h +++ b/spaghetti-monster/anduril/cfg-blf-q8-t1616.h @@ -34,8 +34,8 @@ #define SIMPLE_UI_CEIL 150 #define SIMPLE_UI_STEPS 5 -// also at Sofirn's request, enable 2 click turbo -#define USE_2C_MAX_TURBO +// also at Sofirn's request, enable 2 click turbo (Anduril 1 style) +#define DEFAULT_2C_STYLE 1 // enable SOS in the blinkies group #define USE_SOS_MODE diff --git a/spaghetti-monster/anduril/cfg-blf-q8.h b/spaghetti-monster/anduril/cfg-blf-q8.h index 00bdd8a..6a5055c 100644 --- a/spaghetti-monster/anduril/cfg-blf-q8.h +++ b/spaghetti-monster/anduril/cfg-blf-q8.h @@ -33,8 +33,8 @@ #define SIMPLE_UI_CEIL 150 #define SIMPLE_UI_STEPS 5 -// also at Sofirn's request, enable 2 click turbo -#define USE_2C_MAX_TURBO +// also at Sofirn's request, enable 2 click turbo (Anduril 1 style) +#define DEFAULT_2C_STYLE 1 // enable SOS in the blinkies group #define USE_SOS_MODE diff --git a/spaghetti-monster/anduril/cfg-thefreeman-lin16dac.h b/spaghetti-monster/anduril/cfg-thefreeman-lin16dac.h new file mode 100644 index 0000000..64dcd8c --- /dev/null +++ b/spaghetti-monster/anduril/cfg-thefreeman-lin16dac.h @@ -0,0 +1,45 @@ +// thefreeman's Linear 16 driver using DAC control +#define MODEL_NUMBER "0000" // TBD +#include "hwdef-thefreeman-lin16dac.h" +// ATTINY: 1616 + +// the button lights up +#define USE_INDICATOR_LED +// the button is visible while main LEDs are on +#define USE_INDICATOR_LED_WHILE_RAMPING +// off mode: low (1) +// lockout: blinking (3) +#define INDICATOR_LED_DEFAULT_MODE ((3<<2) + 1) + +#undef BLINK_AT_RAMP_MIDDLE + +// We're abusing the Dynamic PWM functionality to set the VREF instead of PWM TOP. +// We don't want the Gradual functionality to mess with the PWM_TOP value. +#ifdef USE_SET_LEVEL_GRADUALLY +#undef USE_SET_LEVEL_GRADUALLY +#endif + +// level_calc.py ninth 2 150 7135 1 0.03 6.4 7135 1 6.3 1600 +#define RAMP_LENGTH 150 +#define USE_DYN_PWM + +// PWM1: DAC Data, PWM Tops: VREF selector +#define PWM1_LEVELS 25,25,33,41,41,50,58,66,75,83,92,108,117,133,150,167,192,209,234,58,64,71,80,90,99,110,121,134,149,163,180,198,218,241,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,6,6,7,7,8,8,9,10,11,11,12,13,14,15,16,18,19,20,22,23,25,26,28,30,32,34,36,39,41,44,47,50,53,56,59,63,67,71,75,79,84,89,94,100,105,112,118,124,131,139,146,154,163,172,181,191,201,212,223,234,246,57,60,63,66,69,73,76,80,84,88,93,97,102,107,112,117,123,129,135,141,147,154,161,169,176,184,193,201,210,220,229,239,250,255 +#define PWM_TOPS 16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18 + +#define MAX_1x7135 34 +#define HALFSPEED_LEVEL 14 +#define QUARTERSPEED_LEVEL 6 + +#define RAMP_SMOOTH_FLOOR 1 +#define RAMP_SMOOTH_CEIL 120 +// 10, 28, 46, [65], 83, 101, 120 +#define RAMP_DISCRETE_FLOOR 10 +#define RAMP_DISCRETE_CEIL RAMP_SMOOTH_CEIL +#define RAMP_DISCRETE_STEPS 7 + +// stop panicking at ~30% power +#define THERM_FASTER_LEVEL 123 + +// enable 2 click turbo +#define DEFAULT_2C_STYLE 1 diff --git a/spaghetti-monster/anduril/version.h b/spaghetti-monster/anduril/version.h index 0da142b..7560ef2 100644 --- a/spaghetti-monster/anduril/version.h +++ b/spaghetti-monster/anduril/version.h @@ -1 +1 @@ -#define VERSION_NUMBER "20210920" +#define VERSION_NUMBER "20211029" -- cgit v1.2.3