~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-emisar-d4sv2.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
 
#ifndef HWDEF_EMISAR_D4SV2_H
2
 
#define HWDEF_EMISAR_D4SV2_H
 
1
// Emisar D4Sv2 driver layout (attiny1634)
 
2
// Copyright (C) 2019-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
3
5
 
4
 
/* Emisar D4Sv2 driver layout (attiny1634)
 
6
/*
5
7
 * (same layout as D4v2, except it's a FET+3+1 instead of FET+1)
6
8
 *
7
9
 * Pin / Name / Function
10
12
 *   3    PA4   green aux LED
11
13
 *   4    PA3   blue aux LED
12
14
 *   5    PA2   e-switch
13
 
 *   6    PA1   (none)
 
15
 *   6    PA1   button LED?
14
16
 *   7    PA0   (none)
15
17
 *   8    GND   GND
16
18
 *   9    VCC   VCC
19
21
 *  12    PC3   RESET
20
22
 *  13    PC2   (none)
21
23
 *  14    PC1   SCK
22
 
 *  15    PC0   3x7135 PWM (PWM0A)
 
24
 *  15    PC0   3x7135 PWM (PWM0A) (8-bit)
23
25
 *  16    PB3   1x7135 PWM (PWM1A)
24
26
 *  17    PB2   MISO
25
27
 *  18    PB1   MOSI
28
30
 *      ADC12   thermal sensor
29
31
 */
30
32
 
31
 
#ifdef ATTINY
32
 
#undef ATTINY
33
 
#endif
34
33
#define ATTINY 1634
35
34
#include <avr/io.h>
36
35
 
 
36
#define HWDEF_C_FILE hwdef-emisar-d4sv2.c
 
37
 
 
38
// allow using aux LEDs as extra channel modes
 
39
#include "chan-rgbaux.h"
 
40
 
 
41
// channel modes:
 
42
// * 0. FET+3+1 stacked
 
43
// * 1+. aux RGB
 
44
#define NUM_CHANNEL_MODES   (1 + NUM_RGB_AUX_CHANNEL_MODES)
 
45
enum CHANNEL_MODES {
 
46
    CM_MAIN = 0,
 
47
    RGB_AUX_ENUMS
 
48
};
 
49
 
 
50
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
51
 
 
52
// right-most bit first, modes are in fedcba9876543210 order
 
53
#define CHANNEL_MODES_ENABLED 0b0000000000000001
 
54
// no args
 
55
//#define USE_CHANNEL_MODE_ARGS
 
56
//#define CHANNEL_MODE_ARGS     0,0,0,0,0,0,0,0
 
57
 
 
58
 
 
59
#define PWM_CHANNELS 3  // old, remove this
 
60
 
 
61
#define PWM_BITS      16        // dynamic 16-bit, but never goes over 255
 
62
#define PWM_GET       PWM_GET8
 
63
#define PWM_DATATYPE  uint16_t  // is used for PWM_TOPS (which goes way over 255)
 
64
#define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
 
65
#define PWM1_DATATYPE uint8_t   // 1x7135 ramp (16-bit)
 
66
#define PWM2_DATATYPE uint8_t   // 3x7135 ramp (8-bit)
 
67
#define PWM3_DATATYPE uint8_t   // DD FET ramp (16-bit)
 
68
 
 
69
// PWM parameters of FET and 1x7135 channels are tied together because they share a counter
 
70
#define PWM_TOP       ICR1   // holds the TOP value for for variable-resolution PWM
 
71
#define PWM_TOP_INIT  255    // highest value used in top half of ramp
 
72
#define PWM_CNT       TCNT1  // for dynamic PWM, reset phase
 
73
 
 
74
// 1x7135 channel
 
75
#define CH1_PIN  PB3            // pin 16, 1x7135 PWM
 
76
#define CH1_PWM  OCR1A          // OCR1A is the output compare register for PB3
 
77
 
 
78
// 3x7135 channel
 
79
#define CH2_PIN  PC0            // pin 15, 3x7135 PWM
 
