~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-noctigon-k1.h

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef HWDEF_NOCTIGON_K1_H
2
 
#define HWDEF_NOCTIGON_K1_H
 
1
// Noctigon K1 driver layout (attiny1634)
 
2
// Copyright (C) 2019-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
3
5
 
4
 
/* Noctigon K1 driver layout (attiny1634)
 
6
/*
5
7
 * (originally known as Emisar D1S V2)
6
8
 *
7
9
 * Pin / Name / Function
34
36
 * not to change brightness.
35
37
 */
36
38
 
37
 
#ifdef ATTINY
38
 
#undef ATTINY
39
 
#endif
40
39
#define ATTINY 1634
41
40
#include <avr/io.h>
42
41
 
43
 
#define PWM_CHANNELS 1
44
 
#define PWM_BITS 10  // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
45
 
#define PWM_TOP 1023
46
 
 
 
42
#ifndef HWDEF_C_FILE
 
43
#define HWDEF_C_FILE hwdef-noctigon-k1.c
 
44
#endif
 
45
 
 
46
// allow using aux LEDs as extra channel modes
 
47
#include "chan-rgbaux.h"
 
48
 
 
49
// channel modes:
 
50
// * 0. main LED
 
51
// * 1+. aux RGB
 
52
#define NUM_CHANNEL_MODES   (1 + NUM_RGB_AUX_CHANNEL_MODES)
 
53
enum CHANNEL_MODES {
 
54
    CM_MAIN = 0,
 
55
    RGB_AUX_ENUMS
 
56
};
 
57
 
 
58
#define DEFAULT_CHANNEL_MODE  CM_MAIN
 
59
 
 
60
// right-most bit first, modes are in fedcba9876543210 order
 
61
#define CHANNEL_MODES_ENABLED 0b0000000000000001
 
62
// no args
 
63
//#define USE_CHANNEL_MODE_ARGS
 
64
//#define CHANNEL_MODE_ARGS     0,0,0,0,0,0,0,0
 
65
 
 
66
 
 
67
#define PWM_CHANNELS 1  // old, remove this
 
68
 
 
69
#define PWM_BITS      10        // 0 to 1023 at 4 kHz, not 0 to 255 at 16 kHz
 
70
#define PWM_GET       PWM_GET16
 
71
#define PWM_DATATYPE  uint16_t  // is used for PWM_TOPS (which goes way over 255)
 
72
#define PWM_DATATYPE2 uint32_t  // only needs 32-bit if ramp values go over 255
 
73
#define PWM1_DATATYPE uint16_t  // linear ramp
 
74
 
 
75
#define PWM_TOP       ICR1   // holds the TOP value for variable-resolution PWM
 
76
#define PWM_TOP_INIT  1023   // highest value used in top half of ramp
 
77
#define PWM_CNT       TCNT1  // for dynamic PWM, reset phase
 
78
 
 
79
// linear channel
 
80
#define CH1_PIN  PB3            // pin 16, Opamp reference
 
81
#define CH1_PWM  OCR1A          // OCR1A is the output compare register for PB3
 
82
#define CH1_ENABLE_PIN   PB0    // pin 19, Opamp power
 
83
#define CH1_ENABLE_PORT  PORTB  // control port for PB0
 
84
 
 
85
// e-switch
47
86
#define SWITCH_PIN   PA7    // pin 20
48
87
#define SWITCH_PCINT PCINT7 // pin 20 pin change interrupt
49
88
#define SWITCH_PCIE  PCIE0  // PCIE0 is for PCINT[7:0]
50
89
#define SWITCH_PCMSK PCMSK0 // PCMSK0 is for PCINT[7:0]
51
90
#define SWITCH_PORT  PINA   // PINA or PINB or PINC
52
 
 
53
 
#define PWM1_PIN PB3        // pin 16, Opamp reference
54
 
#define PWM1_LVL OCR1A      // OCR1A is the output compare register for PB3
55
 
 
56
 
#define LED_ENABLE_PIN  PB0    // pin 19, Opamp power
57
 
#define LED_ENABLE_PORT PORTB  // control port for PB0
 
91
#define SWITCH_PUE   PUEA   // pullup group A
 
92
#define PCINT_vect   PCINT0_vect  // ISR for PCINT[7:0]
58
93
 
59
94
 
