Advertisement
Guest User

HWDEF_thefreeman_BST_FWAA_MP3432_HDR_DAC_RGB_H

a guest
Sep 11th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.87 KB | None | 0 0
  1. #ifndef HWDEF_thefreeman_BST_FWAA_MP3432_HDR_DAC_RGB_H
  2. #define HWDEF_thefreeman_BST_FWAA_MP3432_HDR_DAC_RGB_H
  3.  
  4. /* thefreeman’s FWAA AA/li-ion Boost driver based on MP3432 and attiny1616 with high dynamic range and DAC control, AUX : RGB
  5.  * hardware version : 1.0
  6.  *
  7.  *
  8.  * Pin / Name / Function
  9.  *   1    PA2   BattLVL (ADC0 - AIN2)
  10.  *   2    PA3  
  11.  *   3    GND   GND
  12.  *   4    VCC   VCC
  13.  *   5    PA4   EN
  14.  *   6    PA5   HDR
  15.  *   7    PA6   DAC
  16.  *   8    PA7  
  17.  *   9    PB5   B
  18.  *  10    PB4   G
  19.  *  11    PB3   R
  20.  *  12    PB2   IN- NFET
  21.  *  13    PB1  
  22.  *  14    PB0  
  23.  *  15    PC0  
  24.  *  16    PC1  
  25.  *  17    PC2  
  26.  *  18    PC3   SWITCH
  27.  *  19    PA0   UDPI
  28.  *  20    PA1  
  29.  *
  30.  * EN enable the boost regulator and op-amp
  31.  * DAC pin sets the current, max current depends on Vset voltage divider and Rsense
  32.  * HDR FET pin switches between high value Rsense (low current range, pin low), and low value Rsense (high current range, pin high)
  33.  * IN- NFET : pull up after BST enable to elimiante startup flash, pull down otherwise
  34.  */
  35.  
  36. #define LAYOUT_DEFINED
  37.  
  38. #ifdef ATTINY
  39. #undef ATTINY
  40. #endif
  41. #define ATTINY 1616
  42. #include <avr/io.h>
  43.  
  44. #define PWM_CHANNELS 1
  45. #define USE_DYN_PWM  // dynamic frequency and speed
  46.  
  47. #ifndef SWITCH_PIN
  48. #define SWITCH_PIN     PIN3_bp    
  49. #define SWITCH_PORT    VPORTC.IN
  50. #define SWITCH_ISC_REG PORTC.PIN3CTRL
  51. #define SWITCH_VECT    PORTC_PORT_vect
  52. #define SWITCH_INTFLG  VPORTC.INTFLAGS
  53. #endif
  54.  
  55. #define PWM1_LVL DAC0.DATA  // use this for DAC voltage output
  56.  
  57. // PWM parameters of both channels are tied together because they share a counter
  58. #define PWM1_TOP VREF.CTRLA   // holds the TOP value for for variable-resolution PWM
  59.  
  60.  
  61. //BST enable
  62. #define LED2_ENABLE_PIN   PIN4_bp
  63. #define LED2_ENABLE_PORT  PORTA_OUT
  64.  
  65. //HDR
  66. #define LED_ENABLE_PIN   PIN5_bp
  67. #define LED_ENABLE_PORT  PORTA_OUT
  68.  
  69. //IN- NFET
  70. // TODO
  71.  
  72. // Voltage divider battLVL
  73. #define USE_VOLTAGE_DIVIDER       // use a dedicated pin, not VCC, because VCC input is flattened
  74. #define DUAL_VOLTAGE_FLOOR    21  // for AA/14500 boost drivers, don't indicate low voltage if below this level
  75. #define DUAL_VOLTAGE_LOW_LOW   7  // the lower voltage range's danger zone 0.7 volts (NiMH)
  76. #define ADMUX_VOLTAGE_DIVIDER ADC_MUXPOS_AIN2_gc  // which ADC channel to read
  77.  
  78. // Raw ADC readings at 4.4V and 2.2V
  79. // calibrate the voltage readout here
  80. // estimated / calculated values are:
  81. //   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
  82. // Resistors are 330k and 100k
  83. #ifndef ADC_44
  84. #define ADC_44 951  // raw value at 4.40V
  85. #endif
  86. #ifndef ADC_22
  87. #define ADC_22 476  // raw value at 2.20V
  88. #endif
  89.  
  90.  
  91.  
  92. // this driver allows for aux LEDs under the optic
  93. #ifndef AUXLED_R_PIN
  94. #define AUXLED_R_PIN  PIN3_bp
  95. #endif
  96. #ifndef AUXLED_G_PIN
  97. #define AUXLED_G_PIN  PIN4_bp
  98. #endif
  99. #ifndef AUXLED_B_PIN
  100. #define AUXLED_B_PIN  PIN5_bp
  101. #endif
  102.  
  103. #define AUXLED_RGB_PORT PORTB  // PORTA or PORTB or PORTC
  104.  
  105.  
  106.  
  107.  
  108.  
  109. // with so many pins, doing this all with #ifdefs gets awkward...
  110. // ... so just hardcode it in each hwdef file instead
  111. inline void hwdef_setup() {
  112.  
  113.     // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
  114.     // TODO: for this DAC controlled-light, try to decrease the clock speed or use the ULP
  115.     _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
  116.  
  117.     VPORTA.DIR = PIN4_bm | PIN5_bm | PIN6_bm; // EN, HDR, DAC
  118.     VPORTB.DIR = PIN2_bm | PIN3_bm | PIN4_bm | PIN5_bm;  // RGB, IN- NFET
  119.     //VPORTC.DIR = PIN0_bm | PIN1_bm;  //
  120.  
  121.  
  122.  
  123.     // enable pullups on the input pins to reduce power
  124.     PORTA.PIN0CTRL = PORT_PULLUPEN_bm; //
  125.     PORTA.PIN1CTRL = PORT_PULLUPEN_bm; //
  126.     //PORTA.PIN2CTRL = PORT_PULLUPEN_bm; // BattLVL
  127.     PORTA.PIN3CTRL = PORT_PULLUPEN_bm; //
  128.     //PORTA.PIN4CTRL = PORT_PULLUPEN_bm; // EN
  129.     //PORTA.PIN5CTRL = PORT_PULLUPEN_bm; // HDR
  130.     //PORTA.PIN6CTRL = PORT_PULLUPEN_bm; // DAC
  131.     PORTA.PIN7CTRL = PORT_PULLUPEN_bm; //
  132.    
  133.     PORTB.PIN0CTRL = PORT_PULLUPEN_bm; //
  134.     PORTB.PIN1CTRL = PORT_PULLUPEN_bm; //
  135.     //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // IN- NFET
  136.     //PORTB.PIN3CTRL = PORT_PULLUPEN_bm; // R
  137.     //PORTB.PIN4CTRL = PORT_PULLUPEN_bm; // G
  138.     //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // B
  139.    
  140.     PORTC.PIN0CTRL = PORT_PULLUPEN_bm; //
  141.     PORTC.PIN1CTRL = PORT_PULLUPEN_bm; //
  142.     PORTC.PIN2CTRL = PORT_PULLUPEN_bm; //
  143.     PORTC.PIN3CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc; // e-switch
  144.  
  145.     // set up the DAC
  146.     // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
  147.     // DAC ranges from 0V to (255 * Vref) / 256
  148.     VREF.CTRLA |= VREF_DAC0REFSEL_2V5_gc; // also VREF_DAC0REFSEL_0V55_gc and VREF_DAC0REFSEL_1V1_gc and VREF_DAC0REFSEL_2V5_gc
  149.     VREF.CTRLB |= VREF_DAC0REFEN_bm;
  150.     DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm;
  151.     DAC0.DATA = 255; // set the output voltage
  152.      
  153.    
  154.        
  155. }
  156.  
  157.  
  158. #endif
  159.  
  160.  
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement