]>
Commit | Line | Data |
---|---|---|
60134830 | 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)); } | |
099701dd | 44 | #ifdef HHKB_POWER_SAVING |
45 | static inline void KEY_POWER_ON(void) { | |
46 | DDRB = 0xFF; PORTB = 0x40; // change pins output | |
47 | DDRD |= (1<<4); PORTD |= (1<<4); // MOS FET switch on | |
48 | /* Without this wait you will miss or get false key events. */ | |
49 | _delay_ms(5); // wait for powering up | |
50 | } | |
51 | static inline void KEY_POWER_OFF(void) { | |
52 | /* input with pull-up consumes less than without it when pin is open. */ | |
53 | DDRB = 0x00; PORTB = 0xFF; // change pins input with pull-up | |
54 | DDRD |= (1<<4); PORTD &= ~(1<<4); // MOS FET switch off | |
55 | } | |
56 | static inline bool KEY_POWER_STATE(void) { return PORTD & (1<<4); } | |
57 | #else | |
60134830 | 58 | static inline void KEY_POWER_ON(void) {} |
59 | static inline void KEY_POWER_OFF(void) {} | |
099701dd | 60 | static inline bool KEY_POWER_STATE(void) { return true; } |
61 | #endif | |
60134830 | 62 | static inline void KEY_INIT(void) |
63 | { | |
099701dd | 64 | /* row,col,prev: output */ |
60134830 | 65 | DDRB = 0xFF; |
099701dd | 66 | PORTB = 0x40; // unable |
67 | /* key: input with pull-up */ | |
60134830 | 68 | DDRD &= ~0x80; |
099701dd | 69 | PORTD |= 0x80; |
60134830 | 70 | #ifdef HHKB_JP |
71 | /* row extention for HHKB JP */ | |
72 | DDRC |= (1<<6|1<<7); | |
c01ecce1 | 73 | PORTC |= (1<<6|1<<7); |
f4a20304 | 74 | #else |
75 | /* input with pull up to save power */ | |
76 | DDRC &= ~(1<<6|1<<7); | |
77 | PORTC |= (1<<6|1<<7); | |
60134830 | 78 | #endif |
79 | KEY_UNABLE(); | |
80 | KEY_PREV_OFF(); | |
099701dd | 81 | |
82 | KEY_POWER_OFF(); | |
60134830 | 83 | } |
84 | static inline void KEY_SELECT(uint8_t ROW, uint8_t COL) | |
85 | { | |
86 | PORTB = (PORTB & 0xC0) | (((COL) & 0x07)<<3) | ((ROW) & 0x07); | |
87 | #ifdef HHKB_JP | |
88 | if ((ROW) & 0x08) PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<6); | |
89 | else PORTC = (PORTC & ~(1<<6|1<<7)) | (1<<7); | |
90 | #endif | |
91 | } | |
92 | ||
93 | ||
94 | #elif defined(__AVR_AT90USB1286__) | |
45286631 | 95 | /* |
60134830 | 96 | * For Teensy++(AT90USB1286) |
97 | * | |
45286631 | 98 | * HHKB pro HHKB pro2 |
99 | * row: PB0-2 (6-8) (5-7) | |
100 | * col: PB3-5,6 (9-12) (8-11) | |
101 | * key: PE6(pull-uped) (4) (3) | |
102 | * prev: PE7 (5) (4) | |
60134830 | 103 | * |
104 | * TODO: convert into 'staitc inline' function | |
105 | */ | |
106 | #define KEY_INIT() do { \ | |
107 | DDRB |= 0x7F; \ | |
108 | DDRE |= (1<<7); \ | |
109 | DDRE &= ~(1<<6); \ | |
110 | PORTE |= (1<<6); \ | |
111 | } while (0) | |
112 | #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \ | |
113 | (((COL) & 0x07)<<3) | \ | |
114 | ((ROW) & 0x07)) | |
115 | #define KEY_ENABLE() (PORTB &= ~(1<<6)) | |
116 | #define KEY_UNABLE() (PORTB |= (1<<6)) | |
117 | #define KEY_STATE() (PINE & (1<<6)) | |
118 | #define KEY_PREV_ON() (PORTE |= (1<<7)) | |
119 | #define KEY_PREV_OFF() (PORTE &= ~(1<<7)) | |
120 | #define KEY_POWER_ON() | |
121 | #define KEY_POWER_OFF() | |
099701dd | 122 | #define KEY_POWER_STATE() true |
60134830 | 123 | |
124 | ||
125 | #else | |
126 | # error "define code for matrix scan" | |
127 | #endif | |
128 | ||
129 | ||
130 | #if 0 | |
131 | // For ATMega328P with V-USB | |
132 | // | |
133 | // #elif defined(__AVR_ATmega328P__) | |
134 | // Ports for V-USB | |
135 | // key: PB0(pull-uped) | |
136 | // prev: PB1 | |
137 | // row: PB2-4 | |
138 | // col: PC0-2,3 | |
139 | // power: PB5(Low:on/Hi-z:off) | |
140 | #define KEY_INIT() do { \ | |
141 | DDRB |= 0x3E; \ | |
142 | DDRB &= ~(1<<0); \ | |
143 | PORTB |= 1<<0; \ | |
144 | DDRC |= 0x0F; \ | |
145 | KEY_UNABLE(); \ | |
146 | KEY_PREV_OFF(); \ | |
147 | } while (0) | |
148 | #define KEY_SELECT(ROW, COL) do { \ | |
149 | PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \ | |
150 | PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \ | |
151 | } while (0) | |
152 | #define KEY_ENABLE() (PORTC &= ~(1<<3)) | |
153 | #define KEY_UNABLE() (PORTC |= (1<<3)) | |
154 | #define KEY_STATE() (PINB & (1<<0)) | |
155 | #define KEY_PREV_ON() (PORTB |= (1<<1)) | |
156 | #define KEY_PREV_OFF() (PORTB &= ~(1<<1)) | |
157 | // Power supply switching | |
158 | #define KEY_POWER_ON() do { \ | |
159 | KEY_INIT(); \ | |
160 | PORTB &= ~(1<<5); \ | |
161 | _delay_ms(1); \ | |
162 | } while (0) | |
163 | #define KEY_POWER_OFF() do { \ | |
164 | DDRB &= ~0x3F; \ | |
165 | PORTB &= ~0x3F; \ | |
166 | DDRC &= ~0x0F; \ | |
167 | PORTC &= ~0x0F; \ | |
168 | } while (0) | |
169 | #endif | |
170 | ||
171 | #endif |