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