]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox_ez/matrix.c
adds immediate i2c return, fixes ez matrix code
[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 // static uint16_t mcp23018_reset_loop;
73
74 #ifdef DEBUG_MATRIX_SCAN_RATE
75 uint32_t matrix_timer;
76 uint32_t matrix_scan_count;
77 #endif
78
79
80 __attribute__ ((weak))
81 void matrix_init_user(void) {}
82
83 __attribute__ ((weak))
84 void matrix_scan_user(void) {}
85
86 __attribute__ ((weak))
87 void matrix_init_kb(void) {
88   matrix_init_user();
89 }
90
91 __attribute__ ((weak))
92 void matrix_scan_kb(void) {
93   matrix_scan_user();
94 }
95
96 inline
97 uint8_t matrix_rows(void)
98 {
99     return MATRIX_ROWS;
100 }
101
102 inline
103 uint8_t matrix_cols(void)
104 {
105     return MATRIX_COLS;
106 }
107
108 void matrix_init(void)
109 {
110     // initialize row and col
111
112     mcp23018_status = init_mcp23018();
113
114
115     unselect_rows();
116     init_cols();
117
118     // initialize matrix state: all keys off
119     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
120         matrix[i] = 0;
121         for (uint8_t j=0; j < MATRIX_COLS; ++j) {
122             debounce_matrix[i * MATRIX_COLS + j] = 0;
123         }
124     }
125
126 #ifdef DEBUG_MATRIX_SCAN_RATE
127     matrix_timer = timer_read32();
128     matrix_scan_count = 0;
129 #endif
130
131     matrix_init_quantum();
132
133 }
134
135 void matrix_power_up(void) {
136     mcp23018_status = init_mcp23018();
137
138     unselect_rows();
139     init_cols();
140
141     // initialize matrix state: all keys off
142     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
143         matrix[i] = 0;
144     }
145
146 #ifdef DEBUG_MATRIX_SCAN_RATE
147     matrix_timer = timer_read32();
148     matrix_scan_count = 0;
149 #endif
150 }
151
152 // Returns a matrix_row_t whose bits are set if the corresponding key should be
153 // eligible to change in this scan.
154 matrix_row_t debounce_mask(uint8_t row) {
155   matrix_row_t result = 0;
156   for (uint8_t j=0; j < MATRIX_COLS; ++j) {
157     if (debounce_matrix[row * MATRIX_COLS + j]) {
158       --debounce_matrix[row * MATRIX_COLS + j];
159     } else {
160       result |= (1 << j);
161     }
162   }
163   return result;
164 }
165
166 // Report changed keys in the given row.  Resets the debounce countdowns
167 // corresponding to each set bit in 'change' to DEBOUNCE.
168 void debounce_report(matrix_row_t change, uint8_t row) {
169   for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
170     if (change & (1 << i)) {
171       debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
172     }
173   }
174 }
175
176 uint8_t matrix_scan(void)
177 {
178     if (mcp23018_status) { // if there was an error
179         if (++mcp23018_reset_loop == 0) {
180         // if (++mcp23018_reset_loop >= 1300) {
181             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
182             // this will be approx bit more frequent than once per second
183             print("trying to reset mcp23018\n");
184             mcp23018_status = init_mcp23018();
185             if (mcp23018_status) {
186                 print("left side not responding\n");
187             } else {
188                 print("left side attached\n");
189                 ergodox_blink_all_leds();
190             }
191         }
192     }
193
194 #ifdef DEBUG_MATRIX_SCAN_RATE
195     matrix_scan_count++;
196
197     uint32_t timer_now = timer_read32();
198     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
199         print("matrix scan frequency: ");
200         pdec(matrix_scan_count);
201         print("\n");
202
203         matrix_timer = timer_now;
204         matrix_scan_count = 0;
205     }
206 #endif
207
208 #ifdef LEFT_LEDS
209     mcp23018_status = ergodox_left_leds_update();
210 #endif // LEFT_LEDS
211     for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
212         select_row(i);
213         // and select on left hand
214         select_row(i + MATRIX_ROWS_PER_SIDE);
215         // we don't need a 30us delay anymore, because selecting a
216         // left-hand row requires more than 30us for i2c.
217         matrix_row_t mask = debounce_mask(i);
218         matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
219         debounce_report(cols ^ matrix[i], i);
220         matrix[i] = cols;
221         // grab cols from right hand
222         mask = debounce_mask(i + MATRIX_ROWS_PER_SIDE);
223         cols = (read_cols(i + MATRIX_ROWS_PER_SIDE) & mask) | (matrix[i + MATRIX_ROWS_PER_SIDE] & ~mask);
224         debounce_report(cols ^ matrix[i + MATRIX_ROWS_PER_SIDE], i + MATRIX_ROWS_PER_SIDE);
225         matrix[i + MATRIX_ROWS_PER_SIDE] = cols;
226         unselect_rows();
227     }
228
229     matrix_scan_quantum();
230
231     return 1;
232 }
233
234 bool matrix_is_modified(void) // deprecated and evidently not called.
235 {
236     return true;
237 }
238
239 inline
240 bool matrix_is_on(uint8_t row, uint8_t col)
241 {
242     return (matrix[row] & ((matrix_row_t)1<<col));
243 }
244
245 inline
246 matrix_row_t matrix_get_row(uint8_t row)
247 {
248     return matrix[row];
249 }
250
251 void matrix_print(void)
252 {
253     print("\nr/c 0123456789ABCDEF\n");
254     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
255         phex(row); print(": ");
256         pbin_reverse16(matrix_get_row(row));
257         print("\n");
258     }
259 }
260
261 uint8_t matrix_key_count(void)
262 {
263     uint8_t count = 0;
264     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
265         count += bitpop16(matrix[i]);
266     }
267     return count;
268 }
269
270 /* Column pin configuration
271  *
272  * Teensy
273  * col: 0   1   2   3   4   5
274  * pin: F0  F1  F4  F5  F6  F7
275  *
276  * MCP23018
277  * col: 0   1   2   3   4   5
278  * pin: B5  B4  B3  B2  B1  B0
279  */
280 static void  init_cols(void)
281 {
282     // init on mcp23018
283     // not needed, already done as part of init_mcp23018()
284
285     // init on teensy
286     // Input with pull-up(DDR:0, PORT:1)
287     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
288     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
289 }
290
291 static matrix_row_t read_cols(uint8_t row)
292 {
293     if (row < 7) {
294         if (mcp23018_status) { // if there was an error
295             return 0;
296         } else {
297             uint8_t data = 0;
298             mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);    if (mcp23018_status) goto out;
299             mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);             if (mcp23018_status) goto out;
300             mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);     if (mcp23018_status) goto out;
301             mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);                if (mcp23018_status < 0) goto out;
302             data = ~((uint8_t)mcp23018_status);
303             mcp23018_status = I2C_STATUS_SUCCESS;
304         out:
305             i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
306             return data;
307         }
308     } else {
309         /* read from teensy
310          * bitmask is 0b11110011, but we want those all
311          * in the lower six bits.
312          * we'll return 1s for the top two, but that's harmless.
313          */
314
315         return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
316     }
317 }
318
319 /* Row pin configuration
320  *
321  * Teensy
322  * row: 7   8   9   10  11  12  13
323  * pin: B0  B1  B2  B3  D2  D3  C6
324  *
325  * MCP23018
326  * row: 0   1   2   3   4   5   6
327  * pin: A0  A1  A2  A3  A4  A5  A6
328  */
329 static void unselect_rows(void)
330 {
331     // no need to unselect on mcp23018, because the select step sets all
332     // the other row bits high, and it's not changing to a different
333     // direction
334
335     // unselect on teensy
336     // Hi-Z(DDR:0, PORT:0) to unselect
337     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
338     PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
339     DDRD  &= ~(1<<2 | 1<<3);
340     PORTD &= ~(1<<2 | 1<<3);
341     DDRC  &= ~(1<<6);
342     PORTC &= ~(1<<6);
343 }
344
345 static void select_row(uint8_t row)
346 {
347     if (row < 7) {
348         // select on mcp23018
349         if (mcp23018_status) { // if there was an error
350             // do nothing
351         } else {
352             // set active row low  : 0
353             // set other rows hi-Z : 1
354             mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);        if (mcp23018_status) goto out;
355             mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);                 if (mcp23018_status) goto out;
356             mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT);      if (mcp23018_status) goto out;
357         out:
358             i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
359         }
360     } else {
361         // select on teensy
362         // Output low(DDR:1, PORT:0) to select
363         switch (row) {
364             case 7:
365                 DDRB  |= (1<<0);
366                 PORTB &= ~(1<<0);
367                 break;
368             case 8:
369                 DDRB  |= (1<<1);
370                 PORTB &= ~(1<<1);
371                 break;
372             case 9:
373                 DDRB  |= (1<<2);
374                 PORTB &= ~(1<<2);
375                 break;
376             case 10:
377                 DDRB  |= (1<<3);
378                 PORTB &= ~(1<<3);
379                 break;
380             case 11:
381                 DDRD  |= (1<<2);
382                 PORTD &= ~(1<<3);
383                 break;
384             case 12:
385                 DDRD  |= (1<<3);
386                 PORTD &= ~(1<<3);
387                 break;
388             case 13:
389                 DDRC  |= (1<<6);
390                 PORTC &= ~(1<<6);
391                 break;
392         }
393     }
394 }
395