~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-Noctigon_KR4.h

  • Committer: Selene Scriven
  • Date: 2015-03-17 08:56:50 UTC
  • mto: This revision was merged to the branch mainline in revision 124.
  • Revision ID: ubuntu@toykeeper.net-20150317085650-s89wr9h28n2co7z1
Added TheStar firmwares from _the_

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef HWDEF_NOCTIGON_KR4_H
2
 
#define HWDEF_NOCTIGON_KR4_H
3
 
 
4
 
/* Noctigon KR4 driver layout (attiny1634)
5
 
 *
6
 
 * Pin / Name / Function
7
 
 *   1    PA6   FET PWM (direct drive) (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   (none)
12
 
 *   6    PA1   (none)
13
 
 *   7    PA0   (none)
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   (none) PWM0A
22
 
 *  16    PB3   main LED PWM (linear) (PWM1A)
23
 
 *  17    PB2   MISO / e-switch (PCINT10)
24
 
 *  18    PB1   MOSI / battery voltage (ADC6)
25
 
 *  19    PB0   Opamp power
26
 
 *  20    PA7   (none)
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 2
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   PB2     // pin 17
48
 
#define SWITCH_PCINT PCINT10 // pin 17 pin change interrupt
49
 
#define SWITCH_PCIE  PCIE1   // PCIE1 is for PCINT[11:8]
50
 
#define SWITCH_PCMSK PCMSK1  // PCMSK1 is for PCINT[11:8]
51
 
#define SWITCH_PORT  PINB    // PINA or PINB or PINC
52
 
#define PCINT_vect   PCINT1_vect  // ISR for PCINT[11:8]
53
 
 
54
 
// the button tends to short out the voltage divider,
55
 
// so ignore voltage while the button is being held
56
 
//#define NO_LVP_WHILE_BUTTON_PRESSED
57
 
 
58
 
 
59
 
#define PWM1_PIN PB3        // pin 16, Opamp reference
60
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
61
 
 
62
 
#define PWM2_PIN PA6        // pin 1, DD FET PWM
63
 
#define PWM2_LVL OCR1B      // OCR1B is the output compare register for PA6
64
 
 
65
 
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
66
 
#define LED_ENABLE_PORT PORTB  // control port for PB0
67
 
 
68
 
 
69
 
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
70
 
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
71
 
// pin to ADC mappings are in DS table 19-4
72
 
#define VOLTAGE_ADC      ADC6D  // digital input disable pin for PB1
73
 
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
74
 
#define VOLTAGE_ADC_DIDR DIDR1  // DIDR channel for ADC6D
75
 
// DS tables 19-3, 19-4
76
 
// Bit   7     6     5      4     3    2    1    0
77
 
//     REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
78
 
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
79
 
// divided by ...
80
 
// REFS[1:0] = 1, 0 for internal 1.1V reference
81
 
// other bits reserved
82
 
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
83
 
#define ADC_PRSCL   0x07    // clk/128
84
 
 
85
 
// TODO: calibrate this
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) * 256 / 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
 
// with so many pins, doing this all with #ifdefs gets awkward...
109
 
// ... so just hardcode it in each hwdef file instead
110
 
inline void hwdef_setup() {
111
 
  // enable output ports
112
 
  // Opamp level and Opamp on/off
113
 
  DDRB = (1 << PWM1_PIN)
114
 
       | (1 << LED_ENABLE_PIN);
115
 
  // DD FET PWM, aux R/G/B
116
 
  DDRA = (1 << PWM2_PIN)
117
 
       | (1 << AUXLED_R_PIN)
118
 
       | (1 << AUXLED_G_PIN)
119
 
       | (1 << AUXLED_B_PIN)
120
 
       ;
121
 
 
122
 
  // configure PWM
123
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
124
 
  // pre-scale for timer: N = 1
125
 
  // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
126
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
127
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
128
 
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
129
 
  TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
130
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
131
 
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
132
 
          ;
133
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
134
 
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
135
 
          ;
136
 
 
137
 
  // set up e-switch
138
 
  //PORTB = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
139
 
  PUEB = (1 << SWITCH_PIN);  // pull-up for e-switch
140
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
141
 
}
142
 
 
143
 
#define LAYOUT_DEFINED
144
 
 
145
 
#endif