~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-noctigon-dm11.h

  • Committer: Selene ToyKeeper
  • Date: 2023-07-23 00:08:12 UTC
  • mto: (483.1.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20230723000812-lae16ybrvfm1lvlf
converted noctigon-dm11-* builds to multi-channel
(but I could only test dm11-boost on actual hardware)
(also, it looks like dm11-sbt90 is almost identical to the base kr4 build,
 so I removed its hwdef)

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 *   2    PA5   R: red aux LED (PWM0B)
12
12
 *   3    PA4   G: green aux LED
13
13
 *   4    PA3   B: blue aux LED
14
 
 *   5    PA2   L: button LED
 
14
 *   5    PA2   L: button LED (on some models)
15
15
 *   6    PA1   (none)
16
16
 *   7    PA0   (none)
17
17
 *   8    GND   GND
22
22
 *  13    PC2   (none)
23
23
 *  14    PC1   SCK
24
24
 *  15    PC0   (none) PWM0A
25
 
 *  16    PB3   main LED PWM (PWM1A)
 
25
 *  16    PB3   main LED PWM (linear) (PWM1A)
26
26
 *  17    PB2   MISO / (none) (PCINT10)
27
27
 *  18    PB1   MOSI / battery voltage (ADC6)
28
28
 *  19    PB0   Opamp power
37
37
 * Some models also have a direct-drive FET for turbo.
38
38
 */
39
39
 
40
 
#ifdef ATTINY
41
 
#undef ATTINY
42
 
#endif
43
40
#define ATTINY 1634
44
41
#include <avr/io.h>
45
42
 
46
 
#define PWM_CHANNELS 2  // override this for the no-FET version
47
 
#define PWM_BITS 16  // data type needs 16 bits, not 8
48
 
#define PWM_TOP  255 // highest value used in top half of ramp
49
 
#define USE_DYN_PWM  // dynamic frequency and speed
50
 
 
 
43
#ifndef HWDEF_C_FILE
 
44
#define HWDEF_C_FILE hwdef-noctigon-kr4.c
 
45
#endif
 
46
 
 
47
// allow using aux LEDs as extra channel modes
 
48
#include "chan-rgbaux.h"
 
49
 
 
50
// channel modes:
 
51
// * 0. linear + DD FET stacked
 
52
// * 1+. aux RGB
 
53
#define NUM_CHANNEL_MODES   (1 + NUM_RGB_AUX_CHANNEL_MODES)
 
54
enum CHANNEL_MODES {
 
55
    CM_MAIN = 0,
 
56
    RGB_AUX_ENUMS
 
57
};
 
58
 
 
59
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
60
 
 
61
// right-most bit first, modes are in fedcba9876543210 order
 
62
#define CHANNEL_MODES_ENABLED 0b0000000000000001
 
63
// no args
 
64
//#define USE_CHANNEL_MODE_ARGS
 
65
//#define CHANNEL_MODE_ARGS     0,0,0,0,0,0,0,0
 
66
 
 
67
 
 
68
#define PWM_CHANNELS 2  // old, remove this
 
69
 
 
70
#define PWM_BITS      16        // dynamic 16-bit, but never goes over 255
 
71
#define PWM_GET       PWM_GET8
 
72
#define PWM_DATATYPE  uint16_t  // is used for PWM_TOPS (which goes way over 255)
 
73
#define PWM_DATATYPE2 uint16_t  // only needs 32-bit if ramp values go over 255
 
74
#define PWM1_DATATYPE uint8_t   // linear ramp
 
75
#define PWM2_DATATYPE uint8_t   // DD FET ramp
 
76
 
 
77
// PWM parameters of both channels are tied together because they share a counter
 
78
#define PWM_TOP       ICR1   // holds the TOP value for variable-resolution PWM
 
79
#define PWM_TOP_INIT  255    // highest value used in top half of ramp
 
80
#define PWM_CNT       TCNT1  // for dynamic PWM, reset phase
 
81
 
 
82
// linear channel
 
83
#define CH1_PIN  PB3            // pin 16, Opamp reference
 
84
#define CH1_PWM  OCR1A          // OCR1A is the output compare register for PB3
 
85
#define CH1_ENABLE_PIN   PB0    // pin 19, Opamp power
 
86
#define CH1_ENABLE_PORT  PORTB  // control port for PB0
 
87
 
 
88
// DD FET channel
 
89
#define CH2_PIN  PA6            // pin 1, DD FET PWM
 
90
#define CH2_PWM  OCR1B          // OCR1B is the output compare register for PA6
 
91
 
 
92
// e-switch
51
93
#define SWITCH_PIN   PA7    // pin 20
52
94
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
53
95
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
54
96
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
55
97
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
56
 
 
57
 
#define PWM1_PIN PB3        // pin 16, Opamp reference
58
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
59
 
#define PWM1_CNT TCNT1      // for dynamic PWM, reset phase
60
 
#define PWM1_PHASE_RESET_OFF  // force reset while shutting off
61
 
#define PWM1_PHASE_RESET_ON   // force reset while turning on
62
 
#define PWM1_PHASE_SYNC       // manual sync while changing level
63
 
 
64
 
#define PWM2_PIN PA6        // pin 1, DD FET PWM
65
 
#define PWM2_LVL OCR1B      // OCR1B is the output compare register for PA6
66
 
 
67
 
// PWM parameters of both channels are tied together because they share a counter
68
 
#define PWM1_TOP ICR1       // holds the TOP value for for variable-resolution PWM
69
 
 
70
 
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
71
 
#define LED_ENABLE_PORT PORTB  // control port for PB0
 
98
#define SWITCH_PUE   PUEA   // pullup group A
 
99
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
72
100
 
73
101
 
74
102
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
114
142
#define BUTTON_LED_DDR  DDRA   // for all "PA" pins
115
143
#define BUTTON_LED_PUE  PUEA   // for all "PA" pins
116
144
 
117
 
// with so many pins, doing this all with #ifdefs gets awkward...
118
 
// ... so just hardcode it in each hwdef file instead
119
145
inline void hwdef_setup() {
120
 
  // enable output ports
121
 
  // Opamp level and Opamp on/off
122
 
  DDRB = (1 << PWM1_PIN)
123
 
       | (1 << LED_ENABLE_PIN);
124
 
  // DD FET PWM, aux R/G/B, button LED
125
 
  DDRA = (1 << PWM2_PIN)
126
 
       | (1 << AUXLED_R_PIN)
127
 
       | (1 << AUXLED_G_PIN)
128
 
       | (1 << AUXLED_B_PIN)
129
 
       | (1 << BUTTON_LED_PIN)
130
 
       ;
131
 
 
132
 
  // configure PWM
133
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
134
 
  // pre-scale for timer: N = 1
135
 
  // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
136
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
137
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
138
 
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
139
 
  TCCR1A  = (1<<WGM11)  | (0<<WGM10)   // adjustable PWM (TOP=ICR1) (DS table 12-5)
140
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
141
 
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
142
 
          ;
143
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
144
 
          | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
145
 
          ;
146
 
 
147
 
  // set PWM resolution
148
 
  PWM1_TOP = PWM_TOP;
149
 
 
150
 
  // set up e-switch
151
 
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
152
 
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
153
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
146
    // enable output ports
 
147
    // Opamp level and Opamp on/off
 
148
    DDRB = (1 << CH1_PIN)
 
149
         | (1 << CH1_ENABLE_PIN);
 
150
    // DD FET PWM, aux R/G/B, button LED
 
151
    DDRA = (1 << CH2_PIN)
 
152
         | (1 << AUXLED_R_PIN)
 
153
         | (1 << AUXLED_G_PIN)
 
154
         | (1 << AUXLED_B_PIN)
 
155
         | (1 << BUTTON_LED_PIN)
 
156
         ;
 
157
 
 
158
    // configure PWM
 
159
    // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
160
    // pre-scale for timer: N = 1
 
161
    // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
 
162
    // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
163
    // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
164
    // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
 
165
    TCCR1A  = (1<<WGM11)  | (0<<WGM10)   // adjustable PWM (TOP=ICR1) (DS table 12-5)
 
166
            | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
167
            | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
168
            ;
 
169
    TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
170
            | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
 
171
            ;
 
172
 
 
173
    // set PWM resolution
 
174
    PWM_TOP = PWM_TOP_INIT;
 
175
 
 
176
    // set up e-switch
 
177
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
178
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
154
179
}
155
180
 
 
181
 
156
182
#define LAYOUT_DEFINED
157
183