2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
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."
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];
48 // row: HC4051[A,B,C] selects scan row0-7
49 // col: LS145[A,B,C,D] selects scan col0-7 and enable(D)
51 // prev: unknown: output previous key state(negated)?
53 #if defined(__AVR_AT90USB1286__)
57 // key: PE6(pull-uped)
59 #define KEY_INIT() do { \
65 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
66 (((COL) & 0x07)<<3) | \
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()
76 #elif defined(__AVR_ATmega32U4__)
77 // Ports for my designed Alt Controller PCB
80 // key: PD7(pull-uped)
82 #define KEY_INIT() do { \
90 #define KEY_SELECT(ROW, COL) (PORTB = (PORTB & 0xC0) | \
91 (((COL) & 0x07)<<3) | \
93 #define KEY_ENABLE() (PORTB &= ~(1<<6))
94 #define KEY_UNABLE() (PORTB |= (1<<6))
95 #define KEY_STATE() (PIND & (1<<7))
96 #define KEY_PREV_ON() (PORTB |= (1<<7))
97 #define KEY_PREV_OFF() (PORTB &= ~(1<<7))
98 #define KEY_POWER_ON()
99 #define KEY_POWER_OFF()
102 #elif defined(__AVR_ATmega328P__)
104 // key: PB0(pull-uped)
108 // power: PB5(Low:on/Hi-z:off)
109 #define KEY_INIT() do { \
117 #define KEY_SELECT(ROW, COL) do { \
118 PORTB = (PORTB & 0xE3) | ((ROW) & 0x07)<<2; \
119 PORTC = (PORTC & 0xF8) | ((COL) & 0x07); \
121 #define KEY_ENABLE() (PORTC &= ~(1<<3))
122 #define KEY_UNABLE() (PORTC |= (1<<3))
123 #define KEY_STATE() (PINB & (1<<0))
124 #define KEY_PREV_ON() (PORTB |= (1<<1))
125 #define KEY_PREV_OFF() (PORTB &= ~(1<<1))
126 // Power supply switching
127 #define KEY_POWER_ON() do { \
132 #define KEY_POWER_OFF() do { \
140 # error "define code for matrix scan"
145 uint8_t matrix_rows(void)
151 uint8_t matrix_cols(void)
156 void matrix_init(void)
160 debug_keyboard = true;
165 // initialize matrix state: all keys off
166 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
167 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
169 matrix_prev = _matrix1;
172 uint8_t matrix_scan(void)
177 matrix_prev = matrix;
181 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
182 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
183 KEY_SELECT(row, col);
186 // Not sure this is needed. This just emulates HHKB controller's behaviour.
187 if (matrix_prev[row] & (1<<col)) {
192 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
193 // If V-USB interrupts in this section we could lose 40us or so
194 // and would read invalid value from KEY_STATE.
195 uint8_t last = TIMER_RAW;
198 // Wait for KEY_STATE outputs its value.
199 // 1us was ok on one HHKB, but not worked on another.
200 // no wait doesn't work on Teensy++ with pro(1us works)
201 // no wait does work on tmk PCB(8MHz) with pro2
202 // 1us wait does work on both of above
203 // 10us wait does work on Teensy++ with pro
204 // 10us wait does work on 328p+iwrap with pro
205 // 10us wait doesn't work on tmk PCB(8MHz) with pro2(very lagged scan)
209 matrix[row] &= ~(1<<col);
211 matrix[row] |= (1<<col);
214 // Ignore if this code region execution time elapses more than 20us.
215 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
216 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
217 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
218 matrix[row] = matrix_prev[row];
223 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
224 // This takes 25us or more to make sure KEY_STATE returns to idle state.
232 bool matrix_is_modified(void)
234 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
235 if (matrix[i] != matrix_prev[i])
242 bool matrix_has_ghost(void)
248 bool matrix_is_on(uint8_t row, uint8_t col)
250 return (matrix[row] & (1<<col));
254 matrix_row_t matrix_get_row(uint8_t row)
259 void matrix_print(void)
261 print("\nr/c 01234567\n");
262 for (uint8_t row = 0; row < matrix_rows(); row++) {
263 xprintf("%02X: %08b\n", row, bitrev(matrix_get_row(row)));