~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-gchart-fet1-t1616.h

  • Committer: Selene ToyKeeper
  • Date: 2023-11-02 17:05:02 UTC
  • mfrom: (483.12.159 multi-channel)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20231102170502-sinkm18qjxlorsxa
merged multi-channel branch with a major refactor and half a year of updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef HWDEF_GCH_FET1_T1616_H
2
 
#define HWDEF_GCH_FET1_T1616_H
3
 
 
4
 
/* gChart's custom FET+1 driver layout
5
 
 
6
 
PB0 - PWM for FET (TCA - WO0)
7
 
PB1 - PWM for 7135 (TCA - WO1)
8
 
PB2 - Switch pin, internal pullup
9
 
PB3 - Aux LED with 4700 Ohm series resistor
10
 
Read voltage from VCC pin, has diode with ~0.4v drop
11
 
 
 
1
// gChart's custom FET+1 driver layout
 
2
// Copyright (C) 2020-2023 gchart, Selene ToyKeeper
 
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
12
12
*/
13
13
 
14
 
 
15
 
#define LAYOUT_DEFINED
16
 
 
17
 
#ifdef ATTINY
18
 
#undef ATTINY
19
 
#endif
20
14
#define ATTINY 1616
21
15
#include <avr/io.h>
22
16
 
23
 
#define PWM_CHANNELS 2
24
 
 
25
 
#ifndef SWITCH_PIN
26
 
#define SWITCH_PIN     PIN2_bp
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
36
 
#define PWM1_PIN PB1               //
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
42
 
#define PWM2_PIN PB0               //
43
 
#define PWM2_LVL TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
44
 
#endif
 
17
// nearly all t1616-based FET+1 drivers work pretty much the same
 
18
// (this one has single-color aux like the TS10)
 
19
#define HWDEF_C_FILE hwdef-wurkkos-ts10.c
 
20
 
 
21
// allow using aux LEDs as extra channel modes
 
22
#include "chan-aux.h"
 
23
 
 
24
// channel modes:
 
25
// * 0. FET+7135 stacked
 
26
// * 1. aux LEDs
 
27
#define NUM_CHANNEL_MODES  2
 
28
enum CHANNEL_MODES {
 
29
    CM_MAIN = 0,
 
30
    CM_AUX
 
31
};
 
32
 
 
33
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
34
 
 
35
// right-most bit first, modes are in fedcba9876543210 order
 
36
#define CHANNEL_MODES_ENABLED 0b00000001
 
37
 
 
38
 
 
39
#define PWM_CHANNELS 2  // old, remove this
 
40
 
 
41
#define PWM_BITS      16        // dynamic 16-bit, but never goes over 255
 
42
#define PWM_GET       PWM_GET8
 
43
#define PWM_DATATYPE  uint16_t  // is used for PWM_TOPS (which goes way over 255)
 
44
#define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
 
45
#define PWM1_DATATYPE uint8_t   // 1x7135 ramp
 
46
#define PWM2_DATATYPE uint8_t   // DD FET ramp
 
47
 
 
48
// PWM parameters of both channels are tied together because they share a counter
 
49
#define PWM_TOP TCA0.SINGLE.PERBUF   // holds the TOP value for for variable-resolution PWM
 
50
#define PWM_TOP_INIT  255    // highest value used in top half of ramp
 
51
// not necessary when double-buffered "BUF" registers are used
 
52
#define PWM_CNT TCA0.SINGLE.CNT   // for resetting phase after each TOP adjustment
 
53
 
 
54
// 1x7135 channel
 
55
#define CH1_PIN  PB1
 
56
#define CH1_PWM  TCA0.SINGLE.CMP1BUF  // CMP1 is the output compare register for PB1
 
57
 
 
58
// DD FET channel
 
59
#define CH2_PIN  PB0
 
60
#define CH2_PWM  TCA0.SINGLE.CMP0BUF  // CMP0 is the output compare register for PB0
 
61
 
 
62
// e-switch
 
63
#define SWITCH_PIN      PIN2_bp
 
64
#define SWITCH_PORT     VPORTB.IN
 
65
#define SWITCH_ISC_REG  PORTB.PIN2CTRL
 
66
#define SWITCH_VECT     PORTB_PORT_vect
 
67
#define SWITCH_INTFLG   VPORTB.INTFLAGS
45
68
 
46
69
// average drop across diode on this hardware
47
70
#ifndef VOLTAGE_FUDGE_FACTOR
48
71
#define VOLTAGE_FUDGE_FACTOR 8  // 4 = add 0.20V
49
72
#endif
50
73
 
51
 
 
52
74
// lighted button
53
 
#ifndef AUXLED_PIN
54
 
#define AUXLED_PIN  PIN3_bp
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
 
75
#define AUXLED_PIN   PIN3_bp
 
76
#define AUXLED_PORT  PORTB
 
77
 
 
78
 
61
79
inline void hwdef_setup() {
62
80
 
63
81
    // 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 );
 
82
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
83
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
65
84
 
66
85
    //VPORTA.DIR = 0b00000010;
67
 
    VPORTB.DIR = PIN0_bm | PIN1_bm | PIN3_bm;
 
86
    // Outputs
 
87
    VPORTB.DIR = PIN0_bm   // DD FET
 
88
               | PIN1_bm   // 7135
 
89
               | PIN3_bm;  // Aux LED
68
90
    //VPORTC.DIR = 0b00000000;
69
91
 
70
92
    // enable pullups on the input pins to reduce power
97
119
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
98
120
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
99
121
    // 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;
101
 
    TCA0.SINGLE.PER = 255;
102
 
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
 
122
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
 
123
                      | TCA_SINGLE_CMP1EN_bm
 
124
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
 
125
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
 
126
                      | TCA_SINGLE_ENABLE_bm;
 
127
 
 
128
    PWM_TOP = PWM_TOP_INIT;
 
129
 
103
130
}
104
131
 
105
132
 
106
 
#endif
 
133
#define LAYOUT_DEFINED
 
134