~toykeeper/flashlight-firmware/trunk

188.12.1 by Selene Scriven
merged a sanitized copy of the Emisar D4v2 branch; history summarized below:
1
#ifndef HWDEF_EMISAR_D4V2_H
2
#define HWDEF_EMISAR_D4V2_H
3
4
/* Emisar D4v2 driver layout (attiny1634)
5
 *
6
 * Pin / Name / Function
7
 *   1    PA6   FET PWM (PWM1B)
8
 *   2    PA5   red aux LED (PWM0B)
9
 *   3    PA4   green aux LED
10
 *   4    PA3   blue aux LED
11
 *   5    PA2   e-switch
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   7135 PWM (PWM1A)
23
 *  17    PB2   MISO
24
 *  18    PB1   MOSI
25
 *  19    PB0   (none)
26
 *  20    PA7   (none)
27
 *      ADC12   thermal sensor
28
 */
29
30
#ifdef ATTINY
31
#undef ATTINY
32
#endif
33
#define ATTINY 1634
34
#include <avr/io.h>
35
36
#define PWM_CHANNELS 2
37
38
#define SWITCH_PIN   PA2    // pin 5
39
#define SWITCH_PCINT PCINT2 // pin 5 pin change interrupt
40
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
41
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
42
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
43
44
#define PWM1_PIN PB3        // pin 16, 1x7135 PWM
45
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
46
47
#define PWM2_PIN PA6        // pin 1, FET PWM
48
#define PWM2_LVL OCR1B      // OCR1B is the output compare register for PB1
49
50
51
#define ADC_PRSCL   0x06    // clk/64
52
53
// average drop across diode on this hardware
54
#ifndef VOLTAGE_FUDGE_FACTOR
55
#define VOLTAGE_FUDGE_FACTOR 4  // add 0.20V  (measured 0.22V)
56
#endif
57
58
#define TEMP_CHANNEL 0b00001111
59
60
// this light has aux LEDs under the optic
61
#define AUXLED_R_PIN   PA5    // pin 2
62
#define AUXLED_G_PIN   PA4    // pin 3
63
#define AUXLED_B_PIN   PA3    // pin 4
64
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
65
#define AUXLED_RGB_DDR DDRA   // DDRA or DDRB or DDRC
66
#define AUXLED_RGB_PUE PUEA   // PUEA or PUEB or PUEC
67
68
// with so many pins, doing this all with #ifdefs gets awkward...
69
// ... so just hardcode it in each hwdef file instead
70
inline void hwdef_setup() {
71
  // enable output ports
72
  // 7135
73
  DDRB = (1 << PWM1_PIN);
74
  // FET, aux R/G/B
75
  DDRA = (1 << PWM2_PIN)
76
       | (1 << AUXLED_R_PIN)
77
       | (1 << AUXLED_G_PIN)
78
       | (1 << AUXLED_B_PIN)
79
       ;
80
81
  // configure PWM
82
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
83
  // pre-scale for timer: N = 1
84
  TCCR1A  = (0<<WGM11)  | (1<<WGM10)   // 8-bit (TOP=0xFF) (DS table 12-5)
85
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
86
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
87
          ;
88
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
89
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
90
          ;
91
92
  // set up e-switch
93
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
94
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
95
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
96
}
97
98
#define LAYOUT_DEFINED
99
100
#endif