2 Copyright 2017 Balz Guenat
3 based on work by Jun Wako <wakojun@gmail.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
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."
39 * Pin configuration for ATMega32U4
42 * Col: PB0-2, 3(Z5 ~EN), 4(Z4 ~EN)
46 static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
47 static inline void KEY_UNABLE(void) { (PORTD |= (1<<7)); }
48 static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
49 static inline void KEY_HYS_ON(void) { (PORTC |= (1<<7)); }
50 static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
51 static inline void KEY_INIT(void)
55 /* Key: input with pull-up */
66 static inline void SET_ROW(uint8_t ROW)
68 // set row with unabling key
69 PORTD = (PORTD & 0x0F) | (1<<7) | ((ROW & 0x07) << 4);
71 static inline void SET_COL(uint8_t COL)
73 // |PB3(Z5 ~EN)|PB4(Z4 ~EN)
74 // --------|-----------|-----------
77 PORTB = (PORTB & 0xE0) | ((COL & 0x08) ? 1<<4 : 1<<3) | (COL & 0x07);
80 static uint32_t matrix_last_modified = 0;
82 // matrix state buffer(1:on, 0:off)
83 static matrix_row_t *matrix;
84 static matrix_row_t *matrix_prev;
85 static matrix_row_t _matrix0[MATRIX_ROWS];
86 static matrix_row_t _matrix1[MATRIX_ROWS];
90 __attribute__ ((weak))
91 void matrix_init_kb(void) {
95 __attribute__ ((weak))
96 void matrix_scan_kb(void) {
100 __attribute__ ((weak))
101 void matrix_init_user(void) {
104 __attribute__ ((weak))
105 void matrix_scan_user(void) {
109 void matrix_init(void)
113 // LEDs on CapsLock and Insert
114 DDRB |= (1<<5) | (1<<6);
115 PORTB |= (1<<5) | (1<<6);
117 // initialize matrix state: all keys off
118 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
119 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
121 matrix_prev = _matrix1;
122 matrix_init_quantum();
125 uint8_t matrix_scan(void)
130 matrix_prev = matrix;
134 for (col = 0; col < MATRIX_COLS; col++) {
136 for (row = 0; row < MATRIX_ROWS; row++) {
137 //KEY_SELECT(row, col);
141 // Not sure this is needed. This just emulates HHKB controller's behaviour.
142 if (matrix_prev[row] & (1<<col)) {
147 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
148 // If V-USB interrupts in this section we could lose 40us or so
149 // and would read invalid value from KEY_STATE.
150 uint8_t last = TIMER_RAW;
154 // Wait for KEY_STATE outputs its value.
158 matrix[row] &= ~(1<<col);
160 matrix[row] |= (1<<col);
163 // Ignore if this code region execution time elapses more than 20us.
164 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
165 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
166 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
167 matrix[row] = matrix_prev[row];
174 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
175 // This takes 25us or more to make sure KEY_STATE returns to idle state.
178 if (matrix[row] ^ matrix_prev[row]) {
179 matrix_last_modified = timer_read32();
182 matrix_scan_quantum();
187 matrix_row_t matrix_get_row(uint8_t row) {
191 void matrix_print(void)
193 #if (MATRIX_COLS <= 8)
194 print("r/c 01234567\n");
195 #elif (MATRIX_COLS <= 16)
196 print("r/c 0123456789ABCDEF\n");
197 #elif (MATRIX_COLS <= 32)
198 print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
201 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
203 #if (MATRIX_COLS <= 8)
204 xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
205 #elif (MATRIX_COLS <= 16)
206 xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
207 #elif (MATRIX_COLS <= 32)
208 xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
210 #ifdef MATRIX_HAS_GHOST
211 matrix_has_ghost_in_row(row) ? " <ghost" : ""