~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-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// BLF LT1S Pro driver layout using the Attiny1616
 
2
// Copyright (C) 2022-2023 (FIXME)
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
/*
 
7
 * Driver pinout:
 
8
 * eSwitch:    PA5
 
9
 * Aux LED:    PB5
 
10
 * WW PWM:     PB0 (TCA0 WO0)
 
11
 * CW PWM:     PB1 (TCA0 WO1)
 
12
 * Red PWM:    PB2 (TCA0 WO2)
 
13
 * Voltage:    VCC
 
14
 */
 
15
 
 
16
#define ATTINY 1616
 
17
#include <avr/io.h>
 
18
 
 
19
#define HWDEF_C_FILE hwdef-sofirn-lt1s-pro.c
 
20
 
 
21
// channel modes:
 
22
// * 0. warm/cool white blend
 
23
// * 1. auto 2ch white blend (warm -> cool by ramp level)
 
24
// * 2. auto 3ch blend (red -> warm -> cool by ramp level)
 
25
// * 3. red only
 
26
// * 4. red + white blend
 
27
#define NUM_CHANNEL_MODES 5
 
28
enum channel_modes_e {
 
29
    CM_WHITE = 0,
 
30
    CM_AUTO2,
 
31
    CM_AUTO3,
 
32
    CM_RED,
 
33
    CM_WHITE_RED,
 
34
};
 
35
 
 
36
#define CHANNEL_MODES_ENABLED 0b00011111
 
37
#define USE_CHANNEL_MODE_ARGS
 
38
// 128=middle CCT, _, _, _, 255=100% red
 
39
#define CHANNEL_MODE_ARGS     128,0,0,0,255
 
40
 
 
41
// can use some of the common handlers
 
42
#define USE_CALC_2CH_BLEND
 
43
//#define USE_CALC_AUTO_3CH_BLEND
 
44
 
 
45
 
 
46
#define PWM_CHANNELS   1  // old, remove this
 
47
 
 
48
// only using 16-bit PWM on this light
 
49
#define PWM_BITS      16
 
50
 
 
51
#define PWM_GET       PWM_GET16
 
52
#define PWM_DATATYPE  uint16_t
 
53
#define PWM1_DATATYPE uint16_t
 
54
#define PWM_DATATYPE2 uint32_t
 
55
 
 
56
// dynamic PWM
 
57
// PWM parameters of all channels are tied together because they share a counter
 
58
#define PWM_TOP_INIT  511  // highest value used in the top half of the ramp
 
59
#define PWM_TOP TCA0.SINGLE.PERBUF   // holds the TOP value for for variable-resolution PWM
 
60
#define PWM_CNT TCA0.SINGLE.CNT   // for resetting phase after each TOP adjustment
 
61
 
 
62
// warm LEDs
 
63
//#define WARM_PWM_PIN PB0
 
64
#define WARM_PWM_LVL TCA0.SINGLE.CMP0BUF  // CMP1 is the output compare register for PB0
 
65
 
 
66
// cold LEDs
 
67
//#define COOL_PWM_PIN PB1
 
68
#define COOL_PWM_LVL TCA0.SINGLE.CMP1BUF  // CMP0 is the output compare register for PB1
 
69
 
 
70
// red LEDs
 
71
//#define RED_PWM_PIN PB2
 
72
#define RED_PWM_LVL TCA0.SINGLE.CMP2BUF   // CMP2 is the output compare register for PB2
 
73
 
 
74
// lighted button
 
75
#define AUXLED_PIN   PIN5_bp
 
76
#define AUXLED_PORT  PORTB
 
77
 
 
78
// the button lights up
 
79
#define USE_INDICATOR_LED
 
80
// the button is visible while main LEDs are on
 
81
#define USE_INDICATOR_LED_WHILE_RAMPING
 
82
 
 
83
// e-switch
 
84
#define SWITCH_PIN     PIN5_bp
 
85
#define SWITCH_PORT    VPORTA.IN
 
86
#define SWITCH_ISC_REG PORTA.PIN2CTRL
 
87
#define SWITCH_VECT    PORTA_PORT_vect
 
88
#define SWITCH_INTFLG  VPORTA.INTFLAGS
 
89
 
 
90
// average drop across diode on this hardware
 
91
#ifndef VOLTAGE_FUDGE_FACTOR
 
92
#define VOLTAGE_FUDGE_FACTOR 7  // add 0.35V
 
93
#endif
 
94
 
 
95
 
 
96
inline void hwdef_setup() {
 
97
 
 
98
    // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
 
99
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
100
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
 
101
 
 
102
    //VPORTA.DIR = ...;
 
103
    // Outputs
 
104
    VPORTB.DIR = PIN0_bm   // warm white
 
105
               | PIN1_bm   // cool white
 
106
               | PIN2_bm   // red
 
107
               | PIN5_bm;  // aux LED
 
108
    //VPORTC.DIR = ...;
 
109
 
 
110
    // enable pullups on the unused pins to reduce power
 
111
    PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
 
112
    PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
 
113
    PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
 
114
    PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
 
115
    PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
 
116
    PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;  // eSwitch
 
117
    PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
 
118
    PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
 
119
 
 
120
    //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // warm tint channel
 
121
    //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // cold tint channel
 
122
    //PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // red LEDs
 
123
    PORTB.PIN3CTRL = PORT_PULLUPEN_bm;
 
124
    PORTB.PIN4CTRL = PORT_PULLUPEN_bm;
 
125
    //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED
 
126
 
 
127
    PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
 
128
    PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
 
129
    PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
 
130
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
 
131
 
 
132
    // set up the PWM
 
133
    // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
 
134
    // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm
 
135
    // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm
 
136
    // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm
 
137
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
 
138
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
 
139
    // TODO: add references to MCU documentation
 
140
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
 
141
                      | TCA_SINGLE_CMP1EN_bm
 
142
                      | TCA_SINGLE_CMP2EN_bm
 
143
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
 
144
    PWM_TOP = PWM_TOP_INIT;
 
145
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
 
146
                      | TCA_SINGLE_ENABLE_bm;
 
147
}
 
148
 
 
149
 
 
150
#define LAYOUT_DEFINED
 
151