~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-blf-q8-t1616.h

  • Committer: Selene ToyKeeper
  • Date: 2023-10-27 09:24:25 UTC
  • mto: (188.22.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 250.
  • Revision ID: bzr@toykeeper.net-20231027092425-1mopynzcowvd1ka9
converted blf-q8-t1616 to new API, using wurkkos-ts10 and wurkkos-ts25 as a base
(since the Q8 driver was the direct predecessor to those Wurkkos drivers,
 they're almost identical)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// BLF Q8 driver layout using the Attiny1616
2
 
// Copyright (C) 2021-2023 (FIXME)
 
2
// Copyright (C) 2021-2023 gchart, Selene ToyKeeper
3
3
// SPDX-License-Identifier: GPL-3.0-or-later
4
4
#pragma once
5
5
 
6
6
/*
 
7
 * (based on Wurkkos TS10 driver layout,
 
8
 *  which in turn was based on an older version of this BLF-Q8-t1616 driver)
 
9
 * (should probably merge the two files at some point)
7
10
 * Driver pinout:
8
11
 * eSwitch:    PA5
9
12
 * Aux LED:    PB5
10
13
 * PWM FET:    PB0 (TCA0 WO0)
11
14
 * PWM 1x7135: PB1 (TCA0 WO1)
12
15
 * Voltage:    VCC
13
 
*/
14
 
 
15
 
 
16
 
#define LAYOUT_DEFINED
17
 
 
18
 
#ifdef ATTINY
19
 
#undef ATTINY
20
 
#endif
 
16
 */
 
17
 
21
18
#define ATTINY 1616
22
19
#include <avr/io.h>
23
20
 
24
 
#define PWM_CHANNELS 2
25
 
 
26
 
#ifndef SWITCH_PIN
27
 
#define SWITCH_PIN     PIN5_bp
28
 
#define SWITCH_PORT    VPORTA.IN
29
 
#define SWITCH_ISC_REG PORTA.PIN2CTRL
30
 
#define SWITCH_VECT    PORTA_PORT_vect
31
 
#define SWITCH_INTFLG  VPORTA.INTFLAGS
32
 
#endif
33
 
 
34
 
 
35
 
// 7135 channel
36
 
#ifndef PWM1_PIN
37
 
#define PWM1_PIN PB1               //
38
 
#define PWM1_LVL TCA0.SINGLE.CMP1  // CMP1 is the output compare register for PB1
39
 
#endif
40
 
 
41
 
// FET channel
42
 
#ifndef PWM2_PIN
43
 
#define PWM2_PIN PB0               //
44
 
#define PWM2_LVL TCA0.SINGLE.CMP0  // CMP0 is the output compare register for PB0
45
 
#endif
 
21
// nearly all t1616-based FET+1 drivers work pretty much the same
 
22
// (this one has single-color aux like the TS10)
 
23
#define HWDEF_C_FILE hwdef-wurkkos-ts10.c
 
24
 
 
25
// allow using aux LEDs as extra channel modes
 
26
#include "chan-aux.h"
 
27
 
 
28
// channel modes:
 
29
// * 0. FET+7135 stacked
 
30
// * 1. aux LEDs
 
31
#define NUM_CHANNEL_MODES  2
 
32
enum CHANNEL_MODES {
 
33
    CM_MAIN = 0,
 
34
    CM_AUX
 
35
};
 
36
 
 
37
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
38
 
 
39
// right-most bit first, modes are in fedcba9876543210 order
 
40
#define CHANNEL_MODES_ENABLED 0b00000001
 
41
 
 
42
 
 
43
#define PWM_CHANNELS 2  // old, remove this
 
44
 
 
45
#define PWM_BITS      16        // dynamic 16-bit, but never goes over 255
 
46
#define PWM_GET       PWM_GET8
 
47
#define PWM_DATATYPE  uint16_t  // is used for PWM_TOPS (which goes way over 255)
 
48
#define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
 
49
#define PWM1_DATATYPE uint8_t   // 1x7135 ramp
 
50
#define PWM2_DATATYPE uint8_t   // DD FET ramp
 
51
 
 
52
// PWM parameters of both channels are tied together because they share a counter
 
53
#define PWM_TOP TCA0.SINGLE.PERBUF   // holds the TOP value for for variable-resolution PWM
 
54
#define PWM_TOP_INIT  255    // highest value used in top half of ramp
 
55
// not necessary when double-buffered "BUF" registers are used
 
56
#define PWM_CNT TCA0.SINGLE.CNT   // for resetting phase after each TOP adjustment
 
57
 
 
58
// 1x7135 channel
 
59
#define CH1_PIN  PB1
 
60
#define CH1_PWM  TCA0.SINGLE.CMP1BUF  // CMP1 is the output compare register for PB1
 
61
 
 
62
// DD FET channel
 
63
#define CH2_PIN  PB0
 
64
#define CH2_PWM  TCA0.SINGLE.CMP0BUF  // CMP0 is the output compare register for PB0
 
65
 
 
66
// e-switch
 
67
#define SWITCH_PIN      PIN5_bp
 
68
#define SWITCH_PORT     VPORTA.IN
 
69
#define SWITCH_ISC_REG  PORTA.PIN2CTRL
 
70
#define SWITCH_VECT     PORTA_PORT_vect
 
71
#define SWITCH_INTFLG   VPORTA.INTFLAGS
46
72
 
47
73
// average drop across diode on this hardware
48
74
#ifndef VOLTAGE_FUDGE_FACTOR
49
75
#define VOLTAGE_FUDGE_FACTOR 7  // add 0.35V
50
76
#endif
51
77
 
52
 
 
53
78
// lighted button
54
 
#ifndef AUXLED_PIN
55
 
#define AUXLED_PIN  PIN5_bp
56
 
#define AUXLED_PORT PORTB
57
 
#endif
58
 
 
59
 
 
60
 
// with so many pins, doing this all with #ifdefs gets awkward...
61
 
// ... so just hardcode it in each hwdef file instead
 
79
#define AUXLED_PIN   PIN5_bp
 
80
#define AUXLED_PORT  PORTB
 
81
 
 
82
 
62
83
inline void hwdef_setup() {
63
84
 
64
85
    // set up the system clock to run at 10 MHz instead of the default 3.33 MHz
65
 
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
 
86
    _PROTECTED_WRITE( CLKCTRL.MCLKCTRLB,
 
87
                      CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm );
66
88
 
67
89
    //VPORTA.DIR = ...;
68
 
    VPORTB.DIR = PIN0_bm | PIN1_bm | PIN5_bm;  // Outputs: Aux LED and PWMs
 
90
    // Outputs
 
91
    VPORTB.DIR = PIN0_bm   // DD FET
 
92
               | PIN1_bm   // 7135
 
93
               | PIN5_bm;  // Aux LED
69
94
    //VPORTC.DIR = ...;
70
95
 
71
96
    // enable pullups on the unused pins to reduce power
98
123
    // For Fast (Single Slope) PWM use TCA_SINGLE_WGMODE_SINGLESLOPE_gc
99
124
    // For Phase Correct (Dual Slope) PWM use TCA_SINGLE_WGMODE_DSBOTTOM_gc
100
125
    // See the manual for other pins, clocks, configs, portmux, etc
101
 
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_CMP1EN_bm | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
102
 
    TCA0.SINGLE.PER = 255;
103
 
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
 
126
    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm
 
127
                      | TCA_SINGLE_CMP1EN_bm
 
128
                      | TCA_SINGLE_WGMODE_DSBOTTOM_gc;
 
129
    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc
 
130
                      | TCA_SINGLE_ENABLE_bm;
 
131
 
 
132
    PWM_TOP = PWM_TOP_INIT;
 
133
 
104
134
}
105
135
 
 
136
 
 
137
#define LAYOUT_DEFINED
 
138