~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-wurkkos-ts10.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
// Wurkkos TS10 driver layout
 
2
// Copyright (C) 2021-2023 gchart, Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
/*
 
7
 * (based on BLF Q8-t1616 driver layout)
 
8
 * Driver pinout:
 
9
 * eSwitch:    PA5
 
10
 * Aux LED:    PB5
 
11
 * PWM FET:    PB0 (TCA0 WO0)
 
12
 * PWM 1x7135: PB1 (TCA0 WO1)
 
13
 * Voltage:    VCC
 
14
 */
 
15
 
 
16
#define ATTINY 1616
 
17
#include <avr/io.h>
 
18
 
 
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      PIN5_bp
 
64
//#define SWITCH_PCINT    PCINT0
 
65
#define SWITCH_PORT     VPORTA.IN
 
66
#define SWITCH_ISC_REG  PORTA.PIN2CTRL
 
67
#define SWITCH_VECT     PORTA_PORT_vect
 
68
#define SWITCH_INTFLG   VPORTA.INTFLAGS
 
69
//#define PCINT_vect      PCINT0_vect
 
70
 
 
71
// average drop across diode on this hardware
 
72
#ifndef VOLTAGE_FUDGE_FACTOR
 
73
#define VOLTAGE_FUDGE_FACTOR 7  // add 0.35V
 
74
#endif
 
75
 
 
76
// front-facing aux LEDs
 
77
#define AUXLED_PIN  PIN5_bp
 
78
#define AUXLED_PORT PORTB
 
79
 
 
80
 
 
81
inline void hwdef_setup() {
 
82
 
 
83
    // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
 
84
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
85
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
 
86
 
 
87
    //VPORTA.DIR = ...;
 
88
    // Outputs
 
89
    VPORTB.DIR = PIN0_bm   // DD FET
 
90
               | PIN1_bm   // 7135
 
91
               | PIN5_bm;  // Aux LED
 
92
    //VPORTC.DIR = ...;
 
93
 
 
94
    // enable pullups on the unused pins to reduce power
 
95
    PORTA.PIN0CTRL = PORT_PULLUPEN_bm;
 
96
    PORTA.PIN1CTRL = PORT_PULLUPEN_bm;
 
97
    PORTA.PIN2CTRL = PORT_PULLUPEN_bm;
 
98
    PORTA.PIN3CTRL = PORT_PULLUPEN_bm;
 
99
    PORTA.PIN4CTRL = PORT_PULLUPEN_bm;
 
100
    PORTA.PIN5CTRL = PORT_PULLUPEN_bm | PORT_ISC_BOTHEDGES_gc;  // eSwitch
 
101
    PORTA.PIN6CTRL = PORT_PULLUPEN_bm;
 
102
    PORTA.PIN7CTRL = PORT_PULLUPEN_bm;
 
103
 
 
104
    //PORTB.PIN0CTRL = PORT_PULLUPEN_bm; // FET channel
 
105
    //PORTB.PIN1CTRL = PORT_PULLUPEN_bm; // 7135 channel
 
106
    PORTB.PIN2CTRL = PORT_PULLUPEN_bm;
 
107
    PORTB.PIN3CTRL = PORT_PULLUPEN_bm;
 
108
    PORTB.PIN4CTRL = PORT_PULLUPEN_bm;
 
109
    //PORTB.PIN5CTRL = PORT_PULLUPEN_bm; // Aux LED
 
110
 
 
111
    PORTC.PIN0CTRL = PORT_PULLUPEN_bm;
 
112
    PORTC.PIN1CTRL = PORT_PULLUPEN_bm;
 
113
    PORTC.PIN2CTRL = PORT_PULLUPEN_bm;
 
114
    PORTC.PIN3CTRL = PORT_PULLUPEN_bm;
 
115
 
 
116
    // set up the PWM
 
117
    // https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny1614-16-17-DataSheet-DS40002204A.pdf
 
118
    // PB0 is TCA0:WO0, use TCA_SINGLE_CMP0EN_bm
 
119
    // PB1 is TCA0:WO1, use TCA_SINGLE_CMP1EN_bm
 
120
    // PB2 is TCA0:WO2, use TCA_SINGLE_CMP2EN_bm
 
121
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
 
122
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
 
123
    // See the manual for other pins, clocks, configs, portmux, etc
 
124
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
 
125
                      | TCA_SINGLE_CMP1EN_bm
 
126
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
 
127
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
 
128
                      | TCA_SINGLE_ENABLE_bm;
 
129
 
 
130
    PWM_TOP = PWM_TOP_INIT;
 
131
 
 
132
}
 
133
 
 
134
 
 
135
#define LAYOUT_DEFINED
 
136