~toykeeper/flashlight-firmware/fsm

483.12.14 by Selene ToyKeeper
switched the rest of FSM + Anduril to use SPDX license headers
1
// gChart's custom FET+1 driver layout
2
// Copyright (C) 2020-2023 (FIXME)
3
// SPDX-License-Identifier: GPL-3.0-or-later
4
#pragma once
5
6
/*
7
 * PB0 - PWM for FET (TCA - WO0)
8
 * PB1 - PWM for 7135 (TCA - WO1)
9
 * PB2 - Switch pin, internal pullup
10
 * PB3 - Aux LED with 4700 Ohm series resistor
11
 * Read voltage from VCC pin, has diode with ~0.4v drop
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
12
*/
13
14
15
#define LAYOUT_DEFINED
16
17
#ifdef ATTINY
18
#undef ATTINY
19
#endif
20
#define ATTINY 1616
21
#include <avr/io.h>
22
23
#define PWM_CHANNELS 2
24
25
#ifndef SWITCH_PIN
483.4.6 by Selene Scriven
minor formatting cleanup on t1616 hwdef files
26
#define SWITCH_PIN     PIN2_bp
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
27
#define SWITCH_PORT    VPORTB.IN
28
#define SWITCH_ISC_REG PORTB.PIN2CTRL
29
#define SWITCH_VECT    PORTB_PORT_vect
30
#define SWITCH_INTFLG  VPORTB.INTFLAGS
31
#endif
32
33
34
// 7135 channel
35
#ifndef PWM1_PIN
483.4.6 by Selene Scriven
minor formatting cleanup on t1616 hwdef files
36
#define PWM1_PIN PB1               //
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
37
#define PWM1_LVL TCA0.SINGLE.CMP1  // CMP1 is the output compare register for PB1
38
#endif
39
40
// FET channel
41
#ifndef PWM2_PIN
483.4.6 by Selene Scriven
minor formatting cleanup on t1616 hwdef files
42
#define PWM2_PIN PB0               //
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
43
#define PWM2_LVL TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
44
#endif
45
46
// average drop across diode on this hardware
47
#ifndef VOLTAGE_FUDGE_FACTOR
48
#define VOLTAGE_FUDGE_FACTOR 8  // 4 = add 0.20V
49
#endif
50
51
52
// lighted button
53
#ifndef AUXLED_PIN
483.3.9 by Gabriel Hart
Add RGB Aux functionality for 1-Series
54
#define AUXLED_PIN  PIN3_bp
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
55
#define AUXLED_PORT PORTB
56
#endif
57
58
59
// with so many pins, doing this all with #ifdefs gets awkward...
60
// ... so just hardcode it in each hwdef file instead
61
inline void hwdef_setup() {
62
483.3.12 by Gabriel Hart
Update 1-Series to use 10 MHz clock and Phase Correct PWM. Add PWM documentation. Also clear thermal offset on factory reset instead of setting it to 21*C.
63
    // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
64
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
65
66
    //VPORTA.DIR = 0b00000010;
67
    VPORTB.DIR = PIN0_bm | PIN1_bm | PIN3_bm;
68
    //VPORTC.DIR = 0b00000000;
69
70
    // enable pullups on the input pins to reduce power
71
    PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
72
    PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
73
    PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
74
    PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
75
    PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
76
    PORTA.PIN5CTRL = PORT_PULLUPEN_bm;
77
    PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
78
    PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
483.4.6 by Selene Scriven
minor formatting cleanup on t1616 hwdef files
79
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
80
    //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // FET channel
81
    //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // 7135 channel
82
    PORTB.PIN2CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;  // switch
83
    //PORTB.PIN3CTRL = PORT_PULLUPEN_bm; // Aux LED
84
    PORTB.PIN4CTRL = PORT_PULLUPEN_bm;
85
    PORTB.PIN5CTRL = PORT_PULLUPEN_bm;
483.4.6 by Selene Scriven
minor formatting cleanup on t1616 hwdef files
86
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
87
    PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
88
    PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
89
    PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
90
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
483.4.6 by Selene Scriven
minor formatting cleanup on t1616 hwdef files
91
92
    // set up the PWM
483.3.12 by Gabriel Hart
Update 1-Series to use 10 MHz clock and Phase Correct PWM. Add PWM documentation. Also clear thermal offset on factory reset instead of setting it to 21*C.
93
    // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
94
    // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm
95
    // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm
96
    // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm
97
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
98
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
99
    // See the manual for other pins, clocks, configs, portmux, etc
100
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_CMP1EN_bm | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
483.3.1 by Gabriel Hart
Add AVR 1-Series and t1616 board and scripts
101
    TCA0.SINGLE.PER = 255;
102
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
103
}
104