Advertisement
Guest User

hwdef-quad-buck.h

a guest
Jan 16th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | Source Code | 0 0
  1. // Quadruppel 21mm buck driver
  2. // Copyright (C) 2023 Quadruppel, Selene ToyKeeper
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4.  
  5.  
  6.  
  7. #define ATTINY 1616
  8. #include <avr/io.h>
  9.  
  10. #define HWDEF_C_FILE hwdef-quad-buck.c
  11.  
  12. // allow using aux LEDs as extra channel modes
  13. #include "chan-aux.h"
  14.  
  15. // channel modes:
  16. // * 0. main LEDs
  17. // * 1+. aux RGB
  18. #define NUM_CHANNEL_MODES  2
  19. enum CHANNEL_MODES {
  20.     CM_MAIN = 0,
  21.     CM_AUX
  22. };
  23.  
  24. #define DEFAULT_CHANNEL_MODE  CM_MAIN
  25.  
  26. // right-most bit first, modes are in fedcba9876543210 order
  27. #define CHANNEL_MODES_ENABLED 0b0000000000000001
  28.  
  29. #define PWM_BITS      8         // 8-bit DAC
  30. #define PWM_GET       PWM_GET8
  31. #define PWM_DATATYPE  uint8_t
  32. #define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
  33. #define PWM1_DATATYPE uint8_t   // main LED ramp
  34.  
  35. // main LED outputs
  36. #define DAC_LVL   DAC0.DATA    // 0 to 255, for 0V to Vref
  37. #define DAC_VREF  VREF.CTRLA   // 0.55V or 2.5V
  38. //#define PWM_TOP_INIT  255      // highest value used in top half of ramp (unused?)
  39. // Vref values
  40. #define V055  16
  41. #define V11   17
  42. #define V25   18
  43. #define V43   19
  44. #define V15   20
  45.  
  46. // Opamp and voltage regulator enable
  47. #define OPAMP_ENABLE_PIN   PIN3_bp
  48. #define OPAMP_ENABLE_PORT  PORTB_OUT
  49.  
  50. // HDR
  51. #define HDR_ENABLE_PIN   PIN5_bp
  52. #define HDR_ENABLE_PORT  PORTA_OUT
  53.  
  54. //Switch
  55. #define SWITCH_PIN     PIN4_bp
  56. #define SWITCH_PORT    VPORTB.IN
  57. #define SWITCH_ISC_REG PORTB.PIN4CTRL
  58. #define SWITCH_VECT    PORTB_PORT_vect
  59. #define SWITCH_INTFLG  VPORTB.INTFLAGS
  60.  
  61. // average drop across diode on this hardware
  62. #define VOLTAGE_FUDGE_FACTOR 0  // using a PFET so no appreciable drop
  63.  
  64. // lighted button
  65. #define AUXLED_PIN   PIN1_bp
  66. #define AUXLED_PORT  PORTB
  67.  
  68. inline void hwdef_setup() {
  69.  
  70.         _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm ); //original CLKCTRL_PDIV_2X_gc = 10MHZ
  71.  
  72.         VPORTA.DIR = PIN5_bm | PIN6_bm;
  73.         VPORTB.DIR = PIN1_bm | PIN3_bm;
  74.  
  75.         // enable pullups on the input pins to reduce power
  76.         PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
  77.         PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
  78.         PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
  79.         PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
  80.         PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
  81.         //PORTA.PIN5CTRL = PORT_PULLUPEN_bm;  // HDR channel selection
  82.         //PORTA.PIN6CTRL = PORT_PULLUPEN_bm;  // DAC ouput
  83.         PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
  84.  
  85.         PORTB.PIN0CTRL = PORT_PULLUPEN_bm;
  86.         //PORTB.PIN1CTRL = PORT_PULLUPEN_bm;  // Aux LED
  87.         PORTB.PIN2CTRL = PORT_PULLUPEN_bm;
  88.         //PORTB.PIN3CTRL = PORT_PULLUPEN_bm;  // Op-amp enable pin
  89.         PORTB.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;  // switch
  90.         PORTB.PIN5CTRL = PORT_PULLUPEN_bm;
  91.  
  92.         PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
  93.         PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
  94.         PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
  95.         PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
  96.  
  97.         // set up the DAC
  98.         // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
  99.         // DAC ranges from 0V to (255 * Vref) / 256
  100.         VREF.CTRLA |= VREF_DAC0REFSEL_2V5_gc; // also VREF_DAC0REFSEL_0V55_gc and VREF_DAC0REFSEL_1V1_gc and VREF_DAC0REFSEL_2V5_gc
  101.         VREF.CTRLB |= VREF_DAC0REFEN_bm;
  102.         DAC0.CTRLA = DAC_ENABLE_bm | DAC_OUTEN_bm;
  103.         DAC0.DATA = 255; // set the output voltage
  104.  
  105. }
  106.  
  107.  
  108. #define LAYOUT_DEFINED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement