diff options
Diffstat (limited to 'quantum/split_common/matrix.c')
| -rw-r--r-- | quantum/split_common/matrix.c | 160 |
1 files changed, 87 insertions, 73 deletions
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 4af90209f..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 | ||
| 66 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | 83 | static 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 | ||
| 72 | static uint8_t error_count = 0; | 89 | static uint8_t error_count = 0; |
| 73 | 90 | ||
| 74 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | 91 | static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; |
| 75 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | 92 | static 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) */ |
| 78 | static matrix_row_t matrix[MATRIX_ROWS]; | 95 | static matrix_row_t matrix[MATRIX_ROWS]; |
| @@ -128,15 +145,24 @@ uint8_t matrix_cols(void) | |||
| 128 | 145 | ||
| 129 | void matrix_init(void) | 146 | void 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 | |||
| 307 | typedef 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 | |||
| 312 | volatile Serial_s2m_buffer_t serial_s2m_buffer = {}; | ||
| 313 | volatile Serial_m2s_buffer_t serial_m2s_buffer = {}; | ||
| 314 | uint8_t volatile status0 = 0; | ||
| 315 | |||
| 316 | SSTD_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 | |||
| 323 | void serial_master_init(void) | ||
| 324 | { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); } | ||
| 325 | |||
| 326 | void serial_slave_init(void) | ||
| 327 | { soft_serial_target_init(transactions, TID_LIMIT(transactions)); } | ||
| 328 | |||
| 280 | int serial_transaction(void) { | 329 | int 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,27 +387,10 @@ 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 | } | ||
| 343 | #endif | ||
| 344 | #ifdef USE_I2C | ||
| 345 | #ifdef BACKLIGHT_ENABLE | ||
| 346 | // Read backlight level sent from master and update level on slave | ||
| 347 | backlight_set(i2c_slave_buffer[0]); | ||
| 348 | #endif | ||
| 349 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 350 | i2c_slave_buffer[i+1] = matrix[offset+i]; | ||
| 351 | } | ||
| 352 | #else // USE_SERIAL | ||
| 353 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 354 | serial_slave_buffer[i] = matrix[offset+i]; | ||
| 355 | } | 393 | } |
| 356 | |||
| 357 | #ifdef BACKLIGHT_ENABLE | ||
| 358 | // Read backlight level sent from master and update level on slave | ||
| 359 | backlight_set(serial_master_buffer[SERIAL_BACKLIT_START]); | ||
| 360 | #endif | ||
| 361 | #endif | 394 | #endif |
| 362 | matrix_slave_scan_user(); | 395 | matrix_slave_scan_user(); |
| 363 | } | 396 | } |
| @@ -404,9 +437,7 @@ uint8_t matrix_key_count(void) | |||
| 404 | static void init_cols(void) | 437 | static void init_cols(void) |
| 405 | { | 438 | { |
| 406 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | 439 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { |
| 407 | uint8_t pin = col_pins[x]; | 440 | setPinInputHigh(col_pins[x]); |
| 408 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
| 409 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
| 410 | } | 441 | } |
| 411 | } | 442 | } |
| 412 | 443 | ||
| @@ -424,13 +455,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
| 424 | 455 | ||
| 425 | // For each col... | 456 | // For each col... |
| 426 | 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++) { |
| 427 | |||
| 428 | // Select the col pin to read (active low) | ||
| 429 | uint8_t pin = col_pins[col_index]; | ||
| 430 | uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); | ||
| 431 | |||
| 432 | // Populate the matrix row with the state of the col pin | 458 | // Populate the matrix row with the state of the col pin |
| 433 | 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); |
| 434 | } | 460 | } |
| 435 | 461 | ||
| 436 | // Unselect row | 462 | // Unselect row |
| @@ -441,24 +467,19 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) | |||
| 441 | 467 | ||
| 442 | static void select_row(uint8_t row) | 468 | static void select_row(uint8_t row) |
| 443 | { | 469 | { |
| 444 | uint8_t pin = row_pins[row]; | 470 | writePinLow(row_pins[row]); |
| 445 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | 471 | setPinOutput(row_pins[row]); |
| 446 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
| 447 | } | 472 | } |
| 448 | 473 | ||
| 449 | static void unselect_row(uint8_t row) | 474 | static void unselect_row(uint8_t row) |
| 450 | { | 475 | { |
| 451 | uint8_t pin = row_pins[row]; | 476 | setPinInputHigh(row_pins[row]); |
| 452 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
| 453 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
| 454 | } | 477 | } |
| 455 | 478 | ||
| 456 | static void unselect_rows(void) | 479 | static void unselect_rows(void) |
| 457 | { | 480 | { |
| 458 | for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { | 481 | for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { |
| 459 | uint8_t pin = row_pins[x]; | 482 | setPinInputHigh(row_pins[x]); |
| 460 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
| 461 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
| 462 | } | 483 | } |
| 463 | } | 484 | } |
| 464 | 485 | ||
| @@ -467,9 +488,7 @@ static void unselect_rows(void) | |||
| 467 | static void init_rows(void) | 488 | static void init_rows(void) |
| 468 | { | 489 | { |
| 469 | for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { | 490 | for(uint8_t x = 0; x < ROWS_PER_HAND; x++) { |
| 470 | uint8_t pin = row_pins[x]; | 491 | setPinInputHigh(row_pins[x]); |
| 471 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
| 472 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
| 473 | } | 492 | } |
| 474 | } | 493 | } |
| 475 | 494 | ||
| @@ -489,15 +508,15 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
| 489 | matrix_row_t last_row_value = current_matrix[row_index]; | 508 | matrix_row_t last_row_value = current_matrix[row_index]; |
| 490 | 509 | ||
| 491 | // Check row pin state | 510 | // Check row pin state |
| 492 | if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) | 511 | if (readPin(row_pins[row_index])) |
| 493 | { | 512 | { |
| 494 | // Pin LO, set col bit | 513 | // Pin HI, clear col bit |
| 495 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); | 514 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); |
| 496 | } | 515 | } |
| 497 | else | 516 | else |
| 498 | { | 517 | { |
| 499 | // Pin HI, clear col bit | 518 | // Pin LO, set col bit |
| 500 | current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); | 519 | current_matrix[row_index] |= (ROW_SHIFTER << current_col); |
| 501 | } | 520 | } |
| 502 | 521 | ||
| 503 | // Determine if the matrix changed state | 522 | // Determine if the matrix changed state |
| @@ -515,24 +534,19 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
| 515 | 534 | ||
| 516 | static void select_col(uint8_t col) | 535 | static void select_col(uint8_t col) |
| 517 | { | 536 | { |
| 518 | uint8_t pin = col_pins[col]; | 537 | writePinLow(col_pins[col]); |
| 519 | _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT | 538 | setPinOutput(col_pins[col]); |
| 520 | _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW | ||
| 521 | } | 539 | } |
| 522 | 540 | ||
| 523 | static void unselect_col(uint8_t col) | 541 | static void unselect_col(uint8_t col) |
| 524 | { | 542 | { |
| 525 | uint8_t pin = col_pins[col]; | 543 | setPinInputHigh(col_pins[col]); |
| 526 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
| 527 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
| 528 | } | 544 | } |
| 529 | 545 | ||
| 530 | static void unselect_cols(void) | 546 | static void unselect_cols(void) |
| 531 | { | 547 | { |
| 532 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { | 548 | for(uint8_t x = 0; x < MATRIX_COLS; x++) { |
| 533 | uint8_t pin = col_pins[x]; | 549 | setPinInputHigh(col_pins[x]); |
| 534 | _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN | ||
| 535 | _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI | ||
| 536 | } | 550 | } |
| 537 | } | 551 | } |
| 538 | 552 | ||