60
95
#define USE_VOLTAGE_DIVIDER  // use a dedicated pin, not VCC, because VCC input is flattened
76
111
// Raw ADC readings at 4.4V and 2.2V
77
112
// calibrate the voltage readout here
78
113
// estimated / calculated values are:
79
 
//   (voltage - D1) * (R2/(R2+R1) * 256 / 1.1)
 
114
//   (voltage - D1) * (R2/(R2+R1) * 1024 / 1.1)
80
115
// D1, R1, R2 = 0, 330, 100
81
116
#ifndef ADC_44
82
117
//#define ADC_44 981  // raw value at 4.40V
95
130
#define AUXLED_RGB_DDR  DDRA   // DDRA or DDRB or DDRC
96
131
#define AUXLED_RGB_PUE  PUEA   // PUEA or PUEB or PUEC
97
132
 
98
 
// with so many pins, doing this all with #ifdefs gets awkward...
99
 
// ... so just hardcode it in each hwdef file instead
 
133
 
100
134
inline void hwdef_setup() {
101
 
  // enable output ports
102
 
  // Opamp level and Opamp on/off
103
 
  DDRB = (1 << PWM1_PIN)
104
 
       | (1 << LED_ENABLE_PIN);
105
 
  // aux R/G/B
106
 
  DDRA = (1 << AUXLED_R_PIN)
107
 
       | (1 << AUXLED_G_PIN)
108
 
       | (1 << AUXLED_B_PIN)
109
 
       ;
110
 
 
111
 
  // configure PWM
112
 
  // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
113
 
  // pre-scale for timer: N = 1
114
 
  // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
115
 
  // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
116
 
  // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
117
 
  // COM1B[1:0]:    0,0: PWM OC1B disabled (DS table 12-4)
118
 
  TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
119
 
          | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
120
 
          | (0<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
121
 
          ;
122
 
  TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
123
 
          | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
124
 
          ;
125
 
 
126
 
  // set up e-switch
127
 
  //PORTA = (1 << SWITCH_PIN);  // TODO: configure PORTA / PORTB / PORTC?
128
 
  PUEA = (1 << SWITCH_PIN);  // pull-up for e-switch
129
 
  SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
 
135
    // enable output ports
 
136
    // Opamp level and Opamp on/off
 
137
    DDRB = (1 << CH1_PIN)
 
138
         | (1 << CH1_ENABLE_PIN);
 
139
    // aux R/G/B
 
140
    DDRA = (1 << AUXLED_R_PIN)
 
141
         | (1 << AUXLED_G_PIN)
 
142
         | (1 << AUXLED_B_PIN)
 
143
         ;
 
144
 
 
145
    // configure PWM
 
146
    // Setup PWM. F_pwm = F_clkio / 2 / N / TOP, where N = prescale factor, TOP = top of counter
 
147
    // pre-scale for timer: N = 1
 
148
    // WGM1[3:0]: 0,0,1,1: PWM, Phase Correct, 10-bit (DS table 12-5)
 
149
    // CS1[2:0]:    0,0,1: clk/1 (No prescaling) (DS table 12-6)
 
150
    // COM1A[1:0]:    1,0: PWM OC1A in the normal direction (DS table 12-4)
 
151
    // COM1B[1:0]:    0,0: PWM OC1B disabled (DS table 12-4)
 
152
    TCCR1A  = (1<<WGM11)  | (1<<WGM10)   // 10-bit (TOP=0x03FF) (DS table 12-5)
 
153
            | (1<<COM1A1) | (0<<COM1A0)  // PWM 1A in normal direction (DS table 12-4)
 
154
            | (0<<COM1B1) | (0<<COM1B0)  // PWM 1B in normal direction (DS table 12-4)
 
155
            ;
 
156
    TCCR1B  = (0<<CS12)   | (0<<CS11) | (1<<CS10)  // clk/1 (no prescaling) (DS table 12-6)
 
157
            | (0<<WGM13)  | (0<<WGM12)  // phase-correct PWM (DS table 12-5)
 
158
            ;
 
159
 
 
160
    // set PWM resolution
 
161
    //PWM_TOP = PWM_TOP_INIT;
 
162
 
 
163
    // set up e-switch
 
164
    SWITCH_PUE = (1 << SWITCH_PIN);  // pull-up for e-switch
 
165
    SWITCH_PCMSK = (1 << SWITCH_PCINT);  // enable pin change interrupt
130
166
}
131
167
 
 
168
 
132
169
#define LAYOUT_DEFINED
133
170
 
134
 
#endif