80
#define CH2_PWM  OCR0A          // OCR0A is the output compare register for PC0
 
81
 
 
82
// DD FET channel
 
83
#define CH3_PIN  PA6            // pin 1, DD FET PWM
 
84
#define CH3_PWM  OCR1B          // OCR1B is the output compare register for PB1
 
85
 
 
86
// e-switch
37
87
#define SWITCH_PIN   PA2    // pin 5
38
88
#define SWITCH_PCINT PCINT2 // pin 5 pin change interrupt
39
89
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
40
90
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
41
91
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
42
 
 
43
 
#define PWM_CHANNELS 3
44
 
 
45
 
#define PWM1_PIN PB3        // pin 16, 1x7135 PWM
46
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
47
 
 
48
 
#define PWM2_PIN PC0        // pin 15, 3x7135 PWM
49
 
#define PWM2_LVL OCR0A      // OCR0A is the output compare register for PC0
50
 
 
51
 
#define PWM3_PIN PA6        // pin 1, FET PWM
52
 
#define PWM3_LVL OCR1B      // OCR1B is the output compare register for PB1
 
92
#define SWITCH_PUE   PUEA   // pullup group A
 
93
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
53
94
 
54
95
 
55
96
#define ADC_PRSCL   0x07    // clk/128
60
101
#endif
61
102
 
62
103
// this light has aux LEDs under the optic
63
 
#define AUXLED_R_PIN   PA5    // pin 2
64
 
#define AUXLED_G_PIN   PA4    // pin 3
65
 
#define AUXLED_B_PIN   PA3    // pin 4
66
 
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
67
 
#define AUXLED_RGB_DDR DDRA   // DDRA or DDRB or DDRC
68
 
#define AUXLED_RGB_PUE PUEA   // PUEA or PUEB or PUEC
69
 
 
70
 
// with so many pins, doing this all with #ifdefs gets awkward...
71
 
// ... so just hardcode it in each hwdef file instead
 
104
#define AUXLED_R_PIN    PA5    // pin 2
 
105
#define AUXLED_G_PIN    PA4    // pin 3
 
106
#define AUXLED_B_PIN    PA3    // pin 4
 
107
#define AUXLED_RGB_PORT PORTA  // PORTA or PORTB or PORTC
 
108
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
 
109
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
 
110
 
 
111
#define BUTTON_LED_PIN  PA1    // pin 6
 
112
#define BUTTON_LED_PORT PORTA  // for all "PA" pins
 
113
#define BUTTON_LED_DDR  DDRA   // for all "PA" pins
 
114
#define BUTTON_LED_PUE  PUEA   // for all "PA" pins
 
115
 
 
116
// this light has three aux LED channels: R, G, B
 
117
#define USE_AUX_RGB_LEDS
 
118
// it also has an independent LED in the button
 
119
#define USE_BUTTON_LED
 
120
// the aux LEDs are front-facing, so turn them off while main LEDs are on
 
121
#ifdef USE_INDICATOR_LED_WHILE_RAMPING
 
122
#undef USE_INDICATOR_LED_WHILE_RAMPING
 
123
#endif
 
124
 
 
125
 
