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