diff options
Diffstat (limited to '')
| -rw-r--r-- | hw/wurkkos/ts26/anduril.h | 102 | ||||
| -rw-r--r-- | hw/wurkkos/ts26/hwdef.c | 101 | ||||
| -rw-r--r-- | hw/wurkkos/ts26/hwdef.h | 174 | ||||
| -rw-r--r-- | hw/wurkkos/ts26/model | 1 |
4 files changed, 378 insertions, 0 deletions
diff --git a/hw/wurkkos/ts26/anduril.h b/hw/wurkkos/ts26/anduril.h new file mode 100644 index 0000000..6e77269 --- /dev/null +++ b/hw/wurkkos/ts26/anduril.h @@ -0,0 +1,102 @@ +// Wurkkos TS26 +// Copyright (C) 2023 Selene ToyKeeper +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + +#define HWDEF_H wurkkos/ts26/hwdef.h +#include "wurkkos/anduril.h" + +// this light has three aux LED channels: R, G, B +#define USE_AUX_RGB_LEDS + +// don't turn on the aux LEDs while main LEDs are on +#ifdef USE_INDICATOR_LED_WHILE_RAMPING +#undef USE_INDICATOR_LED_WHILE_RAMPING +#endif +// (nope, 20250210 prototype has front aux instead of RGB button) +// show real-time battery status on RGB button +// when main LEDs are bright enough to overpower the front aux +//#define USE_INDICATOR_LED_WHILE_RAMPING +//#define USE_AUX_RGB_LEDS_WHILE_ON 20 + + +#define RAMP_SIZE 150 + +// delta-sigma modulated PWM (0b0HHHHHHHHLLLLLLL = 0, 8xHigh, 7xLow bits) +// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM) + +// full power, lowest floor +// level_calc.py 5.01 1 150 7135 0 0.2 2000 --pwm 32640 +//#define PWM1_LEVELS 0,1,2,3,4,5,6,7,9,10,12,14,17,19,22,25,28,32,36,41,45,50,56,62,69,76,84,92,101,110,121,132,143,156,169,184,199,215,232,251,270,291,313,336,360,386,414,442,473,505,539,574,612,651,693,736,782,829,880,932,987,1045,1105,1168,1233,1302,1374,1449,1527,1608,1693,1781,1873,1969,2068,2172,2279,2391,2507,2628,2753,2883,3018,3158,3303,3454,3609,3771,3938,4111,4289,4475,4666,4864,5068,5280,5498,5724,5957,6197,6445,6701,6965,7237,7518,7808,8106,8413,8730,9056,9392,9737,10093,10459,10835,11223,11621,12031,12452,12884,13329,13786,14255,14737,15232,15741,16262,16798,17347,17911,18489,19082,19691,20314,20954,21609,22281,22969,23674,24397,25137,25895,26671,27465,28279,29111,29963,30835,31727,32640 + +// full power but higher floor, because the driver can't go low +// level_calc.py 5.01 1 150 7135 20 2 2000 --pwm 32640 +#define PWM1_LEVELS 20,24,27,31,36,40,45,51,56,62,69,76,83,91,100,109,119,129,140,151,163,176,190,204,219,236,252,270,289,309,330,352,375,399,424,451,479,508,538,570,604,639,675,714,754,795,839,884,931,980,1031,1084,1140,1197,1257,1320,1384,1451,1521,1593,1668,1746,1827,1911,1997,2087,2180,2276,2376,2479,2585,2695,2809,2927,3048,3174,3303,3437,3575,3718,3865,4016,4172,4333,4499,4670,4846,5028,5215,5407,5605,5809,6018,6234,6455,6683,6917,7158,7406,7660,7921,8189,8465,8748,9038,9336,9642,9955,10277,10607,10946,11293,11649,12014,12387,12770,13163,13565,13977,14399,14830,15273,15725,16189,16663,17148,17645,18152,18672,19203,19747,20302,20870,21451,22045,22651,23271,23904,24552,25213,25888,26577,27282,28001,28735,29484,30249,31030,31827,32640 + +// limit to 3/4ths power +// level_calc.py 5.01 1 150 7135 0 0.2 1500 --pwm 24480 +//#define PWM1_LEVELS 0,1,2,3,4,5,6,7,8,9,11,13,15,17,19,22,25,28,31,35,39,43,48,53,59,64,71,77,85,93,101,110,119,129,140,152,164,177,191,205,221,237,255,273,292,313,335,357,381,407,433,461,490,521,553,587,623,660,699,740,783,827,874,922,973,1026,1082,1139,1199,1262,1327,1395,1466,1539,1615,1695,1777,1863,1951,2044,2139,2239,2341,2448,2558,2673,2791,2914,3041,3172,3307,3448,3593,3743,3898,4057,4223,4393,4569,4750,4938,5131,5330,5535,5747,5964,6189,6420,6658,6903,7155,7415,7682,7956,8239,8529,8828,9134,9450,9774,10106,10448,10799,11159,11529,11909,12299,12698,13108,13529,13960,14402,14856,15320,15797,16285,16785,17297,17822,18359,18909,19472,20049,20639,21244,21862,22494,23141,23803,24480 + +// limit to 2/3rds power +// level_calc.py 5.01 1 150 7135 0 0.2 1333 --pwm 21760 +//#define PWM1_LEVELS 0,1,2,3,4,5,6,7,8,9,10,12,14,16,18,21,24,26,30,33,37,41,45,50,55,60,66,72,79,86,94,102,111,120,130,140,152,164,176,189,204,219,234,251,269,287,307,328,349,372,396,422,448,476,505,536,568,602,637,674,712,752,794,838,884,932,981,1033,1087,1144,1202,1263,1326,1392,1461,1532,1606,1683,1762,1845,1930,2019,2111,2207,2305,2408,2513,2623,2736,2854,2975,3100,3230,3363,3502,3644,3791,3943,4100,4262,4429,4601,4778,4961,5149,5343,5543,5749,5961,6179,6403,6634,6871,7115,7366,7624,7890,8162,8442,8730,9025,9329,9640,9960,10288,10625,10971,11325,11689,12062,12444,12837,13238,13650,14072,14505,14948,15402,15866,16342,16830,17328,17839,18361,18896,19443,20003,20576,21161,21760 + +// half power +// level_calc.py 5.01 1 150 7135 0 0.2 1000 --pwm 16320 +//#define PWM1_LEVELS 0,1,2,3,4,5,6,7,8,9,10,11,12,14,16,18,21,23,26,29,32,35,39,43,47,51,56,61,67,73,79,85,93,100,108,117,126,135,145,156,167,179,192,205,219,234,249,266,283,301,320,340,361,382,405,429,454,480,508,537,566,598,630,664,700,737,775,815,857,900,945,992,1041,1092,1144,1199,1255,1314,1375,1438,1504,1571,1642,1714,1789,1867,1948,2031,2117,2206,2298,2393,2492,2593,2698,2806,2917,3032,3150,3272,3398,3528,3662,3800,3941,4088,4238,4393,4552,4716,4884,5057,5236,5419,5607,5800,5999,6203,6413,6628,6849,7076,7309,7548,7793,8044,8302,8567,8838,9116,9401,9693,9993,10299,10613,10935,11265,11602,11947,12301,12663,13033,13412,13800,14197,14603,15018,15442,15876,16320 + +#define MIN_THERM_STEPDOWN 50 +#define DEFAULT_LEVEL 70 +#define MAX_1x7135 150 +// moon at 1/4th speed for max runtime +// the rest at full speed, to reduce PWM +// (speed changes make a big visible bump because PWM) +#define HALFSPEED_LEVEL 1 +#define QUARTERSPEED_LEVEL 1 + +#define RAMP_SMOOTH_FLOOR 5 +#define RAMP_SMOOTH_CEIL 150 +// 10, 30, 50, [70], 90, 110, 130 +#define RAMP_DISCRETE_FLOOR 10 +#define RAMP_DISCRETE_CEIL 130 +#define RAMP_DISCRETE_STEPS 7 + +// at Wurkkos's request, reduce the Simple UI ceiling a little bit +// 25 50 [75] 100 125 +#define SIMPLE_UI_FLOOR 25 +#define SIMPLE_UI_CEIL 125 +#define SIMPLE_UI_STEPS 5 + +// stop panicking at ~50% power +#define THERM_FASTER_LEVEL 120 // throttle back faster when high + +#define BLINK_BRIGHTNESS 30 +#define BLINK_ONCE_TIME 12 + +// show each channel while it scroll by in the menu +#define USE_CONFIG_COLORS + +// blink numbers on the main LEDs by default +// (so battcheck will be visible while charging) +#define DEFAULT_BLINK_CHANNEL CM_MAIN +// blink numbers on the aux LEDs by default +//#define DEFAULT_BLINK_CHANNEL CM_AUXWHT + +// slow down party strobe; this driver can't pulse for less than 2ms +#define PARTY_STROBE_ONTIME 2 +//#define STROBE_OFF_LEVEL 1 // keep the regulator chip on between pulses + +// use aux red + aux blue for police strobe +#define USE_POLICE_COLOR_STROBE_MODE +#define POLICE_STROBE_USES_AUX +#define POLICE_COLOR_STROBE_CH1 CM_AUXRED +#define POLICE_COLOR_STROBE_CH2 CM_AUXBLU + +// the default of 26 looks a bit rough, so increase it to make it smoother +#define CANDLE_AMPLITUDE 33 + +// don't blink mid-ramp +#ifdef BLINK_AT_RAMP_MIDDLE +#undef BLINK_AT_RAMP_MIDDLE +#endif + diff --git a/hw/wurkkos/ts26/hwdef.c b/hw/wurkkos/ts26/hwdef.c new file mode 100644 index 0000000..e6c77a2 --- /dev/null +++ b/hw/wurkkos/ts26/hwdef.c @@ -0,0 +1,101 @@ +// Wurkkos TS26 helper functions +// Copyright (C) 2023 Selene ToyKeeper +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + +#include "fsm/chan-rgbaux.c" + + +void set_level_zero(); + +void set_level_main(uint8_t level); +bool gradual_tick_main(uint8_t gt); + + +Channel channels[] = { + { // channel 1 only + .set_level = set_level_main, + .gradual_tick = gradual_tick_main + }, + RGB_AUX_CHANNELS +}; + + +void set_level_zero() { + // disable timer overflow interrupt + // (helps improve button press handling from Off state) + DSM_INTCTRL = 0; + + // turn off all LEDs + ch1_dsm_lvl = 0; + CH1_PWM = 0; + PWM_CNT = 0; // reset phase + CH1_ENABLE_PORT &= ~(1 << CH1_ENABLE_PIN); // disable regulator +} + +// single set of LEDs with single power channel, boost +void set_level_main(uint8_t level) { + if (level == actual_level - 1) return; // prevent flicker on no-op + + PWM_DATATYPE ch1 = PWM_GET(pwm1_levels, level); + + // set delta-sigma soft levels + ch1_dsm_lvl = ch1; + + // set hardware PWM levels and init dsm loop + CH1_PWM = ch1_pwm = ch1 >> 7; + + // enable timer overflow interrupt so DSM can work + DSM_INTCTRL = DSM_OVF_bm; + + // force reset phase when turning on from zero + // (because otherwise the initial response is inconsistent) + if (! actual_level) PWM_CNT = 0; + + CH1_ENABLE_PORT |= (1 << CH1_ENABLE_PIN); // enable regulator +} + +// delta-sigma modulation of PWM outputs +// happens on each Timer overflow (every 512 cpu clock cycles) +// uses 8-bit pwm w/ 7-bit dsm (0b 0PPP PPPP PDDD DDDD) +ISR(DSM_vect) { + // set new hardware values first, + // for best timing (reduce effect of interrupt jitter) + CH1_PWM = ch1_pwm; + + // calculate next values, now that timing matters less + + // accumulate error + ch1_dsm += (ch1_dsm_lvl & 0x007f); + // next PWM = base PWM value + carry bit + ch1_pwm = (ch1_dsm_lvl >> 7) + (ch1_dsm > 0x7f); + // clear carry bit + ch1_dsm &= 0x7f; + + // clear the interrupt flag to indicate it was handled + // as per: https://onlinedocs.microchip.com/pr/GUID-C37FFBA8-82C6-4339-A2B1-ABD9A0F6C162-en-US-8/index.html?GUID-C2A2BEFD-158F-413D-B9D4-0F0556AA79BD + DSM_INTFLAGS = DSM_OVF_bm; +} + + +bool gradual_tick_main(uint8_t gt) { + PWM_DATATYPE ch1 = PWM_GET(pwm1_levels, gt); + + // adjust multiple times based on current brightness + // (so it adjusts faster/coarser when bright, slower/finer when dim) + + // higher shift = slower/finer adjustments + const uint8_t shift = 9; // ((255 << 7) >> 9) = 63 max + uint8_t steps; + + steps = ch1_dsm_lvl >> shift; + for (uint8_t i=0; i<=steps; i++) + GRADUAL_ADJUST_SIMPLE(ch1, ch1_dsm_lvl); + + if ((ch1 == ch1_dsm_lvl) + ) { + return true; // done + } + return false; // not done yet +} + diff --git a/hw/wurkkos/ts26/hwdef.h b/hw/wurkkos/ts26/hwdef.h new file mode 100644 index 0000000..391994e --- /dev/null +++ b/hw/wurkkos/ts26/hwdef.h @@ -0,0 +1,174 @@ +// Wurkkos TS26 driver layout +// Copyright (C) 2023 Selene ToyKeeper +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + +/* Boost driver based on MP3432 and attiny1616 + * with high dynamic range and DAC control, AUX : RGB + button + * hardware version : 2.0+ + * compatible with BST20-FWxA v1.0 (no button LED A) + * + * Pin / Name / Function + * 1 PA2 - + * 2 PA3 - + * 3 GND GND + * 4 VCC VCC + * 5 PA4 - + * 6 PA5 e-switch + * 7 PA6 - + * 8 PA7 - + * 9 PB5 - + * 10 PB4 - + * 11 PB3 - + * 12 PB2 EN: boost enable + * 13 PB1 - + * 14 PB0 PWM + * 15 PC0 - + * 16 PC1 B: aux blue + * 17 PC2 G: aux green + * 18 PC3 R: aux red + * 19 PA0 UDPI + * 20 PA1 Vbat sense (voltage divider) + * + */ + +#define HWDEF_C wurkkos/ts26/hwdef.c + +// allow using aux LEDs as extra channel modes +#include "fsm/chan-rgbaux.h" + +// channel modes: +// * 0. main LEDs +// * 1+. aux RGB +#define NUM_CHANNEL_MODES (1 + NUM_RGB_AUX_CHANNEL_MODES) +enum CHANNEL_MODES { + CM_MAIN = 0, + RGB_AUX_ENUMS +}; + +#define DEFAULT_CHANNEL_MODE CM_MAIN + +// right-most bit first, modes are in fedcba9876543210 order +#define CHANNEL_MODES_ENABLED 0b0000000000000001 + + +#define PWM_BITS 16 // 0 to 32640 (0 to 255 PWM + 0 to 127 DSM) at constant kHz +#define PWM_GET PWM_GET16 +#define PWM_DATATYPE uint16_t +#define PWM_DATATYPE2 uint32_t // only needs 32-bit if ramp values go over 255 +#define PWM1_DATATYPE uint16_t // 15-bit PWM+DSM ramp + +#define PWM_TOP TCA0.SINGLE.PERBUF // holds the TOP value for for variable-resolution PWM +#define PWM_TOP_INIT 255 +#define PWM_CNT TCA0.SINGLE.CNT // for checking / resetting phase +// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM) +#define DSM_TOP (255<<7) // 15-bit resolution leaves 1 bit for carry + +// timer interrupt for DSM +#define DSM_vect TCA0_OVF_vect +#define DSM_INTCTRL TCA0.SINGLE.INTCTRL +#define DSM_INTFLAGS TCA0.SINGLE.INTFLAGS +#define DSM_OVF_bm TCA_SINGLE_OVF_bm + +#define DELAY_FACTOR 90 // less time in delay() because more time spent in interrupts + +// regulated channel +uint16_t ch1_dsm_lvl; +uint8_t ch1_pwm, ch1_dsm; +#define CH1_PIN PB0 +#define CH1_PWM TCA0.SINGLE.CMP0BUF // CMP0 is the output compare register for PB0 + +#define CH1_ENABLE_PIN PIN2_bp +#define CH1_ENABLE_PORT PORTB_OUT + +// e-switch +#define SWITCH_PIN PIN5_bp +#define SWITCH_PORT VPORTA.IN +#define SWITCH_ISC_REG PORTA.PIN2CTRL +#define SWITCH_VECT PORTA_PORT_vect +#define SWITCH_INTFLG VPORTA.INTFLAGS + +// Voltage divider battLVL +#define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is regulated +#define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN1_gc // which ADC channel to read +// don't use the default VDD converter +#undef voltage_raw2cooked +#define voltage_raw2cooked mcu_vdivider_raw2cooked + +// Raw ADC readings at 4.4V and 2.2V +// calibrate the voltage readout here +// estimated / calculated values are: +// (voltage - D1) * (R2/(R2+R1) * 4096 / 1.1) +// Resistors are 330k and 100k +#define ADC_44 4095 // raw value at 4.40V +#define ADC_22 2048 // raw value at 2.20V + +// this light has aux LEDs under the optic +#define AUXLED_R_PIN PIN3_bp // pin 3 +#define AUXLED_G_PIN PIN2_bp // pin 2 +#define AUXLED_B_PIN PIN1_bp // pin 1 +#define AUXLED_RGB_PORT PORTC // PORTA or PORTB or PORTC + +// this light has three aux LED channels: R, G, B +#define USE_AUX_RGB_LEDS + + +inline void hwdef_setup() { + + mcu_clock_speed(); + + // Outputs: + //VPORTA.DIR = ...; + VPORTB.DIR = PIN0_bm // PWM + | PIN2_bm; // boost enable + VPORTC.DIR = PIN1_bm // aux B + | PIN2_bm // aux G + | PIN3_bm; // aux R + + // enable pullups on the unused pins to reduce power + PORTA.PIN0CTRL = PORT_PULLUPEN_bm; + //PORTA.PIN1CTRL = PORT_PULLUPEN_bm; // Vbat sense + PORTA.PIN2CTRL = PORT_PULLUPEN_bm; + PORTA.PIN3CTRL = PORT_PULLUPEN_bm; + PORTA.PIN4CTRL = PORT_PULLUPEN_bm; + PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // eSwitch + PORTA.PIN6CTRL = PORT_PULLUPEN_bm; + PORTA.PIN7CTRL = PORT_PULLUPEN_bm; + + //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // regulated channel + PORTB.PIN1CTRL = PORT_PULLUPEN_bm; + //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // boost enable + PORTB.PIN3CTRL = PORT_PULLUPEN_bm; + PORTB.PIN4CTRL = PORT_PULLUPEN_bm; + PORTB.PIN5CTRL = PORT_PULLUPEN_bm; + + PORTC.PIN0CTRL = PORT_PULLUPEN_bm; + //PORTC.PIN1CTRL = PORT_PULLUPEN_bm; // RGB Aux + //PORTC.PIN2CTRL = PORT_PULLUPEN_bm; // RGB Aux + //PORTC.PIN3CTRL = PORT_PULLUPEN_bm; // RGB Aux + + // set up the PWM + // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf + // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm + // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm + // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm + // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc + // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc + // See the manual for other pins, clocks, configs, portmux, etc + TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm + //| TCA_SINGLE_CMP1EN_bm + | TCA_SINGLE_WGMODE_DSBOTTOM_gc; + TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc + | TCA_SINGLE_ENABLE_bm; + + PWM_TOP = PWM_TOP_INIT; + + // set up interrupt for delta-sigma modulation + // (moved to hwdef.c functions so it can be enabled/disabled based on ramp level) + //DSM_INTCTRL |= DSM_OVF_bm; // interrupt once for each timer cycle + +} + + +#define LAYOUT_DEFINED + diff --git a/hw/wurkkos/ts26/model b/hw/wurkkos/ts26/model new file mode 100644 index 0000000..028b833 --- /dev/null +++ b/hw/wurkkos/ts26/model @@ -0,0 +1 @@ +0721 |
