~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-blf-lt1-t1616.h

  • Committer: Selene ToyKeeper
  • Date: 2023-10-28 13:02:38 UTC
  • mto: (483.1.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20231028130238-ksjbjyqenih49cyf
switched blf-lt1-t1616 from plain PWM to PWM+DSM
(and made DSM interrupt definitions a bit cleaner)

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
#define USE_CALC_2CH_BLEND
47
47
 
48
48
 
49
 
#define PWM_CHANNELS   1  // old, remove this
50
 
 
51
 
#define PWM_BITS      8  //
52
 
 
53
 
#define PWM_GET       PWM_GET8
54
 
#define PWM_DATATYPE  uint8_t
55
 
#define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
56
 
#define PWM1_DATATYPE uint8_t   //
 
49
#define PWM_CHANNELS  1  // old, remove this
 
50
 
 
51
#define PWM_BITS      16     // 0 to 32640 (0 to 255 PWM + 0 to 127 DSM) at constant kHz
 
52
#define PWM_GET       PWM_GET16
 
53
#define PWM_DATATYPE  uint16_t
 
54
#define PWM_DATATYPE2 uint32_t  // only needs 32-bit if ramp values go over 255
 
55
#define PWM1_DATATYPE uint16_t  // 15-bit PWM+DSM ramp
57
56
 
58
57
// PWM parameters of both channels are tied together because they share a counter
59
 
#define PWM_TOP_INIT  255  // highest value used in the top half of the ramp
60
 
#define PWM_TOP TCA0.SINGLE.PERBUF   // holds the TOP value for for variable-resolution PWM
61
 
#define PWM_CNT TCA0.SINGLE.CNT   // for resetting phase after each TOP adjustment
62
 
// TODO: implement DSM
 
58
// dynamic PWM
 
59
#define PWM_TOP  TCA0.SINGLE.PERBUF  // holds the TOP value for for variable-resolution PWM
 
60
#define PWM_TOP_INIT  255
 
61
#define PWM_CNT  TCA0.SINGLE.CNT  // for resetting phase after each TOP adjustment
 
62
// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM)
63
63
#define DSM_TOP       (255<<7) // 15-bit resolution leaves 1 bit for carry
64
64
 
 
65
// timer interrupt for DSM
 
66
#define DSM_vect     TCA0_OVF_vect
 
67
#define DSM_INTCTRL  TCA0.SINGLE.INTCTRL
 
68
#define DSM_OVF_bm   TCA_SINGLE_OVF_bm
 
69
 
65
70
// warm LEDs
 
71
uint16_t ch1_dsm_lvl;
 
72
uint8_t ch1_pwm, ch1_dsm;
66
73
#define CH1_PIN  PB1
67
 
#define CH1_PWM TCA0.SINGLE.CMP1  // CMP1 is the output compare register for PB1
 
74
#define CH1_PWM  TCA0.SINGLE.CMP1  // CMP1 is the output compare register for PB1
68
75
 
69
76
// cold LEDs
70
 
#define CH2_PIN PB0
71
 
#define CH2_PWM TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
 
77
uint16_t ch2_dsm_lvl;
 
78
uint8_t ch2_pwm, ch2_dsm;
 
79
#define CH2_PIN  PB0
 
80
#define CH2_PWM  TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
72
81
 
73
82
// lighted button
74
83
#define AUXLED_PIN   PIN5_bp
75
84
#define AUXLED_PORT  PORTB
76
85
 
77
86
// e-switch
78
 
#define SWITCH_PIN     PIN5_bp
79
 
#define SWITCH_PORT    VPORTA.IN
80
 
#define SWITCH_ISC_REG PORTA.PIN2CTRL
81
 
#define SWITCH_VECT    PORTA_PORT_vect
82
 
#define SWITCH_INTFLG  VPORTA.INTFLAGS
 
87
#define SWITCH_PIN      PIN5_bp
 
88
#define SWITCH_PORT     VPORTA.IN
 
89
#define SWITCH_ISC_REG  PORTA.PIN2CTRL
 
90
#define SWITCH_VECT     PORTA_PORT_vect
 
91
#define SWITCH_INTFLG   VPORTA.INTFLAGS
83
92
 
84
93
// average drop across diode on this hardware
85
94
#ifndef VOLTAGE_FUDGE_FACTOR
89
98
 
90
99
inline void hwdef_setup() {
91
100
 
92
 
    // set up the system clock to run at 5 MHz instead of the default 3.33 MHz
93
 
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm );
 
101
    // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
 
102
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
103
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
94
104
 
95
105
    //VPORTA.DIR = ...;
96
 
    // Outputs:
 
106
    // Outputs
97
107
    VPORTB.DIR = PIN0_bm   // cool white
98
108
               | PIN1_bm   // warm white
99
109
               | PIN5_bm;  // aux LED
130
140
    // TODO: add references to MCU documentation
131
141
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
132
142
                      | TCA_SINGLE_CMP1EN_bm
133
 
                      | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
134
 
    PWM_TOP = PWM_TOP_INIT;
 
143
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
135
144
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
136
145
                      | TCA_SINGLE_ENABLE_bm;
 
146
 
 
147
    PWM_TOP = PWM_TOP_INIT;
 
148
 
 
149
    // set up interrupt for delta-sigma modulation
 
150
    // (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
 
151
    //DSM_INTCTRL |= DSM_OVF_bm;  // interrupt once for each timer cycle
 
152
 
137
153
}
138
154
 
139
155