]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/matrix.c
Move disable JTAG code from `keyboard_init` to `keyboard_setup`
[qmk_firmware.git] / quantum / split_common / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include "wait.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29 #include "split_util.h"
30 #include "pro_micro.h"
31 #include "config.h"
32 #include "timer.h"
33 #include "split_flags.h"
34
35 #ifdef RGBLIGHT_ENABLE
36 #   include "rgblight.h"
37 #endif
38 #ifdef BACKLIGHT_ENABLE
39 #   include "backlight.h"
40     extern backlight_config_t backlight_config;
41 #endif
42
43 #if defined(USE_I2C) || defined(EH)
44 #  include "i2c.h"
45 #else // USE_SERIAL
46 #  include "serial.h"
47 #endif
48
49 #ifndef DEBOUNCING_DELAY
50 #   define DEBOUNCING_DELAY 5
51 #endif
52
53 #if (DEBOUNCING_DELAY > 0)
54     static uint16_t debouncing_time;
55     static bool debouncing = false;
56 #endif
57
58 #if (MATRIX_COLS <= 8)
59 #    define print_matrix_header()  print("\nr/c 01234567\n")
60 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
61 #    define matrix_bitpop(i)       bitpop(matrix[i])
62 #    define ROW_SHIFTER ((uint8_t)1)
63 #else
64 #    error "Currently only supports 8 COLS"
65 #endif
66 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
67
68 #define ERROR_DISCONNECT_COUNT 5
69
70 #define ROWS_PER_HAND (MATRIX_ROWS/2)
71
72 static uint8_t error_count = 0;
73
74 static uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
75 static uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
76
77 /* matrix state(1:on, 0:off) */
78 static matrix_row_t matrix[MATRIX_ROWS];
79 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
80
81 #if (DIODE_DIRECTION == COL2ROW)
82     static void init_cols(void);
83     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
84     static void unselect_rows(void);
85     static void select_row(uint8_t row);
86     static void unselect_row(uint8_t row);
87 #elif (DIODE_DIRECTION == ROW2COL)
88     static void init_rows(void);
89     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
90     static void unselect_cols(void);
91     static void unselect_col(uint8_t col);
92     static void select_col(uint8_t col);
93 #endif
94
95 __attribute__ ((weak))
96 void matrix_init_kb(void) {
97     matrix_init_user();
98 }
99
100 __attribute__ ((weak))
101 void matrix_scan_kb(void) {
102     matrix_scan_user();
103 }
104
105 __attribute__ ((weak))
106 void matrix_init_user(void) {
107 }
108
109 __attribute__ ((weak))
110 void matrix_scan_user(void) {
111 }
112
113 __attribute__ ((weak))
114 void matrix_slave_scan_user(void) {
115 }
116
117 inline
118 uint8_t matrix_rows(void)
119 {
120     return MATRIX_ROWS;
121 }
122
123 inline
124 uint8_t matrix_cols(void)
125 {
126     return MATRIX_COLS;
127 }
128
129 void matrix_init(void)
130 {
131     debug_enable = true;
132     debug_matrix = true;
133     debug_mouse = true;
134
135     // Set pinout for right half if pinout for that half is defined
136     if (!isLeftHand) {
137 #ifdef MATRIX_ROW_PINS_RIGHT
138         const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
139         for (uint8_t i = 0; i < MATRIX_ROWS; i++)
140             row_pins[i] = row_pins_right[i];
141 #endif
142 #ifdef MATRIX_COL_PINS_RIGHT
143         const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
144         for (uint8_t i = 0; i < MATRIX_COLS; i++)
145             col_pins[i] = col_pins_right[i];
146 #endif
147     }
148
149     // initialize row and col
150 #if (DIODE_DIRECTION == COL2ROW)
151     unselect_rows();
152     init_cols();
153 #elif (DIODE_DIRECTION == ROW2COL)
154     unselect_cols();
155     init_rows();
156 #endif
157
158     // initialize matrix state: all keys off
159     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
160         matrix[i] = 0;
161         matrix_debouncing[i] = 0;
162     }
163     
164     matrix_init_quantum();
165     
166 }
167
168 uint8_t _matrix_scan(void)
169 {
170     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
171 #if (DIODE_DIRECTION == COL2ROW)
172     // Set row, read cols
173     for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
174 #       if (DEBOUNCING_DELAY > 0)
175             bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
176
177             if (matrix_changed) {
178                 debouncing = true;
179                 debouncing_time = timer_read();
180             }
181
182 #       else
183             read_cols_on_row(matrix+offset, current_row);
184 #       endif
185
186     }
187
188 #elif (DIODE_DIRECTION == ROW2COL)
189     // Set col, read rows
190     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
191 #       if (DEBOUNCING_DELAY > 0)
192             bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
193             if (matrix_changed) {
194                 debouncing = true;
195                 debouncing_time = timer_read();
196             }
197 #       else
198              read_rows_on_col(matrix+offset, current_col);
199 #       endif
200
201     }
202 #endif
203
204 #   if (DEBOUNCING_DELAY > 0)
205         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
206             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
207                 matrix[i+offset] = matrix_debouncing[i+offset];
208             }
209             debouncing = false;
210         }
211 #   endif
212
213     return 1;
214 }
215
216 #if defined(USE_I2C) || defined(EH)
217
218 // Get rows from other half over i2c
219 int i2c_transaction(void) {
220     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
221     int err = 0;
222     
223     // write backlight info
224     #ifdef BACKLIGHT_ENABLE
225         if (BACKLIT_DIRTY) {
226             err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
227             if (err) goto i2c_error;
228             
229             // Backlight location
230             err = i2c_master_write(I2C_BACKLIT_START);
231             if (err) goto i2c_error;
232             
233             // Write backlight 
234             i2c_master_write(get_backlight_level());
235             
236             BACKLIT_DIRTY = false;
237         }
238     #endif
239
240     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
241     if (err) goto i2c_error;
242
243     // start of matrix stored at I2C_KEYMAP_START
244     err = i2c_master_write(I2C_KEYMAP_START);
245     if (err) goto i2c_error;
246
247     // Start read
248     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
249     if (err) goto i2c_error;
250
251     if (!err) {
252         int i;
253         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
254             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
255         }
256         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
257         i2c_master_stop();
258     } else {
259 i2c_error: // the cable is disconnceted, or something else went wrong
260         i2c_reset_state();
261         return err;
262     }
263     
264     #ifdef RGBLIGHT_ENABLE
265         if (RGB_DIRTY) {
266             err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
267             if (err) goto i2c_error;
268             
269             // RGB Location
270             err = i2c_master_write(I2C_RGB_START);
271             if (err) goto i2c_error;
272             
273             uint32_t dword = eeconfig_read_rgblight();
274             
275             // Write RGB
276             err = i2c_master_write_data(&dword, 4);
277             if (err) goto i2c_error;
278             
279             RGB_DIRTY = false;
280             i2c_master_stop();
281         }
282     #endif
283
284     return 0;
285 }
286
287 #else // USE_SERIAL
288
289 int serial_transaction(void) {
290     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
291
292     if (serial_update_buffers()) {
293         return 1;
294     }
295
296     for (int i = 0; i < ROWS_PER_HAND; ++i) {
297         matrix[slaveOffset+i] = serial_slave_buffer[i];
298     }
299     
300     #ifdef RGBLIGHT_ENABLE
301         // Code to send RGB over serial goes here (not implemented yet)
302     #endif
303     
304     #ifdef BACKLIGHT_ENABLE
305         // Write backlight level for slave to read
306         serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0;
307     #endif
308
309     return 0;
310 }
311 #endif
312
313 uint8_t matrix_scan(void)
314 {
315     uint8_t ret = _matrix_scan();
316
317 #if defined(USE_I2C) || defined(EH)
318     if( i2c_transaction() ) {
319 #else // USE_SERIAL
320     if( serial_transaction() ) {
321 #endif
322
323         error_count++;
324
325         if (error_count > ERROR_DISCONNECT_COUNT) {
326             // reset other half if disconnected
327             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
328             for (int i = 0; i < ROWS_PER_HAND; ++i) {
329                 matrix[slaveOffset+i] = 0;
330             }
331         }
332     } else {
333         error_count = 0;
334     }
335     matrix_scan_quantum();
336     return ret;
337 }
338
339 void matrix_slave_scan(void) {
340     _matrix_scan();
341
342     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
343
344 #if defined(USE_I2C) || defined(EH)
345     for (int i = 0; i < ROWS_PER_HAND; ++i) {
346         i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
347     }   
348 #else // USE_SERIAL
349     for (int i = 0; i < ROWS_PER_HAND; ++i) {
350         serial_slave_buffer[i] = matrix[offset+i];
351     }
352 #endif
353     matrix_slave_scan_user();
354 }
355
356 bool matrix_is_modified(void)
357 {
358     if (debouncing) return false;
359     return true;
360 }
361
362 inline
363 bool matrix_is_on(uint8_t row, uint8_t col)
364 {
365     return (matrix[row] & ((matrix_row_t)1<<col));
366 }
367
368 inline
369 matrix_row_t matrix_get_row(uint8_t row)
370 {
371     return matrix[row];
372 }
373
374 void matrix_print(void)
375 {
376     print("\nr/c 0123456789ABCDEF\n");
377     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
378         phex(row); print(": ");
379         pbin_reverse16(matrix_get_row(row));
380         print("\n");
381     }
382 }
383
384 uint8_t matrix_key_count(void)
385 {
386     uint8_t count = 0;
387     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
388         count += bitpop16(matrix[i]);
389     }
390     return count;
391 }
392
393 #if (DIODE_DIRECTION == COL2ROW)
394
395 static void init_cols(void)
396 {
397     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
398         uint8_t pin = col_pins[x];
399         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
400         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
401     }
402 }
403
404 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
405 {
406     // Store last value of row prior to reading
407     matrix_row_t last_row_value = current_matrix[current_row];
408
409     // Clear data in matrix row
410     current_matrix[current_row] = 0;
411
412     // Select row and wait for row selecton to stabilize
413     select_row(current_row);
414     wait_us(30);
415
416     // For each col...
417     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
418
419         // Select the col pin to read (active low)
420         uint8_t pin = col_pins[col_index];
421         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
422
423         // Populate the matrix row with the state of the col pin
424         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
425     }
426
427     // Unselect row
428     unselect_row(current_row);
429
430     return (last_row_value != current_matrix[current_row]);
431 }
432
433 static void select_row(uint8_t row)
434 {
435     uint8_t pin = row_pins[row];
436     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
437     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
438 }
439
440 static void unselect_row(uint8_t row)
441 {
442     uint8_t pin = row_pins[row];
443     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
444     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
445 }
446
447 static void unselect_rows(void)
448 {
449     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
450         uint8_t pin = row_pins[x];
451         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
452         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
453     }
454 }
455
456 #elif (DIODE_DIRECTION == ROW2COL)
457
458 static void init_rows(void)
459 {
460     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
461         uint8_t pin = row_pins[x];
462         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
463         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
464     }
465 }
466
467 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
468 {
469     bool matrix_changed = false;
470
471     // Select col and wait for col selecton to stabilize
472     select_col(current_col);
473     wait_us(30);
474
475     // For each row...
476     for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
477     {
478
479         // Store last value of row prior to reading
480         matrix_row_t last_row_value = current_matrix[row_index];
481
482         // Check row pin state
483         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
484         {
485             // Pin LO, set col bit
486             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
487         }
488         else
489         {
490             // Pin HI, clear col bit
491             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
492         }
493
494         // Determine if the matrix changed state
495         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
496         {
497             matrix_changed = true;
498         }
499     }
500
501     // Unselect col
502     unselect_col(current_col);
503
504     return matrix_changed;
505 }
506
507 static void select_col(uint8_t col)
508 {
509     uint8_t pin = col_pins[col];
510     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
511     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
512 }
513
514 static void unselect_col(uint8_t col)
515 {
516     uint8_t pin = col_pins[col];
517     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
518     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
519 }
520
521 static void unselect_cols(void)
522 {
523     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
524         uint8_t pin = col_pins[x];
525         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
526         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
527     }
528 }
529
530 #endif