]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/split_common/matrix.c
Add support for a different pinout on Split boards (#3869)
[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 #ifdef DISABLE_JTAG
132   // JTAG disable for PORT F. write JTD bit twice within four cycles.
133   MCUCR |= (1<<JTD);
134   MCUCR |= (1<<JTD);
135 #endif
136
137     debug_enable = true;
138     debug_matrix = true;
139     debug_mouse = true;
140
141     // Set pinout for right half if pinout for that half is defined
142     if (!isLeftHand) {
143 #ifdef MATRIX_ROW_PINS_RIGHT
144         const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
145         for (uint8_t i = 0; i < MATRIX_ROWS; i++)
146             row_pins[i] = row_pins_right[i];
147 #endif
148 #ifdef MATRIX_COL_PINS_RIGHT
149         const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
150         for (uint8_t i = 0; i < MATRIX_COLS; i++)
151             col_pins[i] = col_pins_right[i];
152 #endif
153     }
154
155     // initialize row and col
156 #if (DIODE_DIRECTION == COL2ROW)
157     unselect_rows();
158     init_cols();
159 #elif (DIODE_DIRECTION == ROW2COL)
160     unselect_cols();
161     init_rows();
162 #endif
163
164     // initialize matrix state: all keys off
165     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
166         matrix[i] = 0;
167         matrix_debouncing[i] = 0;
168     }
169     
170     matrix_init_quantum();
171     
172 }
173
174 uint8_t _matrix_scan(void)
175 {
176     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
177 #if (DIODE_DIRECTION == COL2ROW)
178     // Set row, read cols
179     for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
180 #       if (DEBOUNCING_DELAY > 0)
181             bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
182
183             if (matrix_changed) {
184                 debouncing = true;
185                 debouncing_time = timer_read();
186             }
187
188 #       else
189             read_cols_on_row(matrix+offset, current_row);
190 #       endif
191
192     }
193
194 #elif (DIODE_DIRECTION == ROW2COL)
195     // Set col, read rows
196     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
197 #       if (DEBOUNCING_DELAY > 0)
198             bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
199             if (matrix_changed) {
200                 debouncing = true;
201                 debouncing_time = timer_read();
202             }
203 #       else
204              read_rows_on_col(matrix+offset, current_col);
205 #       endif
206
207     }
208 #endif
209
210 #   if (DEBOUNCING_DELAY > 0)
211         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
212             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
213                 matrix[i+offset] = matrix_debouncing[i+offset];
214             }
215             debouncing = false;
216         }
217 #   endif
218
219     return 1;
220 }
221
222 #if defined(USE_I2C) || defined(EH)
223
224 // Get rows from other half over i2c
225 int i2c_transaction(void) {
226     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
227     int err = 0;
228     
229     // write backlight info
230     #ifdef BACKLIGHT_ENABLE
231         if (BACKLIT_DIRTY) {
232             err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
233             if (err) goto i2c_error;
234             
235             // Backlight location
236             err = i2c_master_write(I2C_BACKLIT_START);
237             if (err) goto i2c_error;
238             
239             // Write backlight 
240             i2c_master_write(get_backlight_level());
241             
242             BACKLIT_DIRTY = false;
243         }
244     #endif
245
246     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
247     if (err) goto i2c_error;
248
249     // start of matrix stored at I2C_KEYMAP_START
250     err = i2c_master_write(I2C_KEYMAP_START);
251     if (err) goto i2c_error;
252
253     // Start read
254     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
255     if (err) goto i2c_error;
256
257     if (!err) {
258         int i;
259         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
260             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
261         }
262         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
263         i2c_master_stop();
264     } else {
265 i2c_error: // the cable is disconnceted, or something else went wrong
266         i2c_reset_state();
267         return err;
268     }
269     
270     #ifdef RGBLIGHT_ENABLE
271         if (RGB_DIRTY) {
272             err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
273             if (err) goto i2c_error;
274             
275             // RGB Location
276             err = i2c_master_write(I2C_RGB_START);
277             if (err) goto i2c_error;
278             
279             uint32_t dword = eeconfig_read_rgblight();
280             
281             // Write RGB
282             err = i2c_master_write_data(&dword, 4);
283             if (err) goto i2c_error;
284             
285             RGB_DIRTY = false;
286             i2c_master_stop();
287         }
288     #endif
289
290     return 0;
291 }
292
293 #else // USE_SERIAL
294
295 int serial_transaction(void) {
296     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
297
298     if (serial_update_buffers()) {
299         return 1;
300     }
301
302     for (int i = 0; i < ROWS_PER_HAND; ++i) {
303         matrix[slaveOffset+i] = serial_slave_buffer[i];
304     }
305     
306     #ifdef RGBLIGHT_ENABLE
307         // Code to send RGB over serial goes here (not implemented yet)
308     #endif
309     
310     #ifdef BACKLIGHT_ENABLE
311         // Write backlight level for slave to read
312         serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0;
313     #endif
314
315     return 0;
316 }
317 #endif
318
319 uint8_t matrix_scan(void)
320 {
321     uint8_t ret = _matrix_scan();
322
323 #if defined(USE_I2C) || defined(EH)
324     if( i2c_transaction() ) {
325 #else // USE_SERIAL
326     if( serial_transaction() ) {
327 #endif
328
329         error_count++;
330
331         if (error_count > ERROR_DISCONNECT_COUNT) {
332             // reset other half if disconnected
333             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
334             for (int i = 0; i < ROWS_PER_HAND; ++i) {
335                 matrix[slaveOffset+i] = 0;
336             }
337         }
338     } else {
339         error_count = 0;
340     }
341     matrix_scan_quantum();
342     return ret;
343 }
344
345 void matrix_slave_scan(void) {
346     _matrix_scan();
347
348     int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
349
350 #if defined(USE_I2C) || defined(EH)
351     for (int i = 0; i < ROWS_PER_HAND; ++i) {
352         i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
353     }   
354 #else // USE_SERIAL
355     for (int i = 0; i < ROWS_PER_HAND; ++i) {
356         serial_slave_buffer[i] = matrix[offset+i];
357     }
358 #endif
359 #ifdef USE_I2C
360 #ifdef BACKLIGHT_ENABLE
361     // Read backlight level sent from master and update level on slave
362     backlight_set(i2c_slave_buffer[0]);
363 #endif
364     for (int i = 0; i < ROWS_PER_HAND; ++i) {
365         i2c_slave_buffer[i+1] = matrix[offset+i];
366     }
367 #else // USE_SERIAL
368     for (int i = 0; i < ROWS_PER_HAND; ++i) {
369         serial_slave_buffer[i] = matrix[offset+i];
370     }
371
372 #ifdef BACKLIGHT_ENABLE
373     // Read backlight level sent from master and update level on slave
374     backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]);
375 #endif
376 #endif
377     matrix_slave_scan_user();
378 }
379
380 bool matrix_is_modified(void)
381 {
382     if (debouncing) return false;
383     return true;
384 }
385
386 inline
387 bool matrix_is_on(uint8_t row, uint8_t col)
388 {
389     return (matrix[row] & ((matrix_row_t)1<<col));
390 }
391
392 inline
393 matrix_row_t matrix_get_row(uint8_t row)
394 {
395     return matrix[row];
396 }
397
398 void matrix_print(void)
399 {
400     print("\nr/c 0123456789ABCDEF\n");
401     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
402         phex(row); print(": ");
403         pbin_reverse16(matrix_get_row(row));
404         print("\n");
405     }
406 }
407
408 uint8_t matrix_key_count(void)
409 {
410     uint8_t count = 0;
411     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
412         count += bitpop16(matrix[i]);
413     }
414     return count;
415 }
416
417 #if (DIODE_DIRECTION == COL2ROW)
418
419 static void init_cols(void)
420 {
421     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
422         uint8_t pin = col_pins[x];
423         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
424         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
425     }
426 }
427
428 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
429 {
430     // Store last value of row prior to reading
431     matrix_row_t last_row_value = current_matrix[current_row];
432
433     // Clear data in matrix row
434     current_matrix[current_row] = 0;
435
436     // Select row and wait for row selecton to stabilize
437     select_row(current_row);
438     wait_us(30);
439
440     // For each col...
441     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
442
443         // Select the col pin to read (active low)
444         uint8_t pin = col_pins[col_index];
445         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
446
447         // Populate the matrix row with the state of the col pin
448         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
449     }
450
451     // Unselect row
452     unselect_row(current_row);
453
454     return (last_row_value != current_matrix[current_row]);
455 }
456
457 static void select_row(uint8_t row)
458 {
459     uint8_t pin = row_pins[row];
460     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
461     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
462 }
463
464 static void unselect_row(uint8_t row)
465 {
466     uint8_t pin = row_pins[row];
467     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
468     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
469 }
470
471 static void unselect_rows(void)
472 {
473     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
474         uint8_t pin = row_pins[x];
475         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
476         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
477     }
478 }
479
480 #elif (DIODE_DIRECTION == ROW2COL)
481
482 static void init_rows(void)
483 {
484     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
485         uint8_t pin = row_pins[x];
486         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
487         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
488     }
489 }
490
491 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
492 {
493     bool matrix_changed = false;
494
495     // Select col and wait for col selecton to stabilize
496     select_col(current_col);
497     wait_us(30);
498
499     // For each row...
500     for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
501     {
502
503         // Store last value of row prior to reading
504         matrix_row_t last_row_value = current_matrix[row_index];
505
506         // Check row pin state
507         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
508         {
509             // Pin LO, set col bit
510             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
511         }
512         else
513         {
514             // Pin HI, clear col bit
515             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
516         }
517
518         // Determine if the matrix changed state
519         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
520         {
521             matrix_changed = true;
522         }
523     }
524
525     // Unselect col
526     unselect_col(current_col);
527
528     return matrix_changed;
529 }
530
531 static void select_col(uint8_t col)
532 {
533     uint8_t pin = col_pins[col];
534     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
535     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
536 }
537
538 static void unselect_col(uint8_t col)
539 {
540     uint8_t pin = col_pins[col];
541     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
542     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
543 }
544
545 static void unselect_cols(void)
546 {
547     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
548         uint8_t pin = col_pins[x];
549         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
550         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
551     }
552 }
553
554 #endif