]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox_ez/matrix.c
i2c fix
[qmk_firmware.git] / keyboards / ergodox_ez / matrix.c
1 /*
2
3 Note for ErgoDox EZ customizers: Here be dragons!
4 This is not a file you want to be messing with.
5 All of the interesting stuff for you is under keymaps/ :)
6 Love, Erez
7
8 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  * scan matrix
26  */
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <avr/io.h>
30 #include "wait.h"
31 #include "action_layer.h"
32 #include "print.h"
33 #include "debug.h"
34 #include "util.h"
35 #include "matrix.h"
36 #include QMK_KEYBOARD_H
37 #ifdef DEBUG_MATRIX_SCAN_RATE
38 #include  "timer.h"
39 #endif
40
41 /*
42  * This constant define not debouncing time in msecs, but amount of matrix
43  * scan loops which should be made to get stable debounced results.
44  *
45  * On Ergodox matrix scan rate is relatively low, because of slow I2C.
46  * Now it's only 317 scans/second, or about 3.15 msec/scan.
47  * According to Cherry specs, debouncing time is 5 msec.
48  *
49  * However, some switches seem to have higher debouncing requirements, or
50  * something else might be wrong. (Also, the scan speed has improved since
51  * that comment was written.)
52  */
53
54 #ifndef DEBOUNCE
55 #   define DEBOUNCE     5
56 #endif
57
58 /* matrix state(1:on, 0:off) */
59 static matrix_row_t matrix[MATRIX_ROWS];
60
61 // Debouncing: store for each key the number of scans until it's eligible to
62 // change.  When scanning the matrix, ignore any changes in keys that have
63 // already changed in the last DEBOUNCE scans.
64 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
65
66 static matrix_row_t read_cols(uint8_t row);
67 static void init_cols(void);
68 static void unselect_rows(void);
69 static void select_row(uint8_t row);
70
71 static uint8_t mcp23018_reset_loop;
72
73 #ifdef DEBUG_MATRIX_SCAN_RATE
74 uint32_t matrix_timer;
75 uint32_t matrix_scan_count;
76 #endif
77
78
79 __attribute__ ((weak))
80 void matrix_init_user(void) {}
81
82 __attribute__ ((weak))
83 void matrix_scan_user(void) {}
84
85 __attribute__ ((weak))
86 void matrix_init_kb(void) {
87   matrix_init_user();
88 }
89
90 __attribute__ ((weak))
91 void matrix_scan_kb(void) {
92   matrix_scan_user();
93 }
94
95 inline
96 uint8_t matrix_rows(void)
97 {
98     return MATRIX_ROWS;
99 }
100
101 inline
102 uint8_t matrix_cols(void)
103 {
104     return MATRIX_COLS;
105 }
106
107 void matrix_init(void)
108 {
109     // initialize row and col
110
111     mcp23018_status = init_mcp23018();
112
113
114     unselect_rows();
115     init_cols();
116
117     // initialize matrix state: all keys off
118     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
119         matrix[i] = 0;
120         for (uint8_t j=0; j < MATRIX_COLS; ++j) {
121             debounce_matrix[i * MATRIX_COLS + j] = 0;
122         }
123     }
124
125 #ifdef DEBUG_MATRIX_SCAN_RATE
126     matrix_timer = timer_read32();
127     matrix_scan_count = 0;
128 #endif
129
130     matrix_init_quantum();
131
132 }
133
134 void matrix_power_up(void) {
135     mcp23018_status = init_mcp23018();
136
137     unselect_rows();
138     init_cols();
139
140     // initialize matrix state: all keys off
141     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
142         matrix[i] = 0;
143     }
144
145 #ifdef DEBUG_MATRIX_SCAN_RATE
146     matrix_timer = timer_read32();
147     matrix_scan_count = 0;
148 #endif
149 }
150
151 // Returns a matrix_row_t whose bits are set if the corresponding key should be
152 // eligible to change in this scan.
153 matrix_row_t debounce_mask(uint8_t row) {
154   matrix_row_t result = 0;
155   for (uint8_t j=0; j < MATRIX_COLS; ++j) {
156     if (debounce_matrix[row * MATRIX_COLS + j]) {
157       --debounce_matrix[row * MATRIX_COLS + j];
158     } else {
159       result |= (1 << j);
160     }
161   }
162   return result;
163 }
164
165 // Report changed keys in the given row.  Resets the debounce countdowns
166 // corresponding to each set bit in 'change' to DEBOUNCE.
167 void debounce_report(matrix_row_t change, uint8_t row) {
168   for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
169     if (change & (1 << i)) {
170       debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
171     }
172   }
173 }
174
175 uint8_t matrix_scan(void)
176 {
177     if (mcp23018_status) { // if there was an error
178         if (++mcp23018_reset_loop == 0) {
179             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
180             // this will be approx bit more frequent than once per second
181             print("trying to reset mcp23018\n");
182             mcp23018_status = init_mcp23018();
183             if (mcp23018_status) {
184                 print("left side not responding\n");
185             } else {
186                 print("left side attached\n");
187                 ergodox_blink_all_leds();
188             }
189         }
190     }
191
192 #ifdef DEBUG_MATRIX_SCAN_RATE
193     matrix_scan_count++;
194
195     uint32_t timer_now = timer_read32();
196     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
197         print("matrix scan frequency: ");
198         pdec(matrix_scan_count);
199         print("\n");
200
201         matrix_timer = timer_now;
202         matrix_scan_count = 0;
203     }
204 #endif
205
206 #ifdef LEFT_LEDS
207     mcp23018_status = ergodox_left_leds_update();
208 #endif // LEFT_LEDS
209     for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
210         select_row(i);
211         // and select on left hand
212         select_row(i + MATRIX_ROWS_PER_SIDE);
213         // we don't need a 30us delay anymore, because selecting a
214         // left-hand row requires more than 30us for i2c.
215         matrix_row_t mask = debounce_mask(i);
216         matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
217         debounce_report(cols ^ matrix[i], i);
218         matrix[i] = cols;
219         // grab cols from right hand
220         mask = debounce_mask(i + MATRIX_ROWS_PER_SIDE);
221         cols = (read_cols(i + MATRIX_ROWS_PER_SIDE) & mask) | (matrix[i + MATRIX_ROWS_PER_SIDE] & ~mask);
222         debounce_report(cols ^ matrix[i + MATRIX_ROWS_PER_SIDE], i + MATRIX_ROWS_PER_SIDE);
223         matrix[i + MATRIX_ROWS_PER_SIDE] = cols;
224         unselect_rows();
225     }
226
227     matrix_scan_quantum();
228
229     return 1;
230 }
231
232 bool matrix_is_modified(void) // deprecated and evidently not called.
233 {
234     return true;
235 }
236
237 inline
238 bool matrix_is_on(uint8_t row, uint8_t col)
239 {
240     return (matrix[row] & ((matrix_row_t)1<<col));
241 }
242
243 inline
244 matrix_row_t matrix_get_row(uint8_t row)
245 {
246     return matrix[row];
247 }
248
249 void matrix_print(void)
250 {
251     print("\nr/c 0123456789ABCDEF\n");
252     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
253         phex(row); print(": ");
254         pbin_reverse16(matrix_get_row(row));
255         print("\n");
256     }
257 }
258
259 uint8_t matrix_key_count(void)
260 {
261     uint8_t count = 0;
262     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
263         count += bitpop16(matrix[i]);
264     }
265     return count;
266 }
267
268 /* Column pin configuration
269  *
270  * Teensy
271  * col: 0   1   2   3   4   5
272  * pin: F0  F1  F4  F5  F6  F7
273  *
274  * MCP23018
275  * col: 0   1   2   3   4   5
276  * pin: B5  B4  B3  B2  B1  B0
277  */
278 static void  init_cols(void)
279 {
280     // init on mcp23018
281     // not needed, already done as part of init_mcp23018()
282
283     // init on teensy
284     // Input with pull-up(DDR:0, PORT:1)
285     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
286     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
287 }
288
289 static matrix_row_t read_cols(uint8_t row)
290 {
291     if (row < 7) {
292         if (mcp23018_status) { // if there was an error
293             return 0;
294         } else {
295             uint8_t data = 0;
296             mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
297             mcp23018_status = i2c_write(GPIOB);             if (mcp23018_status) goto out;
298             mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
299             data = i2c_read_nack();
300             data = ~data;
301         out:
302             i2c_stop();
303             return data;
304         }
305     } else {
306         /* read from teensy
307          * bitmask is 0b11110011, but we want those all
308          * in the lower six bits.
309          * we'll return 1s for the top two, but that's harmless.
310          */
311
312         return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
313     }
314 }
315
316 /* Row pin configuration
317  *
318  * Teensy
319  * row: 7   8   9   10  11  12  13
320  * pin: B0  B1  B2  B3  D2  D3  C6
321  *
322  * MCP23018
323  * row: 0   1   2   3   4   5   6
324  * pin: A0  A1  A2  A3  A4  A5  A6
325  */
326 static void unselect_rows(void)
327 {
328     // no need to unselect on mcp23018, because the select step sets all
329     // the other row bits high, and it's not changing to a different
330     // direction
331
332     // unselect on teensy
333     // Hi-Z(DDR:0, PORT:0) to unselect
334     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
335     PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
336     DDRD  &= ~(1<<2 | 1<<3);
337     PORTD &= ~(1<<2 | 1<<3);
338     DDRC  &= ~(1<<6);
339     PORTC &= ~(1<<6);
340 }
341
342 static void select_row(uint8_t row)
343 {
344     if (row < 7) {
345         // select on mcp23018
346         if (mcp23018_status) { // if there was an error
347             // do nothing
348         } else {
349             // set active row low  : 0
350             // set other rows hi-Z : 1
351             mcp23018_status = i2c_start(I2C_ADDR_WRITE);        if (mcp23018_status) goto out;
352             mcp23018_status = i2c_write(GPIOA);                 if (mcp23018_status) goto out;
353             mcp23018_status = i2c_write(0xFF & ~(1<<row));      if (mcp23018_status) goto out;
354         out:
355             i2c_stop();
356         }
357     } else {
358         // select on teensy
359         // Output low(DDR:1, PORT:0) to select
360         switch (row) {
361             case 7:
362                 DDRB  |= (1<<0);
363                 PORTB &= ~(1<<0);
364                 break;
365             case 8:
366                 DDRB  |= (1<<1);
367                 PORTB &= ~(1<<1);
368                 break;
369             case 9:
370                 DDRB  |= (1<<2);
371                 PORTB &= ~(1<<2);
372                 break;
373             case 10:
374                 DDRB  |= (1<<3);
375                 PORTB &= ~(1<<3);
376                 break;
377             case 11:
378                 DDRD  |= (1<<2);
379                 PORTD &= ~(1<<3);
380                 break;
381             case 12:
382                 DDRD  |= (1<<3);
383                 PORTD &= ~(1<<3);
384                 break;
385             case 13:
386                 DDRC  |= (1<<6);
387                 PORTC &= ~(1<<6);
388                 break;
389         }
390     }
391 }
392