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];
88 __attribute__ ((weak))
89 void matrix_init_quantum(void) {
93 __attribute__ ((weak))
94 void matrix_scan_quantum(void) {
98 __attribute__ ((weak))
99 void matrix_init_kb(void) {
103 __attribute__ ((weak))
104 void matrix_scan_kb(void) {
108 __attribute__ ((weak))
109 void matrix_init_user(void) {
112 __attribute__ ((weak))
113 void matrix_scan_user(void) {
117 void matrix_init(void)
124 // LEDs on NumLock, CapsLock and ScrollLock(PB4, PB5, PB6)
125 DDRB |= (1<<4) | (1<<5) | (1<<6);
126 PORTB |= (1<<4) | (1<<5) | (1<<6);
128 // initialize matrix state: all keys off
129 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
130 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
132 matrix_prev = _matrix1;
133 matrix_init_quantum();
136 uint8_t matrix_scan(void)
141 matrix_prev = matrix;
145 for (col = 0; col < MATRIX_COLS; col++) {
147 for (row = 0; row < MATRIX_ROWS; row++) {
148 //KEY_SELECT(row, col);
152 // Not sure this is needed. This just emulates HHKB controller's behaviour.
153 if (matrix_prev[row] & (1<<col)) {
158 // NOTE: KEY_STATE is valid only in 20us after KEY_ENABLE.
159 // If V-USB interrupts in this section we could lose 40us or so
160 // and would read invalid value from KEY_STATE.
161 uint8_t last = TIMER_RAW;
165 // Wait for KEY_STATE outputs its value.
169 matrix[row] &= ~(1<<col);
171 matrix[row] |= (1<<col);
174 // Ignore if this code region execution time elapses more than 20us.
175 // MEMO: 20[us] * (TIMER_RAW_FREQ / 1000000)[count per us]
176 // MEMO: then change above using this rule: a/(b/c) = a*1/(b/c) = a*(c/b)
177 if (TIMER_DIFF_RAW(TIMER_RAW, last) > 20/(1000000/TIMER_RAW_FREQ)) {
178 matrix[row] = matrix_prev[row];
185 // NOTE: KEY_STATE keep its state in 20us after KEY_ENABLE.
186 // This takes 25us or more to make sure KEY_STATE returns to idle state.
189 if (matrix[row] ^ matrix_prev[row]) {
190 matrix_last_modified = timer_read32();
193 matrix_scan_quantum();
198 matrix_row_t matrix_get_row(uint8_t row) {
202 void matrix_print(void)
204 #if (MATRIX_COLS <= 8)
205 print("r/c 01234567\n");
206 #elif (MATRIX_COLS <= 16)
207 print("r/c 0123456789ABCDEF\n");
208 #elif (MATRIX_COLS <= 32)
209 print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
212 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
214 #if (MATRIX_COLS <= 8)
215 xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
216 #elif (MATRIX_COLS <= 16)
217 xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
218 #elif (MATRIX_COLS <= 32)
219 xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
221 #ifdef MATRIX_HAS_GHOST
222 matrix_has_ghost_in_row(row) ? " <ghost" : ""