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 */
45 #ifndef DEBOUNCING_DELAY
46 # define DEBOUNCING_DELAY 5
49 #if (DEBOUNCING_DELAY > 0)
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) {
139 i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset);
140 //uint8_t i2c_transaction_numpad(void);
141 //uint8_t i2c_transaction_arrow(void);
143 //this replases tmk code
144 void matrix_setup(void){
148 void matrix_init(void) {
150 // initialize row and col
151 #if (DIODE_DIRECTION == COL2ROW)
154 #elif (DIODE_DIRECTION == ROW2COL)
159 // initialize matrix state: all keys off
160 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
162 matrix_debouncing[i] = 0;
165 matrix_init_quantum();
168 uint8_t matrix_scan(void)
171 #if (DIODE_DIRECTION == COL2ROW)
173 // Set row, read cols
174 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
175 # if (DEBOUNCING_DELAY > 0)
176 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
178 if (matrix_changed) {
180 debouncing_time = timer_read();
184 read_cols_on_row(matrix, current_row);
189 #elif (DIODE_DIRECTION == ROW2COL)
191 // Set col, read rows
192 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
193 # if (DEBOUNCING_DELAY > 0)
194 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
195 if (matrix_changed) {
197 debouncing_time = timer_read();
200 read_rows_on_col(matrix, current_col);
207 # if (DEBOUNCING_DELAY > 0)
208 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
209 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
210 matrix[i] = matrix_debouncing[i];
216 if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half
218 if (error_count_right > ERROR_DISCONNECT_COUNT){ //disconnect half
219 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
220 matrix[i] &= 0x3F; //mask bits to keep
224 error_count_right = 0;
227 if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster
229 if (error_count_arrow > ERROR_DISCONNECT_COUNT){ //disconnect arrow cluster
230 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
231 matrix[i] &= 0x3FFF; //mask bits to keep
235 error_count_arrow = 0;
238 if (i2c_transaction(SLAVE_I2C_ADDRESS_NUMPAD, 0x1FFFF, 11)){ //error has occured for numpad
239 error_count_numpad++;
240 if (error_count_numpad > ERROR_DISCONNECT_COUNT){ //disconnect numpad
241 for (uint8_t i = 0; i < MATRIX_ROWS ; i++) {
242 matrix[i] &= 0x1FFFF; //mask bits to keep
246 error_count_numpad = 0;
249 matrix_scan_quantum();
253 bool matrix_is_modified(void)
255 #if (DEBOUNCING_DELAY > 0)
256 if (debouncing) return false;
262 bool matrix_is_on(uint8_t row, uint8_t col)
264 return (matrix[row] & ((matrix_row_t)1<col));
268 matrix_row_t matrix_get_row(uint8_t row)
270 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
271 // switch blocker installed and the switch is always pressed.
273 return matrix[row] & matrix_mask[row];
279 void matrix_print(void)
281 print_matrix_header();
283 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
284 phex(row); print(": ");
285 print_matrix_row(row);
290 uint8_t matrix_key_count(void)
293 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
294 count += matrix_bitpop(i);
301 #if (DIODE_DIRECTION == COL2ROW)
303 static void init_cols(void)
305 for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
306 uint8_t pin = col_pins[x];
307 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
308 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
312 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
314 // Store last value of row prior to reading
315 matrix_row_t last_row_value = current_matrix[current_row];
317 // Clear data in matrix row
318 current_matrix[current_row] = 0;
320 // Select row and wait for row selecton to stabilize
321 select_row(current_row);
325 for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) {
327 // Select the col pin to read (active low)
328 uint8_t pin = col_pins[col_index];
329 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
331 // Populate the matrix row with the state of the col pin
332 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
336 unselect_row(current_row);
338 return (last_row_value != current_matrix[current_row]);
341 static void select_row(uint8_t row)
343 uint8_t pin = row_pins[row];
344 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
345 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
348 static void unselect_row(uint8_t row)
350 uint8_t pin = row_pins[row];
351 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
352 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
355 static void unselect_rows(void)
357 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
358 uint8_t pin = row_pins[x];
359 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
360 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
364 #elif (DIODE_DIRECTION == ROW2COL)
366 static void init_rows(void)
368 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
369 uint8_t pin = row_pins[x];
370 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
371 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
375 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
377 bool matrix_changed = false;
379 // Select col and wait for col selecton to stabilize
380 select_col(current_col);
384 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
387 // Store last value of row prior to reading
388 matrix_row_t last_row_value = current_matrix[row_index];
390 // Check row pin state
391 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
393 // Pin LO, set col bit
394 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
398 // Pin HI, clear col bit
399 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
402 // Determine if the matrix changed state
403 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
405 matrix_changed = true;
410 unselect_col(current_col);
412 return matrix_changed;
415 static void select_col(uint8_t col)
417 uint8_t pin = col_pins[col];
418 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
419 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
422 static void unselect_col(uint8_t col)
424 uint8_t pin = col_pins[col];
425 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
426 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
429 static void unselect_cols(void)
431 for(uint8_t x = 0; x < MATRIX_COLS_SCANNED; x++) {
432 uint8_t pin = col_pins[x];
433 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
434 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
440 // Complete rows from other modules over i2c
441 i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset) {
442 i2c_status_t err = i2c_start((address << 1) | I2C_WRITE, 10);
447 i2c_start((address << 1) | I2C_READ, 10);
450 err = i2c_read_ack(10);
451 if (err == 0x55) { //synchronization byte
453 for (uint8_t i = 0; i < MATRIX_ROWS-1 ; i++) { //assemble slave matrix in main matrix
454 matrix[i] &= mask; //mask bits to keep
455 err = i2c_read_ack(10);
457 matrix[i] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end
462 //last read request must be followed by a NACK
463 matrix[MATRIX_ROWS - 1] &= mask; //mask bits to keep
464 err = i2c_read_nack(10);
466 matrix[MATRIX_ROWS - 1] |= ((uint32_t)err << (MATRIX_COLS_SCANNED + col_offset)); //add new bits at the end