]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/fc660c/matrix.c
Usbasploader bootloader option addition (#6304)
[qmk_firmware.git] / keyboards / fc660c / matrix.c
1 /*
2 Copyright 2017 Balz Guenat
3 based on work by Jun Wako <wakojun@gmail.com>
4
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.
9
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.
14
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/>.
17 */
18
19 /*
20  * scan matrix
21  */
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include "wait.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "timer.h"
29 #include "matrix.h"
30 #include "led.h"
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
38 /*
39  * Pin configuration for ATMega32U4
40  *
41  * Row:     PD4-6, 7(~EN)
42  * Col:     PB0-2, 3(Z5 ~EN), 4(Z4 ~EN)
43  * Key:     PC6(pull-uped)
44  * Hys:     PC7
45  */
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)
52 {
53     /* Col */
54     DDRB  |=  0x1F;
55     /* Key: input with pull-up */
56     DDRC  &= ~(1<<6);
57     PORTC |=  (1<<6);
58     /* Hys */
59     DDRC  |=  (1<<7);
60     /* Row */
61     DDRD  |=  0xF0;
62
63     KEY_UNABLE();
64     KEY_HYS_OFF();
65 }
66 static inline void SET_ROW(uint8_t ROW)
67 {
68     // set row with unabling key
69     PORTD = (PORTD & 0x0F) | (1<<7) | ((ROW & 0x07) << 4);
70 }
71 static inline void SET_COL(uint8_t COL)
72 {
73     //         |PB3(Z5 ~EN)|PB4(Z4 ~EN)
74     // --------|-----------|-----------
75     // Col:0-7 |high       |low
76     // Col:8-F |low        |high
77     PORTB = (PORTB & 0xE0) | ((COL & 0x08) ? 1<<4 : 1<<3) | (COL & 0x07);
78 }
79
80 static uint32_t matrix_last_modified = 0;
81
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];
87
88
89
90 __attribute__ ((weak))
91 void matrix_init_kb(void) {
92     matrix_init_user();
93 }
94
95 __attribute__ ((weak))
96 void matrix_scan_kb(void) {
97     matrix_scan_user();
98 }
99
100 __attribute__ ((weak))
101 void matrix_init_user(void) {
102 }
103
104 __attribute__ ((weak))
105 void matrix_scan_user(void) {
106 }
107
108
109 void matrix_init(void)
110 {
111     KEY_INIT();
112
113     // LEDs on CapsLock and Insert
114     DDRB  |= (1<<5) | (1<<6);
115     PORTB |= (1<<5) | (1<<6);
116
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;
120     matrix = _matrix0;
121     matrix_prev = _matrix1;
122     matrix_init_quantum();
123 }
124
125 uint8_t matrix_scan(void)
126 {
127     matrix_row_t *tmp;
128
129     tmp = matrix_prev;
130     matrix_prev = matrix;
131     matrix = tmp;
132
133     uint8_t row, col;
134     for (col = 0; col < MATRIX_COLS; col++) {
135         SET_COL(col);
136         for (row = 0; row < MATRIX_ROWS; row++) {
137             //KEY_SELECT(row, col);
138             SET_ROW(row);
139             _delay_us(2);
140
141             // Not sure this is needed. This just emulates HHKB controller's behaviour.
142             if (matrix_prev[row] & (1<<col)) {
143                 KEY_HYS_ON();
144             }
145             _delay_us(10);
146
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;
151
152             KEY_ENABLE();
153
154             // Wait for KEY_STATE outputs its value.
155             _delay_us(2);
156
157             if (KEY_STATE()) {
158                 matrix[row] &= ~(1<<col);
159             } else {
160                 matrix[row] |= (1<<col);
161             }
162
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];
168             }
169
170             _delay_us(5);
171             KEY_HYS_OFF();
172             KEY_UNABLE();
173
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.
176             _delay_us(75);
177         }
178         if (matrix[row] ^ matrix_prev[row]) {
179             matrix_last_modified = timer_read32();
180         }
181     }
182     matrix_scan_quantum();
183     return 1;
184 }
185
186 inline
187 matrix_row_t matrix_get_row(uint8_t row) {
188     return matrix[row];
189 }
190
191 void matrix_print(void)
192 {
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");
199 #endif
200
201     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
202
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)),
209 #endif
210 #ifdef MATRIX_HAS_GHOST
211         matrix_has_ghost_in_row(row) ?  " <ghost" : ""
212 #else
213         ""
214 #endif
215         );
216     }
217 }