~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-noctigon-k1-boost.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_NOCTIGON_K1_12V_H
2
 
#define HWDEF_NOCTIGON_K1_12V_H
 
1
// Noctigon K1 12V 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
 
/* Noctigon K1 driver layout (attiny1634)
5
 
 * (originally known as Emisar D1S V2)
6
 
 *
 
6
/*
7
7
 * Pin / Name / Function
8
8
 *   1    PA6   (none) (PWM1B) (reserved for DD drivers)
9
9
 *   2    PA5   R: red aux LED (PWM0B)
29
29
 *
30
30
 * Main LED power uses one pin to turn the Opamp on/off,
31
31
 * and one pin to control Opamp power level.
32
 
 * All brightness control uses the power level pin, with 4 kHz 10-bit PWM.
 
32
 * Regulated brightness control uses the power level pin, with PWM+DSM.
33
33
 * The on/off pin is only used to turn the main LED on and off,
34
34
 * not to change brightness.
35
35
 */
36
36
 
37
 
#ifdef ATTINY
38
 
#undef ATTINY
39
 
#endif
40
37
#define ATTINY 1634
41
38
#include <avr/io.h>
42
39
 
43
 
#define PWM_CHANNELS 1
44
 
#define PWM_BITS 10  // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
45
 
#define PWM_TOP 1023
46
 
 
47
 
#define SWITCH_PIN   PA7    // pin 20
48
 
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
49
 
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
50
 
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
51
 
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
52
 
 
53
 
#define PWM1_PIN PB3        // pin 16, Opamp reference
54
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
55
 
 
56
 
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
57
 
#define LED_ENABLE_PORT PORTB  // control port for PB0
58
 
 
59
 
#define LED_ENABLE2_PIN  PC0    // pin 15, boost PMIC enable
60
 
#define LED_ENABLE2_PORT PORTC  // control port for PC0
61
 
 
 
40
#define HWDEF_C_FILE hwdef-noctigon-dm11-boost.c
 
41
 
 
42
// allow using aux LEDs as extra channel modes
 
43
#include "chan-rgbaux.h"
 
44
 
 
45
// channel modes:
 
46
// * 0. main LEDs
 
47
// * 1+. aux RGB
 
48
#define NUM_CHANNEL_MODES   (1 + NUM_RGB_AUX_CHANNEL_MODES)
 
49
enum CHANNEL_MODES {
 
50
    CM_MAIN = 0,
 
51
    RGB_AUX_ENUMS
 
52
};
 
53
 
 
54
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
55
 
 
56
// right-most bit first, modes are in fedcba9876543210 order
 
57
#define CHANNEL_MODES_ENABLED 0b0000000000000001
 
58
// no args
 
59
//#define USE_CHANNEL_MODE_ARGS
 
60
//#define CHANNEL_MODE_ARGS     0,0,0,0,0,0,0,0
 
61
 
 
62
 
 
63
#define PWM_CHANNELS  1  // old, remove this
 
64
 
 
65
#define PWM_BITS      16        // 0 to 32640 (0 to 255 PWM + 0 to 127 DSM) at constant kHz
 
66
#define PWM_GET       PWM_GET16
 
67
#define PWM_DATATYPE  uint16_t
 
68
#define PWM_DATATYPE2 uint32_t  // only needs 32-bit if ramp values go over 255
 
69
#define PWM1_DATATYPE uint16_t  // 15-bit PWM+DSM ramp
 
70
 
 
71
#define PWM_TOP       ICR1   // holds the TOP value for variable-resolution PWM
 
72
#define PWM_TOP_INIT  255
 
73
#define PWM_CNT       TCNT1  // for checking / resetting phase
 
74
// (max is (255 << 7), because it's 8-bit PWM plus 7 bits of DSM)
 
75
#define DSM_TOP       (255<<7) // 15-bit resolution leaves 1 bit for carry
 
76
 
 
77
// timer interrupt for DSM
 
78
#define DSM_vect     TIMER1_OVF_vect
 
79
#define DSM_INTCTRL  TIMSK
 
80
#define DSM_OVF_bm   (1<<TOIE1)
 
81
 
 
82
#define DELAY_FACTOR 90  // less time in delay() because more time spent in interrupts
 
83
 
 
84
// regulated channel
 
85
uint16_t ch1_dsm_lvl;
 
86
uint8_t ch1_pwm, ch1_dsm;
 
