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/>.
31 // #include "fc980c.h"
34 // Timer resolution check
35 #if (1000000/TIMER_RAW_FREQ > 20)
36 # error "Timer resolution(>20us) is not enough for HHKB matrix scan tweak on V-USB."
41 * Pin configuration for ATMega32U4
43 * Row: PD4-6, PD7(~EN)
48 static inline void KEY_ENABLE(void) { (PORTD &= ~(1<<7)); }
49 static inline void KEY_UNABLE(void) { (PORTD |= (1<<7)); }
50 static inline bool KEY_STATE(void) { return (PINC & (1<<6)); }
51 static inline void KEY_HYS_ON(void) { (PORTC |= (1<<7)); }
52 static inline void KEY_HYS_OFF(void) { (PORTC &= ~(1<<7)); }
53 static inline void KEY_INIT(void)
57 /* Key: input with pull-up */
68 static inline void SET_ROW(uint8_t ROW)
71 PORTD = (PORTD & 0x8F) | ((ROW & 0x07) << 4);
73 static inline void SET_COL(uint8_t COL)
76 PORTB = (PORTB & 0xF0) | (COL & 0x0F);
79 static uint32_t matrix_last_modified = 0;
81 // matrix state buffer(1:on, 0:off)
82 static matrix_row_t *matrix;
83 static matrix_row_t *matrix_prev;
84 static matrix_row_t _matrix0[MATRIX_ROWS];
85 static matrix_row_t _matrix1[MATRIX_ROWS];
89 __attribute__ ((weak))
90 void matrix_init_kb(void) {
94 __attribute__ ((weak))
95 void matrix_scan_kb(void) {
99 __attribute__ ((weak))
100 void matrix_init_user(void) {
103 __attribute__ ((weak))
104 void matrix_scan_user(void) {
108 void matrix_init(void)
115 // LEDs on NumLock, CapsLock and ScrollLock(PB4, PB5, PB6)
116 DDRB |= (1<<4) | (1<<5) | (1<<6);
117 PORTB |= (1<<4) | (1<<5) | (1<<6);
119 // initialize matrix state: all keys off
120 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
121 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
123 matrix_prev = _matrix1;
124 matrix_init_quantum();
127 uint8_t matrix_scan(void)
132 matrix_prev = matrix;
136 for (col = 0; col < MATRIX_COLS; col++) {
138 for (row = 0; row < MATRIX_ROWS; row++) {
139 //KEY_SELECT(row, col);
143 // Not sure this is needed. This just emulates HHKB controller's behaviour.
144 if (matrix_prev[row] & (1<<col)) {
149 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
150 // If V-USB interrupts in this section we could lose 40us or so
151 // and would read invalid value from KEY_STATE.
152 uint8_t last = TIMER_RAW;
156 // Wait for KEY_STATE outputs its value.
160 matrix[row] &= ~(1<<col);
162 matrix[row] |= (1<<col);
165 // Ignore if this code region execution time elapses more than 20us.
166 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
167 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
168 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
169 matrix[row] = matrix_prev[row];
176 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
177 // This takes 25us or more to make sure KEY_STATE returns to idle state.
180 if (matrix[row] ^ matrix_prev[row]) {
181 matrix_last_modified = timer_read32();
184 matrix_scan_quantum();
189 matrix_row_t matrix_get_row(uint8_t row) {
193 void matrix_print(void)
195 #if (MATRIX_COLS <= 8)
196 print("r/c 01234567\n");
197 #elif (MATRIX_COLS <= 16)
198 print("r/c 0123456789ABCDEF\n");
199 #elif (MATRIX_COLS <= 32)
200 print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
203 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
205 #if (MATRIX_COLS <= 8)
206 xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
207 #elif (MATRIX_COLS <= 16)
208 xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
209 #elif (MATRIX_COLS <= 32)
210 xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
212 #ifdef MATRIX_HAS_GHOST
213 matrix_has_ghost_in_row(row) ? " <ghost" : ""