aboutsummaryrefslogtreecommitdiff
path: root/keyboards/gergo/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/gergo/matrix.c')
-rw-r--r--keyboards/gergo/matrix.c517
1 files changed, 517 insertions, 0 deletions
diff --git a/keyboards/gergo/matrix.c b/keyboards/gergo/matrix.c
new file mode 100644
index 000000000..29fe48ccb
--- /dev/null
+++ b/keyboards/gergo/matrix.c
@@ -0,0 +1,517 @@
1/*
2Note for ErgoDox EZ customizers: Here be dragons!
3This is not a file you want to be messing with.
4All of the interesting stuff for you is under keymaps/ :)
5Love, Erez
6
7Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
8
9This program is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along 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 "pointing_device.h"
33#include QMK_KEYBOARD_H
34#ifdef DEBUG_MATRIX_SCAN_RATE
35#include "timer.h"
36#endif
37
38#ifdef BALLER
39#include <avr/interrupt.h>
40#endif
41
42#ifndef DEBOUNCE
43# define DEBOUNCE 5
44#endif
45
46// MCP Pin Defs
47#define RROW1 (1<<3)
48#define RROW2 (1<<2)
49#define RROW3 (1<<1)
50#define RROW4 (1<<0)
51#define COL0 (1<<0)
52#define COL1 (1<<1)
53#define COL2 (1<<2)
54#define COL3 (1<<3)
55#define COL4 (1<<4)
56#define COL5 (1<<5)
57#define COL6 (1<<6)
58
59// ATmega pin defs
60#define ROW1 (1<<6)
61#define ROW2 (1<<5)
62#define ROW3 (1<<4)
63#define ROW4 (1<<1)
64#define COL7 (1<<0)
65#define COL8 (1<<1)
66#define COL9 (1<<2)
67#define COL10 (1<<3)
68#define COL11 (1<<2)
69#define COL12 (1<<3)
70#define COL13 (1<<6)
71
72//Trackball pin defs
73#define TRKUP (1<<4)
74#define TRKDN (1<<5)
75#define TRKLT (1<<6)
76#define TRKRT (1<<7)
77#define TRKBTN (1<<6)
78
79
80// Multiple for mouse moves
81#ifndef TRKSTEP
82#define TRKSTEP 20
83#endif
84
85// multiple for mouse scroll
86#ifndef SCROLLSTEP
87#define SCROLLSTEP 5
88#endif
89
90// bit masks
91#define BMASK (COL7 | COL8 | COL9 | COL10)
92#define CMASK (COL13)
93#define DMASK (COL11 | COL12)
94#define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
95#define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
96#define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
97#define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
98
99// Trackball interrupts accumulate over here. Processed on scan
100// Stores prev state of mouse, high bits store direction
101uint8_t trkState = 0;
102uint8_t trkBtnState = 0;
103
104volatile uint8_t tbUpCnt = 0;
105volatile uint8_t tbDnCnt = 0;
106volatile uint8_t tbLtCnt = 0;
107volatile uint8_t tbRtCnt = 0;
108
109/* matrix state(1:on, 0:off) */
110static matrix_row_t matrix[MATRIX_ROWS];
111/*
112 * matrix state(1:on, 0:off)
113 * contains the raw values without debounce filtering of the last read cycle.
114 */
115static matrix_row_t raw_matrix[MATRIX_ROWS];
116
117// Debouncing: store for each key the number of scans until it's eligible to
118// change. When scanning the matrix, ignore any changes in keys that have
119// already changed in the last DEBOUNCE scans.
120static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
121
122static matrix_row_t read_cols(uint8_t row);
123static void init_cols(void);
124static void unselect_rows(void);
125static void select_row(uint8_t row);
126static void enableInterrupts(void);
127
128static uint8_t mcp23018_reset_loop;
129// static uint16_t mcp23018_reset_loop;
130
131#ifdef DEBUG_MATRIX_SCAN_RATE
132uint32_t matrix_timer;
133uint32_t matrix_scan_count;
134#endif
135
136
137__attribute__ ((weak))
138void matrix_init_user(void) {}
139
140__attribute__ ((weak))
141void matrix_scan_user(void) {}
142
143__attribute__ ((weak))
144void matrix_init_kb(void) {
145 matrix_init_user();
146}
147
148__attribute__ ((weak))
149void matrix_scan_kb(void) {
150 matrix_scan_user();
151}
152
153inline
154uint8_t matrix_rows(void)
155{
156 return MATRIX_ROWS;
157}
158
159inline
160uint8_t matrix_cols(void)
161{
162 return MATRIX_COLS;
163}
164
165
166void matrix_init(void)
167{
168 // initialize row and col
169 mcp23018_status = init_mcp23018();
170 unselect_rows();
171 init_cols();
172
173 // initialize matrix state: all keys off
174 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
175 matrix[i] = 0;
176 raw_matrix[i] = 0;
177 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
178 debounce_matrix[i * MATRIX_COLS + j] = 0;
179 }
180 }
181
182#ifdef DEBUG_MATRIX_SCAN_RATE
183 matrix_timer = timer_read32();
184 matrix_scan_count = 0;
185#endif
186 matrix_init_quantum();
187}
188
189void matrix_power_up(void) {
190 mcp23018_status = init_mcp23018();
191
192 unselect_rows();
193 init_cols();
194
195 // initialize matrix state: all keys off
196 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
197 matrix[i] = 0;
198 }
199
200#ifdef DEBUG_MATRIX_SCAN_RATE
201 matrix_timer = timer_read32();
202 matrix_scan_count = 0;
203#endif
204
205}
206
207// Returns a matrix_row_t whose bits are set if the corresponding key should be
208// eligible to change in this scan.
209matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
210 matrix_row_t result = 0;
211 matrix_row_t change = rawcols ^ raw_matrix[row];
212 raw_matrix[row] = rawcols;
213 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
214 if (debounce_matrix[row * MATRIX_COLS + i]) {
215 --debounce_matrix[row * MATRIX_COLS + i];
216 } else {
217 result |= (1 << i);
218 }
219 if (change & (1 << i)) {
220 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
221 }
222 }
223 return result;
224}
225
226matrix_row_t debounce_read_cols(uint8_t row) {
227 // Read the row without debouncing filtering and store it for later usage.
228 matrix_row_t cols = read_cols(row);
229 // Get the Debounce mask.
230 matrix_row_t mask = debounce_mask(cols, row);
231 // debounce the row and return the result.
232 return (cols & mask) | (matrix[row] & ~mask);;
233}
234
235uint8_t matrix_scan(void)
236{
237 // TODO: Find what is trashing interrupts
238 enableInterrupts();
239
240 // First we handle the mouse inputs
241 #ifdef BALLER
242 uint8_t pBtn = PINE & TRKBTN;
243
244 #ifdef DEBUG_BALLER
245 // Compare to previous, mod report
246 if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
247 xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
248 #endif
249
250 // Modify the report
251 report_mouse_t pRprt = pointing_device_get_report();
252
253 // Scroll by default, move on layer
254 if (layer_state == 0) {
255 pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
256 pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
257 pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
258 pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
259 } else {
260 pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
261 pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
262 pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
263 pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
264 }
265
266 #ifdef DEBUG_BALLER
267 if (pRprt.x != 0 || pRprt.y != 0)
268 xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
269 #endif
270
271 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
272 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
273
274 // Save state, push update
275 if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
276 pointing_device_set_report(pRprt);
277
278 trkBtnState = pBtn;
279 #endif
280
281 // Then the keyboard
282 if (mcp23018_status) { // if there was an error
283 if (++mcp23018_reset_loop == 0) {
284 // if (++mcp23018_reset_loop >= 1300) {
285 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
286 // this will be approx bit more frequent than once per second
287 print("trying to reset mcp23018\n");
288 mcp23018_status = init_mcp23018();
289 if (mcp23018_status) {
290 print("left side not responding\n");
291 } else {
292 print("left side attached\n");
293 }
294 }
295 }
296
297#ifdef DEBUG_MATRIX_SCAN_RATE
298 matrix_scan_count++;
299 uint32_t timer_now = timer_read32();
300 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
301 print("matrix scan frequency: ");
302 pdec(matrix_scan_count);
303 print("\n");
304
305 matrix_timer = timer_now;
306 matrix_scan_count = 0;
307 }
308#endif
309 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
310 select_row(i);
311 // and select on left hand
312 select_row(i + MATRIX_ROWS_PER_SIDE);
313 // we don't need a 30us delay anymore, because selecting a
314 // left-hand row requires more than 30us for i2c.
315
316 // grab cols from left hand
317 matrix[i] = debounce_read_cols(i);
318 // grab cols from right hand
319 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
320
321 unselect_rows();
322 }
323
324 matrix_scan_quantum();
325 enableInterrupts();
326
327#ifdef DEBUG_MATRIX
328 for (uint8_t c = 0; c < MATRIX_COLS; c++)
329 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
330 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
331#endif
332
333 return 1;
334}
335
336bool matrix_is_modified(void) // deprecated and evidently not called.
337{
338 return true;
339}
340
341inline
342bool matrix_is_on(uint8_t row, uint8_t col)
343{
344 return (matrix[row] & ((matrix_row_t)1<<col));
345}
346
347inline
348matrix_row_t matrix_get_row(uint8_t row)
349{
350 return matrix[row];
351}
352
353void matrix_print(void)
354{
355 print("\nr/c 0123456789ABCDEF\n");
356 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
357 phex(row); print(": ");
358 pbin_reverse16(matrix_get_row(row));
359 print("\n");
360 }
361}
362
363uint8_t matrix_key_count(void)
364{
365 uint8_t count = 0;
366 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
367 count += bitpop16(matrix[i]);
368 }
369 return count;
370}
371
372// Remember this means ROWS
373static void init_cols(void)
374{
375 // init on mcp23018
376 // not needed, already done as part of init_mcp23018()
377
378 // Input with pull-up(DDR:0, PORT:1)
379 DDRF &= ~FMASK;
380 PORTF |= FMASK;
381}
382
383static matrix_row_t read_cols(uint8_t row)
384{
385 if (row < 7) {
386 if (mcp23018_status) { // if there was an error
387 return 0;
388 } else {
389 uint8_t data = 0;
390 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
391 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
392 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
393 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
394 data = ~((uint8_t)mcp23018_status);
395 mcp23018_status = I2C_STATUS_SUCCESS;
396 out:
397 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
398
399#ifdef DEBUG_MATRIX
400 if (data != 0x00) xprintf("I2C: %d\n", data);
401#endif
402 return data;
403 }
404 } else {
405 /* read from teensy
406 * bitmask is 0b0111001, but we want the lower four
407 * we'll return 1s for the top two, but that's harmless.
408 */
409 // So I need to confuckulate all this
410 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
411 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
412 return ~(
413 (((PINF & ROW4) >> 1)
414 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
415 & 0xF);
416 }
417}
418
419// Row pin configuration
420static void unselect_rows(void)
421{
422 // no need to unselect on mcp23018, because the select step sets all
423 // the other row bits high, and it's not changing to a different
424 // direction
425 // Hi-Z(DDR:0, PORT:0) to unselect
426 DDRB &= ~(BMASK | TRKMASK);
427 PORTB &= ~(BMASK);
428 DDRC &= ~CMASK;
429 PORTC &= ~CMASK;
430 DDRD &= ~DMASK;
431 PORTD &= ~DMASK;
432
433 // Fix trashing of DDRB for TB
434 PORTB |= TRKMASK;
435}
436
437static void select_row(uint8_t row)
438{
439 if (row < 7) {
440 // select on mcp23018
441 if (mcp23018_status) { // do nothing on error
442 } else { // set active row low : 0 // set other rows hi-Z : 1
443 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
444 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
445 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
446 out:
447 i2c_stop(ERGODOX_EZ_I2C_TIMEOUT);
448 }
449 } else {
450 // Output low(DDR:1, PORT:0) to select
451 switch (row) {
452 case 7:
453 DDRB |= COL7;
454 PORTB &= ~COL7;
455 break;
456 case 8:
457 DDRB |= COL8;
458 PORTB &= ~COL8;
459 break;
460 case 9:
461 DDRB |= COL9;
462 PORTB &= ~COL9;
463 break;
464 case 10:
465 DDRB |= COL10;
466 PORTB &= ~COL10;
467 break;
468 case 11:
469 DDRD |= COL11;
470 PORTD &= ~COL11;
471 break;
472 case 12:
473 DDRD |= COL12;
474 PORTD &= ~COL12;
475 break;
476 case 13:
477 DDRC |= COL13;
478 PORTC &= ~COL13;
479 break;
480 }
481 }
482}
483
484
485// Trackball Interrupts
486static void enableInterrupts(void) {
487 #ifdef BALLER
488 // Set interrupt mask
489 // Set port defs
490 DDRB &= ~TRKMASK;
491 PORTB |= TRKMASK;
492 DDRE &= ~TRKBTN;
493 PORTE |= TRKBTN;
494
495 // Interrupt shenanigans
496 //EIMSK |= (1 << PCIE0);
497 PCMSK0 |= TRKMASK;
498 PCICR |= (1 << PCIE0);
499 sei();
500 #endif
501
502 return;
503}
504#ifdef BALLER
505ISR (PCINT0_vect) {
506 // Don't get fancy, we're in a interrupt here
507 // PCINT reports a interrupt for a change on the bus
508 // We hand the button at scantime for debounce
509 volatile uint8_t pState = PINB & TRKMASK;
510 if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
511 if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
512 if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
513 if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
514 trkState = pState;
515
516}
517#endif