aboutsummaryrefslogtreecommitdiff
path: root/quantum/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r--quantum/matrix.c49
1 files changed, 16 insertions, 33 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 3600d4e7b..9b5ce33d2 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -1,5 +1,5 @@
1/* 1/*
2Copyright 2012-2017 Jun Wako, Jack Humbert 2Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
3 3
4This program is free software: you can redistribute it and/or modify 4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by 5it under the terms of the GNU General Public License as published by
@@ -16,15 +16,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17#include <stdint.h> 17#include <stdint.h>
18#include <stdbool.h> 18#include <stdbool.h>
19#if defined(__AVR__)
20#include <avr/io.h>
21#endif
22#include "wait.h" 19#include "wait.h"
23#include "print.h" 20#include "print.h"
24#include "debug.h" 21#include "debug.h"
25#include "util.h" 22#include "util.h"
26#include "matrix.h" 23#include "matrix.h"
27#include "timer.h" 24#include "timer.h"
25#include "quantum.h"
28 26
29 27
30/* Set 0 if debouncing isn't needed */ 28/* Set 0 if debouncing isn't needed */
@@ -60,8 +58,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
60#endif 58#endif
61 59
62#if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) 60#if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
63static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 61static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
64static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 62static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
65#endif 63#endif
66 64
67/* matrix state(1:on, 0:off) */ 65/* matrix state(1:on, 0:off) */
@@ -271,9 +269,7 @@ uint8_t matrix_key_count(void)
271static void init_cols(void) 269static void init_cols(void)
272{ 270{
273 for(uint8_t x = 0; x < MATRIX_COLS; x++) { 271 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
274 uint8_t pin = col_pins[x]; 272 setPinInputHigh(col_pins[x]);
275 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
276 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
277 } 273 }
278} 274}
279 275
@@ -293,8 +289,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
293 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { 289 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
294 290
295 // Select the col pin to read (active low) 291 // Select the col pin to read (active low)
296 uint8_t pin = col_pins[col_index]; 292 uint8_t pin_state = readPin(col_pins[col_index]);
297 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
298 293
299 // Populate the matrix row with the state of the col pin 294 // Populate the matrix row with the state of the col pin
300 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index); 295 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
@@ -308,24 +303,19 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
308 303
309static void select_row(uint8_t row) 304static void select_row(uint8_t row)
310{ 305{
311 uint8_t pin = row_pins[row]; 306 setPinOutput(row_pins[row]);
312 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT 307 writePinLow(row_pins[row]);
313 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
314} 308}
315 309
316static void unselect_row(uint8_t row) 310static void unselect_row(uint8_t row)
317{ 311{
318 uint8_t pin = row_pins[row]; 312 setPinInputHigh(row_pins[row]);
319 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
320 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
321} 313}
322 314
323static void unselect_rows(void) 315static void unselect_rows(void)
324{ 316{
325 for(uint8_t x = 0; x < MATRIX_ROWS; x++) { 317 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
326 uint8_t pin = row_pins[x]; 318 setPinInput(row_pins[x]);
327 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
328 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
329 } 319 }
330} 320}
331 321
@@ -334,9 +324,7 @@ static void unselect_rows(void)
334static void init_rows(void) 324static void init_rows(void)
335{ 325{
336 for(uint8_t x = 0; x < MATRIX_ROWS; x++) { 326 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
337 uint8_t pin = row_pins[x]; 327 setPinInputHigh(row_pins[x]);
338 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
339 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
340 } 328 }
341} 329}
342 330
@@ -356,7 +344,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
356 matrix_row_t last_row_value = current_matrix[row_index]; 344 matrix_row_t last_row_value = current_matrix[row_index];
357 345
358 // Check row pin state 346 // Check row pin state
359 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) 347 if (readPin(row_pins[row_index]) == 0)
360 { 348 {
361 // Pin LO, set col bit 349 // Pin LO, set col bit
362 current_matrix[row_index] |= (ROW_SHIFTER << current_col); 350 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
@@ -382,24 +370,19 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
382 370
383static void select_col(uint8_t col) 371static void select_col(uint8_t col)
384{ 372{
385 uint8_t pin = col_pins[col]; 373 setPinOutput(col_pins[col]);
386 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT 374 writePinLow(col_pins[col]);
387 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
388} 375}
389 376
390static void unselect_col(uint8_t col) 377static void unselect_col(uint8_t col)
391{ 378{
392 uint8_t pin = col_pins[col]; 379 setPinInputHigh(col_pins[col]);
393 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
394 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
395} 380}
396 381
397static void unselect_cols(void) 382static void unselect_cols(void)
398{ 383{
399 for(uint8_t x = 0; x < MATRIX_COLS; x++) { 384 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
400 uint8_t pin = col_pins[x]; 385 setPinInputHigh(col_pins[x]);
401 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
402 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
403 } 386 }
404} 387}
405 388