~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-Sofirn_LT1S-Pro.h

  • Committer: Selene ToyKeeper
  • Date: 2023-04-15 00:01:03 UTC
  • mto: (483.1.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20230415000103-iz1pfn2ephnkeeeo
LT1S Pro: added dynamic PWM (much better low modes!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
#define HWDEF_C_FILE hwdef-Sofirn_LT1S-Pro.c
17
17
 
18
 
#ifdef ATTINY
19
 
#undef ATTINY
20
 
#endif
21
18
#define ATTINY 1616
22
19
#include <avr/io.h>
23
20
 
39
36
// TODO: blend mode should enable this automatically?
40
37
#define USE_CHANNEL_MODE_ARGS
41
38
// TODO: or maybe if args are defined, the USE_ should be auto-set?
 
39
// 128=middle CCT, N/A, N/A, 255=100% red
42
40
#define CHANNEL_MODE_ARGS 128,0,0,255
43
 
#define SET_LEVEL_MODES      set_level_2ch_blend, \
44
 
                             set_level_auto_3ch_blend, \
45
 
                             set_level_1ch, \
 
41
#define SET_LEVEL_MODES      set_level_2ch_dyn_blend, \
 
42
                             set_level_auto_3ch_dyn_blend, \
 
43
                             set_level_1ch_dyn, \
46
44
                             set_level_red_white_blend
47
45
// TODO: gradual ticking for thermal regulation
48
46
#define GRADUAL_TICK_MODES   gradual_tick_2ch_blend, \
49
47
                             gradual_tick_auto_3ch_blend, \
50
48
                             gradual_tick_1ch, \
51
49
                             gradual_tick_red_white_blend
52
 
// can use some of the common handlers
53
 
#define USE_SET_LEVEL_2CH_BLEND
 
50
// can use some of the common handlers?
 
51
//#define USE_SET_LEVEL_2CH_BLEND
54
52
//#define USE_SET_LEVEL_AUTO_3CH_BLEND
55
 
#define USE_SET_LEVEL_1CH
 
53
//#define USE_SET_LEVEL_1CH
56
54
//#define USE_SET_LEVEL_RED_WHITE_BLEND
57
55
// TODO:
58
56
//#define USE_GRADUAL_TICK_2CH_BLEND
78
76
#define SWITCH_INTFLG  VPORTA.INTFLAGS
79
77
 
80
78
 
 
79
// dynamic PWM
 
80
// PWM parameters of all channels are tied together because they share a counter
 
81
#define PWM_TOP_INIT 511  // highest value used in the top half of the ramp
 
82
#define PWM_TOP TCA0.SINGLE.PERBUF   // holds the TOP value for for variable-resolution PWM
 
83
#define PWM_CNT TCA0.SINGLE.CNT   // for resetting phase after each TOP adjustment
 
84
 
81
85
// warm tint channel
82
 
#define WARM_PWM_PIN PB0
83
 
#define WARM_PWM_LVL TCA0.SINGLE.CMP0  // CMP1 is the output compare register for PB0
 
86
//#define WARM_PWM_PIN PB0
 
87
#define WARM_PWM_LVL TCA0.SINGLE.CMP0BUF  // CMP1 is the output compare register for PB0
84
88
 
85
89
// cold tint channel
86
 
#define COOL_PWM_PIN PB1
87
 
#define COOL_PWM_LVL TCA0.SINGLE.CMP1  // CMP0 is the output compare register for PB1
 
90
//#define COOL_PWM_PIN PB1
 
91
#define COOL_PWM_LVL TCA0.SINGLE.CMP1BUF  // CMP0 is the output compare register for PB1
88
92
 
89
93
// red channel
90
 
#define RED_PWM_PIN PB0                //
91
 
#define RED_PWM_LVL TCA0.SINGLE.CMP2   // CMP2 is the output compare register for PB2
92
 
 
93
 
// translate cfg names to FSM names
94
 
#define LOW_PWM_LEVELS  RED_PWM_LEVELS
95
 
#define LOW_PWM_LVL     RED_PWM_LVL
96
 
#define LOW_PWM_PIN     RED_PWM_PIN
97
 
 
98
 
// only using 8-bit on this light
99
 
#define PWM_GET       PWM_GET8
100
 
#define PWM_DATATYPE  uint8_t
101
 
#define BLEND_PWM_DATATYPE  uint8_t
 
94
//#define RED_PWM_PIN PB2
 
95
#define RED_PWM_LVL TCA0.SINGLE.CMP2BUF   // CMP2 is the output compare register for PB2
 
96
 
 
97
// only using 16-bit PWM on this light
 
98
#define PWM_BITS 16
 
99
#define PWM_GET       PWM_GET16
 
100
#define PWM_DATATYPE  uint16_t
 
101
#define PWM1_DATATYPE uint16_t
 
102
#define PWM_DATATYPE2 uint32_t
102
103
 
103
104
 
104
105
// average drop across diode on this hardware
118
119
 
119
120
 
120
121
// custom channel modes
121
 
void set_level_auto_3ch_blend(uint8_t level);
 
122
void set_level_1ch_dyn(uint8_t level);
 
123
void set_level_2ch_dyn_blend(uint8_t level);
 
124
void set_level_auto_3ch_dyn_blend(uint8_t level);
122
125
void set_level_red_white_blend(uint8_t level);
123
126
 
124
127
 
128
131
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
129
132
 
130
133
    //VPORTA.DIR = ...;
131
 
    VPORTB.DIR = PIN0_bm | PIN1_bm | PIN2_bm | PIN5_bm;  // Outputs: Aux LED and PWMs
 
134
    // Outputs:
 
135
    VPORTB.DIR = PIN0_bm   // warm white
 
136
               | PIN1_bm   // cool white
 
137
               | PIN2_bm   // red
 
138
               | PIN5_bm;  // aux LED
132
139
    //VPORTC.DIR = ...;
133
140
 
134
141
    // enable pullups on the unused pins to reduce power
154
161
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
155
162
 
156
163
    // set up the PWM
 
164
    // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
 
165
    // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm
 
166
    // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm
 
167
    // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm
 
168
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
 
169
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
157
170
    // TODO: add references to MCU documentation
158
 
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_CMP1EN_bm | TCA_SINGLE_CMP2EN_bm | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
159
 
    TCA0.SINGLE.PER = 255;
160
 
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
 
171
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
 
172
                      | TCA_SINGLE_CMP1EN_bm
 
173
                      | TCA_SINGLE_CMP2EN_bm
 
174
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
 
175
    PWM_TOP = PWM_TOP_INIT;
 
176
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
 
177
                      | TCA_SINGLE_ENABLE_bm;
161
178
}
162
179
 
163
180