~digitalcircuit/flashlight-firmware/anduril2_define_autolock

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-Noctigon_K9.3.h

  • Committer: Selene Scriven
  • Date: 2021-01-25 05:21:06 UTC
  • mfrom: (575.1.13 k9.3)
  • Revision ID: bzr@toykeeper.net-20210125052106-g6la42mdcu9dl42r
merged noctigon k9.3 branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef HWDEF_NOCTIGON_K93_H
 
2
#define HWDEF_NOCTIGON_K93_H
 
3
 
 
4
/* Noctigon K9.3 driver layout (attiny1634)
 
5
 *
 
6
 * Pin / Name / Function
 
7
 *   1    PA6   2nd LED PWM (linear) (PWM1B)
 
8
 *   2    PA5   R: red aux LED (PWM0B)
 
9
 *   3    PA4   G: green aux LED
 
10
 *   4    PA3   B: blue aux LED
 
11
 *   5    PA2   button LED
 
12
 *   6    PA1   Opamp 2 enable (2nd LEDs)
 
13
 *   7    PA0   Opamp 1 enable (main LEDs)
 
14
 *   8    GND   GND
 
15
 *   9    VCC   VCC
 
16
 *  10    PC5   (none)
 
17
 *  11    PC4   (none)
 
18
 *  12    PC3   RESET
 
19
 *  13    PC2   (none)
 
20
 *  14    PC1   SCK
 
21
 *  15    PC0   main LED PWM (FET) (PWM0A)
 
22
 *  16    PB3   main LED PWM (linear) (PWM1A)
 
23
 *  17    PB2   MISO
 
24
 *  18    PB1   MOSI / battery voltage (ADC6)
 
25
 *  19    PB0   (none)
 
26
 *  20    PA7   e-switch (PCINT7)
 
27
 *      ADC12   thermal sensor
 
28
 *
 
29
 * Main LED power uses one pin to turn the Opamp on/off,
 
30
 * and one pin to control Opamp power level.
 
31
 * Main brightness control uses the power level pin, with 4 kHz 10-bit PWM.
 
32
 * The on/off pin is only used to turn the main LED on and off,
 
33
 * not to change brightness.
 
34
 * Some models also have a direct-drive FET for turbo.
 
35
 */
 
36
 
 
37
#ifdef ATTINY
 
38
#undef ATTINY
 
39
#endif
 
40
#define ATTINY 1634
 
41
#include <avr/io.h>
 
42
 
 
43
#define PWM_CHANNELS 3  // 2 for main LEDs, 1 for 2nd LEDs
 
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   // PCIE1 is for PCINT[7:0]
 
50
#define SWITCH_PCMSK PCMSK0  // PCMSK1 is for PCINT[7:0]
 
51
#define SWITCH_PORT  PINA    // PINA or PINB or PINC
 
52
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
 
53
 
 
54
#define PWM1_PIN PB3        // pin 16, Opamp reference
 
55
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
 
56
 
 
57
#define PWM2_PIN PC0        // pin 15, DD FET PWM
 
58
#define PWM2_LVL OCR0A      // OCR0A is the output compare register for PC0
 
59
 
 
60
#define PWM3_PIN PA6        // pin 1, 2nd LED Opamp reference
 
61
#define PWM3_LVL OCR1B      // OCR1B is the output compare register for PA6
 
62
 
 
63
#define LED_ENABLE_PIN  PA0    // pin 7, Opamp power
 
64
#define LED_ENABLE_PORT PORTA  // control port for PA0
 
65
 
 
66
#define LED2_ENABLE_PIN  PA1    // pin 6, Opamp power
 
67
#define LED2_ENABLE_PORT PORTA  // control port for PA1
 
68
 
 
69
 
 
70
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
 
71
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
 
72
// pin to ADC mappings are in DS table 19-4
 
73
#define VOLTAGE_ADC      ADC6D  // digital input disable pin for PB1
 
74
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
 
75
#define VOLTAGE_ADC_DIDR DIDR1  // DIDR channel for ADC6D
 
76
// DS tables 19-3, 19-4
 
77
// Bit   7     6     5      4     3    2    1    0
 
78
//     REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
 
79
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
 
