]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/matrix.c
1bacea1bed32c7cfbc093b2351164d32c4452f14
[qmk_firmware.git] / quantum / matrix.c
1 /*
2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
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 #include <stdint.h>
19 #include <stdbool.h>
20 #if defined(__AVR__)
21 #include <avr/io.h>
22 #endif
23 #include "wait.h"
24 #include "print.h"
25 #include "debug.h"
26 #include "util.h"
27 #include "matrix.h"
28
29 #if (MATRIX_COLS <= 8)
30 #    define print_matrix_header()  print("\nr/c 01234567\n")
31 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
32 #    define matrix_bitpop(i)       bitpop(matrix[i])
33 #    define ROW_SHIFTER ((uint8_t)1)
34 #elif (MATRIX_COLS <= 16)
35 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF\n")
36 #    define print_matrix_row(row)  print_bin_reverse16(matrix_get_row(row))
37 #    define matrix_bitpop(i)       bitpop16(matrix[i])
38 #    define ROW_SHIFTER ((uint16_t)1)
39 #elif (MATRIX_COLS <= 32)
40 #    define print_matrix_header()  print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
41 #    define print_matrix_row(row)  print_bin_reverse32(matrix_get_row(row))
42 #    define matrix_bitpop(i)       bitpop32(matrix[i])
43 #    define ROW_SHIFTER  ((uint32_t)1)
44 #endif
45
46 #ifdef MATRIX_MASKED
47 extern const matrix_row_t matrix_mask[];
48 #endif
49
50 /* Set 0 if debouncing isn't needed */
51
52 #ifndef DEBOUNCING_DELAY
53 #   define DEBOUNCING_DELAY 5
54 #endif
55 static uint8_t debouncing = DEBOUNCING_DELAY;
56
57 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
58 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
59
60 /* matrix state(1:on, 0:off) */
61 static matrix_row_t matrix[MATRIX_ROWS];
62
63 static matrix_row_t matrix_raw[MATRIX_ROWS];
64
65
66 #if DIODE_DIRECTION == COL2ROW
67     static matrix_row_t matrix_debouncing[MATRIX_ROWS];
68 #else // ROW2COL
69     static matrix_col_t matrix_transposed[MATRIX_COLS];
70     static matrix_col_t matrix_transposed_debouncing[MATRIX_COLS];
71 #endif
72
73 #if (DIODE_DIRECTION == COL2ROW)
74     static void init_cols(void);
75     static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
76     static void unselect_rows(void);
77     static void select_row(uint8_t row);
78     static void unselect_row(uint8_t row);
79 #else // ROW2COL
80     static void init_rows(void);
81     static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
82     static void unselect_cols(void);
83     static void unselect_col(uint8_t col);
84     static void select_col(uint8_t col);
85 #endif
86
87 __attribute__ ((weak))
88 void matrix_init_quantum(void) {
89     matrix_init_kb();
90 }
91
92 __attribute__ ((weak))
93 void matrix_scan_quantum(void) {
94     matrix_scan_kb();
95 }
96
97 __attribute__ ((weak))
98 void matrix_init_kb(void) {
99     matrix_init_user();
100 }
101
102 __attribute__ ((weak))
103 void matrix_scan_kb(void) {
104     matrix_scan_user();
105 }
106
107 __attribute__ ((weak))
108 void matrix_init_user(void) {
109 }
110
111 __attribute__ ((weak))
112 void matrix_scan_user(void) {
113 }
114
115 inline
116 uint8_t matrix_rows(void) {
117     return MATRIX_ROWS;
118 }
119
120 inline
121 uint8_t matrix_cols(void) {
122     return MATRIX_COLS;
123 }
124
125 // void matrix_power_up(void) {
126 // #if (DIODE_DIRECTION == COL2ROW)
127 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
128 //         /* DDRxn */
129 //         _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
130 //         toggle_row(r);
131 //     }
132 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
133 //         /* PORTxn */
134 //         _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
135 //     }
136 // #else
137 //     for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
138 //         /* DDRxn */
139 //         _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
140 //         toggle_col(c);
141 //     }
142 //     for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
143 //         /* PORTxn */
144 //         _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
145 //     }
146 // #endif
147 // }
148
149 void matrix_init(void) {
150
151     // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
152     #if  (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
153         MCUCR |= _BV(JTD);
154         MCUCR |= _BV(JTD);
155     #endif
156
157     // initialize row and col
158 #if (DIODE_DIRECTION == COL2ROW)
159     unselect_rows();
160     init_cols();
161
162     // initialize matrix state: all keys off
163     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
164         matrix[i] = 0;
165         matrix_raw[i] = 0;
166         matrix_debouncing[i] = 0;
167     }
168
169 #else // ROW2COL
170     unselect_cols();
171     init_rows();
172
173     // initialize matrix state: all keys off
174     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
175         matrix_raw[i] = 0;
176         matrix[i] = 0;
177     }
178
179     // initialize matrix state: all keys off
180     for (uint8_t i=0; i < MATRIX_COLS; i++) {
181         matrix_transposed_debouncing[i] = 0;
182     }
183 #endif
184
185     matrix_init_quantum();
186 }
187
188 uint8_t matrix_scan(void)
189 {
190
191 #if (DIODE_DIRECTION == COL2ROW)
192
193     // Set row, read cols
194     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
195         read_cols_on_row(matrix, current_row);
196     }
197
198     //     select_row(i);
199     //     wait_us(30);  // without this wait read unstable value.
200     //     matrix_row_t current_row = read_cols();
201     //     if (matrix_debouncing[i] != current_row) {
202     //         matrix_debouncing[i] = current_row;
203     //         if (debouncing) {
204     //             debug("bounce!: "); debug_hex(debouncing); debug("\n");
205     //         }
206     //         debouncing = DEBOUNCING_DELAY;
207     //     }
208     //     unselect_row(i);
209     // }
210
211     // if (debouncing) {
212     //     if (--debouncing) {
213     //         wait_ms(1);
214     //     } else {
215     //         for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
216     //             matrix[i] = matrix_debouncing[i];
217     //         }
218     //     }
219     // }
220
221 #else // ROW2COL
222
223     // Set col, read rows
224     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
225         read_rows_on_col(matrix, current_col);
226     }
227
228
229     // for (uint8_t i = 0; i < MATRIX_COLS; i++) {
230     //     select_col(i);
231     //     wait_us(30);  // without this wait read unstable value.
232     //     matrix_col_t current_col = read_rows();
233     //     if (matrix_transposed_debouncing[i] != current_col) {
234     //         matrix_transposed_debouncing[i] = current_col;
235     //         if (debouncing) {
236     //             debug("bounce!: "); debug_hex(debouncing); debug("\n");
237     //         }
238     //         debouncing = DEBOUNCING_DELAY;
239     //     }
240     //     unselect_col(i);
241     // }
242
243     // if (debouncing) {
244     //     if (--debouncing) {
245     //         wait_ms(1);
246     //     } else {
247     //         for (uint8_t i = 0; i < MATRIX_COLS; i++) {
248     //             matrix_transposed[i] = matrix_transposed_debouncing[i];
249     //         }
250     //     }
251     // }
252
253     // // Untranspose matrix
254     // for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
255     //     matrix_row_t row = 0;
256     //     for (uint8_t x = 0; x < MATRIX_COLS; x++) {
257     //         row |= ((matrix_transposed[x] & (1<<y)) >> y) << x;
258     //     }
259     //     matrix[y] = row;
260     // }
261
262 #endif
263
264     matrix_scan_quantum();
265
266     return 1;
267 }
268
269 bool matrix_is_modified(void)
270 {
271     if (debouncing) return false;
272     return true;
273 }
274
275 inline
276 bool matrix_is_on(uint8_t row, uint8_t col)
277 {
278     return (matrix[row] & ((matrix_row_t)1<col));
279 }
280
281 inline
282 matrix_row_t matrix_get_row(uint8_t row)
283 {
284     // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
285     // switch blocker installed and the switch is always pressed.
286 #ifdef MATRIX_MASKED
287     return matrix[row] & matrix_mask[row];
288 #else
289     return matrix[row];
290 #endif
291 }
292
293 void matrix_print(void)
294 {
295     print_matrix_header();
296
297     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
298         phex(row); print(": ");
299         print_matrix_row(row);
300         print("\n");
301     }
302 }
303
304 uint8_t matrix_key_count(void)
305 {
306     uint8_t count = 0;
307     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
308         count += matrix_bitpop(i);
309     }
310     return count;
311 }
312
313
314
315 #if (DIODE_DIRECTION == COL2ROW)
316
317 static void init_cols(void)
318 {
319     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
320         uint8_t pin = col_pins[x];
321         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
322         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
323     }
324 }
325
326 static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
327 {
328     // Clear data in matrix row
329     current_matrix[current_row] = 0;
330
331     // Select row and wait for row selecton to stabilize
332     select_row(current_row);
333     wait_us(30);
334
335     // For each col...
336     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
337
338         // Select the col pin to read (active low)
339         uint8_t pin = col_pins[col_index];
340         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
341
342         // Populate the matrix row with the state of the col pin
343         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
344     }
345 }
346
347 static void select_row(uint8_t row)
348 {
349     uint8_t pin = row_pins[row];
350     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
351     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
352 }
353
354 static void unselect_row(uint8_t row)
355 {
356     uint8_t pin = row_pins[row];
357     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
358     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
359 }
360
361 static void unselect_rows(void)
362 {
363     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
364         uint8_t pin = row_pins[x];
365         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
366         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
367     }
368 }
369
370 #else // ROW2COL
371
372 static void init_rows(void)
373 {
374     for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
375         uint8_t pin = row_pins[x];
376         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
377         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
378     }
379 }
380
381 static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
382 {
383
384     // Select col and wait for col selecton to stabilize
385     select_col(current_col);
386     wait_us(30);
387
388     // For each row...
389     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
390
391         // Select the row pin to read (active low)
392         uint8_t pin = row_pins[row_index];
393         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
394
395         // Populate the matrix row with the state of the col pin
396         current_matrix[row_index] &= pin_state ? ~(ROW_SHIFTER << current_col) : 0;
397     }
398 }
399
400 static void select_col(uint8_t col)
401 {
402     uint8_t pin = col_pins[col];
403     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
404     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
405 }
406
407 static void unselect_col(uint8_t col)
408 {
409     uint8_t pin = col_pins[col];
410     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
411     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
412 }
413
414 static void unselect_cols(void)
415 {
416     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
417         uint8_t pin = col_pins[x];
418         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
419         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
420     }
421 }
422
423 #endif