]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/handwired/frenchdev/matrix.c
Moved frenchdev to handwired
[qmk_firmware.git] / keyboards / handwired / frenchdev / 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 Note to self, the "column" and "row" in here actually refers to the opposite on the keyboard
9 see definition of KEYMAP in v1.h, the grid is transposed so that a "row" in here is actually a "column" on the physical keyboard
10 Nicolas
11
12 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 2 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program.  If not, see <http://www.gnu.org/licenses/>.
26 */
27
28 /*
29  * scan matrix
30  */
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <avr/io.h>
34 #include "wait.h"
35 #include "action_layer.h"
36 #include "print.h"
37 #include "debug.h"
38 #include "util.h"
39 #include "matrix.h"
40 #include "frenchdev.h"
41 #include "i2cmaster.h"
42 #ifdef DEBUG_MATRIX_SCAN_RATE
43 #include  "timer.h"
44 #endif
45
46 /*
47  * This constant define not debouncing time in msecs, but amount of matrix
48  * scan loops which should be made to get stable debounced results.
49  *
50  * On Ergodox matrix scan rate is relatively low, because of slow I2C.
51  * Now it's only 317 scans/second, or about 3.15 msec/scan.
52  * According to Cherry specs, debouncing time is 5 msec.
53  *
54  * And so, there is no sense to have DEBOUNCE higher than 2.
55  */
56
57 #ifndef DEBOUNCE
58 #   define DEBOUNCE     5
59 #endif
60 static uint8_t debouncing = DEBOUNCE;
61
62 /* matrix state(1:on, 0:off) */
63 static matrix_row_t matrix[MATRIX_ROWS];
64 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
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     debug_enable = true;
111     debug_matrix = true;
112     debug_keyboard = true;
113     debug_mouse = true;
114
115     mcp23018_status = init_mcp23018();
116
117
118     unselect_rows();
119     init_cols();
120
121     // initialize matrix state: all keys off
122     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
123         matrix[i] = 0;
124         matrix_debouncing[i] = 0;
125     }
126
127 #ifdef DEBUG_MATRIX_SCAN_RATE
128     matrix_timer = timer_read32();
129     matrix_scan_count = 0;
130 #endif
131
132     matrix_init_quantum();
133
134 }
135
136 void matrix_power_up(void) {
137     mcp23018_status = init_mcp23018();
138
139     unselect_rows();
140     init_cols();
141
142     // initialize matrix state: all keys off
143     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
144         matrix[i] = 0;
145         matrix_debouncing[i] = 0;
146     }
147
148 #ifdef DEBUG_MATRIX_SCAN_RATE
149     matrix_timer = timer_read32();
150     matrix_scan_count = 0;
151 #endif
152
153 }
154
155 uint8_t matrix_scan(void)
156 {
157     if (mcp23018_status) { // if there was an error
158         if (++mcp23018_reset_loop == 0) {
159             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
160             // this will be approx bit more frequent than once per second
161             print("trying to reset mcp23018\n");
162             mcp23018_status = init_mcp23018();
163             if (mcp23018_status) {
164                 print("left side not responding\n");
165             } else {
166                 print("left side attached\n");
167                 ergodox_blink_all_leds();
168             }
169         }
170     }
171
172 #ifdef DEBUG_MATRIX_SCAN_RATE
173     matrix_scan_count++;
174
175     uint32_t timer_now = timer_read32();
176     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
177         print("matrix scan frequency: ");
178         pdec(matrix_scan_count);
179         print("\n");
180
181         matrix_timer = timer_now;
182         matrix_scan_count = 0;
183     }
184 #endif
185
186     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
187         select_row(i);
188         wait_us(30);  // without this wait read unstable value.
189         matrix_row_t cols = read_cols(i);
190         if (matrix_debouncing[i] != cols) {
191             matrix_debouncing[i] = cols;
192             if (debouncing) {
193                 debug("bounce!: "); debug_hex(debouncing); debug("\n");
194             }
195             debouncing = DEBOUNCE;
196         }
197         unselect_rows();
198     }
199
200     if (debouncing) {
201         if (--debouncing) {
202             wait_us(1);
203             // this should be wait_ms(1) but has been left as-is at EZ's request
204         } else {
205             for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
206                 matrix[i] = matrix_debouncing[i];
207             }
208         }
209     }
210
211     matrix_scan_quantum();
212
213     return 1;
214 }
215
216 bool matrix_is_modified(void)
217 {
218     if (debouncing) return false;
219     return true;
220 }
221
222 inline
223 bool matrix_is_on(uint8_t row, uint8_t col)
224 {
225     return (matrix[row] & ((matrix_row_t)1<<col));
226 }
227
228 inline
229 matrix_row_t matrix_get_row(uint8_t row)
230 {
231     return matrix[row];
232 }
233
234 void matrix_print(void)
235 {
236     print("\nr/c 0123456789ABCDEF\n");
237     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
238         phex(row); print(": ");
239         pbin_reverse16(matrix_get_row(row));
240         print("\n");
241     }
242 }
243
244 uint8_t matrix_key_count(void)
245 {
246     uint8_t count = 0;
247     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
248         count += bitpop16(matrix[i]);
249     }
250     return count;
251 }
252
253 /* Column pin configuration
254  *
255  * Teensy
256  * col: 0   1   2   3   4   5
257  * pin: F0  F1  F4  F5  F6  F7
258  *
259  * MCP23018
260  * col: 0   1   2   3   4   5
261  * pin: B5  B4  B3  B2  B1  B0
262  */
263 static void  init_cols(void)
264 {
265     // init on mcp23018
266     // not needed, already done as part of init_mcp23018()
267
268     // init on teensy
269     // Input with pull-up(DDR:0, PORT:1)
270     DDRF  &= ~(1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
271     PORTF |=  (1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<1 | 1<<0);
272 }
273
274 static matrix_row_t read_cols(uint8_t row)
275 {
276     if (row < 8) {
277         if (mcp23018_status) { // if there was an error
278             return 0;
279         } else {
280             uint8_t data = 0;
281             mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
282             mcp23018_status = i2c_write(GPIOB);             if (mcp23018_status) goto out;
283             mcp23018_status = i2c_start(I2C_ADDR_READ);     if (mcp23018_status) goto out;
284             data = i2c_readNak();
285             data = ~data;
286         out:
287             i2c_stop();
288             return data;
289         }
290     } else {
291         // read from teensy
292         return
293             (PINF&(1<<0) ? 0 : (1<<0)) |
294             (PINF&(1<<1) ? 0 : (1<<1)) |
295             (PINF&(1<<4) ? 0 : (1<<2)) |
296             (PINF&(1<<5) ? 0 : (1<<3)) |
297             (PINF&(1<<6) ? 0 : (1<<4)) |
298             (PINF&(1<<7) ? 0 : (1<<5)) ;
299     }
300 }
301
302 /* Row pin configuration
303  *
304  * Teensy
305  * row: 7   8   9   10  11  12  13
306  * pin: B0  B1  B2  B3  D2  D3  C6
307  *
308  * MCP23018
309  * row: 0   1   2   3   4   5   6
310  * pin: A0  A1  A2  A3  A4  A5  A6
311  */
312 static void unselect_rows(void)
313 {
314     // unselect on mcp23018
315     if (mcp23018_status) { // if there was an error
316         // do nothing
317     } else {
318         // set all rows hi-Z : 1
319         mcp23018_status = i2c_start(I2C_ADDR_WRITE);    if (mcp23018_status) goto out;
320         mcp23018_status = i2c_write(GPIOA);             if (mcp23018_status) goto out;
321         mcp23018_status = i2c_write( 0xFF
322                               & ~(0<<8)
323                           );                            if (mcp23018_status) goto out;
324     out:
325         i2c_stop();
326     }
327
328     // unselect on teensy
329     // Hi-Z(DDR:0, PORT:0) to unselect
330     DDRB  &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
331     PORTB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3);
332     DDRD  &= ~(1<<2 | 1<<3);
333     PORTD &= ~(1<<2 | 1<<3);
334     DDRC  &= ~(1<<6 | 1<<7);
335     PORTC &= ~(1<<6 | 1<<7);
336 }
337
338 static void select_row(uint8_t row)
339 {
340     if (row < 8) {
341         // select on mcp23018
342         if (mcp23018_status) { // if there was an error
343             // do nothing
344         } else {
345             // set active row low  : 0
346             // set other rows hi-Z : 1
347             mcp23018_status = i2c_start(I2C_ADDR_WRITE);        if (mcp23018_status) goto out;
348             mcp23018_status = i2c_write(GPIOA);                 if (mcp23018_status) goto out;
349             mcp23018_status = i2c_write( 0xFF & ~(1<<row)
350                                   & ~(0<<8)
351                               );                                if (mcp23018_status) goto out;
352         out:
353             i2c_stop();
354         }
355     } else {
356         // select on teensy
357         // Output low(DDR:1, PORT:0) to select
358         switch (row) {
359             case 8:
360                 DDRB  |= (1<<0);
361                 PORTB &= ~(1<<0);
362                 break;
363             case 9:
364                 DDRB  |= (1<<1);
365                 PORTB &= ~(1<<1);
366                 break;
367             case 10:
368                 DDRB  |= (1<<2);
369                 PORTB &= ~(1<<2);
370                 break;
371             case 11:
372                 DDRB  |= (1<<3);
373                 PORTB &= ~(1<<3);
374                 break;
375             case 12:
376                 DDRD  |= (1<<2);
377                 PORTD &= ~(1<<3);
378                 break;
379             case 13:
380                 DDRD  |= (1<<3);
381                 PORTD &= ~(1<<3);
382                 break;
383             case 14:
384                 DDRC  |= (1<<6);
385                 PORTC &= ~(1<<6);
386                 break;
387             case 15:
388                 DDRC  |= (1<<7);
389                 PORTC &= ~(1<<7);
390                 break;
391         }
392     }
393 }
394