Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef HWDEF_TF_BOOST_HDR_H
- #define HWDEF_TF_BOOST_HDR_H
- /* thefreeman’s boost driver based on TPS61288 and attiny1616 with high dynamic range
- * hardware version : 0.3
- * BLF thread : https://budgetlightforum.com/node/76762
- *
- * Pin / Name / Function
- * 1 PA2 NC
- * 2 PA3 NC
- * 3 GND GND
- * 4 VCC VCC
- * 5 PA4 NC
- * 6 PA5 NC
- * 7 PA6 high current range
- * 8 PA7 batt voltage (2:1 divider) ADC0 AIN7
- * 9 PB5 e-switch
- * 10 PB4 blue aux LED
- * 11 PB3 boost enable
- * 12 PB2 red aux LED
- * 13 PB1 green aux LED
- * 14 PB0 PB0 (TCA0 WO0)
- * 15 PC0 low current PWM TCD WOC
- * 16 PC1 high current PWM TCD WOD
- * 17 PC2 NC
- * 18 PC3 NC
- * 19 PA0 UDPI
- * 20 PA1 NC
- *
- *
- * one pin enables the boost IC and op-amp for the main LED
- * one pin controls the mosfet for the current range (pin low : low current range, pin high : high current range)
- * one pin sets the sense voltage via PWM to control the level within the low current range
- * another pin for the high current range. both are the same hardware channel, the pins are joined on PCB (unsure if one pin is needed for each ramp, or if only one pin for both is ok)
- */
- #define LAYOUT_DEFINED
- #ifdef ATTINY
- #undef ATTINY
- #endif
- #define ATTINY 1616
- #include <avr/io.h>
- #define PWM_CHANNELS 1
- // commented out for testing with 8bit
- //#define PWM_BITS 10 // 0 to 1023, not 0 to 255
- //#define PWM_TOP 1023
- #ifndef SWITCH_PIN
- #define SWITCH_PIN PIN5_bp
- #define SWITCH_PORT VPORTB.IN
- #define SWITCH_ISC_REG PORTB.PIN5CTRL
- #define SWITCH_VECT PORTB_PORT_vect
- #define SWITCH_INTFLG VPORTB.INTFLAGS
- #endif
- #ifndef PWM1_PIN
- #define PWM1_PIN PB0 //
- #define PWM1_LVL TCA0.SINGLE.CMP0 // CMP0 is the output compare register for PB0
- #endif
- //boost enable
- #define LED_ENABLE_PIN PIN3_bp
- #define LED_ENABLE_PORT PORTB_OUT
- #define USE_VOLTAGE_DIVIDER // use a dedicated pin, not VCC, because VCC input is flattened
- #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN7_gc // which ADC channel to read, AIN7 for PA7
- // Raw ADC readings at 4.4V and 2.2V
- // calibrate the voltage readout here
- // estimated / calculated values are:
- // [(Vbatt)*(R2/(R2+R1)) / VREG] * 1023
- // R1 = R2 = 100kR, VREG = 2.8V
- #ifndef ADC_44
- #define ADC_44 804
- #endif
- #ifndef ADC_22
- #define ADC_22 402
- #endif
- // this driver allows for aux LEDs under the optic
- #ifndef AUXLED_R_PIN
- #define AUXLED_R_PIN PIN2_bp
- #define AUXLED_R_PORT PORTB
- #endif
- #ifndef AUXLED_G_PIN
- #define AUXLED_G_PIN PIN1_bp
- #define AUXLED_G_PORT PORTB
- #endif
- #ifndef AUXLED_B_PIN
- #define AUXLED_B_PIN PIN4_bp
- #define AUXLED_B_PORT PORTB
- #endif
- //is that right ??
- #define AUXLED_RGB_PORT PORTB // PORTA or PORTB or PORTC
- #define AUXLED_RGB_DDR DDRB // DDRA or DDRB or DDRC
- #define AUXLED_RGB_PUE PUEB // PUEA or PUEB or PUEC
- // 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 5 MHz instead of the default 3.33 MHz
- _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm );
- VPORTB.DIR = PIN3_bm; // boost enable pin
- VPORTC.DIR = PIN0_bm | PIN1_bm; // PWM pins as output
- //VPORTA.DIR = PIN6_bm; // current range pin
- // 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; // current range
- //PORTA.PIN7CTRL = PORT_PULLUPEN_bm; // Vbat
- //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // PWM
- //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // Green aux LED
- //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // red
- //PORTB.PIN3CTRL = PORT_PULLUPEN_bm; // boost enable
- //PORTB.PIN4CTRL = PORT_PULLUPEN_bm; // Blue Aux LED
- PORTB.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // e-switch
- PORTC.PIN0CTRL = PORT_PULLUPEN_bm; // low current PWM
- PORTC.PIN1CTRL = PORT_PULLUPEN_bm; // high current PWM
- PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
- PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
- //set up PWM TCA
- TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
- TCA0.SINGLE.PER = 255;
- TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement