~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-Noctigon_DM11-SBT90.h

  • Committer: Selene ToyKeeper
  • Date: 2023-07-23 00:08:12 UTC
  • mto: (483.1.175 anduril2)
  • mto: This revision was merged to the branch mainline in revision 491.
  • Revision ID: bzr@toykeeper.net-20230723000812-lae16ybrvfm1lvlf
converted noctigon-dm11-* builds to multi-channel
(but I could only test dm11-boost on actual hardware)
(also, it looks like dm11-sbt90 is almost identical to the base kr4 build,
 so I removed its hwdef)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Noctigon DM11-SBT90.2 driver layout (attiny1634)
2
 
// Copyright (C) 2021-2023 Selene ToyKeeper
3
 
// SPDX-License-Identifier: GPL-3.0-or-later
4
 
#pragma once
5
 
 
6
 
/*
7
 
 * (based on Noctigon K1 and DM11)
8
 
 *
9
 
 * Pin / Name / Function
10
 
 *   1    PA6   FET PWM (direct drive) (PWM1B)
11
 
 *   2    PA5   R: red aux LED (PWM0B)
12
 
 *   3    PA4   G: green aux LED
13
 
 *   4    PA3   B: blue aux LED
14
 
 *   5    PA2   L: button LED
15
 
 *   6    PA1   (none)
16
 
 *   7    PA0   (none)
17
 
 *   8    GND   GND
18
 
 *   9    VCC   VCC
19
 
 *  10    PC5   (none)
20
 
 *  11    PC4   (none)
21
 
 *  12    PC3   RESET
22
 
 *  13    PC2   (none)
23
 
 *  14    PC1   SCK
24
 
 *  15    PC0   (none) PWM0A
25
 
 *  16    PB3   main LED PWM (PWM1A)
26
 
 *  17    PB2   MISO / e-switch (PCINT10)
27
 
 *  18    PB1   MOSI / battery voltage (ADC6)
28
 
 *  19    PB0   Opamp power
29
 
 *  20    PA7   (none)  (PCINT7)
30
 
 *      ADC12   thermal sensor
31
 
 *
32
 
 * Main LED power uses one pin to turn the Opamp on/off,
33
 
 * and one pin to control Opamp power level.
34
 
 * Linear brightness control uses the power level pin, with dynamic PWM.
35
 
 * The on/off pin is only used to turn the main LED on and off,
36
 
 * not to change brightness.
37
 
 * Also has a direct-drive FET for turbo.
38
 
 */
39
 
 
40
 
#ifdef ATTINY
41
 
#undef ATTINY
42
 
#endif
43
 
#define ATTINY 1634
44
 
#include <avr/io.h>
45
 
 
46
 
#define PWM_CHANNELS 2  // override this for the no-FET version
47
 
#define PWM_BITS 16  // data type needs 16 bits, not 8
48
 
#define PWM_TOP  255 // highest value used in top half of ramp
49
 
#define USE_DYN_PWM  // dynamic frequency and speed
50
 
 
51
 
#define SWITCH_PIN   PB2     // pin 17
52
 
#define SWITCH_PCINT PCINT10 // pin 17 pin change interrupt
53
 
#define SWITCH_PCIE  PCIE1   // PCIE1 is for PCINT[11:8]
54
 
#define SWITCH_PCMSK PCMSK1  // PCMSK1 is for PCINT[11:8]
55
 
#define SWITCH_PORT  PINB    // PINA or PINB or PINC
56
 
#define PCINT_vect   PCINT1_vect  // ISR for PCINT[11:8]
57
 
 
58
 
#define PWM1_PIN PB3        // pin 16, Opamp reference
59
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
60
 
#define PWM1_CNT TCNT1      // for dynamic PWM, reset phase
61
 
#define PWM1_PHASE_RESET_OFF  // force reset while shutting off
62
 
#define PWM1_PHASE_RESET_ON   // force reset while turning on
63
 
#define PWM1_PHASE_SYNC       // manual sync while changing level
64
 
 
65
 
#define PWM2_PIN PA6        // pin 1, DD FET PWM
66
 
#define PWM2_LVL OCR1B      // OCR1B is the output compare register for PA6
67
 
 
68
 
// PWM parameters of both channels are tied together because they share a counter
69
 
#define PWM1_TOP ICR1       // holds the TOP value for for variable-resolution PWM
70
 
 
71
 
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
72
 
#define LED_ENABLE_PORT PORTB  // control port for PB0
73
 
 
74
 
 
75
 
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
76
 
#define VOLTAGE_PIN PB1      // Pin 18 / PB1 / ADC6
77
 
// pin to ADC mappings are in DS table 19-4
78
 
#define VOLTAGE_ADC      ADC6D  // digital input disable pin for PB1
79
 
// DIDR0/DIDR1 mappings are in DS section 19.13.5, 19.13.6
80
 
#define VOLTAGE_ADC_DIDR DIDR1  // DIDR channel for ADC6D
81
 
// DS tables 19-3, 19-4
82
 
// Bit   7     6     5      4     3    2    1    0
83
 
//     REFS1 REFS0 REFEN ADC0EN MUX3 MUX2 MUX1 MUX0
84
 
// MUX[3:0] = 0, 1, 1, 0 for ADC6 / PB1
85
 
// divided by ...
86
 
// REFS[1:0] = 1, 0 for internal 1.1V reference
87
 
// other bits reserved
88
 
#define ADMUX_VOLTAGE_DIVIDER 0b10000110
89
 
#define ADC_PRSCL   0x07    // clk/128
90
 
 
91
 
// Raw ADC readings at 4.4V and 2.2V
92
 
// calibrate the voltage readout here
93
 
// estimated / calculated values are:
94
 
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
95
 
// D1, R1, R2 = 0, 330, 100
96
 
#ifndef ADC_44
97
 
//#define ADC_44 981  // raw value at 4.40V
98
 
#define ADC_44 967  // manually tweaked so 4.16V will blink out 4.2
99
 
#endif
100
 
#ifndef ADC_22
101
 
//#define ADC_22 489  // raw value at 2.20V
102
 
#define ADC_22 482  // manually tweaked so 4.16V will blink out 4.2
103
 
#endif
104
 
 
105
 
// this light has aux LEDs under the optic
106
 
#define AUXLED_R_PIN    PA5    // pin 2
107
 
#define AUXLED_G_PIN    PA4    // pin 3
108
 
#define AUXLED_B_PIN    PA3    // pin 4
109
 
#define AUXLED_RGB_PORT PORTA  // PORTA or PORTB or PORTC
110
 
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
111
 
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
112
 
 
113
 
#define BUTTON_LED_PIN  PA2    // pin 5
114
 
#define BUTTON_LED_PORT PORTA  // for all "PA" pins
115
 
#define BUTTON_LED_DDR  DDRA   // for all "PA" pins
116
 
#define BUTTON_LED_PUE  PUEA   // for all "PA" pins
117
 
 
118
 
// with so many pins, doing this all with #ifdefs gets awkward...
119
 
// ... so just hardcode it in each hwdef file instead
120
 
inline void hwdef_setup() {
121
 
  // enable output ports
122
 
  // Opamp level and Opamp on/off
123
 
  DDRB = (1 << PWM1_PIN)
124
 
       | (1 << LED_ENABLE_PIN);
125
 
  // DD FET PWM, aux R/G/B, button LED
126
 
  DDRA = (1 << PWM2_PIN)
127
 
       | (1 << AUXLED_R_PIN)
128
 
       | (1 << AUXLED_G_PIN)
129
 
       | (1 << AUXLED_B_PIN)
130
 
       | (1 << BUTTON_LED_PIN)
131
 
       ;
132
 
 
133
 
  // configure PWM
134
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
135
 
  // pre-scale for timer: N = 1
136
 
  // WGM1[3:0]: 1,0,1,0: PWM, Phase Correct, adjustable (DS table 12-5)
137
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
138
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
139
 
  // COM1B[1:0]:    1,0: PWM OC1B in the normal direction (DS table 12-4)
140
 
  TCCR1A  = (1<<WGM11)  | (0<<WGM10)   // adjustable PWM (TOP=ICR1) (DS table 12-5)
141
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
142
 
          | (1<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
143
 
          ;
144
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
145
 
          | (1<<WGM13)  | (0<<WGM12)  // phase-correct adjustable PWM (DS table 12-5)
146
 
          ;
147
 
 
148
 
  // set PWM resolution
149
 
  PWM1_TOP = PWM_TOP;
150
 
 
151
 
  // set up e-switch
152
 
  //PORTB = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
153
 
  PUEB = (1 << SWITCH_PIN);  // pull-up for e-switch
154
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
155
 
}
156
 
 
157
 
#define LAYOUT_DEFINED
158