2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
29 #include "split_util.h"
30 #include "pro_micro.h"
33 #include "split_flags.h"
35 #ifdef BACKLIGHT_ENABLE
36 # include "backlight.h"
37 extern backlight_config_t backlight_config;
40 #if defined(USE_I2C) || defined(EH)
46 #ifndef DEBOUNCING_DELAY
47 # define DEBOUNCING_DELAY 5
50 #if (DEBOUNCING_DELAY > 0)
51 static uint16_t debouncing_time;
52 static bool debouncing = false;
55 #if defined(USE_I2C) || defined(EH)
57 #if (MATRIX_COLS <= 8)
58 # define print_matrix_header() print("\nr/c 01234567\n")
59 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
60 # define matrix_bitpop(i) bitpop(matrix[i])
61 # define ROW_SHIFTER ((uint8_t)1)
63 # error "Currently only supports 8 COLS"
68 #if (MATRIX_COLS <= 8)
69 # define print_matrix_header() print("\nr/c 01234567\n")
70 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
71 # define matrix_bitpop(i) bitpop(matrix[i])
72 # define ROW_SHIFTER ((uint8_t)1)
73 #elif (MATRIX_COLS <= 16)
74 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
75 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
76 # define matrix_bitpop(i) bitpop16(matrix[i])
77 # define ROW_SHIFTER ((uint16_t)1)
78 #elif (MATRIX_COLS <= 32)
79 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
80 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
81 # define matrix_bitpop(i) bitpop32(matrix[i])
82 # define ROW_SHIFTER ((uint32_t)1)
86 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
88 #define ERROR_DISCONNECT_COUNT 5
90 #define ROWS_PER_HAND (MATRIX_ROWS/2)
92 static uint8_t error_count = 0;
94 static uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
95 static uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
97 /* matrix state(1:on, 0:off) */
98 static matrix_row_t matrix[MATRIX_ROWS];
99 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
101 #if (DIODE_DIRECTION == COL2ROW)
102 static void init_cols(void);
103 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
104 static void unselect_rows(void);
105 static void select_row(uint8_t row);
106 static void unselect_row(uint8_t row);
107 #elif (DIODE_DIRECTION == ROW2COL)
108 static void init_rows(void);
109 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
110 static void unselect_cols(void);
111 static void unselect_col(uint8_t col);
112 static void select_col(uint8_t col);
115 __attribute__ ((weak))
116 void matrix_init_kb(void) {
120 __attribute__ ((weak))
121 void matrix_scan_kb(void) {
125 __attribute__ ((weak))
126 void matrix_init_user(void) {
129 __attribute__ ((weak))
130 void matrix_scan_user(void) {
133 __attribute__ ((weak))
134 void matrix_slave_scan_user(void) {
138 uint8_t matrix_rows(void)
144 uint8_t matrix_cols(void)
149 void matrix_init(void)
155 // Set pinout for right half if pinout for that half is defined
157 #ifdef MATRIX_ROW_PINS_RIGHT
158 const uint8_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT;
159 for (uint8_t i = 0; i < MATRIX_ROWS; i++)
160 row_pins[i] = row_pins_right[i];
162 #ifdef MATRIX_COL_PINS_RIGHT
163 const uint8_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
164 for (uint8_t i = 0; i < MATRIX_COLS; i++)
165 col_pins[i] = col_pins_right[i];
169 // initialize row and col
170 #if (DIODE_DIRECTION == COL2ROW)
173 #elif (DIODE_DIRECTION == ROW2COL)
178 // initialize matrix state: all keys off
179 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
181 matrix_debouncing[i] = 0;
184 matrix_init_quantum();
188 uint8_t _matrix_scan(void)
190 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
191 #if (DIODE_DIRECTION == COL2ROW)
192 // Set row, read cols
193 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
194 # if (DEBOUNCING_DELAY > 0)
195 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
197 if (matrix_changed) {
199 debouncing_time = timer_read();
203 read_cols_on_row(matrix+offset, current_row);
208 #elif (DIODE_DIRECTION == ROW2COL)
209 // Set col, read rows
210 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
211 # if (DEBOUNCING_DELAY > 0)
212 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
213 if (matrix_changed) {
215 debouncing_time = timer_read();
218 read_rows_on_col(matrix+offset, current_col);
224 # if (DEBOUNCING_DELAY > 0)
225 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
226 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
227 matrix[i+offset] = matrix_debouncing[i+offset];
236 #if defined(USE_I2C) || defined(EH)
238 // Get rows from other half over i2c
239 int i2c_transaction(void) {
240 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
243 // write backlight info
244 #ifdef BACKLIGHT_ENABLE
246 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
247 if (err) goto i2c_error;
249 // Backlight location
250 err = i2c_master_write(I2C_BACKLIT_START);
251 if (err) goto i2c_error;
254 i2c_master_write(get_backlight_level());
256 BACKLIT_DIRTY = false;
260 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
261 if (err) goto i2c_error;
263 // start of matrix stored at I2C_KEYMAP_START
264 err = i2c_master_write(I2C_KEYMAP_START);
265 if (err) goto i2c_error;
268 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
269 if (err) goto i2c_error;
273 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
274 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
276 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
279 i2c_error: // the cable is disconnceted, or something else went wrong
284 #ifdef RGBLIGHT_ENABLE
286 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
287 if (err) goto i2c_error;
290 err = i2c_master_write(I2C_RGB_START);
291 if (err) goto i2c_error;
293 uint32_t dword = eeconfig_read_rgblight();
296 err = i2c_master_write_data(&dword, 4);
297 if (err) goto i2c_error;
310 typedef struct _Serial_s2m_buffer_t {
311 // TODO: if MATRIX_COLS > 8 change to uint8_t packed_matrix[] for pack/unpack
312 matrix_row_t smatrix[ROWS_PER_HAND];
313 } Serial_s2m_buffer_t;
315 volatile Serial_s2m_buffer_t serial_s2m_buffer = {};
316 volatile Serial_m2s_buffer_t serial_m2s_buffer = {};
317 uint8_t volatile status0 = 0;
319 SSTD_t transactions[] = {
320 { (uint8_t *)&status0,
321 sizeof(serial_m2s_buffer), (uint8_t *)&serial_m2s_buffer,
322 sizeof(serial_s2m_buffer), (uint8_t *)&serial_s2m_buffer
326 void serial_master_init(void)
327 { soft_serial_initiator_init(transactions, TID_LIMIT(transactions)); }
329 void serial_slave_init(void)
330 { soft_serial_target_init(transactions, TID_LIMIT(transactions)); }
332 int serial_transaction(void) {
333 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
335 if (soft_serial_transaction()) {
339 // TODO: if MATRIX_COLS > 8 change to unpack()
340 for (int i = 0; i < ROWS_PER_HAND; ++i) {
341 matrix[slaveOffset+i] = serial_s2m_buffer.smatrix[i];
344 #if defined(RGBLIGHT_ENABLE) && defined(RGBLIGHT_SPLIT)
345 // Code to send RGB over serial goes here (not implemented yet)
348 #ifdef BACKLIGHT_ENABLE
349 // Write backlight level for slave to read
350 serial_m2s_buffer.backlight_level = backlight_config.enable ? backlight_config.level : 0;
357 uint8_t matrix_scan(void)
359 uint8_t ret = _matrix_scan();
361 #if defined(USE_I2C) || defined(EH)
362 if( i2c_transaction() ) {
364 if( serial_transaction() ) {
369 if (error_count > ERROR_DISCONNECT_COUNT) {
370 // reset other half if disconnected
371 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
372 for (int i = 0; i < ROWS_PER_HAND; ++i) {
373 matrix[slaveOffset+i] = 0;
379 matrix_scan_quantum();
383 void matrix_slave_scan(void) {
386 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
388 #if defined(USE_I2C) || defined(EH)
389 for (int i = 0; i < ROWS_PER_HAND; ++i) {
390 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
393 // TODO: if MATRIX_COLS > 8 change to pack()
394 for (int i = 0; i < ROWS_PER_HAND; ++i) {
395 serial_s2m_buffer.smatrix[i] = matrix[offset+i];
398 matrix_slave_scan_user();
401 bool matrix_is_modified(void)
403 if (debouncing) return false;
408 bool matrix_is_on(uint8_t row, uint8_t col)
410 return (matrix[row] & ((matrix_row_t)1<<col));
414 matrix_row_t matrix_get_row(uint8_t row)
419 void matrix_print(void)
421 print("\nr/c 0123456789ABCDEF\n");
422 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
423 phex(row); print(": ");
424 pbin_reverse16(matrix_get_row(row));
429 uint8_t matrix_key_count(void)
432 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
433 count += bitpop16(matrix[i]);
438 #if (DIODE_DIRECTION == COL2ROW)
440 static void init_cols(void)
442 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
443 uint8_t pin = col_pins[x];
444 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
445 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
449 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
451 // Store last value of row prior to reading
452 matrix_row_t last_row_value = current_matrix[current_row];
454 // Clear data in matrix row
455 current_matrix[current_row] = 0;
457 // Select row and wait for row selecton to stabilize
458 select_row(current_row);
462 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
464 // Select the col pin to read (active low)
465 uint8_t pin = col_pins[col_index];
466 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
468 // Populate the matrix row with the state of the col pin
469 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
473 unselect_row(current_row);
475 return (last_row_value != current_matrix[current_row]);
478 static void select_row(uint8_t row)
480 uint8_t pin = row_pins[row];
481 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
482 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
485 static void unselect_row(uint8_t row)
487 uint8_t pin = row_pins[row];
488 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
489 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
492 static void unselect_rows(void)
494 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
495 uint8_t pin = row_pins[x];
496 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
497 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
501 #elif (DIODE_DIRECTION == ROW2COL)
503 static void init_rows(void)
505 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
506 uint8_t pin = row_pins[x];
507 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
508 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
512 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
514 bool matrix_changed = false;
516 // Select col and wait for col selecton to stabilize
517 select_col(current_col);
521 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
524 // Store last value of row prior to reading
525 matrix_row_t last_row_value = current_matrix[row_index];
527 // Check row pin state
528 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
530 // Pin LO, set col bit
531 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
535 // Pin HI, clear col bit
536 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
539 // Determine if the matrix changed state
540 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
542 matrix_changed = true;
547 unselect_col(current_col);
549 return matrix_changed;
552 static void select_col(uint8_t col)
554 uint8_t pin = col_pins[col];
555 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
556 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
559 static void unselect_col(uint8_t col)
561 uint8_t pin = col_pins[col];
562 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
563 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
566 static void unselect_cols(void)
568 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
569 uint8_t pin = col_pins[x];
570 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
571 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI