~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

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

  • Committer: Selene ToyKeeper
  • Date: 2023-07-21 21:47:52 UTC
  • mto: (483.1.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20230721214752-o95ojcz2zteh8g8q
converted noctigon-k1 to multi-channel

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 * not to change brightness.
37
37
 */
38
38
 
39
 
#ifdef ATTINY
40
 
#undef ATTINY
41
 
#endif
42
39
#define ATTINY 1634
43
40
#include <avr/io.h>
44
41
 
45
 
#define PWM_CHANNELS 1
46
 
#define PWM_BITS 10  // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
47
 
#define PWM_TOP 1023
48
 
 
 
42
#ifndef HWDEF_C_FILE
 
43
#define HWDEF_C_FILE hwdef-noctigon-k1.c
 
44
#endif
 
45
 
 
46
// allow using aux LEDs as extra channel modes
 
47
#include "chan-rgbaux.h"
 
48
 
 
49
// channel modes:
 
50
// * 0. main LED
 
51
// * 1+. aux RGB
 
52
#define NUM_CHANNEL_MODES   (1 + NUM_RGB_AUX_CHANNEL_MODES)
 
53
enum CHANNEL_MODES {
 
54
    CM_MAIN = 0,
 
55
    RGB_AUX_ENUMS
 
56
};
 
57
 
 
58
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
59
 
 
60
// right-most bit first, modes are in fedcba9876543210 order
 
61
#define CHANNEL_MODES_ENABLED 0b0000000000000001
 
62
// no args
 
63
//#define USE_CHANNEL_MODE_ARGS
 
64
//#define CHANNEL_MODE_ARGS     0,0,0,0,0,0,0,0
 
65
 
 
66
 
 
67
#define PWM_CHANNELS 1  // old, remove this
 
68
 
 
69
#define PWM_BITS      10        // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
 
70
#define PWM_GET       PWM_GET16
 
71
#define PWM_DATATYPE  uint16_t  // is used for PWM_TOPS (which goes way over 255)
 
72
#define PWM_DATATYPE2 uint32_t  // only needs 32-bit if ramp values go over 255
 
73
#define PWM1_DATATYPE uint16_t  // linear ramp
 
74
 
 
75
#define PWM_TOP       ICR1   // holds the TOP value for variable-resolution PWM
 
76
#define PWM_TOP_INIT  1023   // highest value used in top half of ramp
 
77
#define PWM_CNT       TCNT1  // for dynamic PWM, reset phase
 
78
 
 
79
// linear channel
 
80
#define CH1_PIN  PB3            // pin 16, Opamp reference
 
81
#define CH1_PWM  OCR1A          // OCR1A is the output compare register for PB3
 
82
#define CH1_ENABLE_PIN   PB0    // pin 19, Opamp power
 
83
#define CH1_ENABLE_PORT  PORTB  // control port for PB0
 
84
 
 
85
// e-switch
49
86
#define SWITCH_PIN   PA7    // pin 20
50
87
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
51
88
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
52
89
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
53
90
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
54
 
 
55
 
#define PWM1_PIN PB3        // pin 16, Opamp reference
56
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
57
 
 
58
 
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
59
 
#define LED_ENABLE_PORT PORTB  // control port for PB0
 
91
#define SWITCH_PUE   PUEA   // pullup group A
 
92
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
60
93
 
61
94
 
62
95
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
78
111
// Raw ADC readings at 4.4V and 2.2V
79
112
// calibrate the voltage readout here
80
113
// estimated / calculated values are:
81
 
//   (voltage - D1) * (R2/(R2+R1) * 256 / 1.1)
 
114
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
82
115
// D1, R1, R2 = 0, 330, 100
83
116
#ifndef ADC_44
84
117
//#define ADC_44 981  // raw value at 4.40V
97
130
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
98
131
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
99
132
 
100
 
// with so many pins, doing this all with #ifdefs gets awkward...
101
 
// ... so just hardcode it in each hwdef file instead
 
133
 
102
134
inline void hwdef_setup() {
103
 
  // enable output ports
104
 
  // Opamp level and Opamp on/off
105
 
  DDRB = (1 << PWM1_PIN)
106
 
       | (1 << LED_ENABLE_PIN);
107
 
  // aux R/G/B
108
 
  DDRA = (1 << AUXLED_R_PIN)
109
 
       | (1 << AUXLED_G_PIN)
110
 
       | (1 << AUXLED_B_PIN)
111
 
       ;
112
 
 
113
 
  // configure PWM
114
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
115
 
  // pre-scale for timer: N = 1
116
 
  // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
117
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
118
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
119
 
  // COM1B[1:0]:    0,0: PWM OC1B disabled (DS table 12-4)
120
 
  TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
121
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
122
 
          | (0<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
123
 
          ;
124
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
125
 
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
126
 
          ;
127
 
 
128
 
  // set up e-switch
129
 
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
130
 
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
131
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
135
    // enable output ports
 
136
    // Opamp level and Opamp on/off
 
137
    DDRB = (1 << CH1_PIN)
 
138
         | (1 << CH1_ENABLE_PIN);
 
139
    // aux R/G/B
 
140
    DDRA = (1 << AUXLED_R_PIN)
 
141
         | (1 << AUXLED_G_PIN)
 
142
         | (1 << AUXLED_B_PIN)
 
143
         ;
 
144
 
 
145
    // configure PWM
 
146
    // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
147
    // pre-scale for timer: N = 1
 
148
    // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
 
149
    // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
150
    // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
151
    // COM1B[1:0]:    0,0: PWM OC1B disabled (DS table 12-4)
 
152
    TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
 
153
            | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
154
            | (0<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
155
            ;
 
156
    TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
157
            | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
 
158
            ;
 
159
 
 
160
    // set PWM resolution
 
161
    //PWM_TOP = PWM_TOP_INIT;
 
162
 
 
163
    // set up e-switch
 
164
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
165
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
132
166
}
133
167
 
 
168
 
134
169
#define LAYOUT_DEFINED
135
170