]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb/matrix.c
01473d1ec79e1c908237061ad758453075071a3b
[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 "debug.h"
27 #include "util.h"
28 #include "timer.h"
29 #include "matrix.h"
30
31
32 // Timer resolution check
33 #if (1000000/TIMER_RAW_FREQ > 20)
34 # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
35 #endif
36
37 #if (MATRIX_COLS > 16)
38 # error "MATRIX_COLS must not exceed 16"
39 #endif
40 #if (MATRIX_ROWS > 255)
41 # error "MATRIX_ROWS must not exceed 255"
42 #endif
43
44
45 // matrix state buffer(1:on, 0:off)
46 static matrix_row_t *matrix;
47 static matrix_row_t *matrix_prev;
48 static matrix_row_t _matrix0[MATRIX_ROWS];
49 static matrix_row_t _matrix1[MATRIX_ROWS];
50
51
52 // Matrix I/O ports
53 //
54 // row: HC4051[A,B,C] selects scan row0-7
55 // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
56 // key: on: 0/off: 1
57 // prev: unknown: output previous key state(negated)?
58
59 #if defined(__AVR_AT90USB1286__)
60 // Ports for Teensy++
61 // row: PB0-2
62 // col: PB3-5,6
63 // key: PE6(pull-uped)
64 // prev: PE7
65 #define KEY_INIT() do { \
66 DDRB |= 0x7F; \
67 DDRE |= (1<<7); \
68 DDRE &= ~(1<<6); \
69 PORTE |= (1<<6); \
70 } while (0)
71 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
72 (((COL) & 0x07)<<3) | \
73 ((ROW) & 0x07))
74 #define KEY_ENABLE() (PORTB &= ~(1<<6))
75 #define KEY_UNABLE() (PORTB |= (1<<6))
76 #define KEY_STATE() (PINE & (1<<6))
77 #define KEY_PREV_ON() (PORTE |= (1<<7))
78 #define KEY_PREV_OFF() (PORTE &= ~(1<<7))
79 #define KEY_POWER_ON()
80 #define KEY_POWER_OFF()
81
82 #elif defined(__AVR_ATmega328P__)
83 // Ports for V-USB
84 // key: PB0(pull-uped)
85 // prev: PB1
86 // row: PB2-4
87 // col: PC0-2,3
88 // power: PB5(Low:on/Hi-z:off)
89 #define KEY_INIT() do { \
90 DDRB |= 0x3E; \
91 DDRB &= ~(1<<0); \
92 PORTB |= 1<<0; \
93 DDRC |= 0x0F; \
94 KEY_UNABLE(); \
95 KEY_PREV_OFF(); \
96 } while (0)
97 #define KEY_SELECT(ROW, COL) do { \
98 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
99 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
100 } while (0)
101 #define KEY_ENABLE() (PORTC &= ~(1<<3))
102 #define KEY_UNABLE() (PORTC |= (1<<3))
103 #define KEY_STATE() (PINB & (1<<0))
104 #define KEY_PREV_ON() (PORTB |= (1<<1))
105 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
106 // Power supply switching
107 #define KEY_POWER_ON() do { \
108 KEY_INIT(); \
109 PORTB &= ~(1<<5); \
110 _delay_ms(1); \
111 } while (0)
112 #define KEY_POWER_OFF() do { \
113 DDRB &= ~0x3F; \
114 PORTB &= ~0x3F; \
115 DDRC &= ~0x0F; \
116 PORTC &= ~0x0F; \
117 } while (0)
118
119 #else
120 # error "define code for matrix scan"
121 #endif
122
123
124 inline
125 uint8_t matrix_rows(void)
126 {
127 return MATRIX_ROWS;
128 }
129
130 inline
131 uint8_t matrix_cols(void)
132 {
133 return MATRIX_COLS;
134 }
135
136 void matrix_init(void)
137 {
138 KEY_INIT();
139
140 // initialize matrix state: all keys off
141 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
142 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
143 matrix = _matrix0;
144 matrix_prev = _matrix1;
145 }
146
147 uint8_t matrix_scan(void)
148 {
149 uint8_t *tmp;
150
151 tmp = matrix_prev;
152 matrix_prev = matrix;
153 matrix = tmp;
154
155 KEY_POWER_ON();
156 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
157 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
158 KEY_SELECT(row, col);
159 _delay_us(40);
160
161 // Not sure this is needed. This just emulates HHKB controller's behaviour.
162 if (matrix_prev[row] & (1<<col)) {
163 KEY_PREV_ON();
164 }
165 _delay_us(7);
166
167 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
168 // If V-USB interrupts in this section we could lose 40us or so
169 // and would read invalid value from KEY_STATE.
170 uint8_t last = TIMER_RAW;
171
172 KEY_ENABLE();
173 // Wait for KEY_STATE outputs its value.
174 // 1us was ok on one HHKB, but not worked on another.
175 _delay_us(10);
176 if (KEY_STATE()) {
177 matrix[row] &= ~(1<<col);
178 } else {
179 matrix[row] |= (1<<col);
180 }
181
182 // Ignore if this code region execution time elapses more than 20us.
183 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
184 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
185 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
186 matrix[row] = matrix_prev[row];
187 }
188
189 KEY_PREV_OFF();
190 KEY_UNABLE();
191 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
192 // This takes 25us or more to make sure KEY_STATE returns to idle state.
193 _delay_us(150);
194 }
195 }
196 KEY_POWER_OFF();
197 return 1;
198 }
199
200 bool matrix_is_modified(void)
201 {
202 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
203 if (matrix[i] != matrix_prev[i])
204 return true;
205 }
206 return false;
207 }
208
209 inline
210 bool matrix_has_ghost(void)
211 {
212 return false;
213 }
214
215 inline
216 bool matrix_is_on(uint8_t row, uint8_t col)
217 {
218 return (matrix[row] & (1<<col));
219 }
220
221 inline
222 #if (MATRIX_COLS <= 8)
223 uint8_t matrix_get_row(uint8_t row)
224 #else
225 uint16_t matrix_get_row(uint8_t row)
226 #endif
227 {
228 return matrix[row];
229 }
230
231 void matrix_debug(void)
232 {
233 if (!debug_matrix)
234 return;
235
236 #if (MATRIX_COLS <= 8)
237 debug("\nr/c 01234567\n");
238 #else
239 debug("\nr/c 0123456789ABCDEF\n");
240 #endif
241 for (uint8_t row = 0; row < matrix_rows(); row++) {
242 debug_hex(row); debug(": ");
243 #if (MATRIX_COLS <= 8)
244 debug_bin_reverse(matrix_get_row(row));
245 #else
246 debug_bin_reverse16(matrix_get_row(row));
247 #endif
248 debug("\n");
249 }
250 }
251
252 uint8_t matrix_key_count(void)
253 {
254 uint8_t count = 0;
255 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
256 #if (MATRIX_COLS <= 8)
257 count += bitpop(matrix[i]);
258 #else
259 count += bitpop16(matrix[i]);
260 #endif
261 }
262 return count;
263 }
Imprint / Impressum