From 9ba09ec8ebb21778318316c19611ced9856e44b8 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Thu, 5 Jul 2018 11:50:13 -0700 Subject: [PATCH 1/3] Convert key_state_t to a bit field --- lib/agent | 2 +- right/src/key_states.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/agent b/lib/agent index 15df8d7..32d9635 160000 --- a/lib/agent +++ b/lib/agent @@ -1 +1 @@ -Subproject commit 15df8d71299e0547d953874d1a8d21e9147a396f +Subproject commit 32d9635b340e4dfbb12fdcfc909ec89e488ce56a diff --git a/right/src/key_states.h b/right/src/key_states.h index 847aa24..9f48304 100644 --- a/right/src/key_states.h +++ b/right/src/key_states.h @@ -10,10 +10,10 @@ // Typedefs: typedef struct { - bool previous; - bool current; - bool suppressed; uint8_t debounceCounter; + bool previous : 1; + bool current : 1; + bool suppressed : 1; } key_state_t; // Variables: From 27d12ea31fcf4d26bb6f0449625fc4679e95646d Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Thu, 5 Jul 2018 12:33:55 -0700 Subject: [PATCH 2/3] Remove the debouncing interrupt --- right/src/init_peripherals.c | 3 --- right/src/key_debouncer.c | 32 -------------------------------- right/src/key_debouncer.h | 18 ------------------ right/src/key_states.h | 3 ++- right/src/peripherals/pit.h | 4 ---- right/src/usb_report_updater.c | 12 ++++++++---- shared/key_matrix.h | 1 + 7 files changed, 11 insertions(+), 62 deletions(-) delete mode 100644 right/src/key_debouncer.c delete mode 100644 right/src/key_debouncer.h diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index 7efcb20..35f34e2 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -12,7 +12,6 @@ #include "init_peripherals.h" #include "eeprom.h" #include "timer.h" -#include "key_debouncer.h" #include "usb_api.h" #include "slave_scheduler.h" #include "bootloader/wormhole.h" @@ -67,7 +66,6 @@ static void initInterruptPriorities(void) NVIC_SetPriority(I2C_EEPROM_BUS_IRQ_ID, 0); NVIC_SetPriority(PIT_TIMER_IRQ_ID, 3); NVIC_SetPriority(PIT_KEY_SCANNER_IRQ_ID, 4); - NVIC_SetPriority(PIT_KEY_DEBOUNCER_IRQ_ID, 4); NVIC_SetPriority(I2C_MAIN_BUS_IRQ_ID, 4); NVIC_SetPriority(USB_IRQ_ID, 4); } @@ -159,6 +157,5 @@ void InitPeripherals(void) TestLed_Init(); LedPwm_Init(); InitI2cWatchdog(); - InitKeyDebouncer(); EEPROM_Init(); } diff --git a/right/src/key_debouncer.c b/right/src/key_debouncer.c deleted file mode 100644 index 0ec19e7..0000000 --- a/right/src/key_debouncer.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "key_debouncer.h" -#include "fsl_pit.h" -#include "slot.h" -#include "module.h" -#include "key_states.h" -#include "peripherals/test_led.h" - -void PIT_KEY_DEBOUNCER_HANDLER(void) -{ - TestLed_Toggle(); - for (uint8_t slotId=0; slotIddebounceCounter) { - keyState->current = keyState->previous; + if (keyState->debouncing) { + if ((uint8_t)(Timer_GetCurrentTime() - keyState->timestamp) > KEY_BOUNCE_TIME_MSEC) { + keyState->debouncing = false; + } else { + keyState->current = keyState->previous; + } } else if (!keyState->previous && keyState->current) { - keyState->debounceCounter = KEY_DEBOUNCER_TIMEOUT_MSEC + 1; + keyState->timestamp = Timer_GetCurrentTime(); + keyState->debouncing = true; } if (keyState->current) { diff --git a/shared/key_matrix.h b/shared/key_matrix.h index e58e12c..abe221b 100644 --- a/shared/key_matrix.h +++ b/shared/key_matrix.h @@ -9,6 +9,7 @@ // Macros: #define MAX_KEYS_IN_MATRIX 100 + #define KEY_BOUNCE_TIME_MSEC 100 // Typedefs: From 5a137392ee57b788240901644b85382ca5accfce Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Thu, 5 Jul 2018 12:43:38 -0700 Subject: [PATCH 3/3] Remove the key scanning interrupt --- right/src/init_peripherals.c | 1 - right/src/key_scanner.c | 22 ------------------- right/src/key_scanner.h | 21 ------------------ right/src/main.c | 5 +++-- right/src/peripherals/pit.h | 4 ---- right/src/right_key_matrix.c | 2 ++ right/src/right_key_matrix.h | 1 + .../usb_command_get_debug_buffer.c | 4 ++-- 8 files changed, 8 insertions(+), 52 deletions(-) delete mode 100644 right/src/key_scanner.c delete mode 100644 right/src/key_scanner.h diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index 35f34e2..8294e02 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -65,7 +65,6 @@ static void initInterruptPriorities(void) NVIC_SetPriority(PIT_I2C_WATCHDOG_IRQ_ID, 1); NVIC_SetPriority(I2C_EEPROM_BUS_IRQ_ID, 0); NVIC_SetPriority(PIT_TIMER_IRQ_ID, 3); - NVIC_SetPriority(PIT_KEY_SCANNER_IRQ_ID, 4); NVIC_SetPriority(I2C_MAIN_BUS_IRQ_ID, 4); NVIC_SetPriority(USB_IRQ_ID, 4); } diff --git a/right/src/key_scanner.c b/right/src/key_scanner.c deleted file mode 100644 index e2d10bd..0000000 --- a/right/src/key_scanner.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "fsl_pit.h" -#include "key_scanner.h" - -uint32_t KeyScannerCounter; - -void PIT_KEY_SCANNER_HANDLER(void) -{ - KeyMatrix_ScanRow(&RightKeyMatrix); - KeyScannerCounter++; - PIT_ClearStatusFlags(PIT, PIT_KEY_SCANNER_CHANNEL, PIT_TFLG_TIF_MASK); -} - -void InitKeyScanner(void) -{ - pit_config_t pitConfig; - PIT_GetDefaultConfig(&pitConfig); - PIT_Init(PIT, &pitConfig); - PIT_SetTimerPeriod(PIT, PIT_KEY_SCANNER_CHANNEL, USEC_TO_COUNT(KEY_SCANNER_INTERVAL_USEC, PIT_SOURCE_CLOCK)); - PIT_EnableInterrupts(PIT, PIT_KEY_SCANNER_CHANNEL, kPIT_TimerInterruptEnable); - EnableIRQ(PIT_KEY_SCANNER_IRQ_ID); - PIT_StartTimer(PIT, PIT_KEY_SCANNER_CHANNEL); -} diff --git a/right/src/key_scanner.h b/right/src/key_scanner.h deleted file mode 100644 index f239818..0000000 --- a/right/src/key_scanner.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __KEY_SCANNER_H__ -#define __KEY_SCANNER_H__ - -// Includes: - - #include "peripherals/pit.h" - #include "right_key_matrix.h" - -// Macros: - - #define KEY_SCANNER_INTERVAL_USEC (1000 / RIGHT_KEY_MATRIX_ROWS_NUM) - -// Variables: - - extern uint32_t KeyScannerCounter; - -// Functions: - - void InitKeyScanner(void); - -#endif diff --git a/right/src/main.c b/right/src/main.c index c8918cf..95c494e 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -6,7 +6,7 @@ #include "bus_pal_hardware.h" #include "command.h" #include "eeprom.h" -#include "key_scanner.h" +#include "right_key_matrix.h" #include "usb_commands/usb_command_apply_config.h" #include "peripherals/reset_button.h" #include "config_parser/config_globals.h" @@ -44,7 +44,6 @@ int main(void) } else { InitSlaveScheduler(); KeyMatrix_Init(&RightKeyMatrix); - InitKeyScanner(); InitUsb(); while (1) { @@ -52,6 +51,8 @@ int main(void) UsbCommand_ApplyConfig(); IsConfigInitialized = true; } + KeyMatrix_ScanRow(&RightKeyMatrix); + ++MatrixScanCounter; UpdateUsbReports(); __WFI(); } diff --git a/right/src/peripherals/pit.h b/right/src/peripherals/pit.h index 0b22720..e720da8 100644 --- a/right/src/peripherals/pit.h +++ b/right/src/peripherals/pit.h @@ -17,8 +17,4 @@ #define PIT_TIMER_IRQ_ID PIT1_IRQn #define PIT_TIMER_CHANNEL kPIT_Chnl_1 - #define PIT_KEY_SCANNER_HANDLER PIT2_IRQHandler - #define PIT_KEY_SCANNER_IRQ_ID PIT2_IRQn - #define PIT_KEY_SCANNER_CHANNEL kPIT_Chnl_2 - #endif diff --git a/right/src/right_key_matrix.c b/right/src/right_key_matrix.c index e9786b0..4a0fd2f 100644 --- a/right/src/right_key_matrix.c +++ b/right/src/right_key_matrix.c @@ -1,5 +1,7 @@ #include "right_key_matrix.h" +uint32_t MatrixScanCounter; + key_matrix_t RightKeyMatrix = { .colNum = RIGHT_KEY_MATRIX_COLS_NUM, .rowNum = RIGHT_KEY_MATRIX_ROWS_NUM, diff --git a/right/src/right_key_matrix.h b/right/src/right_key_matrix.h index fe4b84d..8fba355 100644 --- a/right/src/right_key_matrix.h +++ b/right/src/right_key_matrix.h @@ -14,5 +14,6 @@ // Variables: extern key_matrix_t RightKeyMatrix; + extern uint32_t MatrixScanCounter; #endif diff --git a/right/src/usb_commands/usb_command_get_debug_buffer.c b/right/src/usb_commands/usb_command_get_debug_buffer.c index 92d0073..2347594 100644 --- a/right/src/usb_commands/usb_command_get_debug_buffer.c +++ b/right/src/usb_commands/usb_command_get_debug_buffer.c @@ -5,7 +5,7 @@ #include "i2c_watchdog.h" #include "buffer.h" #include "timer.h" -#include "key_scanner.h" +#include "right_key_matrix.h" #include "usb_report_updater.h" #include "usb_interfaces/usb_interface_basic_keyboard.h" #include "usb_interfaces/usb_interface_media_keyboard.h" @@ -20,7 +20,7 @@ void UsbCommand_GetDebugBuffer(void) SetDebugBufferUint32(5, I2cSlaveScheduler_Counter); SetDebugBufferUint32(9, I2cWatchdog_WatchCounter); SetDebugBufferUint32(13, I2cWatchdog_RecoveryCounter); - SetDebugBufferUint32(17, KeyScannerCounter); + SetDebugBufferUint32(17, MatrixScanCounter); SetDebugBufferUint32(21, UsbReportUpdateCounter); SetDebugBufferUint32(25, Timer_GetCurrentTime()); SetDebugBufferUint32(29, UsbGenericHidActionCounter);