From ecc36d8dc7ccb2dcdedcc89bedb6898e2f15f0de Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 27 Jun 2020 20:27:08 +0900 Subject: [PATCH 01/17] fix matrix_io_delay() timing in quantum/matrix.c --- quantum/matrix.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index cab0d2ddcafa..807dd68f8e9a 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -109,7 +109,6 @@ 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); - matrix_io_delay(); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { @@ -122,6 +121,9 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); + if( current_row + 1 < MATRIX_ROWS ) { + matrix_io_delay(); + } // If the row has changed, store the row and return the changed flag. if (current_matrix[current_row] != current_row_value) { @@ -159,7 +161,6 @@ 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); - matrix_io_delay(); // For each row... for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { @@ -185,6 +186,9 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); + if( current_col + 1 < MATRIX_COLS ) { + matrix_io_delay(); + } return matrix_changed; } From 45fe92cac17e9e9f2cef28b98af99a58f553e989 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Thu, 2 Jul 2020 13:24:23 +0900 Subject: [PATCH 02/17] Updated comments explaining the need for matrix_io_delay() in quantum/matrix.c --- quantum/matrix.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 807dd68f8e9a..662ae20269cc 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -107,7 +107,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Start with a clear matrix row matrix_row_t current_row_value = 0; - // Select row and wait for row selecton to stabilize + // Select row select_row(current_row); // For each col... @@ -122,7 +122,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); if( current_row + 1 < MATRIX_ROWS ) { - matrix_io_delay(); + matrix_io_delay(); // wait for row signal to HIGH } // If the row has changed, store the row and return the changed flag. @@ -159,7 +159,7 @@ static void init_pins(void) { static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { bool matrix_changed = false; - // Select col and wait for col selecton to stabilize + // Select col select_col(current_col); // For each row... @@ -187,7 +187,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); if( current_col + 1 < MATRIX_COLS ) { - matrix_io_delay(); + matrix_io_delay(); // wait for col signal to HIGH } return matrix_changed; From b806d574901721ff73329430a0a8235dea30e394 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Thu, 2 Jul 2020 13:50:24 +0900 Subject: [PATCH 03/17] fix matrix_io_delay() timing in quantum/split_common/matrix.c --- quantum/split_common/matrix.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index cd5a024c3df4..0611482d7fa4 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -120,9 +120,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Start with a clear matrix row matrix_row_t current_row_value = 0; - // Select row and wait for row selecton to stabilize + // Select row select_row(current_row); - matrix_io_delay(); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { @@ -135,6 +134,9 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); + if( current_row + 1 < MATRIX_ROWS ) { + matrix_io_delay(); // wait for row signal to HIGH + } // If the row has changed, store the row and return the changed flag. if (current_matrix[current_row] != current_row_value) { @@ -170,9 +172,8 @@ static void init_pins(void) { static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { bool matrix_changed = false; - // Select col and wait for col selecton to stabilize + // Select col select_col(current_col); - matrix_io_delay(); // For each row... for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { @@ -198,6 +199,9 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); + if( current_col + 1 < MATRIX_COLS ) { + matrix_io_delay(); // wait for col signal to HIGH + } return matrix_changed; } From 0f28e2c3699499af46b820d0b2d9f9998bad884a Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:19:24 +0900 Subject: [PATCH 04/17] Update quantum/matrix.c Co-authored-by: Ryan --- quantum/matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 662ae20269cc..e12ea753697b 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -121,8 +121,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); - if( current_row + 1 < MATRIX_ROWS ) { - matrix_io_delay(); // wait for row signal to HIGH + if (current_row + 1 < MATRIX_ROWS) { + matrix_io_delay(); // wait for row signal to go HIGH } // If the row has changed, store the row and return the changed flag. From ab50411fae620dd4c9310e5bfbe413558ae8f7ce Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:19:40 +0900 Subject: [PATCH 05/17] Update quantum/split_common/matrix.c Co-authored-by: Ryan --- quantum/split_common/matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 0611482d7fa4..76b3bd3a59b0 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -199,8 +199,8 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); - if( current_col + 1 < MATRIX_COLS ) { - matrix_io_delay(); // wait for col signal to HIGH + if (current_col + 1 < MATRIX_COLS) { + matrix_io_delay(); // wait for col signal to go HIGH } return matrix_changed; From b8093393f8d646079b1a794c32f9739283420845 Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:20:05 +0900 Subject: [PATCH 06/17] Update quantum/matrix.c Co-authored-by: Ryan --- quantum/matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index e12ea753697b..697b63a729dc 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -186,8 +186,8 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); - if( current_col + 1 < MATRIX_COLS ) { - matrix_io_delay(); // wait for col signal to HIGH + if (current_col + 1 < MATRIX_COLS) { + matrix_io_delay(); // wait for col signal to go HIGH } return matrix_changed; From 0614cefc1596b4da49d1ebcff19dad582643b2f7 Mon Sep 17 00:00:00 2001 From: Takeshi ISHII <2170248+mtei@users.noreply.github.com> Date: Thu, 2 Jul 2020 18:20:18 +0900 Subject: [PATCH 07/17] Update quantum/split_common/matrix.c Co-authored-by: Ryan --- quantum/split_common/matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 76b3bd3a59b0..0ea53030b90b 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -134,8 +134,8 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); - if( current_row + 1 < MATRIX_ROWS ) { - matrix_io_delay(); // wait for row signal to HIGH + if (current_row + 1 < MATRIX_ROWS) { + matrix_io_delay(); // wait for row signal to go HIGH } // If the row has changed, store the row and return the changed flag. From c5ec5b718a53ff96122cadadcfc539e5287308d1 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 12:00:13 +0900 Subject: [PATCH 08/17] add waitOutputPinValid() and wait_cpuclock() into quantum/quantum.h and tmk_core/common/wait.h --- quantum/quantum.h | 7 ++++++ tmk_core/common/wait.h | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/quantum/quantum.h b/quantum/quantum.h index cb0af306ac93..e70c4dd28c78 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -208,6 +208,8 @@ typedef uint8_t pin_t; # define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF)) +# define waitOutputPinValid() wait_cpuclock(1) + #elif defined(PROTOCOL_CHIBIOS) typedef ioline_t pin_t; @@ -223,6 +225,11 @@ typedef ioline_t pin_t; # define readPin(pin) palReadLine(pin) # define togglePin(pin) palToggleLine(pin) + +#endif + +#if defined(__ARMEL__) || defined(__ARMEB__) +# define waitOutputPinValid() wait_cpuclock(STM32_SYSCLK/1000000L / 4) #endif // Atomic macro to help make GPIO and other controls atomic. diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h index c82cd2d65a29..26bd0675680d 100644 --- a/tmk_core/common/wait.h +++ b/tmk_core/common/wait.h @@ -7,10 +7,62 @@ extern "C" { #endif +#if defined(__ARMEL__) || defined(__ARMEB__) +# ifndef __OPTIMIZE__ +# error "Compiler optimizations disabled; wait_cpuclock() won't work as designed" +# endif + +# define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t" + +#define WAIT_CPUCLOCK_DEFINED + +__attribute__((always_inline)) +static inline void wait_cpuclock(unsigned int n) { /* n: 1..135 */ + /* The argument n must be a constant expression. + * That way, compiler optimization will remove unnecessary code. */ + if (n < 1) { return; } + if (n > 8) { + unsigned int n8 = n/8; + n = n - n8*8; + switch (n8) { + case 16: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 15: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 14: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 13: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 12: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 11: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 10: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 9: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 8: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 7: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 6: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 5: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 4: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 3: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 2: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 1: asm volatile (CLOCK_DELAY_NOP8::: "memory"); + case 0: break; + } + } + switch (n) { + case 8: asm volatile ("nop"::: "memory"); + case 7: asm volatile ("nop"::: "memory"); + case 6: asm volatile ("nop"::: "memory"); + case 5: asm volatile ("nop"::: "memory"); + case 4: asm volatile ("nop"::: "memory"); + case 3: asm volatile ("nop"::: "memory"); + case 2: asm volatile ("nop"::: "memory"); + case 1: asm volatile ("nop"::: "memory"); + case 0: break; + } +} +#endif + #if defined(__AVR__) # include # define wait_ms(ms) _delay_ms(ms) # define wait_us(us) _delay_us(us) +# define wait_cpuclock(x) __builtin_avr_delay_cycles(x) #elif defined PROTOCOL_CHIBIOS # include "ch.h" # define wait_ms(ms) \ From dfb4c54a6d56103492cfbc1d930b6decb60f9cf2 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 12:36:29 +0900 Subject: [PATCH 09/17] add matrix_output_select_delay() and matrix_output_unselect_delay() --- quantum/matrix.c | 6 ++++-- quantum/matrix_common.c | 4 ++++ quantum/split_common/matrix.c | 6 ++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 697b63a729dc..8778fb036477 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -109,6 +109,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Select row select_row(current_row); + matrix_output_select_delay(); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { @@ -122,7 +123,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); if (current_row + 1 < MATRIX_ROWS) { - matrix_io_delay(); // wait for row signal to go HIGH + matrix_output_unselect_delay(); // wait for row signal to go HIGH } // If the row has changed, store the row and return the changed flag. @@ -161,6 +162,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Select col select_col(current_col); + matrix_output_select_delay(); // For each row... for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { @@ -187,7 +189,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); if (current_col + 1 < MATRIX_COLS) { - matrix_io_delay(); // wait for col signal to go HIGH + matrix_output_unselect_delay(); // wait for row signal to go HIGH } return matrix_changed; diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index 15f1e0e82e76..d2909ec6d3b6 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c @@ -83,8 +83,12 @@ uint8_t matrix_key_count(void) { return count; } +/* `matrix_io_delay ()` exists for backwards compatibility. From now on, use matrix_output_unselect_delay(). */ __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } +__attribute__((weak)) void matrix_output_select_delay(void) { waitOutputPinValid(); } +__attribute__((weak)) void matrix_output_unselect_delay(void) { matrix_io_delay(); } + // CUSTOM MATRIX 'LITE' __attribute__((weak)) void matrix_init_custom(void) {} diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 0ea53030b90b..5d2c4caba11e 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -122,6 +122,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Select row select_row(current_row); + matrix_output_select_delay(); // For each col... for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { @@ -135,7 +136,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) // Unselect row unselect_row(current_row); if (current_row + 1 < MATRIX_ROWS) { - matrix_io_delay(); // wait for row signal to go HIGH + matrix_output_unselect_delay(); // wait for row signal to go HIGH } // If the row has changed, store the row and return the changed flag. @@ -174,6 +175,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Select col select_col(current_col); + matrix_output_select_delay(); // For each row... for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { @@ -200,7 +202,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); if (current_col + 1 < MATRIX_COLS) { - matrix_io_delay(); // wait for col signal to go HIGH + matrix_output_unselect_delay(); // wait for row signal to go HIGH } return matrix_changed; From 77a2cf3cf83d42dc8c4aed2b4e0671e228568563 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 13:10:33 +0900 Subject: [PATCH 10/17] fix quantum/matrix_common.c, tmk_core/common/matrix.h --- quantum/matrix_common.c | 1 + tmk_core/common/matrix.h | 3 +++ 2 files changed, 4 insertions(+) diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index d2909ec6d3b6..d5a857084464 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c @@ -1,3 +1,4 @@ +#include "quantum.h" #include "matrix.h" #include "debounce.h" #include "wait.h" diff --git a/tmk_core/common/matrix.h b/tmk_core/common/matrix.h index 31ec844302c8..489333f60e81 100644 --- a/tmk_core/common/matrix.h +++ b/tmk_core/common/matrix.h @@ -57,6 +57,9 @@ matrix_row_t matrix_get_row(uint8_t row); /* print matrix for debug */ void matrix_print(void); /* delay between changing matrix pin state and reading values */ +void matrix_output_select_delay(void); +void matrix_output_unselect_delay(void); +/* only for backwards compatibility. delay between changing matrix pin state and reading values */ void matrix_io_delay(void); /* power control */ From e7fc73b4b787a3ed1ce17327c26c5da9afc0d1e2 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 13:49:21 +0900 Subject: [PATCH 11/17] fix tmk_core/common/wait.h --- tmk_core/common/wait.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h index 26bd0675680d..7d0c0e3b70a4 100644 --- a/tmk_core/common/wait.h +++ b/tmk_core/common/wait.h @@ -9,7 +9,7 @@ extern "C" { #if defined(__ARMEL__) || defined(__ARMEB__) # ifndef __OPTIMIZE__ -# error "Compiler optimizations disabled; wait_cpuclock() won't work as designed" +# warning "Compiler optimizations disabled; wait_cpuclock() won't work as designed" # endif # define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t" From c8ef016d260a313f11cc77850c840c49e0e0aa3d Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 18:04:49 +0900 Subject: [PATCH 12/17] fix quantum/quantum.h, tmk_core/common/wait.h --- quantum/quantum.h | 7 ++++++- tmk_core/common/wait.h | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/quantum/quantum.h b/quantum/quantum.h index e70c4dd28c78..7f13cc7cfa9e 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -229,7 +229,12 @@ typedef ioline_t pin_t; #endif #if defined(__ARMEL__) || defined(__ARMEB__) -# define waitOutputPinValid() wait_cpuclock(STM32_SYSCLK/1000000L / 4) +# if defined(STM32_SYSCLK) +# define WAITOUTPUTPINVALID_DELAY (STM32_SYSCLK/1000000L / 4) +# elif defined(KINETIS_SYSCLK_FREQUENCY) +# define WAITOUTPUTPINVALID_DELAY (KINETIS_SYSCLK_FREQUENCY/1000000L / 4) +# endif +# define waitOutputPinValid() wait_cpuclock(WAITOUTPUTPINVALID_DELAY) #endif // Atomic macro to help make GPIO and other controls atomic. diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h index 7d0c0e3b70a4..447bb9275883 100644 --- a/tmk_core/common/wait.h +++ b/tmk_core/common/wait.h @@ -9,7 +9,7 @@ extern "C" { #if defined(__ARMEL__) || defined(__ARMEB__) # ifndef __OPTIMIZE__ -# warning "Compiler optimizations disabled; wait_cpuclock() won't work as designed" +# pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed" # endif # define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t" From b16caee3c7ee1e14f3352107e813b1f3c60def38 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 20:01:50 +0900 Subject: [PATCH 13/17] waitOutputPinValid() rename to waitInputPinDelay() in quantum/quantum.h. --- quantum/quantum.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/quantum/quantum.h b/quantum/quantum.h index 7f13cc7cfa9e..ea1432ba2f01 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -208,7 +208,11 @@ typedef uint8_t pin_t; # define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF)) -# define waitOutputPinValid() wait_cpuclock(1) +/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal. */ +# if !defined(GPIO_INPUT_PIN_DELAY) +# define GPIO_INPUT_PIN_DELAY 1 +# endif +# define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) #elif defined(PROTOCOL_CHIBIOS) typedef ioline_t pin_t; @@ -229,12 +233,24 @@ typedef ioline_t pin_t; #endif #if defined(__ARMEL__) || defined(__ARMEB__) -# if defined(STM32_SYSCLK) -# define WAITOUTPUTPINVALID_DELAY (STM32_SYSCLK/1000000L / 4) -# elif defined(KINETIS_SYSCLK_FREQUENCY) -# define WAITOUTPUTPINVALID_DELAY (KINETIS_SYSCLK_FREQUENCY/1000000L / 4) +/* For GPIOs on ARM-based MCUs, the input pins are sampled by the clock of the bus + * to which the GPIO is connected. + * The connected buses differ depending on the various series of MCUs. + * And since the instruction execution clock of the CPU and the bus clock of GPIO are different, + * there is a delay of several clocks to read the change of the input signal. + * + * Define this delay with the GPIO_INPUT_PIN_DELAY macro. + * If the GPIO_INPUT_PIN_DELAY macro is not defined, the following default values will be used. + * (A fairly large value of 0.25 microseconds is set.) + */ +# if !defined(GPIO_INPUT_PIN_DELAY) +# if defined(STM32_SYSCLK) +# define GPIO_INPUT_PIN_DELAY (STM32_SYSCLK/1000000L / 4) +# elif defined(KINETIS_SYSCLK_FREQUENCY) +# define GPIO_INPUT_PIN_DELAY (KINETIS_SYSCLK_FREQUENCY/1000000L / 4) +# endif # endif -# define waitOutputPinValid() wait_cpuclock(WAITOUTPUTPINVALID_DELAY) +# define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY) #endif // Atomic macro to help make GPIO and other controls atomic. From c4e40c7cd6046acd51b67fea145eee5e96977032 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Sat, 14 Nov 2020 20:08:55 +0900 Subject: [PATCH 14/17] waitOutputPinValid() rename to waitInputPinDelay() in quantum/matrix_common.c --- quantum/matrix_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index d5a857084464..01d2b38e5e51 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c @@ -87,7 +87,7 @@ uint8_t matrix_key_count(void) { /* `matrix_io_delay ()` exists for backwards compatibility. From now on, use matrix_output_unselect_delay(). */ __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } -__attribute__((weak)) void matrix_output_select_delay(void) { waitOutputPinValid(); } +__attribute__((weak)) void matrix_output_select_delay(void) { waitInputPinDelay(); } __attribute__((weak)) void matrix_output_unselect_delay(void) { matrix_io_delay(); } // CUSTOM MATRIX 'LITE' From 7fb62934a9bc59f17fc67e94f437724f831d3f44 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Mon, 16 Nov 2020 17:15:10 +0900 Subject: [PATCH 15/17] update tmk_core/common/wait.h --- tmk_core/common/wait.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tmk_core/common/wait.h b/tmk_core/common/wait.h index 447bb9275883..e224ff38ca16 100644 --- a/tmk_core/common/wait.h +++ b/tmk_core/common/wait.h @@ -12,12 +12,12 @@ extern "C" { # pragma message "Compiler optimizations disabled; wait_cpuclock() won't work as designed" # endif -# define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t" +# define wait_cpuclock(x) wait_cpuclock_allnop(x) -#define WAIT_CPUCLOCK_DEFINED +# define CLOCK_DELAY_NOP8 "nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t nop\n\t" __attribute__((always_inline)) -static inline void wait_cpuclock(unsigned int n) { /* n: 1..135 */ +static inline void wait_cpuclock_allnop(unsigned int n) { /* n: 1..135 */ /* The argument n must be a constant expression. * That way, compiler optimization will remove unnecessary code. */ if (n < 1) { return; } From 37744309075da3e0bb513058199817ecc26b9dea Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Mon, 16 Nov 2020 17:17:09 +0900 Subject: [PATCH 16/17] update comment in quantum/matrix.c, quantum/split_common/matrix.c --- quantum/matrix.c | 2 +- quantum/split_common/matrix.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 8778fb036477..2eec1259831f 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -189,7 +189,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); if (current_col + 1 < MATRIX_COLS) { - matrix_output_unselect_delay(); // wait for row signal to go HIGH + matrix_output_unselect_delay(); // wait for col signal to go HIGH } return matrix_changed; diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 5d2c4caba11e..24fb2ae53b03 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -202,7 +202,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) // Unselect col unselect_col(current_col); if (current_col + 1 < MATRIX_COLS) { - matrix_output_unselect_delay(); // wait for row signal to go HIGH + matrix_output_unselect_delay(); // wait for col signal to go HIGH } return matrix_changed; From 9ce7c0a35e487d7450ab2c6897bd2c91e0c75362 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Mon, 23 Nov 2020 15:12:20 +0900 Subject: [PATCH 17/17] update quantum/quantum.h: Make more margin in the GPIO_INPUT_PIN_DELAY default value. --- quantum/quantum.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/quantum/quantum.h b/quantum/quantum.h index ea1432ba2f01..60523feebdae 100644 --- a/quantum/quantum.h +++ b/quantum/quantum.h @@ -208,9 +208,10 @@ typedef uint8_t pin_t; # define togglePin(pin) (PORTx_ADDRESS(pin) ^= _BV((pin)&0xF)) -/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal. */ +/* The AVR series GPIOs have a one clock read delay for changes in the digital input signal. + * But here's more margin to make it two clocks. */ # if !defined(GPIO_INPUT_PIN_DELAY) -# define GPIO_INPUT_PIN_DELAY 1 +# define GPIO_INPUT_PIN_DELAY 2 # endif # define waitInputPinDelay() wait_cpuclock(GPIO_INPUT_PIN_DELAY)