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