~toykeeper/flashlight-firmware/trunk

188.18.9 by Selene Scriven
added config which matches the spec for K1-SBT90.2, but it's not tested (there's no hardware to test on)
1
#ifndef HWDEF_NOCTIGON_K1SBT90_H
2
#define HWDEF_NOCTIGON_K1SBT90_H
3
4
/* Noctigon K1-SBT90.2 driver layout (attiny1634)
5
 * (mostly the same as KR4 driver)
6
 *
7
 * Pin / Name / Function
8
 *   1    PA6   FET PWM (direct drive) (PWM1B)
9
 *   2    PA5   R: red aux LED (PWM0B)
10
 *   3    PA4   G: green aux LED
11
 *   4    PA3   B: blue aux LED
12
 *   5    PA2   (none)
13
 *   6    PA1   (none)
14
 *   7    PA0   (none)
15
 *   8    GND   GND
16
 *   9    VCC   VCC
17
 *  10    PC5   (none)
18
 *  11    PC4   (none)
19
 *  12    PC3   RESET
20
 *  13    PC2   (none)
21
 *  14    PC1   SCK
22
 *  15    PC0   (none) PWM0A
23
 *  16    PB3   main LED PWM (linear) (PWM1A)
24
 *  17    PB2   MISO / e-switch (PCINT10)
25
 *  18    PB1   MOSI / battery voltage (ADC6)
26
 *  19    PB0   Opamp power
27
 *  20    PA7   (none)
28
 *      ADC12   thermal sensor
29
 *
30
 * Main LED power uses one pin to turn the Opamp on/off,
31
 * and one pin to control Opamp power level.
32
 * Main brightness control uses the power level pin, with 4 kHz 10-bit PWM.
33
 * The on/off pin is only used to turn the main LED on and off,
34
 * not to change brightness.
35
 * Also has a direct-drive FET for turbo.
36
 */
37
38
#ifdef ATTINY
39
#undef ATTINY
40
#endif
41
#define ATTINY 1634
42
#include <avr/io.h>
43
44
#define PWM_CHANNELS 2
45
#define PWM_BITS 10  // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
46
#define PWM_TOP 1023
47
48
#define SWITCH_PIN   PB2     // pin 17
49
#define SWITCH_PCINT PCINT10 // pin 17 pin change interrupt
50
#define SWITCH_PCIE  PCIE1   // PCIE1 is for PCINT[11:8]
51
#define SWITCH_PCMSK PCMSK1  // PCMSK1 is for PCINT[11:8]
52
#define SWITCH_PORT  PINB    // PINA or PINB or PINC
53
#define PCINT_vect   PCINT1_vect  // ISR for PCINT[11:8]
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 PWM2_PIN PA6        // pin 1, DD FET PWM
59
#define PWM2_LVL OCR1B      // OCR1B is the output compare register for PA6
60
61
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
62
#define LED_ENABLE_PORT PORTB  // control port for PB0
63
64
65
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
66
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
67
// pin to ADC mappings are in DS table 19-4
68
#define VOLTAGE_ADC      ADC6D  // digital input disable pin for PB1
69
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
70
#define VOLTAGE_ADC_DIDR DIDR1  // DIDR channel for ADC6D
71
// DS tables 19-3, 19-4
72
// Bit   7     6     5      4     3    2    1    0
73
//     REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
74
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
75
// divided by ...
76
// REFS[1:0] = 1, 0 for internal 1.1V reference
77
// other bits reserved
78
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
79
#define ADC_PRSCL   0x07    // clk/128
80
81
// TODO: calibrate this
82
// Raw ADC readings at 4.4V and 2.2V
83
// calibrate the voltage readout here
84
// estimated / calculated values are:
85
//   (voltage - D1) * (R2/(R2+R1) * 256 / 1.1)
86
// D1, R1, R2 = 0, 330, 100
87
#ifndef ADC_44
88
//#define ADC_44 981  // raw value at 4.40V
89
#define ADC_44 967  // manually tweaked so 4.16V will blink out 4.2
90
#endif
91
#ifndef ADC_22
92
//#define ADC_22 489  // raw value at 2.20V
93
#define ADC_22 482  // manually tweaked so 4.16V will blink out 4.2
94
#endif
95
96
#define TEMP_CHANNEL 0b00001111
97
98
// this light has aux LEDs under the optic
99
#define AUXLED_R_PIN    PA5    // pin 2
100
#define AUXLED_G_PIN    PA4    // pin 3
101
#define AUXLED_B_PIN    PA3    // pin 4
102
#define AUXLED_RGB_PORT PORTA  // PORTA or PORTB or PORTC
103
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
104
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
105
106
// with so many pins, doing this all with #ifdefs gets awkward...
107
// ... so just hardcode it in each hwdef file instead
108
inline void hwdef_setup() {
109
  // enable output ports
110
  // Opamp level and Opamp on/off
111
  DDRB = (1 << PWM1_PIN)
112
       | (1 << LED_ENABLE_PIN);
113
  // DD FET PWM, aux R/G/B
114
  DDRA = (1 << PWM2_PIN)
115
       | (1 << AUXLED_R_PIN)
116
       | (1 << AUXLED_G_PIN)
117
       | (1 << AUXLED_B_PIN)
118
       ;
119
120
  // configure PWM
121
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
122
  // pre-scale for timer: N = 1
123
  // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
124
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
125
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
126
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
127
  TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
128
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
129
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
130
          ;
131
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
132
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
133
          ;
134
135
  // set up e-switch
136
  //PORTB = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
137
  PUEB = (1 << SWITCH_PIN);  // pull-up for e-switch
138
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
139
}
140
141
#define LAYOUT_DEFINED
142
143
#endif