From b9094a7db9f29692a290805b4b8590cb80d07bce Mon Sep 17 00:00:00 2001 From: zvecr Date: Wed, 8 Sep 2021 17:42:32 +0100 Subject: [PATCH 1/2] Backport upstream matrix changes --- keyboards/massdrop/alt/config.h | 16 +-- keyboards/massdrop/alt/matrix.c | 182 ------------------------------- keyboards/massdrop/alt/rules.mk | 3 - keyboards/massdrop/ctrl/config.h | 15 ++- keyboards/massdrop/ctrl/matrix.c | 182 ------------------------------- keyboards/massdrop/ctrl/rules.mk | 3 - quantum/config_common.h | 69 ++++++++++++ quantum/debounce/sym_g.c | 4 +- quantum/matrix.c | 10 +- quantum/quantum.h | 52 +++++++++ 10 files changed, 146 insertions(+), 390 deletions(-) delete mode 100644 keyboards/massdrop/alt/matrix.c delete mode 100644 keyboards/massdrop/ctrl/matrix.c diff --git a/keyboards/massdrop/alt/config.h b/keyboards/massdrop/alt/config.h index 6ed4208166bf..45ef0689b4ca 100644 --- a/keyboards/massdrop/alt/config.h +++ b/keyboards/massdrop/alt/config.h @@ -32,18 +32,18 @@ along with this program. If not, see . #define MATRIX_ROWS 5 #define MATRIX_COLS 15 + +#define MATRIX_ROW_PINS { A00, A01, A02, A03, A04 } +#define MATRIX_COL_PINS { B04, B05, B06, B07, B08, B09, B10, B11, B12, B13, A05, A06, A07, A10, A11 } +#define UNUSED_PINS + +/* COL2ROW, ROW2COL */ +#define DIODE_DIRECTION COL2ROW + /* MCU Port name definitions */ #define PA 0 #define PB 1 -/* Port and Pin definition of key row hardware configuration */ -#define MATRIX_ROW_PORTS PA, PA, PA, PA, PA -#define MATRIX_ROW_PINS 0, 1, 2, 3, 4 - -/* Port and Pin definition of key column hardware configuration */ -#define MATRIX_COL_PORTS PB, PB, PB, PB, PB, PB, PB, PB, PB, PB, PA, PA, PA, PA, PA -#define MATRIX_COL_PINS 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 5, 6, 7, 10, 11 - /* This Shift Register expands available hardware output lines to control additional peripherals */ /* It uses four lines from the MCU to provide 16 output lines */ /* Shift Register Clock configuration (MCU to ShiftRegister.RCLK) */ diff --git a/keyboards/massdrop/alt/matrix.c b/keyboards/massdrop/alt/matrix.c deleted file mode 100644 index 5c31868532c9..000000000000 --- a/keyboards/massdrop/alt/matrix.c +++ /dev/null @@ -1,182 +0,0 @@ -/* -Copyright 2019 Massdrop Inc. - -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 -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -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 . -*/ - -#include "alt.h" - -#include "d51_util.h" -#include "debug.h" -#include "clks.h" -#include - -#define MCU_PORTS_USED 2 //PA, PB - -matrix_row_t mlatest[MATRIX_ROWS]; -matrix_row_t mlast[MATRIX_ROWS]; -matrix_row_t mdebounced[MATRIX_ROWS]; - -uint8_t row_ports[] = { MATRIX_ROW_PORTS }; -uint8_t row_pins[] = { MATRIX_ROW_PINS }; -uint8_t col_ports[] = { MATRIX_COL_PORTS }; -uint8_t col_pins[] = { MATRIX_COL_PINS }; -uint32_t row_masks[MCU_PORTS_USED]; //Array size must match number of unique MCU ports used for reads (PA, PB, PC, etc) - -__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) { -} - -void matrix_init(void) -{ - memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); - memset(mlast, 0, MATRIX_ROWS * sizeof(matrix_row_t)); - memset(mdebounced, 0, MATRIX_ROWS * sizeof(matrix_row_t)); - - memset(row_masks, 0, sizeof(row_masks)); - - //Inputs - uint8_t row; - for (row = 0; row < MATRIX_ROWS; row++) - { - PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; //Input - PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; //Low - PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; //Input Enable, - PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; //Pull Enable - row_masks[row_ports[row]] |= 1 << row_pins[row]; //Add pin to proper row mask - } - - //Outputs - uint8_t col; - for (col = 0; col < MATRIX_COLS; col++) - { - PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; //Output - PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Low - } - - matrix_init_quantum(); -} - -uint64_t mdebouncing = 0; -bool debouncing = false; - -uint8_t matrix_scan(void) -{ - uint64_t timer; - uint8_t row; - uint8_t col; - uint32_t scans[MCU_PORTS_USED]; //Array size must match number of unique MCU ports used for reads (PA, PB, PC, etc) - - memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer - - for (col = 0; col < MATRIX_COLS; col++) - { - //Set output - PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output - wait_us(1); //Delay for output - - //Read input (add unique ports as needed, PA, PB, PC, etc) - scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data - scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PA row pins data - - //Clear output - PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output - - for (row = 0; row < MATRIX_ROWS; row++) - { - //Move scan bits from scans array into proper row bit locations - if (scans[row_ports[row]] & (1 << row_pins[row])) - mlatest[row] |= 1 << col; - } - } - - timer = timer_read64(); - - for (row = 0; row < MATRIX_ROWS; row++) - { - if (mlast[row] != mlatest[row]) { - debouncing = true; - mdebouncing = timer + DEBOUNCE; - } - - mlast[row] = mlatest[row]; - } - - if (debouncing && timer >= mdebouncing) - { - for (row = 0; row < MATRIX_ROWS; row++) { - mdebounced[row] = mlatest[row]; - } - - mdebouncing = 0; - debouncing = false; - } - - matrix_scan_quantum(); - - return 1; -} - -matrix_row_t matrix_get_row(uint8_t row) -{ - return mdebounced[row]; -} - -void matrix_print(void) -{ - char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C"; - char *pbuf = buf+3; - uint32_t cols; - uint32_t rows; - matrix_row_t row; - - for (cols = 1; cols <= MATRIX_COLS; cols++) - { - *pbuf = (cols%10)+48; - pbuf++; - } - *pbuf = '\r'; pbuf++; - *pbuf = '\n'; pbuf++; - - for (rows = 1; rows <= MATRIX_ROWS; rows++) - { - row = matrix_get_row(rows-1); - if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; } - else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; } - for (cols = 0; cols < MATRIX_COLS; cols++) - { - if (row & 1 << cols) *pbuf = 'X'; - else *pbuf = '.'; - pbuf++; - } - *pbuf = '\r'; pbuf++; - *pbuf = '\n'; pbuf++; - } - *pbuf = 0; - dprint(buf); -} diff --git a/keyboards/massdrop/alt/rules.mk b/keyboards/massdrop/alt/rules.mk index faf5faa512db..d792bddbf459 100644 --- a/keyboards/massdrop/alt/rules.mk +++ b/keyboards/massdrop/alt/rules.mk @@ -1,5 +1,4 @@ # project specific files -SRC = matrix.c SRC += config_led.c SRC += spi.c SRC += usb.c @@ -8,8 +7,6 @@ SRC += usb.c ARM_ATSAM = SAMD51J18A MCU = cortex-m4 -CUSTOM_MATRIX = yes - # Build Options # comment out to disable the options. # diff --git a/keyboards/massdrop/ctrl/config.h b/keyboards/massdrop/ctrl/config.h index 035f954218a0..7cd945827c59 100644 --- a/keyboards/massdrop/ctrl/config.h +++ b/keyboards/massdrop/ctrl/config.h @@ -32,18 +32,17 @@ along with this program. If not, see . #define MATRIX_ROWS 11 #define MATRIX_COLS 8 +#define MATRIX_ROW_PINS { B04, B05, B06, B07, B08, B09, A10, A11, B10, B11, B12 } +#define MATRIX_COL_PINS { A00, A01, A02, A03, A04, A05, A06, A07 } +#define UNUSED_PINS + +/* COL2ROW, ROW2COL */ +#define DIODE_DIRECTION COL2ROW + /* MCU Port name definitions */ #define PA 0 #define PB 1 -/* Port and Pin definition of key row hardware configuration */ -#define MATRIX_ROW_PORTS PB, PB, PB, PB, PB, PB, PA, PA, PB, PB, PB -#define MATRIX_ROW_PINS 4, 5, 6, 7, 8, 9, 10, 11, 10, 11, 12 - -/* Port and Pin definition of key column hardware configuration */ -#define MATRIX_COL_PORTS PA, PA, PA, PA, PA, PA, PA, PA -#define MATRIX_COL_PINS 0, 1, 2, 3, 4, 5, 6, 7 - /* This Shift Register expands available hardware output lines to control additional peripherals */ /* It uses four lines from the MCU to provide 16 output lines */ /* Shift Register Clock configuration (MCU to ShiftRegister.RCLK) */ diff --git a/keyboards/massdrop/ctrl/matrix.c b/keyboards/massdrop/ctrl/matrix.c deleted file mode 100644 index b53c1693df9b..000000000000 --- a/keyboards/massdrop/ctrl/matrix.c +++ /dev/null @@ -1,182 +0,0 @@ -/* -Copyright 2019 Massdrop Inc. - -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 -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -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 . -*/ - -#include "ctrl.h" - -#include "d51_util.h" -#include "debug.h" -#include "clks.h" -#include - -#define MCU_PORTS_USED 2 //PA, PB - -matrix_row_t mlatest[MATRIX_ROWS]; -matrix_row_t mlast[MATRIX_ROWS]; -matrix_row_t mdebounced[MATRIX_ROWS]; - -uint8_t row_ports[] = { MATRIX_ROW_PORTS }; -uint8_t row_pins[] = { MATRIX_ROW_PINS }; -uint8_t col_ports[] = { MATRIX_COL_PORTS }; -uint8_t col_pins[] = { MATRIX_COL_PINS }; -uint32_t row_masks[MCU_PORTS_USED]; //Array size must match number of unique MCU ports used for reads (PA, PB, PC, etc) - -__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) { -} - -void matrix_init(void) -{ - memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); - memset(mlast, 0, MATRIX_ROWS * sizeof(matrix_row_t)); - memset(mdebounced, 0, MATRIX_ROWS * sizeof(matrix_row_t)); - - memset(row_masks, 0, sizeof(row_masks)); - - //Inputs - uint8_t row; - for (row = 0; row < MATRIX_ROWS; row++) - { - PORT->Group[row_ports[row]].DIRCLR.reg = 1 << row_pins[row]; //Input - PORT->Group[row_ports[row]].OUTCLR.reg = 1 << row_pins[row]; //Low - PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.INEN = 1; //Input Enable, - PORT->Group[row_ports[row]].PINCFG[row_pins[row]].bit.PULLEN = 1; //Pull Enable - row_masks[row_ports[row]] |= 1 << row_pins[row]; //Add pin to proper row mask - } - - //Outputs - uint8_t col; - for (col = 0; col < MATRIX_COLS; col++) - { - PORT->Group[col_ports[col]].DIRSET.reg = 1 << col_pins[col]; //Output - PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Low - } - - matrix_init_quantum(); -} - -uint64_t mdebouncing = 0; -bool debouncing = false; - -uint8_t matrix_scan(void) -{ - uint64_t timer; - uint8_t row; - uint8_t col; - uint32_t scans[MCU_PORTS_USED]; //Array size must match number of unique MCU ports used for reads (PA, PB, PC, etc) - - memset(mlatest, 0, MATRIX_ROWS * sizeof(matrix_row_t)); //Zero the result buffer - - for (col = 0; col < MATRIX_COLS; col++) - { - //Set output - PORT->Group[col_ports[col]].OUTSET.reg = 1 << col_pins[col]; //Set col output - wait_us(1); //Delay for output - - //Read input (add unique ports as needed, PA, PB, PC, etc) - scans[PA] = PORT->Group[PA].IN.reg & row_masks[PA]; //Read PA row pins data - scans[PB] = PORT->Group[PB].IN.reg & row_masks[PB]; //Read PA row pins data - - //Clear output - PORT->Group[col_ports[col]].OUTCLR.reg = 1 << col_pins[col]; //Clear col output - - for (row = 0; row < MATRIX_ROWS; row++) - { - //Move scan bits from scans array into proper row bit locations - if (scans[row_ports[row]] & (1 << row_pins[row])) - mlatest[row] |= 1 << col; - } - } - - timer = timer_read64(); - - for (row = 0; row < MATRIX_ROWS; row++) - { - if (mlast[row] != mlatest[row]) { - debouncing = true; - mdebouncing = timer + DEBOUNCE; - } - - mlast[row] = mlatest[row]; - } - - if (debouncing && timer >= mdebouncing) - { - for (row = 0; row < MATRIX_ROWS; row++) { - mdebounced[row] = mlatest[row]; - } - - mdebouncing = 0; - debouncing = false; - } - - matrix_scan_quantum(); - - return 1; -} - -matrix_row_t matrix_get_row(uint8_t row) -{ - return mdebounced[row]; -} - -void matrix_print(void) -{ - char buf[(MATRIX_COLS+8)*(MATRIX_ROWS+1)] = "R C"; - char *pbuf = buf+3; - uint32_t cols; - uint32_t rows; - matrix_row_t row; - - for (cols = 1; cols <= MATRIX_COLS; cols++) - { - *pbuf = (cols%10)+48; - pbuf++; - } - *pbuf = '\r'; pbuf++; - *pbuf = '\n'; pbuf++; - - for (rows = 1; rows <= MATRIX_ROWS; rows++) - { - row = matrix_get_row(rows-1); - if (rows < 10) { *pbuf = rows+48; pbuf++; *pbuf = ' '; pbuf++; *pbuf = ' '; pbuf++; } - else { *pbuf = (rows/10)+48; pbuf++; *pbuf = (rows%10)+48; pbuf++; *pbuf = ' '; pbuf++; } - for (cols = 0; cols < MATRIX_COLS; cols++) - { - if (row & 1 << cols) *pbuf = 'X'; - else *pbuf = '.'; - pbuf++; - } - *pbuf = '\r'; pbuf++; - *pbuf = '\n'; pbuf++; - } - *pbuf = 0; - dprint(buf); -} diff --git a/keyboards/massdrop/ctrl/rules.mk b/keyboards/massdrop/ctrl/rules.mk index cc1ed9c18028..5d890eff15d0 100644 --- a/keyboards/massdrop/ctrl/rules.mk +++ b/keyboards/massdrop/ctrl/rules.mk @@ -1,5 +1,4 @@ # project specific files -SRC = matrix.c SRC += config_led.c SRC += spi.c SRC += usb.c @@ -8,8 +7,6 @@ SRC += usb.c ARM_ATSAM = SAMD51J18A MCU = cortex-m4 -CUSTOM_MATRIX = yes - # Build Options # comment out to disable the options. # diff --git a/quantum/config_common.h b/quantum/config_common.h index f42df6357d67..bbf0a0d23335 100644 --- a/quantum/config_common.h +++ b/quantum/config_common.h @@ -281,6 +281,75 @@ # define F14 PAL_LINE(GPIOF, 14) # define F15 PAL_LINE(GPIOF, 15) # endif + +#elif defined(PROTOCOL_ARM_ATSAM) +# include "samd51j18a.h" + +# define A00 PIN_PA00 +# define A01 PIN_PA01 +# define A02 PIN_PA02 +# define A03 PIN_PA03 +# define A04 PIN_PA04 +# define A05 PIN_PA05 +# define A06 PIN_PA06 +# define A07 PIN_PA07 +# define A08 PIN_PA08 +# define A09 PIN_PA09 +# define A10 PIN_PA10 +# define A11 PIN_PA11 +# define A12 PIN_PA12 +# define A13 PIN_PA13 +# define A14 PIN_PA14 +# define A15 PIN_PA15 +# define A16 PIN_PA16 +# define A17 PIN_PA17 +# define A18 PIN_PA18 +# define A19 PIN_PA19 +# define A20 PIN_PA20 +# define A21 PIN_PA21 +# define A22 PIN_PA22 +# define A23 PIN_PA23 +# define A24 PIN_PA24 +# define A25 PIN_PA25 +# define A26 PIN_PA26 +# define A27 PIN_PA27 +# define A28 PIN_PA28 +# define A29 PIN_PA29 +# define A30 PIN_PA30 +# define A31 PIN_PA31 + +# define B00 PIN_PB00 +# define B01 PIN_PB01 +# define B02 PIN_PB02 +# define B03 PIN_PB03 +# define B04 PIN_PB04 +# define B05 PIN_PB05 +# define B06 PIN_PB06 +# define B07 PIN_PB07 +# define B08 PIN_PB08 +# define B09 PIN_PB09 +# define B10 PIN_PB10 +# define B11 PIN_PB11 +# define B12 PIN_PB12 +# define B13 PIN_PB13 +# define B14 PIN_PB14 +# define B15 PIN_PB15 +# define B16 PIN_PB16 +# define B17 PIN_PB17 +# define B18 PIN_PB18 +# define B19 PIN_PB19 +# define B20 PIN_PB20 +# define B21 PIN_PB21 +# define B22 PIN_PB22 +# define B23 PIN_PB23 +# define B24 PIN_PB24 +# define B25 PIN_PB25 +# define B26 PIN_PB26 +# define B27 PIN_PB27 +# define B28 PIN_PB28 +# define B29 PIN_PB29 +# define B30 PIN_PB30 +# define B31 PIN_PB31 #endif /* USART configuration */ diff --git a/quantum/debounce/sym_g.c b/quantum/debounce/sym_g.c index 3ed9055d2a90..d4078761d07f 100644 --- a/quantum/debounce/sym_g.c +++ b/quantum/debounce/sym_g.c @@ -31,10 +31,10 @@ static uint16_t debouncing_time; void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { if (changed) { debouncing = true; - debouncing_time = timer_read(); + debouncing_time = timer_read32(); } - if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { + if (debouncing && timer_elapsed32(debouncing_time) > DEBOUNCE) { for (int i = 0; i < num_rows; i++) { cooked[i] = raw[i]; } diff --git a/quantum/matrix.c b/quantum/matrix.c index 907492a0f620..5ba97abfecbc 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -41,6 +41,10 @@ along with this program. If not, see . # define ROW_SHIFTER ((uint32_t)1) #endif +#ifndef MATRIX_IO_DELAY +# define MATRIX_IO_DELAY 30 +#endif + #ifdef MATRIX_MASKED extern const matrix_row_t matrix_mask[]; #endif @@ -56,6 +60,8 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values static matrix_row_t matrix[MATRIX_ROWS]; // debounced values +__attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } + __attribute__((weak)) void matrix_init_quantum(void) { matrix_init_kb(); } __attribute__((weak)) void matrix_scan_quantum(void) { matrix_scan_kb(); } @@ -167,7 +173,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Select row and wait for row selecton to stabilize select_row(current_row); - wait_us(30); + matrix_io_delay(); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { @@ -211,7 +217,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Select col and wait for col selecton to stabilize select_col(current_col); - wait_us(30); + matrix_io_delay(); // For each row... for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { diff --git a/quantum/quantum.h b/quantum/quantum.h index f5ac97379d33..8473f71a295e 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -177,6 +177,58 @@ typedef ioline_t pin_t; # define writePin(pin, level) ((level) ? writePinHigh(pin) : writePinLow(pin)) # define readPin(pin) palReadLine(pin) +#elif defined(PROTOCOL_ARM_ATSAM) +#include "samd51j18a.h" + +typedef uint8_t pin_t; + +#define SAMD_PORT(pin) ((pin & 0x20) >> 5) +#define SAMD_PIN(pin) (pin & 0x1f) +#define SAMD_PIN_MASK(pin) (1 << (pin & 0x1f)) + +#define setPinInput(pin) \ + do { \ + PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.INEN = 1; \ + PORT->Group[SAMD_PORT(pin)].DIRCLR.reg = SAMD_PIN_MASK(pin); \ + } while (0) + +#define setPinInputHigh(pin) \ + do { \ + PORT->Group[SAMD_PORT(pin)].DIRCLR.reg = SAMD_PIN_MASK(pin); \ + PORT->Group[SAMD_PORT(pin)].OUTSET.reg = SAMD_PIN_MASK(pin); \ + PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.INEN = 1; \ + PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.PULLEN = 1; \ + } while (0) + +#define setPinInputLow(pin) \ + do { \ + PORT->Group[SAMD_PORT(pin)].DIRCLR.reg = SAMD_PIN_MASK(pin); \ + PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \ + PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.INEN = 1; \ + PORT->Group[SAMD_PORT(pin)].PINCFG[SAMD_PIN(pin)].bit.PULLEN = 1; \ + } while (0) + +#define setPinOutput(pin) \ + do { \ + PORT->Group[SAMD_PORT(pin)].DIRSET.reg = SAMD_PIN_MASK(pin); \ + PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \ + } while (0) + +#define writePinHigh(pin) \ + do { \ + PORT->Group[SAMD_PORT(pin)].OUTSET.reg = SAMD_PIN_MASK(pin); \ + } while (0) + +#define writePinLow(pin) \ + do { \ + PORT->Group[SAMD_PORT(pin)].OUTCLR.reg = SAMD_PIN_MASK(pin); \ + } while (0) + +#define writePin(pin, level) ((level) ? (writePinHigh(pin)) : (writePinLow(pin))) + +#define readPin(pin) ((PORT->Group[SAMD_PORT(pin)].IN.reg & SAMD_PIN_MASK(pin)) != 0) + +#define togglePin(pin) (PORT->Group[SAMD_PORT(pin)].OUTTGL.reg = SAMD_PIN_MASK(pin)) #endif // Send string macros From f2d648b6b674dd95599c813e3f0d94232b739be9 Mon Sep 17 00:00:00 2001 From: zvecr Date: Thu, 30 Sep 2021 20:12:17 +0100 Subject: [PATCH 2/2] Swap to eager_pk? --- keyboards/massdrop/alt/config.h | 2 +- keyboards/massdrop/alt/rules.mk | 3 +- keyboards/massdrop/ctrl/config.h | 2 +- keyboards/massdrop/ctrl/rules.mk | 3 +- quantum/debounce/eager_pk.c | 64 +++++++++++++++++++++----------- quantum/debounce/sym_g.c | 2 +- quantum/matrix.c | 2 +- 7 files changed, 51 insertions(+), 27 deletions(-) diff --git a/keyboards/massdrop/alt/config.h b/keyboards/massdrop/alt/config.h index 45ef0689b4ca..99fa1d37e22a 100644 --- a/keyboards/massdrop/alt/config.h +++ b/keyboards/massdrop/alt/config.h @@ -92,7 +92,7 @@ along with this program. If not, see . #define DEBUG_BOOT_TRACING_PIN 23 /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ -#define DEBOUNCE 5 +#define DEBOUNCE 7 // Required BOOT key hold time (in ms) for restarting to bootloader -PS081419 #define BOOTKEY_HOLD_MS 2000 diff --git a/keyboards/massdrop/alt/rules.mk b/keyboards/massdrop/alt/rules.mk index d792bddbf459..d093877850f8 100644 --- a/keyboards/massdrop/alt/rules.mk +++ b/keyboards/massdrop/alt/rules.mk @@ -28,8 +28,9 @@ AUDIO_ENABLE = no # Audio output on port C6 FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) VIRTSER_ENABLE = no # USB Serial Driver -RAW_ENABLE = yes # Raw device +RAW_ENABLE = no # Raw device AUTO_SHIFT_ENABLE = no # Auto Shift +DEBOUNCE_TYPE = eager_pk # Custom RGB matrix handling RGB_MATRIX_ENABLE = custom diff --git a/keyboards/massdrop/ctrl/config.h b/keyboards/massdrop/ctrl/config.h index 7cd945827c59..b8d99cca69ef 100644 --- a/keyboards/massdrop/ctrl/config.h +++ b/keyboards/massdrop/ctrl/config.h @@ -91,7 +91,7 @@ along with this program. If not, see . #define DEBUG_BOOT_TRACING_PIN 23 /* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */ -#define DEBOUNCE 5 +#define DEBOUNCE 7 // Required BOOT key hold time (in ms) for restarting to bootloader -PS081419 #define BOOTKEY_HOLD_MS 2000 diff --git a/keyboards/massdrop/ctrl/rules.mk b/keyboards/massdrop/ctrl/rules.mk index 5d890eff15d0..0edb54a236d8 100644 --- a/keyboards/massdrop/ctrl/rules.mk +++ b/keyboards/massdrop/ctrl/rules.mk @@ -28,8 +28,9 @@ AUDIO_ENABLE = no # Audio output on port C6 FAUXCLICKY_ENABLE = no # Use buzzer to emulate clicky switches HD44780_ENABLE = no # Enable support for HD44780 based LCDs (+400) VIRTSER_ENABLE = no # USB Serial Driver -RAW_ENABLE = yes # Raw device +RAW_ENABLE = no # Raw device AUTO_SHIFT_ENABLE = no # Auto Shift +DEBOUNCE_TYPE = eager_pk # Custom RGB matrix handling RGB_MATRIX_ENABLE = custom diff --git a/quantum/debounce/eager_pk.c b/quantum/debounce/eager_pk.c index 76b978d059e8..b2cc5c70bb62 100644 --- a/quantum/debounce/eager_pk.c +++ b/quantum/debounce/eager_pk.c @@ -1,5 +1,6 @@ /* Copyright 2017 Alex Ong +Copyright 2021 Simon Arlott 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 the Free Software Foundation, either version 2 of the License, or @@ -27,25 +28,25 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred. # define DEBOUNCE 5 #endif -#if (MATRIX_COLS <= 8) -# define ROW_SHIFTER ((uint8_t)1) -#elif (MATRIX_COLS <= 16) -# define ROW_SHIFTER ((uint16_t)1) -#elif (MATRIX_COLS <= 32) -# define ROW_SHIFTER ((uint32_t)1) +// Maximum debounce: 255ms +#if DEBOUNCE > UINT8_MAX +# undef DEBOUNCE +# define DEBOUNCE UINT8_MAX #endif -#define debounce_counter_t uint8_t +#define ROW_SHIFTER ((matrix_row_t)1) + +typedef uint8_t debounce_counter_t; static debounce_counter_t *debounce_counters; +static uint32_t last_time; static bool counters_need_update; static bool matrix_need_update; -#define DEBOUNCE_ELAPSED 251 -#define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) +#define DEBOUNCE_ELAPSED 0 -void update_debounce_counters(uint8_t num_rows, uint8_t current_time); -void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); +static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time); +static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows); // we use num_rows rather than MATRIX_ROWS to support split keyboards void debounce_init(uint8_t num_rows) { @@ -58,27 +59,51 @@ void debounce_init(uint8_t num_rows) { } } +void debounce_free(void) { + free(debounce_counters); + debounce_counters = NULL; +} + void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { - uint8_t current_time = timer_read() % MAX_DEBOUNCE; + bool updated_last = false; + if (counters_need_update) { - update_debounce_counters(num_rows, current_time); + uint32_t now = timer_read32(); + uint32_t elapsed_time = TIMER_DIFF_32(now, last_time); + + last_time = now; + updated_last = true; + if (elapsed_time > UINT8_MAX) { + elapsed_time = UINT8_MAX; + } + + if (elapsed_time > 0) { + update_debounce_counters(num_rows, elapsed_time); + } } if (changed || matrix_need_update) { - transfer_matrix_values(raw, cooked, num_rows, current_time); + if (!updated_last) { + last_time = timer_read32(); + } + + transfer_matrix_values(raw, cooked, num_rows); } } // If the current time is > debounce counter, set the counter to enable input. -void update_debounce_counters(uint8_t num_rows, uint8_t current_time) { +static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) { counters_need_update = false; + matrix_need_update = false; debounce_counter_t *debounce_pointer = debounce_counters; for (uint8_t row = 0; row < num_rows; row++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) { if (*debounce_pointer != DEBOUNCE_ELAPSED) { - if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { + if (*debounce_pointer <= elapsed_time) { *debounce_pointer = DEBOUNCE_ELAPSED; + matrix_need_update = true; } else { + *debounce_pointer -= elapsed_time; counters_need_update = true; } } @@ -88,8 +113,7 @@ void update_debounce_counters(uint8_t num_rows, uint8_t current_time) { } // upload from raw_matrix to final matrix; -void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { - matrix_need_update = false; +static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { debounce_counter_t *debounce_pointer = debounce_counters; for (uint8_t row = 0; row < num_rows; row++) { matrix_row_t delta = raw[row] ^ cooked[row]; @@ -98,11 +122,9 @@ void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t n matrix_row_t col_mask = (ROW_SHIFTER << col); if (delta & col_mask) { if (*debounce_pointer == DEBOUNCE_ELAPSED) { - *debounce_pointer = current_time; + *debounce_pointer = DEBOUNCE; counters_need_update = true; existing_row ^= col_mask; // flip the bit. - } else { - matrix_need_update = true; } } debounce_pointer++; diff --git a/quantum/debounce/sym_g.c b/quantum/debounce/sym_g.c index d4078761d07f..ffaf5d3f0964 100644 --- a/quantum/debounce/sym_g.c +++ b/quantum/debounce/sym_g.c @@ -34,7 +34,7 @@ void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_ debouncing_time = timer_read32(); } - if (debouncing && timer_elapsed32(debouncing_time) > DEBOUNCE) { + if (debouncing && timer_elapsed32(debouncing_time) >= DEBOUNCE) { for (int i = 0; i < num_rows; i++) { cooked[i] = raw[i]; } diff --git a/quantum/matrix.c b/quantum/matrix.c index 5ba97abfecbc..72a8c4fd0f2b 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -42,7 +42,7 @@ along with this program. If not, see . #endif #ifndef MATRIX_IO_DELAY -# define MATRIX_IO_DELAY 30 +# define MATRIX_IO_DELAY 40 #endif #ifdef MATRIX_MASKED