2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <avr/interrupt.h>
24 #include <util/delay.h>
32 #include "i2c_master.h"
34 #define SLAVE_I2C_ADDRESS_RIGHT 0x19
35 #define SLAVE_I2C_ADDRESS_NUMPAD 0x21
36 #define SLAVE_I2C_ADDRESS_ARROW 0x23
38 #define ERROR_DISCONNECT_COUNT 5
39 static uint8_t error_count_right = 0;
40 static uint8_t error_count_numpad = 0;
41 static uint8_t error_count_arrow = 0;
43 /* Set 0 if debouncing isn't needed */
50 static uint16_t debouncing_time;
51 static bool debouncing = false;
54 #if (MATRIX_COLS <= 8)
55 # define print_matrix_header() print("\nr/c 01234567\n")
56 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
57 # define matrix_bitpop(i) bitpop(matrix[i])
58 # define ROW_SHIFTER ((uint8_t)1)
59 #elif (MATRIX_COLS <= 16)
60 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
61 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
62 # define matrix_bitpop(i) bitpop16(matrix[i])
63 # define ROW_SHIFTER ((uint16_t)1)
64 #elif (MATRIX_COLS <= 32)
65 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
66 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
67 # define matrix_bitpop(i) bitpop32(matrix[i])
68 # define ROW_SHIFTER ((uint32_t)1)
72 extern const matrix_row_t matrix_mask[];
75 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
76 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
77 static const uint8_t col_pins[MATRIX_COLS_SCANNED] = MATRIX_COL_PINS;
80 /* matrix state(1:on, 0:off) */
81 static matrix_row_t matrix[MATRIX_ROWS];
83 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
86 #if (DIODE_DIRECTION == COL2ROW)
87 static void init_cols(void);
88 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
89 static void unselect_rows(void);
90 static void select_row(uint8_t row);
91 static void unselect_row(uint8_t row);
92 #elif (DIODE_DIRECTION == ROW2COL)
93 static void init_rows(void);
94 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
95 static void unselect_cols(void);
96 static void unselect_col(uint8_t col);
97 static void select_col(uint8_t col);
100 __attribute__ ((weak))
101 void matrix_init_quantum(void) {
105 __attribute__ ((weak))
106 void matrix_scan_quantum(void) {
110 __attribute__ ((weak))
111 void matrix_init_kb(void) {
115 __attribute__ ((weak))
116 void matrix_scan_kb(void) {
120 __attribute__ ((weak))
121 void matrix_init_user(void) {
124 __attribute__ ((weak))
125 void matrix_scan_user(void) {
129 uint8_t matrix_rows(void) {
134 uint8_t matrix_cols(void) {
138 i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset);
140 //this replases tmk code
141 void matrix_setup(void){
145 void matrix_init(void) {
147 // initialize row and col
148 #if (DIODE_DIRECTION == COL2ROW)
151 #elif (DIODE_DIRECTION == ROW2COL)
156 // initialize matrix state: all keys off
157 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
159 matrix_debouncing[i] = 0;
162 matrix_init_quantum();
165 uint8_t matrix_scan(void)
168 #if (DIODE_DIRECTION == COL2ROW)
170 // Set row, read cols
171 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
173 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
175 if (matrix_changed) {
177 debouncing_time = timer_read();
181 read_cols_on_row(matrix, current_row);
186 #elif (DIODE_DIRECTION == ROW2COL)
188 // Set col, read rows
189 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
191 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
192 if (matrix_changed) {
194 debouncing_time = timer_read();
197 read_rows_on_col(matrix, current_col);
205 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
206 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
207 matrix[i] = matrix_debouncing[i];
213 if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half
215 if (error_count_right > ERROR_DISCONNECT_COUNT){ //disconnect half
216 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
217 matrix[i] &= 0x3F; //mask bits to keep
221 error_count_right = 0;
224 if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster
226 if (error_count_arrow > ERROR_DISCONNECT_COUNT){ //disconnect arrow cluster
227 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
228 matrix[i] &= 0x3FFF; //mask bits to keep
232 error_count_arrow = 0;
235 if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)){ //error has occured for numpad
236 error_count_numpad++;
237 if (error_count_numpad > ERROR_DISCONNECT_COUNT){ //disconnect numpad
238 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
239 matrix[i] &= 0x1FFFF; //mask bits to keep
243 error_count_numpad = 0;
246 matrix_scan_quantum();
250 bool matrix_is_modified(void)
253 if (debouncing) return false;
259 bool matrix_is_on(uint8_t row, uint8_t col)
261 return (matrix[row] & ((matrix_row_t)1<<col));
265 matrix_row_t matrix_get_row(uint8_t row)
267 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
268 // switch blocker installed and the switch is always pressed.
270 return matrix[row] & matrix_mask[row];
276 void matrix_print(void)
278 print_matrix_header();
280 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
281 phex(row); print(": ");
282 print_matrix_row(row);
287 uint8_t matrix_key_count(void)
290 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
291 count += matrix_bitpop(i);
298 #if (DIODE_DIRECTION == COL2ROW)
300 static void init_cols(void)
302 for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
303 uint8_t pin = col_pins[x];
304 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
305 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
309 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
311 // Store last value of row prior to reading
312 matrix_row_t last_row_value = current_matrix[current_row];
314 // Clear data in matrix row
315 current_matrix[current_row] = 0;
317 // Select row and wait for row selecton to stabilize
318 select_row(current_row);
322 for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) {
324 // Select the col pin to read (active low)
325 uint8_t pin = col_pins[col_index];
326 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
328 // Populate the matrix row with the state of the col pin
329 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
333 unselect_row(current_row);
335 return (last_row_value != current_matrix[current_row]);
338 static void select_row(uint8_t row)
340 uint8_t pin = row_pins[row];
341 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
342 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
345 static void unselect_row(uint8_t row)
347 uint8_t pin = row_pins[row];
348 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
349 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
352 static void unselect_rows(void)
354 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
355 uint8_t pin = row_pins[x];
356 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
357 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
361 #elif (DIODE_DIRECTION == ROW2COL)
363 static void init_rows(void)
365 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
366 uint8_t pin = row_pins[x];
367 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
368 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
372 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
374 bool matrix_changed = false;
376 // Select col and wait for col selecton to stabilize
377 select_col(current_col);
381 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
384 // Store last value of row prior to reading
385 matrix_row_t last_row_value = current_matrix[row_index];
387 // Check row pin state
388 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
390 // Pin LO, set col bit
391 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
395 // Pin HI, clear col bit
396 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
399 // Determine if the matrix changed state
400 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
402 matrix_changed = true;
407 unselect_col(current_col);
409 return matrix_changed;
412 static void select_col(uint8_t col)
414 uint8_t pin = col_pins[col];
415 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
416 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
419 static void unselect_col(uint8_t col)
421 uint8_t pin = col_pins[col];
422 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
423 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
426 static void unselect_cols(void)
428 for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
429 uint8_t pin = col_pins[x];
430 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
431 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
437 // Complete rows from other modules over i2c
438 i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
439 i2c_status_t err = i2c_start((address << 1) | I2C_WRITE, 10);
440 i2c_write(0x01, 10); //request data in address 1
442 i2c_start((address << 1) | I2C_READ, 5);
444 err = i2c_read_ack(10);
445 if (err == 0x55) { //synchronization byte
447 for (uint8_t i = 0; i < MATRIX_ROWS-1 ; i++) { //assemble slave matrix in main matrix
448 matrix[i] &= mask; //mask bits to keep
449 err = i2c_read_ack(10);
450 matrix[i] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
452 //last read request must be followed by a NACK
453 matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
454 err = i2c_read_nack(10);
455 matrix[MATRIX_ROWS - 1] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end