~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-BLF_LT1-t1616.h

merged K9.3-tintramp / D4Sv2-tintramp branch
(which also changed the way tint ramping is implemented,
 to make things generally cleaner and more flexible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef HWDEF_BLF_LANTERN_T1616_H
 
2
#define HWDEF_BLF_LANTERN_T1616_H
 
3
 
 
4
/* BLF LT1 driver layout using the Attiny1616
 
5
 
 
6
Driver pinout:
 
7
 * eSwitch:    PA5
 
8
 * Aux LED:    PB5
 
9
 * PWM FET:    PB0 (TCA0 WO0)
 
10
 * PWM 1x7135: PB1 (TCA0 WO1)
 
11
 * Voltage:    VCC
 
12
 
 
13
*/
 
14
 
 
15
 
 
16
#define LAYOUT_DEFINED
 
17
 
 
18
#ifdef ATTINY
 
19
#undef ATTINY
 
20
#endif
 
21
#define ATTINY 1616
 
22
#include <avr/io.h>
 
23
 
 
24
#define PWM_CHANNELS 1
 
25
 
 
26
#ifndef SWITCH_PIN
 
27
#define SWITCH_PIN     PIN5_bp
 
28
#define SWITCH_PORT    VPORTA.IN
 
29
#define SWITCH_ISC_REG PORTA.PIN2CTRL
 
30
#define SWITCH_VECT    PORTA_PORT_vect
 
31
#define SWITCH_INTFLG  VPORTA.INTFLAGS
 
32
#endif
 
33
 
 
34
 
 
35
// usually PWM1_LVL would be a hardware register, but we need to abstract
 
36
// it out to a soft brightness value, in order to handle tint ramping
 
37
// (this allows smooth thermal regulation to work, and makes things
 
38
//  otherwise simpler and easier)
 
39
uint8_t PWM1_LVL;
 
40
 
 
41
// warm tint channel
 
42
#ifndef PWM1_PIN
 
43
#define PWM1_PIN PB1                //
 
44
#define TINT1_LVL TCA0.SINGLE.CMP1  // CMP1 is the output compare register for PB1
 
45
#endif
 
46
 
 
47
// cold tint channel
 
48
#ifndef PWM2_PIN
 
49
#define PWM2_PIN PB0                //
 
50
#define TINT2_LVL TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
 
51
#endif
 
52
 
 
53
// average drop across diode on this hardware
 
54
#ifndef VOLTAGE_FUDGE_FACTOR
 
55
#define VOLTAGE_FUDGE_FACTOR 7  // add 0.35V
 
56
#endif
 
57
 
 
58
 
 
59
// lighted button
 
60
#ifndef AUXLED_PIN
 
61
#define AUXLED_PIN  PIN5_bp
 
62
#define AUXLED_PORT PORTB
 
63
#endif
 
64
 
 
65
 
 
66
// with so many pins, doing this all with #ifdefs gets awkward...
 
67
// ... so just hardcode it in each hwdef file instead
 
68
inline void hwdef_setup() {
 
69
 
 
70
    // set up the system clock to run at 5 MHz instead of the default 3.33 MHz
 
71
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm );
 
72
 
 
73
    //VPORTA.DIR = ...;
 
74
    VPORTB.DIR = PIN0_bm | PIN1_bm | PIN5_bm;  // Outputs: Aux LED and PWMs
 
75
    //VPORTC.DIR = ...;
 
76
 
 
77
    // enable pullups on the unused pins to reduce power
 
78
    PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
 
79
    PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
 
80
    PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
 
81
    PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
 
82
    PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
 
83
    PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;  // eSwitch
 
84
    PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
 
85
    PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
 
86
 
 
87
    //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // cold tint channel
 
88
    //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // warm tint channel
 
89
    PORTB.PIN2CTRL = PORT_PULLUPEN_bm;
 
90
    PORTB.PIN3CTRL = PORT_PULLUPEN_bm;
 
91
    PORTB.PIN4CTRL = PORT_PULLUPEN_bm;
 
92
    //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED
 
93
 
 
94
    PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
 
95
    PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
 
96
    PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
 
97
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
 
98
 
 
99
    // set up the PWM
 
100
    // TODO: add references to MCU documentation
 
101
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_CMP1EN_bm | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;
 
102
    TCA0.SINGLE.PER = 255;
 
103
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
 
104
}
 
105
 
 
106
 
 
107
#endif