]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/dc01/left/matrix.c
Next set of split_common changes (#4974)
[qmk_firmware.git] / keyboards / dc01 / left / matrix.c
1 /*
2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
4
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.
9
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.
14
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/>.
17 */
18 #include <stdint.h>
19 #include <stdbool.h>
20 #if defined(__AVR__)
21 #include <avr/io.h>
22 #include <avr/wdt.h>
23 #include <avr/interrupt.h>
24 #include <util/delay.h>
25 #endif
26 #include "wait.h"
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "timer.h"
32 #include "i2c_master.h"
33
34 #define SLAVE_I2C_ADDRESS_RIGHT 0x19
35 #define SLAVE_I2C_ADDRESS_NUMPAD 0x21
36 #define SLAVE_I2C_ADDRESS_ARROW 0x23
37
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;
42
43 /* Set 0 if debouncing isn't needed */
44
45 #ifndef DEBOUNCING_DELAY
46 #    define DEBOUNCING_DELAY 5
47 #endif
48
49 #if (DEBOUNCING_DELAY > 0)
50     static uint16_t debouncing_time;
51     static bool debouncing = false;
52 #endif
53
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)
69 #endif
70
71 #ifdef MATRIX_MASKED
72     extern const matrix_row_t matrix_mask[];
73 #endif
74
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;
78 #endif
79
80 /* matrix state(1:on, 0:off) */
81 static matrix_row_t matrix[MATRIX_ROWS];
82
83 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
84
85
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);
98 #endif
99
100 __attribute__ ((weak))
101 void matrix_init_quantum(void) {
102     matrix_init_kb();
103 }
104
105 __attribute__ ((weak))
106 void matrix_scan_quantum(void) {
107     matrix_scan_kb();
108 }
109
110 __attribute__ ((weak))
111 void matrix_init_kb(void) {
112     matrix_init_user();
113 }
114
115 __attribute__ ((weak))
116 void matrix_scan_kb(void) {
117     matrix_scan_user();
118 }
119
120 __attribute__ ((weak))
121 void matrix_init_user(void) {
122 }
123
124 __attribute__ ((weak))
125 void matrix_scan_user(void) {
126 }
127
128 inline
129 uint8_t matrix_rows(void) {
130     return MATRIX_ROWS;
131 }
132
133 inline
134 uint8_t matrix_cols(void) {
135     return MATRIX_COLS;
136 }
137
138 i2c_status_t i2c_transaction(uint8_t address, uint32_t mask, uint8_t col_offset);
139
140 //this replases tmk code
141 void matrix_setup(void){
142     i2c_init();
143 }
144
145 void matrix_init(void) {
146
147     // initialize row and col
148 #if (DIODE_DIRECTION == COL2ROW)
149     unselect_rows();
150     init_cols();
151 #elif (DIODE_DIRECTION == ROW2COL)
152     unselect_cols();
153     init_rows();
154 #endif
155
156     // initialize matrix state: all keys off
157     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
158         matrix[i] = 0;
159         matrix_debouncing[i] = 0;
160     }
161
162     matrix_init_quantum();
163 }
164
165 uint8_t matrix_scan(void)
166 {
167
168 #if (DIODE_DIRECTION == COL2ROW)
169
170     // Set row, read cols
171     for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
172 #       if (DEBOUNCING_DELAY > 0)
173             bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
174
175             if (matrix_changed) {
176                 debouncing = true;
177                 debouncing_time = timer_read();
178             }
179
180 #       else
181             read_cols_on_row(matrix, current_row);
182 #       endif
183
184     }
185
186 #elif (DIODE_DIRECTION == ROW2COL)
187
188     // Set col, read rows
189     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
190 #       if (DEBOUNCING_DELAY > 0)
191             bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
192             if (matrix_changed) {
193                 debouncing = true;
194                 debouncing_time = timer_read();
195             }
196 #       else
197              read_rows_on_col(matrix, current_col);
198 #       endif
199
200     }
201
202 #endif
203
204 #   if (DEBOUNCING_DELAY > 0)
205         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
206             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
207                 matrix[i] = matrix_debouncing[i];
208             }
209             debouncing = false;
210         }
211 #   endif
212
213     if (i2c_transaction(SLAVE_I2C_ADDRESS_RIGHT, 0x3F, 0)){ //error has occured for main right half
214         error_count_right++;
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
218             }
219         }
220    }else{ //no error
221         error_count_right = 0;
222     }
223
224     if (i2c_transaction(SLAVE_I2C_ADDRESS_ARROW, 0X3FFF, 8)){ //error has occured for arrow cluster
225         error_count_arrow++;
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
229             }
230         }
231     }else{ //no error
232         error_count_arrow = 0;
233     }
234
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
240             }
241         }
242     }else{ //no error
243         error_count_numpad = 0;
244     }
245
246     matrix_scan_quantum();
247     return 1;
248 }
249
250 bool matrix_is_modified(void)
251 {
252 #if (DEBOUNCING_DELAY > 0)
253     if (debouncing) return false;
254 #endif
255     return true;
256 }
257
258 inline
259 bool matrix_is_on(uint8_t row, uint8_t col)
260 {
261     return (matrix[row] & ((matrix_row_t)1<<col));
262 }
263
264 inline
265 matrix_row_t matrix_get_row(uint8_t row)
266 {
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.
269 #ifdef MATRIX_MASKED
270     return matrix[row] & matrix_mask[row];
271 #else
272     return matrix[row];
273 #endif
274 }
275
276 void matrix_print(void)
277 {
278     print_matrix_header();
279
280     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
281         phex(row); print(": ");
282         print_matrix_row(row);
283         print("\n");
284     }
285 }
286
287 uint8_t matrix_key_count(void)
288 {
289     uint8_t count = 0;
290     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
291         count += matrix_bitpop(i);
292     }
293     return count;
294 }
295
296
297
298 #if (DIODE_DIRECTION == COL2ROW)
299
300 static void init_cols(void)
301 {
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
306     }
307 }
308
309 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
310 {
311     // Store last value of row prior to reading
312     matrix_row_t last_row_value = current_matrix[current_row];
313
314     // Clear data in matrix row
315     current_matrix[current_row] = 0;
316
317     // Select row and wait for row selecton to stabilize
318     select_row(current_row);
319     wait_us(30);
320
321     // For each col...
322     for(uint8_t col_index = 0; col_index < MATRIX_COLS_SCANNED; col_index++) {
323
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));
327
328         // Populate the matrix row with the state of the col pin
329         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
330     }
331
332     // Unselect row
333     unselect_row(current_row);
334
335     return (last_row_value != current_matrix[current_row]);
336 }
337
338 static void select_row(uint8_t row)
339 {
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
343 }
344
345 static void unselect_row(uint8_t row)
346 {
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
350 }
351
352 static void unselect_rows(void)
353 {
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
358     }
359 }
360
361 #elif (DIODE_DIRECTION == ROW2COL)
362
363 static void init_rows(void)
364 {
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
369     }
370 }
371
372 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
373 {
374     bool matrix_changed = false;
375
376     // Select col and wait for col selecton to stabilize
377     select_col(current_col);
378     wait_us(30);
379
380     // For each row...
381     for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
382     {
383
384         // Store last value of row prior to reading
385         matrix_row_t last_row_value = current_matrix[row_index];
386
387         // Check row pin state
388         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
389         {
390             // Pin LO, set col bit
391             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
392         }
393         else
394         {
395             // Pin HI, clear col bit
396             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
397         }
398
399         // Determine if the matrix changed state
400         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
401         {
402             matrix_changed = true;
403         }
404     }
405
406     // Unselect col
407     unselect_col(current_col);
408
409     return matrix_changed;
410 }
411
412 static void select_col(uint8_t col)
413 {
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
417 }
418
419 static void unselect_col(uint8_t col)
420 {
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
424 }
425
426 static void unselect_cols(void)
427 {
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
432     }
433 }
434
435 #endif
436
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
441
442     i2c_start((address << 1) | I2C_READ, 5);
443
444     err = i2c_read_ack(10);
445     if (err == 0x55) { //synchronization byte
446
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
451             }
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
456
457     } else {
458         i2c_stop();
459         return 1;
460     }
461
462     i2c_stop();
463     return 0;
464 }