X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=quantum%2Fmatrix.c;h=ac523482ad101e635b6b432942d768a058bb384f;hb=3d082382b20ab338bb7f41b33329daffc994169a;hp=7d70f728d459d4255a8fa7232e410ae0a7e73e8f;hpb=d09d2da1c25b377b0f68ea83706903344c240ae1;p=qmk_firmware.git diff --git a/quantum/matrix.c b/quantum/matrix.c index 7d70f728d..ac523482a 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -1,6 +1,6 @@ /* -Copyright 2012 Jun Wako -Generated by planckkeyboard.com (2014 Jack Humbert) +Copyright 2012 Jun Wako +Copyright 2014 Jack Humbert This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -15,77 +15,154 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ - -/* - * scan matrix - */ #include #include +#if defined(__AVR__) #include -#include +#endif +#include "wait.h" #include "print.h" #include "debug.h" #include "util.h" #include "matrix.h" +#include "timer.h" + -#ifndef DEBOUNCE -# define DEBOUNCE 10 +/* Set 0 if debouncing isn't needed */ + +#ifndef DEBOUNCING_DELAY +# define DEBOUNCING_DELAY 5 +#endif + +#if (DEBOUNCING_DELAY > 0) + static uint16_t debouncing_time; + static bool debouncing = false; +#endif + +#if (MATRIX_COLS <= 8) +# define print_matrix_header() print("\nr/c 01234567\n") +# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row)) +# define matrix_bitpop(i) bitpop(matrix[i]) +# define ROW_SHIFTER ((uint8_t)1) +#elif (MATRIX_COLS <= 16) +# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n") +# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row)) +# define matrix_bitpop(i) bitpop16(matrix[i]) +# define ROW_SHIFTER ((uint16_t)1) +#elif (MATRIX_COLS <= 32) +# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n") +# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row)) +# define matrix_bitpop(i) bitpop32(matrix[i]) +# define ROW_SHIFTER ((uint32_t)1) +#endif + +#ifdef MATRIX_MASKED + extern const matrix_row_t matrix_mask[]; +#endif + +#if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) +static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; +static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; #endif -static uint8_t debouncing = DEBOUNCE; /* matrix state(1:on, 0:off) */ static matrix_row_t matrix[MATRIX_ROWS]; -static matrix_row_t matrix_debouncing[MATRIX_ROWS]; -#if DIODE_DIRECTION == ROW2COL - static matrix_row_t matrix_reversed[MATRIX_COLS]; - static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS]; -#endif +static matrix_row_t matrix_debouncing[MATRIX_ROWS]; -#if MATRIX_COLS > 16 - #define SHIFTER 1UL -#else - #define SHIFTER 1 +#if (DIODE_DIRECTION == COL2ROW) + static void init_cols(void); + static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); + static void unselect_rows(void); + static void select_row(uint8_t row); + static void unselect_row(uint8_t row); +#elif (DIODE_DIRECTION == ROW2COL) + static void init_rows(void); + static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); + static void unselect_cols(void); + static void unselect_col(uint8_t col); + static void select_col(uint8_t col); #endif -static matrix_row_t read_cols(void); -static void init_cols(void); -static void unselect_rows(void); -static void select_row(uint8_t row); +__attribute__ ((weak)) +void matrix_init_quantum(void) { + matrix_init_kb(); +} __attribute__ ((weak)) -void matrix_init_kb(void) { +void matrix_scan_quantum(void) { + matrix_scan_kb(); +} +__attribute__ ((weak)) +void matrix_init_kb(void) { + matrix_init_user(); } __attribute__ ((weak)) void matrix_scan_kb(void) { + matrix_scan_user(); +} +__attribute__ ((weak)) +void matrix_init_user(void) { +} + +__attribute__ ((weak)) +void matrix_scan_user(void) { } inline -uint8_t matrix_rows(void) -{ +uint8_t matrix_rows(void) { return MATRIX_ROWS; } inline -uint8_t matrix_cols(void) -{ +uint8_t matrix_cols(void) { return MATRIX_COLS; } -void matrix_init(void) -{ - // To use PORTF disable JTAG with writing JTD bit twice within four cycles. - MCUCR |= (1<= 0; --r) { +// /* DDRxn */ +// _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF); +// toggle_row(r); +// } +// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) { +// /* PORTxn */ +// _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF); +// } +// #elif (DIODE_DIRECTION == ROW2COL) +// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) { +// /* DDRxn */ +// _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF); +// toggle_col(c); +// } +// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) { +// /* PORTxn */ +// _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF); +// } +// #endif +// } + +void matrix_init(void) { + // To use PORTF disable JTAG with writing JTD bit twice within four cycles. + #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__)) + MCUCR |= _BV(JTD); + MCUCR |= _BV(JTD); + #endif // initialize row and col +#if (DIODE_DIRECTION == COL2ROW) unselect_rows(); init_cols(); +#elif (DIODE_DIRECTION == ROW2COL) + unselect_cols(); + init_rows(); +#endif // initialize matrix state: all keys off for (uint8_t i=0; i < MATRIX_ROWS; i++) { @@ -93,78 +170,66 @@ void matrix_init(void) matrix_debouncing[i] = 0; } - matrix_init_kb(); + matrix_init_quantum(); } - uint8_t matrix_scan(void) { -#if DIODE_DIRECTION == COL2ROW - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - select_row(i); - _delay_us(30); // without this wait read unstable value. - matrix_row_t cols = read_cols(); - if (matrix_debouncing[i] != cols) { - matrix_debouncing[i] = cols; - if (debouncing) { - debug("bounce!: "); debug_hex(debouncing); debug("\n"); - } - debouncing = DEBOUNCE; - } - unselect_rows(); - } +#if (DIODE_DIRECTION == COL2ROW) - if (debouncing) { - if (--debouncing) { - _delay_ms(1); - } else { - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - matrix[i] = matrix_debouncing[i]; - } - } - } -#else - for (uint8_t i = 0; i < MATRIX_COLS; i++) { - select_row(i); - _delay_us(30); // without this wait read unstable value. - matrix_row_t rows = read_cols(); - if (matrix_reversed_debouncing[i] != rows) { - matrix_reversed_debouncing[i] = rows; - if (debouncing) { - debug("bounce!: "); debug_hex(debouncing); debug("\n"); + // Set row, read cols + for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { +# if (DEBOUNCING_DELAY > 0) + bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row); + + if (matrix_changed) { + debouncing = true; + debouncing_time = timer_read(); } - debouncing = DEBOUNCE; - } - unselect_rows(); + +# else + read_cols_on_row(matrix, current_row); +# endif + } - if (debouncing) { - if (--debouncing) { - _delay_ms(1); - } else { - for (uint8_t i = 0; i < MATRIX_COLS; i++) { - matrix_reversed[i] = matrix_reversed_debouncing[i]; +#elif (DIODE_DIRECTION == ROW2COL) + + // Set col, read rows + for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { +# if (DEBOUNCING_DELAY > 0) + bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col); + if (matrix_changed) { + debouncing = true; + debouncing_time = timer_read(); } - } - } - for (uint8_t y = 0; y < MATRIX_ROWS; y++) { - matrix_row_t row = 0; - for (uint8_t x = 0; x < MATRIX_COLS; x++) { - row |= ((matrix_reversed[x] & (1<> y) << x; - } - matrix[y] = row; +# else + read_rows_on_col(matrix, current_col); +# endif + } + #endif - matrix_scan_kb(); +# if (DEBOUNCING_DELAY > 0) + if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) { + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + matrix[i] = matrix_debouncing[i]; + } + debouncing = false; + } +# endif + matrix_scan_quantum(); return 1; } bool matrix_is_modified(void) { +#if (DEBOUNCING_DELAY > 0) if (debouncing) return false; +#endif return true; } @@ -177,15 +242,22 @@ bool matrix_is_on(uint8_t row, uint8_t col) inline matrix_row_t matrix_get_row(uint8_t row) { + // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a + // switch blocker installed and the switch is always pressed. +#ifdef MATRIX_MASKED + return matrix[row] & matrix_mask[row]; +#else return matrix[row]; +#endif } void matrix_print(void) { - print("\nr/c 0123456789ABCDEF\n"); + print_matrix_header(); + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { phex(row); print(": "); - pbin_reverse16(matrix_get_row(row)); + print_matrix_row(row); print("\n"); } } @@ -194,121 +266,148 @@ uint8_t matrix_key_count(void) { uint8_t count = 0; for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - count += bitpop16(matrix[i]); + count += matrix_bitpop(i); } return count; } + + +#if (DIODE_DIRECTION == COL2ROW) + static void init_cols(void) { - int B = 0, C = 0, D = 0, E = 0, F = 0; - -#if DIODE_DIRECTION == COL2ROW - for(int x = 0; x < MATRIX_COLS; x++) { - int col = COLS[x]; -#else - for(int x = 0; x < MATRIX_ROWS; x++) { - int col = ROWS[x]; -#endif - if ((col & 0xF0) == 0x20) { - B |= (1<<(col & 0x0F)); - } else if ((col & 0xF0) == 0x30) { - C |= (1<<(col & 0x0F)); - } else if ((col & 0xF0) == 0x40) { - D |= (1<<(col & 0x0F)); - } else if ((col & 0xF0) == 0x50) { - E |= (1<<(col & 0x0F)); - } else if ((col & 0xF0) == 0x60) { - F |= (1<<(col & 0x0F)); - } + for(uint8_t x = 0; x < MATRIX_COLS; x++) { + uint8_t pin = col_pins[x]; + _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN + _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI } - DDRB &= ~(B); PORTB |= (B); - DDRC &= ~(C); PORTC |= (C); - DDRD &= ~(D); PORTD |= (D); - DDRE &= ~(E); PORTE |= (E); - DDRF &= ~(F); PORTF |= (F); } -static matrix_row_t read_cols(void) +static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { - matrix_row_t result = 0; + // Store last value of row prior to reading + matrix_row_t last_row_value = current_matrix[current_row]; -#if DIODE_DIRECTION == COL2ROW - for(int x = 0; x < MATRIX_COLS; x++) { - int col = COLS[x]; -#else - for(int x = 0; x < MATRIX_ROWS; x++) { - int col = ROWS[x]; -#endif + // Clear data in matrix row + current_matrix[current_row] = 0; + + // Select row and wait for row selecton to stabilize + select_row(current_row); + wait_us(30); + + // For each col... + for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { + + // Select the col pin to read (active low) + uint8_t pin = col_pins[col_index]; + uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)); - if ((col & 0xF0) == 0x20) { - result |= (PINB&(1<<(col & 0x0F)) ? 0 : (SHIFTER<> 4) + 1) |= _BV(pin & 0xF); // OUT + _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW +} + +static void unselect_row(uint8_t row) +{ + uint8_t pin = row_pins[row]; + _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN + _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI } static void unselect_rows(void) { - int B = 0, C = 0, D = 0, E = 0, F = 0; + for(uint8_t x = 0; x < MATRIX_ROWS; x++) { + uint8_t pin = row_pins[x]; + _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN + _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI + } +} -#if DIODE_DIRECTION == COL2ROW - for(int x = 0; x < MATRIX_ROWS; x++) { - int row = ROWS[x]; -#else - for(int x = 0; x < MATRIX_COLS; x++) { - int row = COLS[x]; -#endif - if ((row & 0xF0) == 0x20) { - B |= (1<<(row & 0x0F)); - } else if ((row & 0xF0) == 0x30) { - C |= (1<<(row & 0x0F)); - } else if ((row & 0xF0) == 0x40) { - D |= (1<<(row & 0x0F)); - } else if ((row & 0xF0) == 0x50) { - E |= (1<<(row & 0x0F)); - } else if ((row & 0xF0) == 0x60) { - F |= (1<<(row & 0x0F)); - } +#elif (DIODE_DIRECTION == ROW2COL) + +static void init_rows(void) +{ + for(uint8_t x = 0; x < MATRIX_ROWS; x++) { + uint8_t pin = row_pins[x]; + _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN + _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI } - DDRB &= ~(B); PORTB |= (B); - DDRC &= ~(C); PORTC |= (C); - DDRD &= ~(D); PORTD |= (D); - DDRE &= ~(E); PORTE |= (E); - DDRF &= ~(F); PORTF |= (F); } -static void select_row(uint8_t row) +static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { + bool matrix_changed = false; -#if DIODE_DIRECTION == COL2ROW - int row_pin = ROWS[row]; -#else - int row_pin = COLS[row]; -#endif + // Select col and wait for col selecton to stabilize + select_col(current_col); + wait_us(30); + + // For each row... + for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) + { + + // Store last value of row prior to reading + matrix_row_t last_row_value = current_matrix[row_index]; + + // Check row pin state + if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0) + { + // Pin LO, set col bit + current_matrix[row_index] |= (ROW_SHIFTER << current_col); + } + else + { + // Pin HI, clear col bit + current_matrix[row_index] &= ~(ROW_SHIFTER << current_col); + } + + // Determine if the matrix changed state + if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) + { + matrix_changed = true; + } + } - if ((row_pin & 0xF0) == 0x20) { - DDRB |= (1<<(row_pin & 0x0F)); - PORTB &= ~(1<<(row_pin & 0x0F)); - } else if ((row_pin & 0xF0) == 0x30) { - DDRC |= (1<<(row_pin & 0x0F)); - PORTC &= ~(1<<(row_pin & 0x0F)); - } else if ((row_pin & 0xF0) == 0x40) { - DDRD |= (1<<(row_pin & 0x0F)); - PORTD &= ~(1<<(row_pin & 0x0F)); - } else if ((row_pin & 0xF0) == 0x50) { - DDRE |= (1<<(row_pin & 0x0F)); - PORTE &= ~(1<<(row_pin & 0x0F)); - } else if ((row_pin & 0xF0) == 0x60) { - DDRF |= (1<<(row_pin & 0x0F)); - PORTF &= ~(1<<(row_pin & 0x0F)); - } -} \ No newline at end of file + // Unselect col + unselect_col(current_col); + + return matrix_changed; +} + +static void select_col(uint8_t col) +{ + uint8_t pin = col_pins[col]; + _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT + _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW +} + +static void unselect_col(uint8_t col) +{ + uint8_t pin = col_pins[col]; + _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN + _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI +} + +static void unselect_cols(void) +{ + for(uint8_t x = 0; x < MATRIX_COLS; x++) { + uint8_t pin = col_pins[x]; + _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN + _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI + } +} + +#endif