]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/georgi/matrix.c
Remove more commented out MCUs
[qmk_firmware.git] / keyboards / georgi / matrix.c
1 /*
2
3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
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
19 #include "matrix.h"
20 #include <stdint.h>
21 #include <stdbool.h>
22 #include <avr/io.h>
23 #include "wait.h"
24 #include "action_layer.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "keymap_steno.h"
29 #include QMK_KEYBOARD_H
30 #ifdef DEBUG_MATRIX_SCAN_RATE
31 #include  "timer.h"
32 #endif
33
34
35 #ifndef DEBOUNCE
36 #   define DEBOUNCE     5
37 #endif
38
39 // MCP Pin Defs
40 #define RROW1 (1<<3)
41 #define RROW2 (1<<2)
42 #define RROW3 (1<<1)
43 #define RROW4 (1<<0)
44 #define COL0 (1<<0)
45 #define COL1 (1<<1)
46 #define COL2 (1<<2)
47 #define COL3 (1<<3)
48 #define COL4 (1<<4)
49 #define COL5 (1<<5)
50 #define COL6 (1<<6)
51
52 // ATmega pin defs
53 #define ROW1  (1<<6)
54 #define ROW2  (1<<5)
55 #define ROW3  (1<<4)
56 #define ROW4  (1<<1)
57 #define COL7 (1<<0)
58 #define COL8 (1<<1)
59 #define COL9 (1<<2)
60 #define COL10 (1<<3)
61 #define COL11 (1<<2)
62 #define COL12 (1<<3)
63 #define COL13 (1<<6)
64
65
66 // bit masks
67 #define BMASK     (COL7 | COL8 | COL9 | COL10)
68 #define CMASK     (COL13)
69 #define DMASK     (COL11 | COL12)
70 #define FMASK     (ROW1 | ROW2 | ROW3 | ROW4)
71 #define RROWMASK  (RROW1 | RROW2 | RROW3 | RROW4)
72 #define MCPMASK   (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
73
74 /* matrix state(1:on, 0:off) */
75 static matrix_row_t matrix[MATRIX_ROWS];
76 /*
77  * matrix state(1:on, 0:off)
78  * contains the raw values without debounce filtering of the last read cycle.
79  */
80 static matrix_row_t raw_matrix[MATRIX_ROWS];
81
82 // Debouncing: store for each key the number of scans until it's eligible to
83 // change.  When scanning the matrix, ignore any changes in keys that have
84 // already changed in the last DEBOUNCE scans.
85 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
86
87 static matrix_row_t read_cols(uint8_t row);
88 static void init_cols(void);
89 static void unselect_rows(void);
90 static void select_row(uint8_t row);
91
92 static uint8_t mcp23018_reset_loop;
93 // static uint16_t mcp23018_reset_loop;
94
95 #ifdef DEBUG_MATRIX_SCAN_RATE
96 uint32_t matrix_timer;
97 uint32_t matrix_scan_count;
98 #endif
99
100
101 __attribute__ ((weak))
102 void matrix_init_user(void) {}
103
104 __attribute__ ((weak))
105 void matrix_scan_user(void) {}
106
107 __attribute__ ((weak))
108 void matrix_init_kb(void) {
109   matrix_init_user();
110 }
111
112 __attribute__ ((weak))
113 void matrix_scan_kb(void) {
114   matrix_scan_user();
115 }
116
117 inline
118 uint8_t matrix_rows(void)
119 {
120     return MATRIX_ROWS;
121 }
122
123 inline
124 uint8_t matrix_cols(void)
125 {
126     return MATRIX_COLS;
127 }
128
129
130 void matrix_init(void)
131 {
132     // initialize row and col
133     mcp23018_status = init_mcp23018();
134     unselect_rows();
135     init_cols();
136
137     // initialize matrix state: all keys off
138     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
139         matrix[i] = 0;
140         raw_matrix[i] = 0;
141         for (uint8_t j=0; j < MATRIX_COLS; ++j) {
142             debounce_matrix[i * MATRIX_COLS + j] = 0;
143         }
144     }
145
146 #ifdef DEBUG_MATRIX_SCAN_RATE
147     matrix_timer = timer_read32();
148     matrix_scan_count = 0;
149 #endif
150     matrix_init_quantum();
151 }
152
153 void matrix_power_up(void) {
154     mcp23018_status = init_mcp23018();
155
156     unselect_rows();
157     init_cols();
158
159     // initialize matrix state: all keys off
160     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
161         matrix[i] = 0;
162     }
163
164 #ifdef DEBUG_MATRIX_SCAN_RATE
165     matrix_timer = timer_read32();
166     matrix_scan_count = 0;
167 #endif
168
169 }
170
171 // Returns a matrix_row_t whose bits are set if the corresponding key should be
172 // eligible to change in this scan.
173 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
174   matrix_row_t result = 0;
175   matrix_row_t change = rawcols ^ raw_matrix[row];
176   raw_matrix[row] = rawcols;
177   for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
178     if (debounce_matrix[row * MATRIX_COLS + i]) {
179       --debounce_matrix[row * MATRIX_COLS + i];
180     } else {
181       result |= (1 << i);
182     }
183     if (change & (1 << i)) {
184       debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
185     }
186   }
187   return result;
188 }
189
190 matrix_row_t debounce_read_cols(uint8_t row) {
191   // Read the row without debouncing filtering and store it for later usage.
192   matrix_row_t cols = read_cols(row);
193   // Get the Debounce mask.
194   matrix_row_t mask = debounce_mask(cols, row);
195   // debounce the row and return the result.
196   return (cols & mask) | (matrix[row] & ~mask);;
197 }
198
199 uint8_t matrix_scan(void)
200 {
201   // Then the keyboard
202   if (mcp23018_status) { // if there was an error
203       if (++mcp23018_reset_loop == 0) {
204       // if (++mcp23018_reset_loop >= 1300) {
205           // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
206           // this will be approx bit more frequent than once per second
207           print("trying to reset mcp23018\n");
208           mcp23018_status = init_mcp23018();
209           if (mcp23018_status) {
210               print("left side not responding\n");
211           } else {
212               print("left side attached\n");
213           }
214       }
215   }
216
217 #ifdef DEBUG_MATRIX_SCAN_RATE
218     matrix_scan_count++;
219     uint32_t timer_now = timer_read32();
220     if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
221         print("matrix scan frequency: ");
222         pdec(matrix_scan_count);
223         print("\n");
224
225         matrix_timer = timer_now;
226         matrix_scan_count = 0;
227     }
228 #endif
229     for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
230         select_row(i);
231         // and select on left hand
232         select_row(i + MATRIX_ROWS_PER_SIDE);
233         // we don't need a 30us delay anymore, because selecting a
234         // left-hand row requires more than 30us for i2c.
235
236         // grab cols from left hand
237         matrix[i] = debounce_read_cols(i);
238         // grab cols from right hand
239         matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
240
241         unselect_rows();
242     }
243
244     matrix_scan_quantum();
245
246 #ifdef DEBUG_MATRIX
247     for (uint8_t c = 0; c < MATRIX_COLS; c++)
248                 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
249                   if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
250 #endif
251
252     return 1;
253 }
254
255 bool matrix_is_modified(void) // deprecated and evidently not called.
256 {
257     return true;
258 }
259
260 inline
261 bool matrix_is_on(uint8_t row, uint8_t col)
262 {
263     return (matrix[row] & ((matrix_row_t)1<<col));
264 }
265
266 inline
267 matrix_row_t matrix_get_row(uint8_t row)
268 {
269     return matrix[row];
270 }
271
272 void matrix_print(void)
273 {
274     print("\nr/c 0123456789ABCDEF\n");
275     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
276         phex(row); print(": ");
277         pbin_reverse16(matrix_get_row(row));
278         print("\n");
279     }
280 }
281
282 uint8_t matrix_key_count(void)
283 {
284     uint8_t count = 0;
285     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
286         count += bitpop16(matrix[i]);
287     }
288     return count;
289 }
290
291 // Remember this means ROWS
292 static void  init_cols(void)
293 {
294     // init on mcp23018
295     // not needed, already done as part of init_mcp23018()
296
297     // Input with pull-up(DDR:0, PORT:1)
298     DDRF  &= ~FMASK;
299     PORTF |=  FMASK;
300 }
301
302 static matrix_row_t read_cols(uint8_t row)
303 {
304     if (row < 7) {
305         if (mcp23018_status) { // if there was an error
306             return 0;
307         } else {
308             uint8_t data = 0;
309             mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);    if (mcp23018_status) goto out;
310             mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT);             if (mcp23018_status) goto out;
311             mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT);     if (mcp23018_status) goto out;
312             mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT);                if (mcp23018_status < 0) goto out;
313             data = ~((uint8_t)mcp23018_status);
314             mcp23018_status = I2C_STATUS_SUCCESS;
315         out:
316             i2c_stop();
317
318 #ifdef DEBUG_MATRIX
319             if (data != 0x00) xprintf("I2C: %d\n", data);
320 #endif
321             return data;
322         }
323     } else {
324          /* read from teensy
325                 * bitmask is 0b0111001, but we want the lower four
326                 * we'll return 1s for the top two, but that's harmless.
327                 */
328         // So I need to confuckulate all this
329         //return ~(((PIND & DMASK) >> 1  | ((PINC & CMASK) >> 6) | (PIN)));
330         //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
331         return ~(
332            (((PINF & ROW4) >> 1)
333           | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
334         & 0xF);
335     }
336 }
337
338 // Row pin configuration
339 static void unselect_rows(void)
340 {
341     // no need to unselect on mcp23018, because the select step sets all
342     // the other row bits high, and it's not changing to a different
343     // direction
344     // Hi-Z(DDR:0, PORT:0) to unselect
345     DDRB  &= ~(BMASK);
346     PORTB &= ~(BMASK);
347     DDRC  &= ~CMASK;
348     PORTC &= ~CMASK;
349     DDRD  &= ~DMASK;
350     PORTD &= ~DMASK;
351 }
352
353 static void select_row(uint8_t row)
354 {
355     if (row < 7) {
356         // select on mcp23018
357         if (mcp23018_status) { // do nothing on error
358         } else { // set active row low  : 0 // set other rows hi-Z : 1
359             mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT);        if (mcp23018_status) goto out;
360             mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT);                 if (mcp23018_status) goto out;
361             mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT);      if (mcp23018_status) goto out;
362         out:
363             i2c_stop();
364         }
365     } else {
366         // Output low(DDR:1, PORT:0) to select
367         switch (row) {
368             case 7:
369                 DDRB  |= COL7;
370                 PORTB &= ~COL7;
371                 break;
372             case 8:
373                 DDRB  |= COL8;
374                 PORTB &= ~COL8;
375                 break;
376             case 9:
377                 DDRB  |= COL9;
378                 PORTB &= ~COL9;
379                 break;
380             case 10:
381                 DDRB  |= COL10;
382                 PORTB &= ~COL10;
383                 break;
384             case 11:
385                 DDRD  |= COL11;
386                 PORTD &= ~COL11;
387                 break;
388             case 12:
389                 DDRD  |= COL12;
390                 PORTD &= ~COL12;
391                 break;
392             case 13:
393                 DDRC  |= COL13;
394                 PORTC &= ~COL13;
395                 break;
396         }
397     }
398 }