80
// divided by ...
 
81
// REFS[1:0] = 1, 0 for internal 1.1V reference
 
82
// other bits reserved
 
83
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
 
84
#define ADC_PRSCL   0x07    // clk/128
 
85
 
 
86
// Raw ADC readings at 4.4V and 2.2V
 
87
// calibrate the voltage readout here
 
88
// estimated / calculated values are:
 
89
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
 
90
// D1, R1, R2 = 0, 330, 100
 
91
#ifndef ADC_44
 
92
//#define ADC_44 981  // raw value at 4.40V
 
93
#define ADC_44 967  // manually tweaked so 4.16V will blink out 4.2
 
94
#endif
 
95
#ifndef ADC_22
 
96
//#define ADC_22 489  // raw value at 2.20V
 
97
#define ADC_22 482  // manually tweaked so 4.16V will blink out 4.2
 
98
#endif
 
99
 
 
100
// this light has aux LEDs under the optic
 
101
#define AUXLED_R_PIN    PA5    // pin 2
 
102
#define AUXLED_G_PIN    PA4    // pin 3
 
103
#define AUXLED_B_PIN    PA3    // pin 4
 
104
#define AUXLED_RGB_PORT PORTA  // PORTA or PORTB or PORTC
 
105
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
 
106
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
 
107
 
 
108
#define BUTTON_LED_PIN  PA2    // pin 5
 
109
#define BUTTON_LED_PORT PORTA  // for all "PA" pins
 
110
#define BUTTON_LED_DDR  DDRA   // for all "PA" pins
 
111
#define BUTTON_LED_PUE  PUEA   // for all "PA" pins
 
112
 
 
113
// with so many pins, doing this all with #ifdefs gets awkward...
 
114
// ... so just hardcode it in each hwdef file instead
 
115
inline void hwdef_setup() {
 
116
  // enable output ports
 
117
  DDRC = (1 << PWM2_PIN);
 
118
  DDRB = (1 << PWM1_PIN);
 
119
  DDRA = (1 << PWM3_PIN)
 
120
       | (1 << AUXLED_R_PIN)
 
121
       | (1 << AUXLED_G_PIN)
 
122
       | (1 << AUXLED_B_PIN)
 
123
       | (1 << BUTTON_LED_PIN)
 
124
       | (1 << LED_ENABLE_PIN)
 
125
       | (1 << LED2_ENABLE_PIN)
 
126
       ;
 
127
 
 
128
  // configure PWM
 
129
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
130
  // pre-scale for timer: N = 1
 
131
  // Linear opamp PWM for both main and 2nd LEDs (10-bit)
 
132
  // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
 
133
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
134
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
135
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
 
136
  TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
 
137
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
138
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
139
          ;
 
140
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
141
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
 
142
          ;
 
143
 
 
144
  // FET PWM (8-bit; this channel can't do 10-bit)
 
145
  // WGM0[2:0]: 0,0,1: PWM, Phase Correct, 8-bit (DS table 11-8)
 
146
  // CS0[2:0]:  0,0,1: clk/1 (No prescaling) (DS table 11-9)
 
147
  // COM0A[1:0]:  1,0: PWM OC0A in the normal direction (DS table 11-4)
 
148
  // COM0B[1:0]:  1,0: PWM OC0B in the normal direction (DS table 11-7)
 
149
  TCCR0A  = (0<<WGM01)  | (1<<WGM00)   // 8-bit (TOP=0xFF) (DS table 11-8)
 
150
          | (1<<COM0A1) | (0<<COM0A0)  // PWM 0A in normal direction (DS table 11-4)
 
151
          //| (1<<COM0B1) | (0<<COM0B0)  // PWM 0B in normal direction (DS table 11-7)
 
152
          ;
 
153
  TCCR0B  = (0<<CS02)   | (0<<CS01) | (1<<CS00)  // clk/1 (no prescaling) (DS table 11-9)
 
154
          | (0<<WGM02)  // phase-correct PWM (DS table 11-8)
 
155
          ;
 
156
 
 
157
  // set up e-switch
 
158
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
 
159
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
160
}
 
161
 
 
162
#define LAYOUT_DEFINED
 
163
 
 
164
#endif