]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/hhkb_rn42/hhkb_avr.h
b7bd507b5d94e4dba7c5bb9e4d1f72c84980d531
[tmk_firmware.git] / keyboard / hhkb_rn42 / hhkb_avr.h
1 #ifndef HHKB_AVR_H
2 #define HHKB_AVR_H
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <avr/io.h>
7 #include <avr/interrupt.h>
8 #include <util/delay.h>
9
10
11 // Timer resolution check
12 #if (1000000/TIMER_RAW_FREQ > 20)
13 #   error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
14 #endif
15
16
17 /*
18  * HHKB Matrix I/O
19  *
20  * row:     HC4051[A,B,C]  selects scan row0-7
21  * row-ext: [En0,En1] row extention for JP
22  * col:     LS145[A,B,C,D] selects scan col0-7 and enable(D)
23  * key:     on: 0/off: 1
24  * prev:    hysteresis control: assert(1) when previous key state is on
25  */
26
27
28 #if defined(__AVR_ATmega32U4__)
29 /*
30  * For TMK HHKB alt controller(ATMega32U4)
31  *
32  * row:     PB0-2
33  * col:     PB3-5,6
34  * key:     PD7(pull-uped)
35  * prev:    PB7
36  * power:   PD4(L:off/H:on)
37  * row-ext: PC6,7 for HHKB JP(active low)
38  */
39 static inline void KEY_ENABLE(void) { (PORTB &= ~(1<<6)); }
40 static inline void KEY_UNABLE(void) { (PORTB |=  (1<<6)); }
41 static inline bool KEY_STATE(void) { return (PIND & (1<<7)); }
42 static inline void KEY_PREV_ON(void) { (PORTB |=  (1<<7)); }
43 static inline void KEY_PREV_OFF(void) { (PORTB &= ~(1<<7)); }
44 static inline void KEY_POWER_ON(void) {}
45 static inline void KEY_POWER_OFF(void) {}
46 static inline void KEY_INIT(void)
47 {
48     DDRB  = 0xFF;
49     PORTB = 0x00;
50     DDRD  &= ~0x80;
51     PORTD |= 0x80;
52     /* keyswitch board power on */
53     DDRD  |=  (1<<4);
54     PORTD |=  (1<<4);
55 #ifdef HHKB_JP
56     /* row extention for HHKB JP */
57     DDRC  |= (1<<6|1<<7);
58     PORTC |= (1<<6|1<<7);
59 #endif
60     KEY_UNABLE();
61     KEY_PREV_OFF();
62 }
63 static inline void KEY_SELECT(uint8_t ROW, uint8_t COL)
64 {
65     PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07);
66 #ifdef HHKB_JP
67     if ((ROW) & 0x08) PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<6);
68     else              PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<7);
69 #endif
70 }
71
72
73 #elif defined(__AVR_AT90USB1286__)
74 /*
75  * For Teensy++(AT90USB1286)
76  *
77  *                          HHKB pro    HHKB pro2
78  * row:     PB0-2           (6-8)       (5-7)
79  * col:     PB3-5,6         (9-12)      (8-11)
80  * key:     PE6(pull-uped)  (4)         (3)
81  * prev:    PE7             (5)         (4)
82  *
83  * TODO: convert into 'staitc inline' function
84  */
85 #define KEY_INIT()              do {    \
86     DDRB |= 0x7F;                       \
87     DDRE |=  (1<<7);                    \
88     DDRE &= ~(1<<6);                    \
89     PORTE |= (1<<6);                    \
90 } while (0)
91 #define KEY_SELECT(ROW, COL)    (PORTB = (PORTB & 0xC0) |       \
92                                          (((COL) & 0x07)<<3) |  \
93                                          ((ROW) & 0x07))
94 #define KEY_ENABLE()            (PORTB &= ~(1<<6))
95 #define KEY_UNABLE()            (PORTB |=  (1<<6))
96 #define KEY_STATE()             (PINE & (1<<6))
97 #define KEY_PREV_ON()           (PORTE |=  (1<<7))
98 #define KEY_PREV_OFF()          (PORTE &= ~(1<<7))
99 #define KEY_POWER_ON()
100 #define KEY_POWER_OFF()
101
102
103 #else
104 #   error "define code for matrix scan"
105 #endif
106
107
108 #if 0
109 // For ATMega328P with V-USB
110 //
111 // #elif defined(__AVR_ATmega328P__)
112 // Ports for V-USB
113 // key:     PB0(pull-uped)
114 // prev:    PB1
115 // row:     PB2-4
116 // col:     PC0-2,3
117 // power:   PB5(Low:on/Hi-z:off)
118 #define KEY_INIT()              do {    \
119     DDRB  |= 0x3E;                      \
120     DDRB  &= ~(1<<0);                   \
121     PORTB |= 1<<0;                      \
122     DDRC  |= 0x0F;                      \
123     KEY_UNABLE();                       \
124     KEY_PREV_OFF();                     \
125 } while (0)
126 #define KEY_SELECT(ROW, COL)    do {    \
127     PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
128     PORTC = (PORTC & 0xF8) | ((COL) & 0x07);    \
129 } while (0)
130 #define KEY_ENABLE()            (PORTC &= ~(1<<3))
131 #define KEY_UNABLE()            (PORTC |=  (1<<3))
132 #define KEY_STATE()             (PINB & (1<<0))
133 #define KEY_PREV_ON()           (PORTB |=  (1<<1))
134 #define KEY_PREV_OFF()          (PORTB &= ~(1<<1))
135 // Power supply switching
136 #define KEY_POWER_ON()          do {    \
137     KEY_INIT();                         \
138     PORTB &= ~(1<<5);                   \
139     _delay_ms(1);                       \
140 } while (0)
141 #define KEY_POWER_OFF()         do {    \
142     DDRB  &= ~0x3F;                     \
143     PORTB &= ~0x3F;                     \
144     DDRC  &= ~0x0F;                     \
145     PORTC &= ~0x0F;                     \
146 } while (0)
147 #endif
148
149 #endif