Advertisement
Guest User

hwdef-TF-boost-HDR.h

a guest
Mar 31st, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.57 KB | None | 0 0
  1. #ifndef HWDEF_TF_BOOST_HDR_H
  2. #define HWDEF_TF_BOOST_HDR_H
  3.  
  4. /* thefreeman’s boost driver based on TPS61288 and attiny1616 with high dynamic range
  5.  * hardware version : 0.3
  6.  * BLF thread : https://budgetlightforum.com/node/76762
  7.  *
  8.  * Pin / Name / Function
  9.  *   1    PA2   NC
  10.  *   2    PA3   NC
  11.  *   3    GND   GND
  12.  *   4    VCC   VCC
  13.  *   5    PA4   NC
  14.  *   6    PA5   NC
  15.  *   7    PA6   high current range
  16.  *   8    PA7   batt voltage (2:1 divider) ADC0 AIN7
  17.  *   9    PB5   e-switch
  18.  *  10    PB4   blue aux LED
  19.  *  11    PB3   boost enable
  20.  *  12    PB2   red aux LED
  21.  *  13    PB1   green aux LED
  22.  *  14    PB0   PB0 (TCA0 WO0)
  23.  *  15    PC0   low current PWM  TCD WOC
  24.  *  16    PC1   high current PWM TCD WOD
  25.  *  17    PC2   NC
  26.  *  18    PC3   NC
  27.  *  19    PA0   UDPI
  28.  *  20    PA1   NC
  29.  *
  30.  *
  31.  * one pin enables the boost IC and op-amp for the main LED
  32.  * one pin controls the mosfet for the current range (pin low : low current range, pin high : high current range)
  33.  * one pin sets the sense voltage via PWM to control the level within the low current range
  34.  * 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)
  35.  */
  36.  
  37. #define LAYOUT_DEFINED
  38.  
  39. #ifdef ATTINY
  40. #undef ATTINY
  41. #endif
  42. #define ATTINY 1616
  43. #include <avr/io.h>
  44.  
  45. #define PWM_CHANNELS 1
  46. // commented out for testing with 8bit
  47. //#define PWM_BITS 10  // 0 to 1023, not 0 to 255
  48. //#define PWM_TOP 1023
  49.  
  50. #ifndef SWITCH_PIN
  51. #define SWITCH_PIN     PIN5_bp    
  52. #define SWITCH_PORT    VPORTB.IN
  53. #define SWITCH_ISC_REG PORTB.PIN5CTRL
  54. #define SWITCH_VECT    PORTB_PORT_vect
  55. #define SWITCH_INTFLG  VPORTB.INTFLAGS
  56. #endif
  57.  
  58. #ifndef PWM1_PIN
  59. #define PWM1_PIN PB0               //
  60. #define PWM1_LVL TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
  61. #endif
  62.  
  63. //boost enable
  64. #define LED_ENABLE_PIN   PIN3_bp
  65. #define LED_ENABLE_PORT  PORTB_OUT
  66.  
  67.  
  68. #define USE_VOLTAGE_DIVIDER       // use a dedicated pin, not VCC, because VCC input is flattened
  69. #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN7_gc  // which ADC channel to read, AIN7 for PA7
  70.  
  71. // Raw ADC readings at 4.4V and 2.2V
  72. // calibrate the voltage readout here
  73. // estimated / calculated values are:
  74. // [(Vbatt)*(R2/(R2+R1)) / VREG] * 1023
  75. // R1 = R2 = 100kR, VREG = 2.8V
  76. #ifndef ADC_44
  77. #define ADC_44 804
  78. #endif
  79. #ifndef ADC_22
  80. #define ADC_22 402
  81. #endif
  82.  
  83. // this driver allows for aux LEDs under the optic
  84. #ifndef AUXLED_R_PIN
  85. #define AUXLED_R_PIN  PIN2_bp
  86. #define AUXLED_R_PORT PORTB
  87. #endif
  88. #ifndef AUXLED_G_PIN
  89. #define AUXLED_G_PIN  PIN1_bp
  90. #define AUXLED_G_PORT PORTB
  91. #endif
  92. #ifndef AUXLED_B_PIN
  93. #define AUXLED_B_PIN  PIN4_bp
  94. #define AUXLED_B_PORT PORTB
  95. #endif
  96. //is that right ??
  97. #define AUXLED_RGB_PORT PORTB  // PORTA or PORTB or PORTC
  98. #define AUXLED_RGB_DDR  DDRB   // DDRA or DDRB or DDRC
  99. #define AUXLED_RGB_PUE  PUEB   // PUEA or PUEB or PUEC
  100.  
  101.  
  102. // with so many pins, doing this all with #ifdefs gets awkward...
  103. // ... so just hardcode it in each hwdef file instead
  104. inline void hwdef_setup() {
  105.  
  106.     // set up the system clock to run at 5 MHz instead of the default 3.33 MHz
  107.     _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm );
  108.  
  109.     VPORTB.DIR = PIN3_bm;  // boost enable pin
  110.     VPORTC.DIR = PIN0_bm | PIN1_bm;  // PWM pins as output
  111.     //VPORTA.DIR = PIN6_bm; // current range pin
  112.  
  113.  
  114.     // enable pullups on the input pins to reduce power
  115.     PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
  116.     PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
  117.     PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
  118.     PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
  119.     PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
  120.     PORTA.PIN5CTRL = PORT_PULLUPEN_bm;
  121.     PORTA.PIN6CTRL = PORT_PULLUPEN_bm; // current range
  122.     //PORTA.PIN7CTRL = PORT_PULLUPEN_bm; // Vbat
  123.    
  124.     //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; //  PWM
  125.     //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // Green aux LED
  126.     //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // red
  127.     //PORTB.PIN3CTRL = PORT_PULLUPEN_bm;  // boost enable
  128.     //PORTB.PIN4CTRL = PORT_PULLUPEN_bm; // Blue Aux LED
  129.     PORTB.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;   // e-switch
  130.    
  131.     PORTC.PIN0CTRL = PORT_PULLUPEN_bm; // low current PWM
  132.     PORTC.PIN1CTRL = PORT_PULLUPEN_bm; // high current PWM
  133.     PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
  134.     PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
  135.  
  136.    
  137.      //set up PWM TCA
  138.     TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
  139.     TCA0.SINGLE.PER = 255;
  140.     TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
  141.        
  142. }
  143.  
  144.  
  145. #endif
  146.  
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement