Fix errors with matrix_output_unselect_delay function calls (#13645)

This commit is contained in:
Drashna Jaelre 2021-07-22 21:42:59 -07:00 committed by GitHub
parent 750f40566a
commit 816accda3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 58 deletions

View file

@ -32,7 +32,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# define MATRIX_DEBUG_DELAY_END() # define MATRIX_DEBUG_DELAY_END()
# define MATRIX_DEBUG_GAP() # define MATRIX_DEBUG_GAP()
#else #else
# define MATRIX_DEBUG_GAP() asm volatile("nop \n nop":::"memory") # define MATRIX_DEBUG_GAP() asm volatile("nop \n nop" ::: "memory")
#endif #endif
#ifndef MATRIX_IO_DELAY_ALWAYS #ifndef MATRIX_IO_DELAY_ALWAYS
@ -44,16 +44,16 @@ static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
# ifdef MATRIX_MUL_SELECT # ifdef MATRIX_MUL_SELECT
static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL; static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL;
# endif # endif
#endif #endif
#ifdef MATRIX_IO_DELAY_PORTS #ifdef MATRIX_IO_DELAY_PORTS
static const pin_t delay_ports[] = { MATRIX_IO_DELAY_PORTS }; static const pin_t delay_ports[] = {MATRIX_IO_DELAY_PORTS};
static const port_data_t delay_masks[] = { MATRIX_IO_DELAY_MASKS }; static const port_data_t delay_masks[] = {MATRIX_IO_DELAY_MASKS};
# ifdef MATRIX_IO_DELAY_MULSEL # ifdef MATRIX_IO_DELAY_MULSEL
static const uint8_t delay_sel[] = { MATRIX_IO_DELAY_MULSEL }; static const uint8_t delay_sel[] = {MATRIX_IO_DELAY_MULSEL};
# endif # endif
#endif #endif
@ -120,10 +120,10 @@ static void unselect_rows(void) {
} }
static void init_pins(void) { static void init_pins(void) {
#ifdef MATRIX_MUL_SELECT # ifdef MATRIX_MUL_SELECT
setPinOutput(MATRIX_MUL_SELECT); setPinOutput(MATRIX_MUL_SELECT);
writePinLow(MATRIX_MUL_SELECT); writePinLow(MATRIX_MUL_SELECT);
#endif # endif
unselect_rows(); unselect_rows();
for (uint8_t x = 0; x < MATRIX_COLS; x++) { for (uint8_t x = 0; x < MATRIX_COLS; x++) {
setPinInputHigh_atomic(col_pins[x]); setPinInputHigh_atomic(col_pins[x]);
@ -141,10 +141,10 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
// For each col... // For each col...
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
// Select the col pin to read (active low) // Select the col pin to read (active low)
#ifdef MATRIX_MUL_SELECT # ifdef MATRIX_MUL_SELECT
writePin(MATRIX_MUL_SELECT,col_sel[col_index]); writePin(MATRIX_MUL_SELECT, col_sel[col_index]);
waitInputPinDelay(); waitInputPinDelay();
#endif # endif
uint8_t pin_state = readPin(col_pins[col_index]); uint8_t pin_state = readPin(col_pins[col_index]);
// Populate the matrix row with the state of the col pin // Populate the matrix row with the state of the col pin
@ -153,37 +153,38 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
// Unselect row // Unselect row
unselect_row(current_row); unselect_row(current_row);
#ifdef MATRIX_IO_DELAY_PORTS # ifdef MATRIX_IO_DELAY_PORTS
if (current_row_value) { // wait for col signal to go HIGH if (current_row_value) { // wait for col signal to go HIGH
bool is_pressed; bool is_pressed;
do { do {
MATRIX_DEBUG_DELAY_START(); MATRIX_DEBUG_DELAY_START();
is_pressed = false; is_pressed = false;
for (uint8_t i = 0; i < sizeof(delay_ports)/sizeof(pin_t); i++ ) { for (uint8_t i = 0; i < sizeof(delay_ports) / sizeof(pin_t); i++) {
# ifdef MATRIX_IO_DELAY_MULSEL # ifdef MATRIX_IO_DELAY_MULSEL
writePin(MATRIX_MUL_SELECT, delay_sel[i]); writePin(MATRIX_MUL_SELECT, delay_sel[i]);
waitInputPinDelay(); waitInputPinDelay();
# endif # endif
is_pressed |= ( (readPort(delay_ports[i]) & delay_masks[i]) != delay_masks[i] ); is_pressed |= ((readPort(delay_ports[i]) & delay_masks[i]) != delay_masks[i]);
} }
MATRIX_DEBUG_DELAY_END(); MATRIX_DEBUG_DELAY_END();
} while (is_pressed); } while (is_pressed);
} }
#endif # endif
#ifdef MATRIX_IO_DELAY_ADAPTIVE # ifdef MATRIX_IO_DELAY_ADAPTIVE
if (current_row_value) { // wait for col signal to go HIGH if (current_row_value) { // wait for col signal to go HIGH
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
MATRIX_DEBUG_DELAY_START(); MATRIX_DEBUG_DELAY_START();
#ifdef MATRIX_MUL_SELECT # ifdef MATRIX_MUL_SELECT
writePin(MATRIX_MUL_SELECT,col_sel[col_index]); writePin(MATRIX_MUL_SELECT, col_sel[col_index]);
waitInputPinDelay(); waitInputPinDelay();
#endif # endif
while (readPin(col_pins[col_index]) == 0) {} while (readPin(col_pins[col_index]) == 0) {
}
MATRIX_DEBUG_DELAY_END(); MATRIX_DEBUG_DELAY_END();
} }
} }
#endif # endif
#ifdef MATRIX_IO_DELAY_ADAPTIVE2 # ifdef MATRIX_IO_DELAY_ADAPTIVE2
if (current_row_value) { // wait for col signal to go HIGH if (current_row_value) { // wait for col signal to go HIGH
pin_t state; pin_t state;
do { do {
@ -192,19 +193,19 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
MATRIX_DEBUG_DELAY_END(); MATRIX_DEBUG_DELAY_END();
MATRIX_DEBUG_DELAY_START(); MATRIX_DEBUG_DELAY_START();
#ifdef MATRIX_MUL_SELECT # ifdef MATRIX_MUL_SELECT
writePin(MATRIX_MUL_SELECT,col_sel[col_index]); writePin(MATRIX_MUL_SELECT, col_sel[col_index]);
waitInputPinDelay(); waitInputPinDelay();
#endif # endif
state |= (readPin(col_pins[col_index]) == 0); state |= (readPin(col_pins[col_index]) == 0);
} }
MATRIX_DEBUG_DELAY_END(); MATRIX_DEBUG_DELAY_END();
} while (state); } while (state);
} }
#endif # endif
if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) { if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) {
MATRIX_DEBUG_DELAY_START(); MATRIX_DEBUG_DELAY_START();
matrix_output_unselect_delay(); // wait for col signal to go HIGH matrix_output_unselect_delay(current_row, current_row_value != 0);
MATRIX_DEBUG_DELAY_END(); MATRIX_DEBUG_DELAY_END();
} }
@ -267,7 +268,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
// Unselect col // Unselect col
unselect_col(current_col); unselect_col(current_col);
if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) { if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) {
matrix_output_unselect_delay(); // wait for col signal to go HIGH matrix_output_unselect_delay(current_row, current_row_value != 0);
} }
return matrix_changed; return matrix_changed;
@ -311,11 +312,13 @@ uint8_t matrix_scan(void) {
changed |= read_rows_on_col(raw_matrix, current_col); changed |= read_rows_on_col(raw_matrix, current_col);
} }
#endif #endif
MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP(); MATRIX_DEBUG_SCAN_END();
MATRIX_DEBUG_GAP();
MATRIX_DEBUG_SCAN_START(); MATRIX_DEBUG_SCAN_START();
debounce(raw_matrix, matrix, MATRIX_ROWS, changed); debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP(); MATRIX_DEBUG_SCAN_END();
MATRIX_DEBUG_GAP();
MATRIX_DEBUG_SCAN_START(); MATRIX_DEBUG_SCAN_START();
matrix_scan_quantum(); matrix_scan_quantum();

View file

@ -4,9 +4,9 @@
/* In tmk_core/common/wait.h, the implementation for PROTOCOL_CHIBIOS /* In tmk_core/common/wait.h, the implementation for PROTOCOL_CHIBIOS
* calls 'chThdSleepMicroseconds(1)' when 'wait_us(0)'. * calls 'chThdSleepMicroseconds(1)' when 'wait_us(0)'.
* However, 'wait_us(0)' should do nothing. */ * However, 'wait_us(0)' should do nothing. */
void matrix_output_unselect_delay(void) { void matrix_output_unselect_delay(uint8_t line, bool key_pressed) {
# if !defined(MATRIX_IO_DELAY) || MATRIX_IO_DELAY > 0 # if !defined(MATRIX_IO_DELAY) || MATRIX_IO_DELAY > 0
matrix_io_delay(); matrix_io_delay();
# endif # endif
} }
#endif #endif

View file

@ -32,21 +32,21 @@ void matrix_init_kb(void) {
// ChibiOS enables the cycle counter in chcore_v7m.c: // ChibiOS enables the cycle counter in chcore_v7m.c:
// https://github.com/ChibiOS/ChibiOS/blob/b63023915c304092acb9f33bbab40f3ec07a7f0e/os/common/ports/ARMCMx/chcore_v7m.c#L263 // https://github.com/ChibiOS/ChibiOS/blob/b63023915c304092acb9f33bbab40f3ec07a7f0e/os/common/ports/ARMCMx/chcore_v7m.c#L263
static void delay_inline(const uint32_t cycles) { static void delay_inline(const uint32_t cycles) {
const uint32_t start = DWT->CYCCNT; const uint32_t start = DWT->CYCCNT;
while ((DWT->CYCCNT - start) < cycles) { while ((DWT->CYCCNT - start) < cycles) {
// busy-loop until time has passed // busy-loop until time has passed
} }
} }
void matrix_output_unselect_delay(void) { void matrix_output_unselect_delay(uint8_t line, bool key_pressed) {
// Use the cycle counter to do precise timing in microseconds. The ChibiOS // Use the cycle counter to do precise timing in microseconds. The ChibiOS
// thread sleep functions only allow sleep durations starting at 1 tick, which // thread sleep functions only allow sleep durations starting at 1 tick, which
// is 100μs in our configuration. // is 100μs in our configuration.
// Empirically: e.g. 5μs is not enough, will result in keys that dont work // Empirically: e.g. 5μs is not enough, will result in keys that dont work
// and ghost key presses. 10μs seems to work well. // and ghost key presses. 10μs seems to work well.
// 600 cycles at 0.6 cycles/ns == 1μs // 600 cycles at 0.6 cycles/ns == 1μs
const uint32_t cycles_per_us = 600; const uint32_t cycles_per_us = 600;
delay_inline(10 * cycles_per_us); delay_inline(10 * cycles_per_us);
} }

View file

@ -19,7 +19,7 @@
#include "transactions.h" #include "transactions.h"
#define ERROR_DISCONNECT_COUNT 5 #define ERROR_DISCONNECT_COUNT 5
#define ROWS_PER_HAND (MATRIX_ROWS / 2) #define ROWS_PER_HAND (MATRIX_ROWS / 2)
static const pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS; static const pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS;
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
@ -141,7 +141,7 @@ uint8_t matrix_scan(void) {
/* Drive row pin high again. */ /* Drive row pin high again. */
ATOMIC_BLOCK_FORCEON { writePinHigh(row_pins[row_idx]); } ATOMIC_BLOCK_FORCEON { writePinHigh(row_pins[row_idx]); }
matrix_output_unselect_delay(); matrix_output_unselect_delay(row_idx, row_pins[row_idx] != 0);
} }
if (memcmp(raw_matrix, current_matrix, sizeof(current_matrix)) != 0) { if (memcmp(raw_matrix, current_matrix, sizeof(current_matrix)) != 0) {
@ -153,4 +153,4 @@ uint8_t matrix_scan(void) {
bool remote_changed = matrix_post_scan(); bool remote_changed = matrix_post_scan();
return (uint8_t)(local_changed || remote_changed); return (uint8_t)(local_changed || remote_changed);
} }

View file

@ -12,24 +12,22 @@
#include "common_oled.h" #include "common_oled.h"
#include <transactions.h> #include <transactions.h>
void keyboard_post_init_kb(void) void keyboard_post_init_kb(void) {
{
touch_encoder_init(); touch_encoder_init();
transaction_register_rpc(TOUCH_ENCODER_SYNC, touch_encoder_slave_sync); transaction_register_rpc(TOUCH_ENCODER_SYNC, touch_encoder_slave_sync);
transaction_register_rpc(RGB_MENU_SYNC, rgb_menu_slave_sync); transaction_register_rpc(RGB_MENU_SYNC, rgb_menu_slave_sync);
keyboard_post_init_user(); keyboard_post_init_user();
} }
void housekeeping_task_kb(void) void housekeeping_task_kb(void) {
{
touch_encoder_update(TOUCH_ENCODER_SYNC); touch_encoder_update(TOUCH_ENCODER_SYNC);
rgb_menu_update(RGB_MENU_SYNC); rgb_menu_update(RGB_MENU_SYNC);
} }
#if defined(BUSY_WAIT) #if defined(BUSY_WAIT)
void matrix_output_unselect_delay(void) { void matrix_output_unselect_delay(uint8_t line, bool key_pressed) {
for (int32_t i = 0; i < BUSY_WAIT_INSTRUCTIONS; i++) { for (int32_t i = 0; i < BUSY_WAIT_INSTRUCTIONS; i++) {
__asm__ volatile("nop" ::: "memory"); __asm__ volatile("nop" ::: "memory");
} }
} }
#endif #endif