~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-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 LT1 driver layout using the Attiny1616
 
2
// Copyright (C) 2021-2023 (gchart), Selene ToyKeeper
 
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
 * PWM cool:   PB0 (TCA0 WO0)
 
11
 * PWM warm:   PB1 (TCA0 WO1)
 
12
 * Voltage:    VCC
 
13
 */
 
14
 
 
15
#define ATTINY 1616
 
16
#include <avr/io.h>
 
17
 
 
18
#define HWDEF_C_FILE hwdef-blf-lt1-t1616.c
 
19
 
 
20
// allow using aux LEDs as extra channel modes
 
21
#include "chan-aux.h"
 
22
 
 
23
// channel modes:
 
24
// * 0. warm only
 
25
// * 1. cool only
 
26
// * 2. both channels, tied together, max "200%" power
 
27
// * 3. both channels, manual blend, max "100%" power
 
28
// * 4. both channels, auto blend, reversible
 
29
#define NUM_CHANNEL_MODES  6
 
30
enum channel_modes_e {
 
31
    CM_CH1 = 0,
 
32
    CM_CH2,
 
33
    CM_BOTH,
 
34
    CM_BLEND,
 
35
    CM_AUTO,
 
36
    CM_AUX
 
37
};
 
38
 
 
39
// right-most bit first, modes are in fedcba9876543210 order
 
40
#define CHANNEL_MODES_ENABLED 0b00011000
 
41
#define USE_CHANNEL_MODE_ARGS
 
42
// _, _, _, 128=middle CCT, 0=warm-to-cool
 
43
#define CHANNEL_MODE_ARGS     0,0,0,128,0,0
 
44
 
 
45
// can use some of the common handlers
 
46
#define USE_CALC_2CH_BLEND
 
47
 
 
48
 
 
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
 
56
 
 
57
// PWM parameters of both channels are tied together because they share a counter
 
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
#define DSM_TOP       (255<<7) // 15-bit resolution leaves 1 bit for carry
 
64
 
 
65
// timer interrupt for DSM
 
66
#define DSM_vect     TCA0_OVF_vect
 
67
#define DSM_INTCTRL  TCA0.SINGLE.INTCTRL
 
68
#define DSM_INTFLAGS TCA0.SINGLE.INTFLAGS
 
69
#define DSM_OVF_bm   TCA_SINGLE_OVF_bm
 
70
 
 
71
#define DELAY_FACTOR 90  // less time in delay() because more time spent in interrupts
 
72
 
 
73
// warm LEDs
 
74
uint16_t ch1_dsm_lvl;
 
75
uint8_t ch1_pwm, ch1_dsm;
 
76
#define CH1_PIN  PB1
 
77
#define CH1_PWM  TCA0.SINGLE.CMP1BUF  // CMP1 is the output compare register for PB1
 
78
 
 
79
// cold LEDs
 
80
uint16_t ch2_dsm_lvl;
 
81
uint8_t ch2_pwm, ch2_dsm;
 
82
#define CH2_PIN  PB0
 
83
#define CH2_PWM  TCA0.SINGLE.CMP0BUF  // CMP0 is the output compare register for PB0
 
84
 
 
85
// lighted button
 
86
#define AUXLED_PIN   PIN5_bp
 
87
#define AUXLED_PORT  PORTB
 
88
 
 
89
// e-switch
 
90
#define SWITCH_PIN      PIN5_bp
 
91
#define SWITCH_PORT     VPORTA.IN
 
92
#define SWITCH_ISC_REG  PORTA.PIN2CTRL
 
93
#define SWITCH_VECT     PORTA_PORT_vect
 
94
#define SWITCH_INTFLG   VPORTA.INTFLAGS
 
95
 
 
96
// average drop across diode on this hardware
 
97
#ifndef VOLTAGE_FUDGE_FACTOR
 
98
#define VOLTAGE_FUDGE_FACTOR 7  // add 0.35V
 
99
#endif
 
100
 
 
101
 
 
102
inline void hwdef_setup() {
 
103
 
 
104
    // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
 
105
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
106
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
 
107
 
 
108
    //VPORTA.DIR = ...;
 
109
    // Outputs
 
110
    VPORTB.DIR = PIN0_bm   // cool white
 
111
               | PIN1_bm   // warm white
 
112
    //           | PIN2_bm   // for testing on LT1S Pro, disable red channel
 
113
               | PIN5_bm;  // aux LED
 
114
    //VPORTC.DIR = ...;
 
115
 
 
116
    // enable pullups on the unused pins to reduce power
 
117
    PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
 
118
    PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
 
119
    PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
 
120
    PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
 
121
    PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
 
122
    PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;  // eSwitch
 
123
    PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
 
124
    PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
 
125
 
 
126
    //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // cold tint channel
 
127
    //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // warm tint channel
 
128
    PORTB.PIN2CTRL = PORT_PULLUPEN_bm; // comment out for testing on LT1S Pro
 
129
    PORTB.PIN3CTRL = PORT_PULLUPEN_bm;
 
130
    PORTB.PIN4CTRL = PORT_PULLUPEN_bm;
 
131
    //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED
 
132
 
 
133
    PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
 
134
    PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
 
135
    PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
 
136
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
 
137
 
 
138
    // set up the PWM
 
139
    // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
 
140
    // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm
 
141
    // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm
 
142
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
 
143
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
 
144
    // TODO: add references to MCU documentation
 
145
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
 
146
                      | TCA_SINGLE_CMP1EN_bm
 
147
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
 
148
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
 
149
                      | TCA_SINGLE_ENABLE_bm;
 
150
 
 
151
    PWM_TOP = PWM_TOP_INIT;
 
152
 
 
153
    // set up interrupt for delta-sigma modulation
 
154
    // (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
 
155
    //DSM_INTCTRL |= DSM_OVF_bm;  // interrupt once for each timer cycle
 
156
 
 
157
}
 
158
 
 
159
 
 
160
#define LAYOUT_DEFINED
 
161