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