aboutsummaryrefslogtreecommitdiff
path: root/keyboards/handwired/pterodactyl/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/handwired/pterodactyl/matrix.c')
-rw-r--r--keyboards/handwired/pterodactyl/matrix.c527
1 files changed, 527 insertions, 0 deletions
diff --git a/keyboards/handwired/pterodactyl/matrix.c b/keyboards/handwired/pterodactyl/matrix.c
new file mode 100644
index 000000000..5f13cb30b
--- /dev/null
+++ b/keyboards/handwired/pterodactyl/matrix.c
@@ -0,0 +1,527 @@
1/*
2Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
3Copyright 2017 Erin Call <hello@erincall.com>
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include <stdint.h>
19#include <stdbool.h>
20#include <avr/io.h>
21#include "wait.h"
22#include "action_layer.h"
23#include "print.h"
24#include "debug.h"
25#include "util.h"
26#include "matrix.h"
27#include "pterodactyl.h"
28#include "i2c_master.h"
29#include "timer.h"
30
31#define I2C_TIMEOUT 100
32
33#define I2C_ADDR 0b0100000
34#define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE )
35#define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ )
36#define IODIRA 0x00 // i/o direction register
37#define IODIRB 0x01
38#define GPPUA 0x0C // GPIO pull-up resistor register
39#define GPPUB 0x0D
40#define GPIOA 0x12 // general purpose i/o port register (write modifies OLAT)
41#define GPIOB 0x13
42
43void init_expander(void);
44
45/* Set 0 if debouncing isn't needed */
46
47#ifndef DEBOUNCE
48# define DEBOUNCE 5
49#endif
50
51#if (DEBOUNCE > 0)
52 static uint16_t debouncing_time;
53 static bool debouncing = false;
54#endif
55
56#ifdef MATRIX_MASKED
57 extern const matrix_row_t matrix_mask[];
58#endif
59
60#if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
61static const uint8_t onboard_row_pins[MATRIX_ROWS] = MATRIX_ONBOARD_ROW_PINS;
62static const uint8_t onboard_col_pins[MATRIX_COLS] = MATRIX_ONBOARD_COL_PINS;
63static const bool col_expanded[MATRIX_COLS] = COL_EXPANDED;
64#endif
65
66/* matrix state(1:on, 0:off) */
67static matrix_row_t matrix[MATRIX_ROWS];
68
69static matrix_row_t matrix_debouncing[MATRIX_ROWS];
70
71#if (DIODE_DIRECTION == COL2ROW)
72 static const uint8_t expander_col_pins[MATRIX_COLS] = MATRIX_EXPANDER_COL_PINS;
73 static void init_cols(void);
74 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
75 static void unselect_rows(void);
76 static void select_row(uint8_t row);
77 static void unselect_row(uint8_t row);
78#elif (DIODE_DIRECTION == ROW2COL)
79 static const uint8_t expander_row_pins[MATRIX_ROWS] = MATRIX_EXPANDER_ROW_PINS;
80 static void init_rows(void);
81 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
82 static void unselect_cols(void);
83 static void select_col(uint8_t col);
84 static void unselect_col(uint8_t col);
85#endif
86
87static uint8_t expander_reset_loop;
88uint8_t expander_status;
89uint8_t expander_input_pin_mask;
90bool i2c_initialized = false;
91
92#define ROW_SHIFTER ((matrix_row_t)1)
93
94__attribute__ ((weak))
95void matrix_init_user(void) {}
96
97__attribute__ ((weak))
98void matrix_scan_user(void) {}
99
100__attribute__ ((weak))
101void matrix_init_kb(void) {
102 matrix_init_user();
103}
104
105__attribute__ ((weak))
106void matrix_scan_kb(void) {
107 matrix_scan_user();
108}
109
110inline
111uint8_t matrix_rows(void)
112{
113 return MATRIX_ROWS;
114}
115
116inline
117uint8_t matrix_cols(void)
118{
119 return MATRIX_COLS;
120}
121
122void matrix_init(void)
123{
124 init_expander();
125
126#if (DIODE_DIRECTION == COL2ROW)
127 unselect_rows();
128 init_cols();
129#elif (DIODE_DIRECTION == ROW2COL)
130 unselect_cols();
131 init_rows();
132#endif
133
134 // initialize matrix state: all keys off
135 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
136 matrix[i] = 0;
137 matrix_debouncing[i] = 0;
138 }
139
140 matrix_init_quantum();
141}
142
143void init_expander(void) {
144 if (! i2c_initialized) {
145 i2c_init();
146 wait_us(1000000);
147 }
148
149 if (! expander_input_pin_mask) {
150#if (DIODE_DIRECTION == COL2ROW)
151 for (int col = 0; col < MATRIX_COLS; col++) {
152 if (col_expanded[col]) {
153 expander_input_pin_mask |= (1 << expander_col_pins[col]);
154 }
155 }
156#elif (DIODE_DIRECTION == ROW2COL)
157 for (int row = 0; row < MATRIX_ROWS; row++) {
158 expander_input_pin_mask |= (1 << expander_row_pins[row]);
159 }
160#endif
161 }
162
163 expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
164 expander_status = i2c_write(IODIRA, I2C_TIMEOUT); if (expander_status) goto out;
165
166 /*
167 Pin direction and pull-up depends on both the diode direction
168 and on whether the column register is GPIOA or GPIOB
169 +-------+---------------+---------------+
170 | | ROW2COL | COL2ROW |
171 +-------+---------------+---------------+
172 | GPIOA | input, output | output, input |
173 +-------+---------------+---------------+
174 | GPIOB | output, input | input, output |
175 +-------+---------------+---------------+
176 */
177
178#if (EXPANDER_COL_REGISTER == GPIOA)
179# if (DIODE_DIRECTION == COL2ROW)
180 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
181 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
182# elif (DIODE_DIRECTION == ROW2COL)
183 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
184 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
185# endif
186#elif (EXPANDER_COL_REGISTER == GPIOB)
187# if (DIODE_DIRECTION == COL2ROW)
188 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
189 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
190# elif (DIODE_DIRECTION == ROW2COL)
191 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
192 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
193# endif
194#endif
195
196 i2c_stop();
197
198 // set pull-up
199 // - unused : off : 0
200 // - input : on : 1
201 // - driving : off : 0
202 expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
203 expander_status = i2c_write(GPPUA, I2C_TIMEOUT); if (expander_status) goto out;
204#if (EXPANDER_COL_REGISTER == GPIOA)
205# if (DIODE_DIRECTION == COL2ROW)
206 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
207 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
208# elif (DIODE_DIRECTION == ROW2COL)
209 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
210 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
211# endif
212#elif (EXPANDER_COL_REGISTER == GPIOB)
213# if (DIODE_DIRECTION == COL2ROW)
214 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
215 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
216# elif (DIODE_DIRECTION == ROW2COL)
217 expander_status = i2c_write(expander_input_pin_mask, I2C_TIMEOUT); if (expander_status) goto out;
218 expander_status = i2c_write(0, I2C_TIMEOUT); if (expander_status) goto out;
219# endif
220#endif
221
222out:
223 i2c_stop();
224}
225
226uint8_t matrix_scan(void)
227{
228 if (expander_status) { // if there was an error
229 if (++expander_reset_loop == 0) {
230 // since expander_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
231 // this will be approx bit more frequent than once per second
232 print("trying to reset expander\n");
233 init_expander();
234 if (expander_status) {
235 print("left side not responding\n");
236 } else {
237 print("left side attached\n");
238 }
239 }
240 }
241
242#if (DIODE_DIRECTION == COL2ROW)
243 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
244# if (DEBOUNCE > 0)
245 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
246
247 if (matrix_changed) {
248 debouncing = true;
249 debouncing_time = timer_read();
250 }
251# else
252 read_cols_on_row(matrix, current_row);
253# endif
254 }
255
256#elif (DIODE_DIRECTION == ROW2COL)
257 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
258# if (DEBOUNCE > 0)
259 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
260
261 if (matrix_changed) {
262 debouncing = true;
263 debouncing_time = timer_read();
264 }
265# else
266 read_rows_on_col(matrix, current_col);
267# endif
268
269 }
270#endif
271
272# if (DEBOUNCE > 0)
273 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
274 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
275 matrix[i] = matrix_debouncing[i];
276 }
277 debouncing = false;
278 }
279# endif
280
281 matrix_scan_quantum();
282 return 1;
283}
284
285bool matrix_is_modified(void) // deprecated and evidently not called.
286{
287#if (DEBOUNCE > 0)
288 if (debouncing) return false;
289#endif
290 return true;
291}
292
293inline
294bool matrix_is_on(uint8_t row, uint8_t col)
295{
296 return (matrix[row] & (ROW_SHIFTER << col));
297}
298
299inline
300matrix_row_t matrix_get_row(uint8_t row)
301{
302#ifdef MATRIX_MASKED
303 return matrix[row] & matrix_mask[row];
304#else
305 return matrix[row];
306#endif
307}
308
309void matrix_print(void)
310{
311 print("\nr/c 0123456789ABCDEF\n");
312 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
313 phex(row); print(": ");
314 pbin_reverse16(matrix_get_row(row));
315 print("\n");
316 }
317}
318
319uint8_t matrix_key_count(void)
320{
321 uint8_t count = 0;
322 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
323 count += bitpop16(matrix[i]);
324 }
325 return count;
326}
327
328#if (DIODE_DIRECTION == COL2ROW)
329
330static void init_cols(void) {
331 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
332 if (! col_expanded[x]) {
333 uint8_t pin = onboard_col_pins[x];
334 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
335 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
336 }
337 }
338}
339
340static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
341 // Store last value of row prior to reading
342 matrix_row_t last_row_value = current_matrix[current_row];
343
344 // Clear data in matrix row
345 current_matrix[current_row] = 0;
346
347 // Select row and wait for row selection to stabilize
348 select_row(current_row);
349 wait_us(30);
350
351 // Read columns from expander, unless it's in an error state
352 if (! expander_status) {
353 expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
354 expander_status = i2c_write(EXPANDER_COL_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
355 expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
356
357 current_matrix[current_row] |= (~i2c_read_nack(I2C_TIMEOUT)) & expander_input_pin_mask;
358
359 out:
360 i2c_stop();
361 }
362
363 // Read columns from onboard pins
364 for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
365 if (! col_expanded[col_index]) {
366 uint8_t pin = onboard_col_pins[col_index];
367 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
368 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
369 }
370 }
371
372 unselect_row(current_row);
373
374 return (last_row_value != current_matrix[current_row]);
375}
376
377static void select_row(uint8_t row) {
378 // select on expander, unless it's in an error state
379 if (! expander_status) {
380 // set active row low : 0
381 // set other rows hi-Z : 1
382 expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
383 expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
384 expander_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (expander_status) goto out;
385 out:
386 i2c_stop();
387 }
388
389 // select on teensy
390 uint8_t pin = onboard_row_pins[row];
391 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
392 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
393}
394
395static void unselect_row(uint8_t row)
396{
397 // No need to explicitly unselect expander pins--their I/O state is
398 // set simultaneously, with a single bitmask sent to i2c_write. When
399 // select_row selects a single pin, it implicitly unselects all the
400 // other ones.
401
402 // unselect on teensy
403 uint8_t pin = onboard_row_pins[row];
404 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // OUT
405 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // LOW
406}
407
408static void unselect_rows(void) {
409 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
410 unselect_row(x);
411 }
412}
413
414#elif (DIODE_DIRECTION == ROW2COL)
415
416static void init_rows(void)
417{
418 for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
419 uint8_t pin = onboard_row_pins[x];
420 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
421 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
422 }
423}
424
425static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
426{
427 bool matrix_changed = false;
428
429 uint8_t column_state = 0;
430
431 //select col and wait for selection to stabilize
432 select_col(current_col);
433 wait_us(30);
434
435 if (current_col < 6) {
436 // read rows from expander
437 if (expander_status) {
438 // it's already in an error state; nothing we can do
439 return false;
440 }
441
442 expander_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (expander_status) goto out;
443 expander_status = i2c_write(EXPANDER_ROW_REGISTER, I2C_TIMEOUT); if (expander_status) goto out;
444 expander_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (expander_status) goto out;
445 column_state = i2c_read_nack(I2C_TIMEOUT);
446
447 out:
448 i2c_stop();
449
450 column_state = ~column_state;
451 } else {
452 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
453 if ((_SFR_IO8(onboard_row_pins[current_row] >> 4) & _BV(onboard_row_pins[current_row] & 0xF)) == 0) {
454 column_state |= (1 << current_row);
455 }
456 }
457 }
458
459 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
460 // Store last value of row prior to reading
461 matrix_row_t last_row_value = current_matrix[current_row];
462
463 if (column_state & (1 << current_row)) {
464 // key closed; set state bit in matrix
465 current_matrix[current_row] |= (ROW_SHIFTER << current_col);
466 } else {
467 // key open; clear state bit in matrix
468 current_matrix[current_row] &= ~(ROW_SHIFTER << current_col);
469 }
470
471 // Determine whether the matrix changed state
472 if ((last_row_value != current_matrix[current_row]) && !(matrix_changed))
473 {
474 matrix_changed = true;
475 }
476 }
477
478 unselect_col(current_col);
479
480 return matrix_changed;
481}
482
483static void select_col(uint8_t col)
484{
485 if (col_expanded[col]) {
486 // select on expander
487 if (expander_status) { // if there was an error
488 // do nothing
489 } else {
490 // set active col low : 0
491 // set other cols hi-Z : 1
492 expander_status = i2c_start(I2C_ADDR_WRITE); if (expander_status) goto out;
493 expander_status = i2c_write(EXPANDER_COL_REGISTER); if (expander_status) goto out;
494 expander_status = i2c_write(0xFF & ~(1<<col)); if (expander_status) goto out;
495 out:
496 i2c_stop();
497 }
498 } else {
499 // select on teensy
500 uint8_t pin = onboard_col_pins[col];
501 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
502 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
503 }
504}
505
506static void unselect_col(uint8_t col)
507{
508 if (col_expanded[col]) {
509 // No need to explicitly unselect expander pins--their I/O state is
510 // set simultaneously, with a single bitmask sent to i2c_write. When
511 // select_col selects a single pin, it implicitly unselects all the
512 // other ones.
513 } else {
514 // unselect on teensy
515 uint8_t pin = onboard_col_pins[col];
516 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
517 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
518 }
519}
520
521static void unselect_cols(void)
522{
523 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
524 unselect_col(x);
525 }
526}
527#endif