aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common/matrix.c
diff options
context:
space:
mode:
authorAlex Ong <the.onga@gmail.com>2019-01-04 19:39:14 +1100
committerAlex Ong <the.onga@gmail.com>2019-01-04 19:39:14 +1100
commit47c91fc7f75ae0a477e55b687aa0fc30da0a283c (patch)
tree65ad39452748ff2e6d4a83ce54ede6ca22c9ada9 /quantum/split_common/matrix.c
parentac9b88e8ccbbf38762871504cd827ff0d941c426 (diff)
parent563ce3f225d981ce460c12ca5130dfe47af41df0 (diff)
downloadqmk_firmware-47c91fc7f75ae0a477e55b687aa0fc30da0a283c.tar.gz
qmk_firmware-47c91fc7f75ae0a477e55b687aa0fc30da0a283c.zip
Merge branch 'master' of https://github.com/qmk/qmk_firmware
Diffstat (limited to 'quantum/split_common/matrix.c')
-rw-r--r--quantum/split_common/matrix.c142
1 files changed, 87 insertions, 55 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c
index 0a79e4256..2c37053f8 100644
--- a/quantum/split_common/matrix.c
+++ b/quantum/split_common/matrix.c
@@ -20,21 +20,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */ 20 */
21#include <stdint.h> 21#include <stdint.h>
22#include <stdbool.h> 22#include <stdbool.h>
23#include <avr/io.h>
24#include "wait.h" 23#include "wait.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h" 24#include "util.h"
28#include "matrix.h" 25#include "matrix.h"
29#include "split_util.h" 26#include "split_util.h"
30#include "pro_micro.h"
31#include "config.h" 27#include "config.h"
32#include "timer.h" 28#include "timer.h"
33#include "split_flags.h" 29#include "split_flags.h"
30#include "quantum.h"
34 31
35#ifdef RGBLIGHT_ENABLE
36# include "rgblight.h"
37#endif
38#ifdef BACKLIGHT_ENABLE 32#ifdef BACKLIGHT_ENABLE
39# include "backlight.h" 33# include "backlight.h"
40 extern backlight_config_t backlight_config; 34 extern backlight_config_t backlight_config;
@@ -55,6 +49,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
55 static bool debouncing = false; 49 static bool debouncing = false;
56#endif 50#endif
57 51
52#if defined(USE_I2C) || defined(EH)
53
58#if (MATRIX_COLS <= 8) 54#if (MATRIX_COLS <= 8)
59# define print_matrix_header() print("\nr/c 01234567\n") 55# define print_matrix_header() print("\nr/c 01234567\n")
60# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) 56# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
@@ -63,6 +59,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
63#else 59#else
64# error "Currently only supports 8 COLS" 60# error "Currently only supports 8 COLS"
65#endif 61#endif
62
63#else // USE_SERIAL
64
65#if (MATRIX_COLS <= 8)
66# define print_matrix_header() print("\nr/c 01234567\n")
67# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
68# define matrix_bitpop(i) bitpop(matrix[i])
69# define ROW_SHIFTER ((uint8_t)1)
70#elif (MATRIX_COLS <= 16)
71# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
72# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
73# define matrix_bitpop(i) bitpop16(matrix[i])
74# define ROW_SHIFTER ((uint16_t)1)
75#elif (MATRIX_COLS <= 32)
76# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
77# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
78# define matrix_bitpop(i) bitpop32(matrix[i])
79# define ROW_SHIFTER ((uint32_t)1)
80#endif
81
82#endif
66static matrix_row_t matrix_debouncing[MATRIX_ROWS]; 83static matrix_row_t matrix_debouncing[MATRIX_ROWS];
67 84
68#define ERROR_DISCONNECT_COUNT 5 85#define ERROR_DISCONNECT_COUNT 5
@@ -71,8 +88,8 @@ static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71 88
72static uint8_t error_count = 0; 89static uint8_t error_count = 0;
73 90
74static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 91static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
75static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 92static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
76 93
77/* matrix state(1:on, 0:off) */ 94/* matrix state(1:on, 0:off) */
78static matrix_row_t matrix[MATRIX_ROWS]; 95static matrix_row_t matrix[MATRIX_ROWS];
@@ -128,15 +145,24 @@ uint8_t matrix_cols(void)
128 145
129void matrix_init(void) 146void matrix_init(void)
130{ 147{
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; 148 debug_enable = true;
138 debug_matrix = true; 149 debug_matrix = true;
139 debug_mouse = true; 150 debug_mouse = true;
151
152 // Set pinout for right half if pinout for that half is defined
153 if (!isLeftHand) {
154#ifdef MATRIX_ROW_PINS_RIGHT
155 const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
156 for (uint8_t i = 0; i < MATRIX_ROWS; i++)
157 row_pins[i] = row_pins_right[i];
158#endif
159#ifdef MATRIX_COL_PINS_RIGHT
160 const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
161 for (uint8_t i = 0; i < MATRIX_COLS; i++)
162 col_pins[i] = col_pins_right[i];
163#endif
164 }
165
140 // initialize row and col 166 // initialize row and col
141#if (DIODE_DIRECTION == COL2ROW) 167#if (DIODE_DIRECTION == COL2ROW)
142 unselect_rows(); 168 unselect_rows();
@@ -277,24 +303,48 @@ i2c_error: // the cable is disconnceted, or something else went wrong
277 303
278#else // USE_SERIAL 304#else // USE_SERIAL
279 305
306
307typedef struct _Serial_s2m_buffer_t {
308 // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
309 matrix_row_t smatrix[ROWS_PER_HAND];
310} Serial_s2m_buffer_t;
311
312volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
313volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
314uint8_t volatile status0 = 0;
315
316SSTD_t transactions[] = {
317 { (uint8_t *)&status0,
318 sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer,
319 sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer
320 }
321};
322
323void serial_master_init(void)
324{ soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
325
326void serial_slave_init(void)
327{ soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
328
280int serial_transaction(void) { 329int serial_transaction(void) {
281 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; 330 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
282 331
283 if (serial_update_buffers()) { 332 if (soft_serial_transaction()) {
284 return 1; 333 return 1;
285 } 334 }
286 335
336 // TODO: if MATRIX_COLS > 8 change to unpack()
287 for (int i = 0; i < ROWS_PER_HAND; ++i) { 337 for (int i = 0; i < ROWS_PER_HAND; ++i) {
288 matrix[slaveOffset+i] = serial_slave_buffer[i]; 338 matrix[slaveOffset+i] = serial_s2m_buffer.smatrix[i];
289 } 339 }
290 340
291 #ifdef RGBLIGHT_ENABLE 341 #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
292 // Code to send RGB over serial goes here (not implemented yet) 342 // Code to send RGB over serial goes here (not implemented yet)
293 #endif 343 #endif
294 344
295 #ifdef BACKLIGHT_ENABLE 345 #ifdef BACKLIGHT_ENABLE
296 // Write backlight level for slave to read 346 // Write backlight level for slave to read
297 serial_master_buffer[SERIAL_BACKLIT_START] = backlight_config.enable ? backlight_config.level : 0; 347 serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
298 #endif 348 #endif
299 349
300 return 0; 350 return 0;
@@ -337,8 +387,9 @@ void matrix_slave_scan(void) {
337 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i]; 387 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
338 } 388 }
339#else // USE_SERIAL 389#else // USE_SERIAL
390 // TODO: if MATRIX_COLS > 8 change to pack()
340 for (int i = 0; i < ROWS_PER_HAND; ++i) { 391 for (int i = 0; i < ROWS_PER_HAND; ++i) {
341 serial_slave_buffer[i] = matrix[offset+i]; 392 serial_s2m_buffer.smatrix[i] = matrix[offset+i];
342 } 393 }
343#endif 394#endif
344 matrix_slave_scan_user(); 395 matrix_slave_scan_user();
@@ -386,9 +437,7 @@ uint8_t matrix_key_count(void)
386static void init_cols(void) 437static void init_cols(void)
387{ 438{
388 for(uint8_t x = 0; x < MATRIX_COLS; x++) { 439 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
389 uint8_t pin = col_pins[x]; 440 setPinInputHigh(col_pins[x]);
390 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
391 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
392 } 441 }
393} 442}
394 443
@@ -406,13 +455,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
406 455
407 // For each col... 456 // For each col...
408 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 457 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
409
410 // Select the col pin to read (active low)
411 uint8_t pin = col_pins[col_index];
412 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
413
414 // Populate the matrix row with the state of the col pin 458 // Populate the matrix row with the state of the col pin
415 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 459 current_matrix[current_row] |= readPin(col_pins[col_index]) ? 0 : (ROW_SHIFTER << col_index);
416 } 460 }
417 461
418 // Unselect row 462 // Unselect row
@@ -423,24 +467,19 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
423 467
424static void select_row(uint8_t row) 468static void select_row(uint8_t row)
425{ 469{
426 uint8_t pin = row_pins[row]; 470 writePinLow(row_pins[row]);
427 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT 471 setPinOutput(row_pins[row]);
428 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
429} 472}
430 473
431static void unselect_row(uint8_t row) 474static void unselect_row(uint8_t row)
432{ 475{
433 uint8_t pin = row_pins[row]; 476 setPinInputHigh(row_pins[row]);
434 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
435 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
436} 477}
437 478
438static void unselect_rows(void) 479static void unselect_rows(void)
439{ 480{
440 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { 481 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
441 uint8_t pin = row_pins[x]; 482 setPinInputHigh(row_pins[x]);
442 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
443 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
444 } 483 }
445} 484}
446 485
@@ -449,9 +488,7 @@ static void unselect_rows(void)
449static void init_rows(void) 488static void init_rows(void)
450{ 489{
451 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { 490 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
452 uint8_t pin = row_pins[x]; 491 setPinInputHigh(row_pins[x]);
453 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
454 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
455 } 492 }
456} 493}
457 494
@@ -471,15 +508,15 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
471 matrix_row_t last_row_value = current_matrix[row_index]; 508 matrix_row_t last_row_value = current_matrix[row_index];
472 509
473 // Check row pin state 510 // Check row pin state
474 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) 511 if (readPin(row_pins[row_index]))
475 { 512 {
476 // Pin LO, set col bit 513 // Pin HI, clear col bit
477 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 514 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
478 } 515 }
479 else 516 else
480 { 517 {
481 // Pin HI, clear col bit 518 // Pin LO, set col bit
482 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); 519 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
483 } 520 }
484 521
485 // Determine if the matrix changed state 522 // Determine if the matrix changed state
@@ -497,24 +534,19 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
497 534
498static void select_col(uint8_t col) 535static void select_col(uint8_t col)
499{ 536{
500 uint8_t pin = col_pins[col]; 537 writePinLow(col_pins[col]);
501 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT 538 setPinOutput(col_pins[col]);
502 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
503} 539}
504 540
505static void unselect_col(uint8_t col) 541static void unselect_col(uint8_t col)
506{ 542{
507 uint8_t pin = col_pins[col]; 543 setPinInputHigh(col_pins[col]);
508 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
509 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
510} 544}
511 545
512static void unselect_cols(void) 546static void unselect_cols(void)
513{ 547{
514 for(uint8_t x = 0; x < MATRIX_COLS; x++) { 548 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
515 uint8_t pin = col_pins[x]; 549 setPinInputHigh(col_pins[x]);
516 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
517 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
518 } 550 }
519} 551}
520 552