~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-Emisar_D4Sv2.h

  • Committer: Selene Scriven
  • Date: 2020-07-06 20:24:28 UTC
  • mfrom: (188.1.294 fsm)
  • Revision ID: bzr@toykeeper.net-20200706202428-7pyen2ow9q2rtd9p
merged nearly a year of updates from the fsm branch, including the new product map

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef HWDEF_EMISAR_D4SV2_H
 
2
#define HWDEF_EMISAR_D4SV2_H
 
3
 
 
4
/* Emisar D4Sv2 driver layout (attiny1634)
 
5
 * (same layout as D4v2, except it's a FET+3+1 instead of FET+1)
 
6
 *
 
7
 * Pin / Name / Function
 
8
 *   1    PA6   FET PWM (PWM1B)
 
9
 *   2    PA5   red aux LED (PWM0B)
 
10
 *   3    PA4   green aux LED
 
11
 *   4    PA3   blue aux LED
 
12
 *   5    PA2   e-switch
 
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   3x7135 PWM (PWM0A)
 
23
 *  16    PB3   1x7135 PWM (PWM1A)
 
24
 *  17    PB2   MISO
 
25
 *  18    PB1   MOSI
 
26
 *  19    PB0   (none)
 
27
 *  20    PA7   (none)
 
28
 *      ADC12   thermal sensor
 
29
 */
 
30
 
 
31
#ifdef ATTINY
 
32
#undef ATTINY
 
33
#endif
 
34
#define ATTINY 1634
 
35
#include <avr/io.h>
 
36
 
 
37
#define SWITCH_PIN   PA2    // pin 5
 
38
#define SWITCH_PCINT PCINT2 // pin 5 pin change interrupt
 
39
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
 
40
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
 
41
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
 
42
 
 
43
#define PWM_CHANNELS 3
 
44
 
 
45
#define PWM1_PIN PB3        // pin 16, 1x7135 PWM
 
46
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
 
47
 
 
48
#define PWM2_PIN PC0        // pin 15, 3x7135 PWM
 
49
#define PWM2_LVL OCR0A      // OCR0A is the output compare register for PC0
 
50
 
 
51
#define PWM3_PIN PA6        // pin 1, FET PWM
 
52
#define PWM3_LVL OCR1B      // OCR1B is the output compare register for PB1
 
53
 
 
54
 
 
55
#define ADC_PRSCL   0x07    // clk/128
 
56
 
 
57
// average drop across diode on this hardware
 
58
#ifndef VOLTAGE_FUDGE_FACTOR
 
59
#define VOLTAGE_FUDGE_FACTOR 4  // add 0.20V  (measured 0.22V)
 
60
#endif
 
61
 
 
62
// this light has aux LEDs under the optic
 
63
#define AUXLED_R_PIN   PA5    // pin 2
 
64
#define AUXLED_G_PIN   PA4    // pin 3
 
65
#define AUXLED_B_PIN   PA3    // pin 4
 
66
#define AUXLED_RGB_PORT PORTA // PORTA or PORTB or PORTC
 
67
#define AUXLED_RGB_DDR DDRA   // DDRA or DDRB or DDRC
 
68
#define AUXLED_RGB_PUE PUEA   // PUEA or PUEB or PUEC
 
69
 
 
70
// with so many pins, doing this all with #ifdefs gets awkward...
 
71
// ... so just hardcode it in each hwdef file instead
 
72
inline void hwdef_setup() {
 
73
  // enable output ports
 
74
  // FET, aux R/G/B
 
75
  DDRA = (1 << PWM3_PIN)
 
76
       | (1 << AUXLED_R_PIN)
 
77
       | (1 << AUXLED_G_PIN)
 
78
       | (1 << AUXLED_B_PIN)
 
79
       ;
 
80
  // 1x7135
 
81
  DDRB = (1 << PWM1_PIN);
 
82
  // 3x7135
 
83
  DDRC = (1 << PWM2_PIN);
 
84
 
 
85
  // configure PWM
 
86
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
87
  // pre-scale for timer: N = 1
 
88
  // WGM1[3:0]: 0,0,0,1: PWM, Phase Correct, 8-bit (DS table 12-5)
 
89
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
90
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
91
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
 
92
  TCCR1A  = (0<<WGM11)  | (1<<WGM10)   // 8-bit (TOP=0xFF) (DS table 12-5)
 
93
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
94
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
95
          ;
 
96
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
97
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
 
98
          ;
 
99
 
 
100
  // WGM0[2:0]: 0,0,1: PWM, Phase Correct (DS table 11-8)
 
101
  // CS0[2:0]:  0,0,1: clk/1 (No prescaling) (DS table 11-9)
 
102
  // COM0A[1:0]:  1,0: PWM OC0A in the normal direction (DS table 11-4)
 
103
  // COM0B[1:0]:  0,0: OC0B disabled (DS table 11-7)
 
104
  // TCCR0A: COM0A1, COM0A0, COM0B1, COM0B0, -, -, WGM01, WGM00
 
105
  TCCR0A  = (0<<WGM01)  | (1<<WGM00)   // PWM, Phase Correct, TOP=0xFF (DS table 11-5)
 
106
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 0A in normal direction (DS table 11-4)
 
107
          | (0<<COM1B1) | (0<<COM1B0)  // PWM 0B disabled (DS table 11-7)
 
108
          ;
 
109
  // TCCR0B: FOC0A, FOC0B, -, -, WGM02, CS02, CS01, CS00
 
110
  TCCR0B  = (0<<CS02)   | (0<<CS01) | (1<<CS00)  // clk/1 (no prescaling) (DS table 11-9)
 
111
          | (0<<WGM02)  // PWM, Phase Correct, TOP=0xFF (DS table 11-8)
 
112
          ;
 
113
 
 
114
  // set up e-switch
 
115
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
 
116
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
 
117
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
118
}
 
119
 
 
120
#define LAYOUT_DEFINED
 
121
 
 
122
#endif