72
126
inline void hwdef_setup() {
73
 
  // enable output ports
74
 
  // FET, aux R/G/B
75
 
  DDRA = (1 << PWM3_PIN)
76
 
       | (1 << AUXLED_R_PIN)
77
 
       | (1 << AUXLED_G_PIN)
78
 
       | (1 << AUXLED_B_PIN)
79
 
       ;
80
 
  // 1x7135
81
 
  DDRB = (1 << PWM1_PIN);
82
 
  // 3x7135
83
 
  DDRC = (1 << PWM2_PIN);
84
 
 
85
 
  // configure PWM
86
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
87
 
  // pre-scale for timer: N = 1
88
 
  // WGM1[3:0]: 0,0,0,1: PWM, Phase Correct, 8-bit (DS table 12-5)
89
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
90
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
91
 
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
92
 
  TCCR1A  = (0<<WGM11)  | (1<<WGM10)   // 8-bit (TOP=0xFF) (DS table 12-5)
93
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
94
 
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
95
 
          ;
96
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
97
 
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
98
 
          ;
99
 
 
100
 
  // WGM0[2:0]: 0,0,1: PWM, Phase Correct (DS table 11-8)
101
 
  // CS0[2:0]:  0,0,1: clk/1 (No prescaling) (DS table 11-9)
102
 
  // COM0A[1:0]:  1,0: PWM OC0A in the normal direction (DS table 11-4)
103
 
  // COM0B[1:0]:  0,0: OC0B disabled (DS table 11-7)
104
 
  // TCCR0A: COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00
105
 
  TCCR0A  = (0<<WGM01)  | (1<<WGM00)   // PWM, Phase Correct, TOP=0xFF (DS table 11-5)
106
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 0A in normal direction (DS table 11-4)
107
 
          | (0<<COM1B1) | (0<<COM1B0)  // PWM 0B disabled (DS table 11-7)
108
 
          ;
109
 
  // TCCR0B: FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00
110
 
  TCCR0B  = (0<<CS02)   | (0<<CS01) | (1<<CS00)  // clk/1 (no prescaling) (DS table 11-9)
111
 
          | (0<<WGM02)  // PWM, Phase Correct, TOP=0xFF (DS table 11-8)
112
 
          ;
113
 
 
114
 
  // set up e-switch
115
 
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
116
 
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
117
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
127
    // enable output ports
 
128
    // 3x7135
 
129
    DDRC = (1 << CH2_PIN);
 
130
    // 1x7135
 
131
    DDRB = (1 << CH1_PIN);
 
132
    // FET, aux R/G/B
 
133
    DDRA = (1 << CH3_PIN)
 
134
         | (1 << AUXLED_R_PIN)
 
135
         | (1 << AUXLED_G_PIN)
 
136
         | (1 << AUXLED_B_PIN)
 
137
         | (1 << BUTTON_LED_PIN)
 
138
         ;
 
139
 
 
140
    // configure PWM
 
141
    // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
142
    // pre-scale for timer: N = 1
 
143
    // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
 
144
    // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
145
    // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
146
    // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
 
147
    TCCR1A  = (1<<WGM11)  | (0<<WGM10)   // adjustable PWM (TOP=ICR1) (DS table 12-5)
 
148
            | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
149
            | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
150
            ;
 
151
    TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
152
            | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
 
153
            ;
 
154
 
 
155
    // WGM0[2:0]: 0,0,1: PWM, Phase Correct (DS table 11-8)
 
156
    // CS0[2:0]:  0,0,1: clk/1 (No prescaling) (DS table 11-9)
 
157
    // COM0A[1:0]:  1,0: PWM OC0A in the normal direction (DS table 11-4)
 
158
    // COM0B[1:0]:  0,0: OC0B disabled (DS table 11-7)
 
159
    // TCCR0A: COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00
 
160
    TCCR0A  = (0<<WGM01)  | (1<<WGM00)   // PWM, Phase Correct, TOP=0xFF (DS table 11-5)
 
161
            | (1<<COM1A1) | (0<<COM1A0)  // PWM 0A in normal direction (DS table 11-4)
 
162
            | (0<<COM1B1) | (0<<COM1B0)  // PWM 0B disabled (DS table 11-7)
 
163
            ;
 
164
    // TCCR0B: FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00
 
165
    TCCR0B  = (0<<CS02)   | (0<<CS01) | (1<<CS00)  // clk/1 (no prescaling) (DS table 11-9)
 
166
            | (0<<WGM02)  // PWM, Phase Correct, TOP=0xFF (DS table 11-8)
 
167
            ;
 
168
 
 
169
    // set PWM resolution
 
170
    PWM_TOP = PWM_TOP_INIT;
 
171
 
 
172
    // set up e-switch
 
173
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
174
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
118
175
}
119
176
 
 
177
 
120
178
#define LAYOUT_DEFINED
121
179
 
122
 
#endif