]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb/matrix.c
Merge branch 'hhkb_tmk'
[tmk_keyboard.git] / keyboard / hhkb / matrix.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * scan matrix
20 */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "print.h"
27 #include "debug.h"
28 #include "util.h"
29 #include "timer.h"
30 #include "matrix.h"
31
32
33 // Timer resolution check
34 #if (1000000/TIMER_RAW_FREQ > 20)
35 # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
36 #endif
37
38
39 // matrix state buffer(1:on, 0:off)
40 static matrix_row_t *matrix;
41 static matrix_row_t *matrix_prev;
42 static matrix_row_t _matrix0[MATRIX_ROWS];
43 static matrix_row_t _matrix1[MATRIX_ROWS];
44
45
46 // Matrix I/O ports
47 //
48 // row: HC4051[A,B,C] selects scan row0-7
49 // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
50 // key: on: 0/off: 1
51 // prev: unknown: output previous key state(negated)?
52
53 #if defined(__AVR_AT90USB1286__)
54 // Ports for Teensy++
55 // row: PB0-2
56 // col: PB3-5,6
57 // key: PE6(pull-uped)
58 // prev: PE7
59 #define KEY_INIT() do { \
60 DDRB |= 0x7F; \
61 DDRE |= (1<<7); \
62 DDRE &= ~(1<<6); \
63 PORTE |= (1<<6); \
64 } while (0)
65 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
66 (((COL) & 0x07)<<3) | \
67 ((ROW) & 0x07))
68 #define KEY_ENABLE() (PORTB &= ~(1<<6))
69 #define KEY_UNABLE() (PORTB |= (1<<6))
70 #define KEY_STATE() (PINE & (1<<6))
71 #define KEY_PREV_ON() (PORTE |= (1<<7))
72 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
73 #define KEY_POWER_ON()
74 #define KEY_POWER_OFF()
75
76 #elif defined(__AVR_ATmega32U4__)
77 // Ports for my designed Alt Controller PCB
78 // row: PB0-2
79 // col: PB3-5,6
80 // key: PD7(pull-uped)
81 // prev: PB7
82 // power: PD4(L:off/H:on)
83 #define KEY_INIT() do { \
84 DDRB = 0xFF; \
85 PORTB = 0x00; \
86 DDRD &= ~0x80; \
87 PORTD |= 0x80; \
88 /* keyswitch board power on */ \
89 DDRD |= (1<<4); \
90 PORTD |= (1<<4); \
91 KEY_UNABLE(); \
92 KEY_PREV_OFF(); \
93 } while (0)
94 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
95 (((COL) & 0x07)<<3) | \
96 ((ROW) & 0x07))
97 #define KEY_ENABLE() (PORTB &= ~(1<<6))
98 #define KEY_UNABLE() (PORTB |= (1<<6))
99 #define KEY_STATE() (PIND & (1<<7))
100 #define KEY_PREV_ON() (PORTB |= (1<<7))
101 #define KEY_PREV_OFF() (PORTB &= ~(1<<7))
102 #define KEY_POWER_ON()
103 #define KEY_POWER_OFF()
104 /*
105 #define KEY_POWER_ON() do { \
106 KEY_INIT(); \
107 PORTD |= (1<<4); \
108 _delay_ms(1); \
109 } while (0)
110 #define KEY_POWER_OFF() do { \
111 PORTD &= ~(1<<4); \
112 DDRB &= ~0xFF; \
113 PORTB &= ~0xFF; \
114 DDRB &= ~0x80; \
115 PORTB &= ~0x80; \
116 } while (0)
117 */
118
119
120 #elif defined(__AVR_ATmega328P__)
121 // Ports for V-USB
122 // key: PB0(pull-uped)
123 // prev: PB1
124 // row: PB2-4
125 // col: PC0-2,3
126 // power: PB5(Low:on/Hi-z:off)
127 #define KEY_INIT() do { \
128 DDRB |= 0x3E; \
129 DDRB &= ~(1<<0); \
130 PORTB |= 1<<0; \
131 DDRC |= 0x0F; \
132 KEY_UNABLE(); \
133 KEY_PREV_OFF(); \
134 } while (0)
135 #define KEY_SELECT(ROW, COL) do { \
136 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
137 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
138 } while (0)
139 #define KEY_ENABLE() (PORTC &= ~(1<<3))
140 #define KEY_UNABLE() (PORTC |= (1<<3))
141 #define KEY_STATE() (PINB & (1<<0))
142 #define KEY_PREV_ON() (PORTB |= (1<<1))
143 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
144 // Power supply switching
145 #define KEY_POWER_ON() do { \
146 KEY_INIT(); \
147 PORTB &= ~(1<<5); \
148 _delay_ms(1); \
149 } while (0)
150 #define KEY_POWER_OFF() do { \
151 DDRB &= ~0x3F; \
152 PORTB &= ~0x3F; \
153 DDRC &= ~0x0F; \
154 PORTC &= ~0x0F; \
155 } while (0)
156
157 #else
158 # error "define code for matrix scan"
159 #endif
160
161
162 inline
163 uint8_t matrix_rows(void)
164 {
165 return MATRIX_ROWS;
166 }
167
168 inline
169 uint8_t matrix_cols(void)
170 {
171 return MATRIX_COLS;
172 }
173
174 void matrix_init(void)
175 {
176 #ifdef DEBUG
177 debug_enable = true;
178 debug_keyboard = true;
179 #endif
180
181 KEY_INIT();
182
183 // initialize matrix state: all keys off
184 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
185 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
186 matrix = _matrix0;
187 matrix_prev = _matrix1;
188 }
189
190 uint8_t matrix_scan(void)
191 {
192 uint8_t *tmp;
193
194 tmp = matrix_prev;
195 matrix_prev = matrix;
196 matrix = tmp;
197
198 KEY_POWER_ON();
199 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
200 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
201 KEY_SELECT(row, col);
202 _delay_us(40);
203
204 // Not sure this is needed. This just emulates HHKB controller's behaviour.
205 if (matrix_prev[row] & (1<<col)) {
206 KEY_PREV_ON();
207 }
208 _delay_us(7);
209
210 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
211 // If V-USB interrupts in this section we could lose 40us or so
212 // and would read invalid value from KEY_STATE.
213 uint8_t last = TIMER_RAW;
214
215 KEY_ENABLE();
216
217 // Wait for KEY_STATE outputs its value.
218 // 1us was ok on one HHKB, but not worked on another.
219 // no wait doesn't work on Teensy++ with pro(1us works)
220 // no wait does work on tmk PCB(8MHz) with pro2
221 // 1us wait does work on both of above
222 // 1us wait doesn't work on tmk(16MHz)
223 // 5us wait does work on tmk(16MHz)
224 // 5us wait does work on tmk(16MHz/2)
225 // 5us wait does work on tmk(8MHz)
226 // 10us wait does work on Teensy++ with pro
227 // 10us wait does work on 328p+iwrap with pro
228 // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
229 _delay_us(5);
230
231 if (KEY_STATE()) {
232 matrix[row] &= ~(1<<col);
233 } else {
234 matrix[row] |= (1<<col);
235 }
236
237 // Ignore if this code region execution time elapses more than 20us.
238 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
239 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
240 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
241 matrix[row] = matrix_prev[row];
242 }
243
244 KEY_PREV_OFF();
245 KEY_UNABLE();
246 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
247 // This takes 25us or more to make sure KEY_STATE returns to idle state.
248 _delay_us(150);
249 }
250 }
251 KEY_POWER_OFF();
252 return 1;
253 }
254
255 bool matrix_is_modified(void)
256 {
257 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
258 if (matrix[i] != matrix_prev[i])
259 return true;
260 }
261 return false;
262 }
263
264 inline
265 bool matrix_has_ghost(void)
266 {
267 return false;
268 }
269
270 inline
271 bool matrix_is_on(uint8_t row, uint8_t col)
272 {
273 return (matrix[row] & (1<<col));
274 }
275
276 inline
277 matrix_row_t matrix_get_row(uint8_t row)
278 {
279 return matrix[row];
280 }
281
282 void matrix_print(void)
283 {
284 print("\nr/c 01234567\n");
285 for (uint8_t row = 0; row < matrix_rows(); row++) {
286 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));
287 }
288 }
Imprint / Impressum