87
#define CH1_PIN          PB3    // pin 16, Opamp reference
 
88
#define CH1_PWM          OCR1A  // OCR1A is the output compare register for PB3
 
89
 
 
90
#define CH1_ENABLE_PIN   PB0    // pin 19, Opamp power
 
91
#define CH1_ENABLE_PORT  PORTB  // control port for PB0
 
92
 
 
93
#define CH1_ENABLE_PIN2  PC0   // pin 15, boost PMIC enable
 
94
#define CH1_ENABLE_PORT2 PORTC // control port for PC0
 
95
 
 
96
// e-switch
 
97
#define SWITCH_PIN   PA7     // pin 20
 
98
#define SWITCH_PCINT PCINT7  // pin 20 pin change interrupt
 
99
#define SWITCH_PCIE  PCIE0   // PCIE0 is for PCINT[7:0]
 
100
#define SWITCH_PCMSK PCMSK0  // PCMSK0 is for PCINT[7:0]
 
101
#define SWITCH_PORT  PINA    // PINA or PINB or PINC
 
102
#define SWITCH_PUE   PUEA    // pullup group A
 
103
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
62
104
 
63
105
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
64
106
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
79
121
// Raw ADC readings at 4.4V and 2.2V
80
122
// calibrate the voltage readout here
81
123
// estimated / calculated values are:
82
 
//   (voltage - D1) * (R2/(R2+R1) * 256 / 1.1)
 
124
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
83
125
// D1, R1, R2 = 0, 330, 100
84
126
#ifndef ADC_44
85
127
//#define ADC_44 981  // raw value at 4.40V
98
140
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
99
141
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
100
142
 
101
 
// with so many pins, doing this all with #ifdefs gets awkward...
102
 
// ... so just hardcode it in each hwdef file instead
103
143
inline void hwdef_setup() {
104
 
  // enable output ports
105
 
  // boost PMIC on/off
106
 
  DDRC = (1 << LED_ENABLE2_PIN);
107
 
  // Opamp level and Opamp on/off
108
 
  DDRB = (1 << PWM1_PIN)
109
 
       | (1 << LED_ENABLE_PIN);
110
 
  // aux R/G/B
111
 
  DDRA = (1 << AUXLED_R_PIN)
112
 
       | (1 << AUXLED_G_PIN)
113
 
       | (1 << AUXLED_B_PIN)
114
 
       ;
115
 
 
116
 
  // configure PWM
117
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
118
 
  // pre-scale for timer: N = 1
119
 
  // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
120
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
121
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
122
 
  // COM1B[1:0]:    0,0: PWM OC1B disabled (DS table 12-4)
123
 
  TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
124
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
125
 
          | (0<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
126
 
          ;
127
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
128
 
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
129
 
          ;
130
 
 
131
 
  // set up e-switch
132
 
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
133
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
144
    // enable output ports
 
145
    // boost PMIC on/off
 
146
    DDRC = (1 << CH1_ENABLE_PIN2);
 
147
    // Opamp level and Opamp on/off
 
148
    DDRB = (1 << CH1_PIN)
 
149
         | (1 << CH1_ENABLE_PIN);
 
150
    // aux R/G/B
 
151
    DDRA = (1 << AUXLED_R_PIN)
 
152
         | (1 << AUXLED_G_PIN)
 
153
         | (1 << AUXLED_B_PIN)
 
154
         ;
 
155
 
 
156
    // configure PWM
 
157
    // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
158
    // pre-scale for timer: N = 1
 
159
    // PWM for both channels
 
160
    // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
 
161
    // WGM1[3:0]: 1,1,1,0: PWM, Fast, 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)  | (1<<WGM12)  // fast adjustable PWM (DS table 12-5)
 
171
           | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
 
172
           ;
 
173
 
 
174
    // set PWM resolution
 
175
    PWM_TOP = PWM_TOP_INIT;
 
176
 
 
177
    // set up interrupt for delta-sigma modulation
 
178
    // (moved to hwdef.c functions so it can be enabled/disabled based on ramp level)
 
179
    //DSM_INTCTRL |= DSM_OVF_bm;  // interrupt once for each timer cycle
 
180
 
 
181
    // set up e-switch
 
182
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
183
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
134
184
}
135
185
 
 
186
 
136
187
#define LAYOUT_DEFINED
137
188
 
138
 
#endif