From 0b100feb3240a06a66260529daed730cf356fd7f Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 02:08:48 +0100 Subject: [PATCH 01/10] Global variables shared between an interrupt and the main code should be volatile See: https://www.embedded.com/electronics-blogs/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword --- right/src/eeprom.c | 2 +- right/src/eeprom.h | 2 +- right/src/init_peripherals.c | 4 ++-- right/src/init_peripherals.h | 4 ++-- right/src/timer.c | 2 +- right/src/timer.h | 2 +- right/src/usb_interfaces/usb_interface_basic_keyboard.c | 2 +- right/src/usb_interfaces/usb_interface_basic_keyboard.h | 2 +- right/src/usb_interfaces/usb_interface_media_keyboard.c | 2 +- right/src/usb_interfaces/usb_interface_media_keyboard.h | 2 +- right/src/usb_interfaces/usb_interface_mouse.c | 2 +- right/src/usb_interfaces/usb_interface_mouse.h | 2 +- right/src/usb_interfaces/usb_interface_system_keyboard.c | 2 +- right/src/usb_interfaces/usb_interface_system_keyboard.h | 2 +- 14 files changed, 16 insertions(+), 16 deletions(-) diff --git a/right/src/eeprom.c b/right/src/eeprom.c index c51181e..3dfa761 100644 --- a/right/src/eeprom.c +++ b/right/src/eeprom.c @@ -6,7 +6,7 @@ #include "config_parser/config_globals.h" #include "buffer.h" -bool IsEepromBusy; +volatile bool IsEepromBusy; static eeprom_operation_t CurrentEepromOperation; static config_buffer_id_t CurrentConfigBufferId; status_t LastEepromTransferStatus; diff --git a/right/src/eeprom.h b/right/src/eeprom.h index 7de803a..f0d6250 100644 --- a/right/src/eeprom.h +++ b/right/src/eeprom.h @@ -24,7 +24,7 @@ // Variables: - extern bool IsEepromBusy; + extern volatile bool IsEepromBusy; extern status_t EepromTransferStatus; // Functions: diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index a7fdc5e..a887f39 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -18,8 +18,8 @@ #include "bootloader/wormhole.h" bool IsBusPalOn; -uint32_t I2cMainBusRequestedBaudRateBps = I2C_MAIN_BUS_NORMAL_BAUD_RATE; -uint32_t I2cMainBusActualBaudRateBps; +volatile uint32_t I2cMainBusRequestedBaudRateBps = I2C_MAIN_BUS_NORMAL_BAUD_RATE; +volatile uint32_t I2cMainBusActualBaudRateBps; void initBusPalState(void) { IsBusPalOn = Wormhole.magicNumber == WORMHOLE_MAGIC_NUMBER && Wormhole.enumerationMode == EnumerationMode_BusPal; diff --git a/right/src/init_peripherals.h b/right/src/init_peripherals.h index 0b0ad61..0d66399 100644 --- a/right/src/init_peripherals.h +++ b/right/src/init_peripherals.h @@ -8,8 +8,8 @@ // Variables: extern bool IsBusPalOn; - extern uint32_t I2cMainBusRequestedBaudRateBps; - extern uint32_t I2cMainBusActualBaudRateBps; + extern volatile uint32_t I2cMainBusRequestedBaudRateBps; + extern volatile uint32_t I2cMainBusActualBaudRateBps; // Functions: diff --git a/right/src/timer.c b/right/src/timer.c index c997310..ac96d4c 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -1,7 +1,7 @@ #include "fsl_pit.h" #include "timer.h" -uint32_t CurrentTime; +volatile uint32_t CurrentTime; void PIT_TIMER_HANDLER(void) { diff --git a/right/src/timer.h b/right/src/timer.h index 012bb49..69b54bc 100644 --- a/right/src/timer.h +++ b/right/src/timer.h @@ -11,7 +11,7 @@ // Variables: - extern uint32_t CurrentTime; + extern volatile uint32_t CurrentTime; // Functions: diff --git a/right/src/usb_interfaces/usb_interface_basic_keyboard.c b/right/src/usb_interfaces/usb_interface_basic_keyboard.c index 3062b28..6d4b4b4 100644 --- a/right/src/usb_interfaces/usb_interface_basic_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_basic_keyboard.c @@ -4,7 +4,7 @@ static usb_basic_keyboard_report_t usbBasicKeyboardReports[2]; uint32_t UsbBasicKeyboardActionCounter; usb_basic_keyboard_report_t* ActiveUsbBasicKeyboardReport = usbBasicKeyboardReports; -bool IsUsbBasicKeyboardReportSent = false; +volatile bool IsUsbBasicKeyboardReportSent = false; static uint8_t usbBasicKeyboardInBuffer[USB_BASIC_KEYBOARD_REPORT_LENGTH]; usb_basic_keyboard_report_t* getInactiveUsbBasicKeyboardReport(void) diff --git a/right/src/usb_interfaces/usb_interface_basic_keyboard.h b/right/src/usb_interfaces/usb_interface_basic_keyboard.h index f3b1c02..2a7fea0 100644 --- a/right/src/usb_interfaces/usb_interface_basic_keyboard.h +++ b/right/src/usb_interfaces/usb_interface_basic_keyboard.h @@ -31,7 +31,7 @@ // Variables: - extern bool IsUsbBasicKeyboardReportSent; + extern volatile bool IsUsbBasicKeyboardReportSent; extern uint32_t UsbBasicKeyboardActionCounter; extern usb_basic_keyboard_report_t* ActiveUsbBasicKeyboardReport; diff --git a/right/src/usb_interfaces/usb_interface_media_keyboard.c b/right/src/usb_interfaces/usb_interface_media_keyboard.c index 4693dcc..d818ddf 100644 --- a/right/src/usb_interfaces/usb_interface_media_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_media_keyboard.c @@ -3,7 +3,7 @@ uint32_t UsbMediaKeyboardActionCounter; static usb_media_keyboard_report_t usbMediaKeyboardReports[2]; usb_media_keyboard_report_t* ActiveUsbMediaKeyboardReport = usbMediaKeyboardReports; -bool IsUsbMediaKeyboardReportSent = false; +volatile bool IsUsbMediaKeyboardReportSent = false; usb_media_keyboard_report_t* getInactiveUsbMediaKeyboardReport(void) { diff --git a/right/src/usb_interfaces/usb_interface_media_keyboard.h b/right/src/usb_interfaces/usb_interface_media_keyboard.h index 62859d6..8b39fb2 100644 --- a/right/src/usb_interfaces/usb_interface_media_keyboard.h +++ b/right/src/usb_interfaces/usb_interface_media_keyboard.h @@ -28,7 +28,7 @@ // Variables: - extern bool IsUsbMediaKeyboardReportSent; + extern volatile bool IsUsbMediaKeyboardReportSent; extern uint32_t UsbMediaKeyboardActionCounter; extern usb_media_keyboard_report_t* ActiveUsbMediaKeyboardReport; diff --git a/right/src/usb_interfaces/usb_interface_mouse.c b/right/src/usb_interfaces/usb_interface_mouse.c index 27acb64..94a6cad 100644 --- a/right/src/usb_interfaces/usb_interface_mouse.c +++ b/right/src/usb_interfaces/usb_interface_mouse.c @@ -3,7 +3,7 @@ uint32_t UsbMouseActionCounter; static usb_mouse_report_t usbMouseReports[2]; usb_mouse_report_t* ActiveUsbMouseReport = usbMouseReports; -bool IsUsbMouseReportSent = false; +volatile bool IsUsbMouseReportSent = false; usb_mouse_report_t* getInactiveUsbMouseReport(void) { diff --git a/right/src/usb_interfaces/usb_interface_mouse.h b/right/src/usb_interfaces/usb_interface_mouse.h index e0e1e67..e1a2cc7 100644 --- a/right/src/usb_interfaces/usb_interface_mouse.h +++ b/right/src/usb_interfaces/usb_interface_mouse.h @@ -31,7 +31,7 @@ // Variables: - extern bool IsUsbMouseReportSent; + extern volatile bool IsUsbMouseReportSent; extern uint32_t UsbMouseActionCounter; extern usb_mouse_report_t* ActiveUsbMouseReport; diff --git a/right/src/usb_interfaces/usb_interface_system_keyboard.c b/right/src/usb_interfaces/usb_interface_system_keyboard.c index 7e6e61a..34a47a0 100644 --- a/right/src/usb_interfaces/usb_interface_system_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_system_keyboard.c @@ -3,7 +3,7 @@ uint32_t UsbSystemKeyboardActionCounter; static usb_system_keyboard_report_t usbSystemKeyboardReports[2]; usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport = usbSystemKeyboardReports; -bool IsUsbSystemKeyboardReportSent = false; +volatile bool IsUsbSystemKeyboardReportSent = false; usb_system_keyboard_report_t* getInactiveUsbSystemKeyboardReport() { diff --git a/right/src/usb_interfaces/usb_interface_system_keyboard.h b/right/src/usb_interfaces/usb_interface_system_keyboard.h index d19d362..0a84353 100644 --- a/right/src/usb_interfaces/usb_interface_system_keyboard.h +++ b/right/src/usb_interfaces/usb_interface_system_keyboard.h @@ -29,7 +29,7 @@ // Variables: - extern bool IsUsbSystemKeyboardReportSent; + extern volatile bool IsUsbSystemKeyboardReportSent; extern uint32_t UsbSystemKeyboardActionCounter; extern usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport; From 15d09552a4d475032869594cf3a0f6eb2674a822 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 02:12:21 +0100 Subject: [PATCH 02/10] There is no reason to change the active report if it has not changed --- right/src/usb_report_updater.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index f4b0277..ab70eda 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -425,15 +425,33 @@ void UpdateUsbReports(void) updateActiveUsbReports(); - SwitchActiveUsbBasicKeyboardReport(); - SwitchActiveUsbMediaKeyboardReport(); - SwitchActiveUsbSystemKeyboardReport(); - SwitchActiveUsbMouseReport(); + static usb_basic_keyboard_report_t last_basic_report = { .scancodes[0] = 0xFF }; + if (memcmp(ActiveUsbBasicKeyboardReport, &last_basic_report, sizeof(usb_basic_keyboard_report_t)) != 0) { + last_basic_report = *ActiveUsbBasicKeyboardReport; + SwitchActiveUsbBasicKeyboardReport(); + IsUsbBasicKeyboardReportSent = false; + } - IsUsbBasicKeyboardReportSent = false; - IsUsbMediaKeyboardReportSent = false; - IsUsbSystemKeyboardReportSent = false; - IsUsbMouseReportSent = false; + static usb_media_keyboard_report_t last_media_report = { .scancodes[0] = 0xFF }; + if (memcmp(ActiveUsbMediaKeyboardReport, &last_media_report, sizeof(usb_media_keyboard_report_t)) != 0) { + last_media_report = *ActiveUsbMediaKeyboardReport; + SwitchActiveUsbMediaKeyboardReport(); + IsUsbMediaKeyboardReportSent = false; + } + + static usb_system_keyboard_report_t last_system_report = { .scancodes[0] = 0xFF }; + if (memcmp(ActiveUsbSystemKeyboardReport, &last_system_report, sizeof(usb_system_keyboard_report_t)) != 0) { + last_system_report = *ActiveUsbSystemKeyboardReport; + SwitchActiveUsbSystemKeyboardReport(); + IsUsbSystemKeyboardReportSent = false; + } + + static usb_mouse_report_t last_mouse_report = { .buttons = 0xFF }; + if (memcmp(ActiveUsbMouseReport, &last_mouse_report, sizeof(usb_mouse_report_t)) != 0) { + last_mouse_report = *ActiveUsbMouseReport; + SwitchActiveUsbMouseReport(); + IsUsbMouseReportSent = false; + } Timer_SetCurrentTime(&lastUsbUpdateTime); } From 13a718f8716a71bd35ef6e826f601e2d07e74b9e Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 02:36:57 +0100 Subject: [PATCH 03/10] Declare local functions and variables static This both helps the compiler and the programmer --- right/src/eeprom.c | 2 +- right/src/eeprom.h | 1 - right/src/init_peripherals.c | 14 ++++++------- right/src/main.c | 8 ++++---- right/src/peripherals/adc.c | 2 +- .../usb_interface_basic_keyboard.c | 2 +- .../usb_interface_media_keyboard.c | 2 +- .../src/usb_interfaces/usb_interface_mouse.c | 2 +- .../usb_interface_system_keyboard.c | 2 +- right/src/usb_report_updater.c | 20 +++++++++---------- 10 files changed, 27 insertions(+), 28 deletions(-) diff --git a/right/src/eeprom.c b/right/src/eeprom.c index 3dfa761..5dd0893 100644 --- a/right/src/eeprom.c +++ b/right/src/eeprom.c @@ -9,7 +9,7 @@ volatile bool IsEepromBusy; static eeprom_operation_t CurrentEepromOperation; static config_buffer_id_t CurrentConfigBufferId; -status_t LastEepromTransferStatus; +static status_t LastEepromTransferStatus; void (*SuccessCallback)(void); static i2c_master_handle_t i2cHandle; diff --git a/right/src/eeprom.h b/right/src/eeprom.h index f0d6250..4ca6ad7 100644 --- a/right/src/eeprom.h +++ b/right/src/eeprom.h @@ -25,7 +25,6 @@ // Variables: extern volatile bool IsEepromBusy; - extern status_t EepromTransferStatus; // Functions: diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index a887f39..44136a3 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -21,7 +21,7 @@ bool IsBusPalOn; volatile uint32_t I2cMainBusRequestedBaudRateBps = I2C_MAIN_BUS_NORMAL_BAUD_RATE; volatile uint32_t I2cMainBusActualBaudRateBps; -void initBusPalState(void) { +static void initBusPalState(void) { IsBusPalOn = Wormhole.magicNumber == WORMHOLE_MAGIC_NUMBER && Wormhole.enumerationMode == EnumerationMode_BusPal; if (IsBusPalOn) { Wormhole.magicNumber = 0; @@ -29,7 +29,7 @@ void initBusPalState(void) { } } -void initInterruptPriorities(void) +static void initInterruptPriorities(void) { NVIC_SetPriority(PIT_I2C_WATCHDOG_IRQ_ID, 1); NVIC_SetPriority(PIT_TIMER_IRQ_ID, 2); @@ -40,12 +40,12 @@ void initInterruptPriorities(void) NVIC_SetPriority(USB_IRQ_ID, 3); } -void delay(void) +static void delay(void) { for (volatile uint32_t i=0; i<62; i++); } -void recoverI2c(void) +static void recoverI2c(void) { PORT_SetPinMux(I2C_MAIN_BUS_SDA_PORT, I2C_MAIN_BUS_SDA_PIN, kPORT_MuxAsGpio); PORT_SetPinMux(I2C_MAIN_BUS_SCL_PORT, I2C_MAIN_BUS_SCL_PIN, kPORT_MuxAsGpio); @@ -74,7 +74,7 @@ void recoverI2c(void) delay(); } -void initI2cMainBus(void) +static void initI2cMainBus(void) { CLOCK_EnableClock(I2C_MAIN_BUS_SDA_CLOCK); CLOCK_EnableClock(I2C_MAIN_BUS_SCL_CLOCK); @@ -105,7 +105,7 @@ void ReinitI2cMainBus(void) InitSlaveScheduler(); } -void initI2cEepromBus(void) +static void initI2cEepromBus(void) { port_pin_config_t pinConfig = { .pullSelect = kPORT_PullUp, @@ -126,7 +126,7 @@ void initI2cEepromBus(void) I2C_MasterInit(I2C_EEPROM_BUS_BASEADDR, &masterConfig, sourceClock); } -void initI2c(void) +static void initI2c(void) { initI2cMainBus(); initI2cEepromBus(); diff --git a/right/src/main.c b/right/src/main.c index 60a8c61..2897680 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -11,15 +11,15 @@ #include "peripherals/reset_button.h" #include "usb_report_updater.h" -bool IsEepromInitialized = false; -bool IsConfigInitialized = false; +static bool IsEepromInitialized = false; +static bool IsConfigInitialized = false; -void userConfigurationReadFinished(void) +static void userConfigurationReadFinished(void) { IsEepromInitialized = true; } -void hardwareConfigurationReadFinished(void) +static void hardwareConfigurationReadFinished(void) { EEPROM_LaunchTransfer(EepromOperation_Read, ConfigBufferId_StagingUserConfig, userConfigurationReadFinished); } diff --git a/right/src/peripherals/adc.c b/right/src/peripherals/adc.c index 5803db0..0d79eeb 100644 --- a/right/src/peripherals/adc.c +++ b/right/src/peripherals/adc.c @@ -2,7 +2,7 @@ #include "fsl_port.h" #include "peripherals/adc.h" -adc16_channel_config_t adc16ChannelConfigStruct; +static adc16_channel_config_t adc16ChannelConfigStruct; void ADC_Init(void) { diff --git a/right/src/usb_interfaces/usb_interface_basic_keyboard.c b/right/src/usb_interfaces/usb_interface_basic_keyboard.c index 6d4b4b4..f1bef4f 100644 --- a/right/src/usb_interfaces/usb_interface_basic_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_basic_keyboard.c @@ -7,7 +7,7 @@ usb_basic_keyboard_report_t* ActiveUsbBasicKeyboardReport = usbBasicKeyboardRepo volatile bool IsUsbBasicKeyboardReportSent = false; static uint8_t usbBasicKeyboardInBuffer[USB_BASIC_KEYBOARD_REPORT_LENGTH]; -usb_basic_keyboard_report_t* getInactiveUsbBasicKeyboardReport(void) +static usb_basic_keyboard_report_t* getInactiveUsbBasicKeyboardReport(void) { return ActiveUsbBasicKeyboardReport == usbBasicKeyboardReports ? usbBasicKeyboardReports+1 : usbBasicKeyboardReports; } diff --git a/right/src/usb_interfaces/usb_interface_media_keyboard.c b/right/src/usb_interfaces/usb_interface_media_keyboard.c index d818ddf..8147734 100644 --- a/right/src/usb_interfaces/usb_interface_media_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_media_keyboard.c @@ -5,7 +5,7 @@ static usb_media_keyboard_report_t usbMediaKeyboardReports[2]; usb_media_keyboard_report_t* ActiveUsbMediaKeyboardReport = usbMediaKeyboardReports; volatile bool IsUsbMediaKeyboardReportSent = false; -usb_media_keyboard_report_t* getInactiveUsbMediaKeyboardReport(void) +static usb_media_keyboard_report_t* getInactiveUsbMediaKeyboardReport(void) { return ActiveUsbMediaKeyboardReport == usbMediaKeyboardReports ? usbMediaKeyboardReports+1 : usbMediaKeyboardReports; } diff --git a/right/src/usb_interfaces/usb_interface_mouse.c b/right/src/usb_interfaces/usb_interface_mouse.c index 94a6cad..711e3cd 100644 --- a/right/src/usb_interfaces/usb_interface_mouse.c +++ b/right/src/usb_interfaces/usb_interface_mouse.c @@ -5,7 +5,7 @@ static usb_mouse_report_t usbMouseReports[2]; usb_mouse_report_t* ActiveUsbMouseReport = usbMouseReports; volatile bool IsUsbMouseReportSent = false; -usb_mouse_report_t* getInactiveUsbMouseReport(void) +static usb_mouse_report_t* getInactiveUsbMouseReport(void) { return ActiveUsbMouseReport == usbMouseReports ? usbMouseReports+1 : usbMouseReports; } diff --git a/right/src/usb_interfaces/usb_interface_system_keyboard.c b/right/src/usb_interfaces/usb_interface_system_keyboard.c index 34a47a0..e039069 100644 --- a/right/src/usb_interfaces/usb_interface_system_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_system_keyboard.c @@ -5,7 +5,7 @@ static usb_system_keyboard_report_t usbSystemKeyboardReports[2]; usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport = usbSystemKeyboardReports; volatile bool IsUsbSystemKeyboardReportSent = false; -usb_system_keyboard_report_t* getInactiveUsbSystemKeyboardReport() +static usb_system_keyboard_report_t* getInactiveUsbSystemKeyboardReport() { return ActiveUsbSystemKeyboardReport == usbSystemKeyboardReports ? usbSystemKeyboardReports+1 : usbSystemKeyboardReports; } diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index ab70eda..7fb3eeb 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -17,11 +17,11 @@ #include "config_parser/parse_keymap.h" #include "usb_commands/usb_command_get_debug_buffer.h" -uint32_t UsbReportUpdateTime = 0; +static uint32_t UsbReportUpdateTime = 0; static uint32_t elapsedTime; uint16_t DoubleTapSwitchLayerTimeout = 150; -uint16_t DoubleTapSwitchLayerReleaseTimeout = 100; +static uint16_t DoubleTapSwitchLayerReleaseTimeout = 100; static bool activeMouseStates[ACTIVE_MOUSE_STATES_COUNT]; @@ -53,7 +53,7 @@ mouse_kinetic_state_t MouseScrollState = { .acceleratedSpeed = 50, }; -void processMouseKineticState(mouse_kinetic_state_t *kineticState) +static void processMouseKineticState(mouse_kinetic_state_t *kineticState) { float initialSpeed = kineticState->intMultiplier * kineticState->initialSpeed; float acceleration = kineticState->intMultiplier * kineticState->acceleration; @@ -155,7 +155,7 @@ void processMouseKineticState(mouse_kinetic_state_t *kineticState) kineticState->wasMoveAction = isMoveAction; } -void processMouseActions() +static void processMouseActions() { processMouseKineticState(&MouseMoveState); ActiveUsbMouseReport->x = MouseMoveState.xOut; @@ -193,7 +193,7 @@ static uint8_t basicScancodeIndex = 0; static uint8_t mediaScancodeIndex = 0; static uint8_t systemScancodeIndex = 0; -void applyKeyAction(key_state_t *keyState, key_action_t *action) +static void applyKeyAction(key_state_t *keyState, key_action_t *action) { static key_state_t *doubleTapSwitchLayerKey; static uint32_t doubleTapSwitchLayerStartTime; @@ -265,7 +265,7 @@ static uint8_t secondaryRoleSlotId; static uint8_t secondaryRoleKeyId; static secondary_role_t secondaryRole; -void updateActiveUsbReports(void) +static void updateActiveUsbReports(void) { memset(activeMouseStates, 0, ACTIVE_MOUSE_STATES_COUNT); @@ -369,10 +369,10 @@ void updateActiveUsbReports(void) previousLayer = activeLayer; } -bool UsbBasicKeyboardReportEverSent = false; -bool UsbMediaKeyboardReportEverSent = false; -bool UsbSystemKeyboardReportEverSent = false; -bool UsbMouseReportEverSentEverSent = false; +static bool UsbBasicKeyboardReportEverSent = false; +static bool UsbMediaKeyboardReportEverSent = false; +static bool UsbSystemKeyboardReportEverSent = false; +static bool UsbMouseReportEverSentEverSent = false; uint32_t UsbReportUpdateCounter; static uint32_t lastUsbUpdateTime; From 5de0e5ac600cf28b771f55a75fb3d45c70f1483f Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 05:26:51 +0100 Subject: [PATCH 04/10] Turn LEDs, backlight and display off when sleeping Fixes #83 --- right/src/led_display.c | 5 ++++ right/src/led_display.h | 2 ++ right/src/usb_composite_device.c | 45 +++++++++++++++++++++++++++++++- right/src/usb_device_config.h | 4 +-- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/right/src/led_display.c b/right/src/led_display.c index cb3d8c9..d6fab2b 100644 --- a/right/src/led_display.c +++ b/right/src/led_display.c @@ -101,6 +101,11 @@ void LedDisplay_SetLayer(uint8_t layerId) } } +bool LedDisplay_GetIcon(led_display_icon_t icon) +{ + return LedDriverValues[LedDriverId_Left][8 + icon]; +} + void LedDisplay_SetIcon(led_display_icon_t icon, bool isEnabled) { LedDriverValues[LedDriverId_Left][8 + icon] = isEnabled ? IconsAndLayerTextsBrightness : 0; diff --git a/right/src/led_display.h b/right/src/led_display.h index 8e76c23..2d98423 100644 --- a/right/src/led_display.h +++ b/right/src/led_display.h @@ -26,4 +26,6 @@ void LedDisplay_SetLayer(uint8_t layerId); void LedDisplay_SetIcon(led_display_icon_t icon, bool isEnabled); + bool LedDisplay_GetIcon(led_display_icon_t icon); + #endif diff --git a/right/src/usb_composite_device.c b/right/src/usb_composite_device.c index a98b05c..048233d 100644 --- a/right/src/usb_composite_device.c +++ b/right/src/usb_composite_device.c @@ -1,3 +1,6 @@ +#include "config.h" +#include "led_display.h" +#include "slave_drivers/is31fl3731_driver.h" #include "usb_device_config.h" #include "usb_composite_device.h" #include "usb_descriptors/usb_descriptor_hid.h" @@ -162,11 +165,15 @@ static usb_device_class_config_list_struct_t UsbDeviceCompositeConfigList = { static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, void *param) { +#ifdef LED_DRIVERS_ENABLED + static uint8_t oldKeyBacklightBrightness = 0xFF; + static bool capsLockOn = false, agentOn = false, adaptiveOn = false; +#endif usb_status_t status = kStatus_USB_Error; uint16_t *temp16 = (uint16_t*)param; uint8_t *temp8 = (uint8_t*)param; - if (!param && event != kUSB_DeviceEventBusReset && event != kUSB_DeviceEventSetInterface) { + if (!param && event != kUSB_DeviceEventBusReset && event != kUSB_DeviceEventSetInterface && event != kUSB_DeviceEventSuspend && event != kUSB_DeviceEventResume) { return status; } @@ -175,6 +182,42 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, UsbCompositeDevice.attach = 0; status = kStatus_USB_Success; break; + case kUSB_DeviceEventSuspend: + if (UsbCompositeDevice.attach) { +#ifdef LED_DRIVERS_ENABLED + // Save the state of the icons + capsLockOn = LedDisplay_GetIcon(LedDisplayIcon_CapsLock); + agentOn = LedDisplay_GetIcon(LedDisplayIcon_Agent); + adaptiveOn = LedDisplay_GetIcon(LedDisplayIcon_Adaptive); + + // Disable keyboard backlight + oldKeyBacklightBrightness = KeyBacklightBrightness; + KeyBacklightBrightness = 0; + LedSlaveDriver_Init(LedDriverId_Right); + LedSlaveDriver_Init(LedDriverId_Left); + + // Clear the text + LedDisplay_SetText(0, NULL); +#endif + status = kStatus_USB_Success; + } + break; + case kUSB_DeviceEventResume: + if (UsbCompositeDevice.attach) { +#ifdef LED_DRIVERS_ENABLED + // Restore keyboard backlight and text + KeyBacklightBrightness = oldKeyBacklightBrightness; + LedSlaveDriver_Init(LedDriverId_Right); + LedSlaveDriver_Init(LedDriverId_Left); + + // Restore icon states + LedDisplay_SetIcon(LedDisplayIcon_CapsLock, capsLockOn); + LedDisplay_SetIcon(LedDisplayIcon_Agent, agentOn); + LedDisplay_SetIcon(LedDisplayIcon_Adaptive, adaptiveOn); +#endif + status = kStatus_USB_Success; + } + break; case kUSB_DeviceEventSetConfiguration: UsbCompositeDevice.attach = 1; UsbCompositeDevice.currentConfiguration = *temp8; diff --git a/right/src/usb_device_config.h b/right/src/usb_device_config.h index 1aa0013..e418b48 100644 --- a/right/src/usb_device_config.h +++ b/right/src/usb_device_config.h @@ -23,10 +23,10 @@ ) // Whether the device is self-powered: 1 supported, 0 not supported -#define USB_DEVICE_CONFIG_SELF_POWER 1 +#define USB_DEVICE_CONFIG_SELF_POWER 0 // Whether device remote wakeup supported: 1 supported, 0 not supported -#define USB_DEVICE_CONFIG_REMOTE_WAKEUP 0 +#define USB_DEVICE_CONFIG_REMOTE_WAKEUP 1 // The number of control endpoints, which is always 1 #define USB_CONTROL_ENDPOINT_COUNT 1 From 6e2eca7829d0d8a0369c30395a569fc1f4022b32 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 17:59:23 +0100 Subject: [PATCH 05/10] Changed CurrentTime to a static variable and added function for getting the current time instead Global variables are really bad practice and should be avoided --- right/src/timer.c | 6 +++++- right/src/timer.h | 5 +---- right/src/usb_commands/usb_command_get_debug_buffer.c | 2 +- right/src/usb_commands/usb_command_get_device_property.c | 2 +- right/src/usb_report_updater.c | 4 ++-- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/right/src/timer.c b/right/src/timer.c index ac96d4c..4106877 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -1,7 +1,7 @@ #include "fsl_pit.h" #include "timer.h" -volatile uint32_t CurrentTime; +static volatile uint32_t CurrentTime; void PIT_TIMER_HANDLER(void) { @@ -24,6 +24,10 @@ void Timer_Init(void) PIT_StartTimer(PIT, PIT_TIMER_CHANNEL); } +uint32_t Timer_GetCurrentTime() { + return CurrentTime; +} + void Timer_SetCurrentTime(uint32_t *time) { *time = CurrentTime; diff --git a/right/src/timer.h b/right/src/timer.h index 69b54bc..1ce3218 100644 --- a/right/src/timer.h +++ b/right/src/timer.h @@ -9,13 +9,10 @@ #define TIMER_INTERVAL_MSEC 1 -// Variables: - - extern volatile uint32_t CurrentTime; - // Functions: void Timer_Init(void); + uint32_t Timer_GetCurrentTime(); void Timer_SetCurrentTime(uint32_t *time); uint32_t Timer_GetElapsedTime(uint32_t *time); uint32_t Timer_GetElapsedTimeAndSetCurrent(uint32_t *time); 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 4439841..92d0073 100644 --- a/right/src/usb_commands/usb_command_get_debug_buffer.c +++ b/right/src/usb_commands/usb_command_get_debug_buffer.c @@ -22,7 +22,7 @@ void UsbCommand_GetDebugBuffer(void) SetDebugBufferUint32(13, I2cWatchdog_RecoveryCounter); SetDebugBufferUint32(17, KeyScannerCounter); SetDebugBufferUint32(21, UsbReportUpdateCounter); - SetDebugBufferUint32(25, CurrentTime); + SetDebugBufferUint32(25, Timer_GetCurrentTime()); SetDebugBufferUint32(29, UsbGenericHidActionCounter); SetDebugBufferUint32(33, UsbBasicKeyboardActionCounter); SetDebugBufferUint32(37, UsbMediaKeyboardActionCounter); diff --git a/right/src/usb_commands/usb_command_get_device_property.c b/right/src/usb_commands/usb_command_get_device_property.c index 62c1e5a..50d67c6 100644 --- a/right/src/usb_commands/usb_command_get_device_property.c +++ b/right/src/usb_commands/usb_command_get_device_property.c @@ -69,7 +69,7 @@ void UsbCommand_GetDeviceProperty(void) SetUsbTxBufferUint32(6, I2cMainBusActualBaudRateBps); break; case DevicePropertyId_Uptime: - SetUsbTxBufferUint32(1, CurrentTime); + SetUsbTxBufferUint32(1, Timer_GetCurrentTime()); break; default: SetUsbTxBufferUint8(0, UsbStatusCode_GetDeviceProperty_InvalidProperty); diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 7fb3eeb..250de4a 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -245,11 +245,11 @@ static void applyKeyAction(key_state_t *keyState, key_action_t *action) if (!keyState->previous && previousLayer == LayerId_Base && action->switchLayer.mode == SwitchLayerMode_HoldAndDoubleTapToggle) { if (doubleTapSwitchLayerKey && Timer_GetElapsedTimeAndSetCurrent(&doubleTapSwitchLayerStartTime) < DoubleTapSwitchLayerTimeout) { ToggledLayer = action->switchLayer.layer; - doubleTapSwitchLayerTriggerTime = CurrentTime; + doubleTapSwitchLayerTriggerTime = Timer_GetCurrentTime(); } else { doubleTapSwitchLayerKey = keyState; } - doubleTapSwitchLayerStartTime = CurrentTime; + doubleTapSwitchLayerStartTime = Timer_GetCurrentTime(); } break; case KeyActionType_SwitchKeymap: From 3ab2ac18fc4636c55e7c189a813bad9e0a83d6d1 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 19:00:46 +0100 Subject: [PATCH 06/10] The keyboard can now wake up the computer from sleep --- right/src/usb_composite_device.c | 82 ++++++++++++++++++++------------ right/src/usb_composite_device.h | 2 + right/src/usb_report_updater.c | 3 ++ 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/right/src/usb_composite_device.c b/right/src/usb_composite_device.c index 048233d..e5fa973 100644 --- a/right/src/usb_composite_device.c +++ b/right/src/usb_composite_device.c @@ -163,12 +163,56 @@ static usb_device_class_config_list_struct_t UsbDeviceCompositeConfigList = { } }}; +static bool computerSleeping = false; +#ifdef LED_DRIVERS_ENABLED +static uint8_t oldKeyBacklightBrightness = 0xFF; +static bool capsLockOn = false, agentOn = false, adaptiveOn = false; +#endif + +bool IsComputerSleeping(void) { + return computerSleeping; +} + +static void ComputerIsSleeping(void) { + computerSleeping = true; +#ifdef LED_DRIVERS_ENABLED + // Save the state of the icons + capsLockOn = LedDisplay_GetIcon(LedDisplayIcon_CapsLock); + agentOn = LedDisplay_GetIcon(LedDisplayIcon_Agent); + adaptiveOn = LedDisplay_GetIcon(LedDisplayIcon_Adaptive); + + // Disable keyboard backlight + oldKeyBacklightBrightness = KeyBacklightBrightness; + KeyBacklightBrightness = 0; + LedSlaveDriver_Init(LedDriverId_Right); + LedSlaveDriver_Init(LedDriverId_Left); + + // Clear the text + LedDisplay_SetText(0, NULL); +#endif +} + +void WakeupComputer(bool sendResume) { + if (sendResume) // The device should wake up the computer + USB_DeviceSetStatus(UsbCompositeDevice.deviceHandle, kUSB_DeviceStatusBus, NULL); // Send resume signal - this will call USB_DeviceKhciControl(khciHandle, kUSB_DeviceControlResume, NULL); + + computerSleeping = false; // The computer is now awake + +#ifdef LED_DRIVERS_ENABLED + // Restore keyboard backlight and text + KeyBacklightBrightness = oldKeyBacklightBrightness; + LedSlaveDriver_Init(LedDriverId_Right); + LedSlaveDriver_Init(LedDriverId_Left); + + // Restore icon states + LedDisplay_SetIcon(LedDisplayIcon_CapsLock, capsLockOn); + LedDisplay_SetIcon(LedDisplayIcon_Agent, agentOn); + LedDisplay_SetIcon(LedDisplayIcon_Adaptive, adaptiveOn); +#endif +} + static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, void *param) { -#ifdef LED_DRIVERS_ENABLED - static uint8_t oldKeyBacklightBrightness = 0xFF; - static bool capsLockOn = false, agentOn = false, adaptiveOn = false; -#endif usb_status_t status = kStatus_USB_Error; uint16_t *temp16 = (uint16_t*)param; uint8_t *temp8 = (uint8_t*)param; @@ -184,37 +228,13 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, break; case kUSB_DeviceEventSuspend: if (UsbCompositeDevice.attach) { -#ifdef LED_DRIVERS_ENABLED - // Save the state of the icons - capsLockOn = LedDisplay_GetIcon(LedDisplayIcon_CapsLock); - agentOn = LedDisplay_GetIcon(LedDisplayIcon_Agent); - adaptiveOn = LedDisplay_GetIcon(LedDisplayIcon_Adaptive); - - // Disable keyboard backlight - oldKeyBacklightBrightness = KeyBacklightBrightness; - KeyBacklightBrightness = 0; - LedSlaveDriver_Init(LedDriverId_Right); - LedSlaveDriver_Init(LedDriverId_Left); - - // Clear the text - LedDisplay_SetText(0, NULL); -#endif + ComputerIsSleeping(); // The computer sends this event when it goes to sleep, so turn off all the LEDs status = kStatus_USB_Success; } break; case kUSB_DeviceEventResume: - if (UsbCompositeDevice.attach) { -#ifdef LED_DRIVERS_ENABLED - // Restore keyboard backlight and text - KeyBacklightBrightness = oldKeyBacklightBrightness; - LedSlaveDriver_Init(LedDriverId_Right); - LedSlaveDriver_Init(LedDriverId_Left); - - // Restore icon states - LedDisplay_SetIcon(LedDisplayIcon_CapsLock, capsLockOn); - LedDisplay_SetIcon(LedDisplayIcon_Agent, agentOn); - LedDisplay_SetIcon(LedDisplayIcon_Adaptive, adaptiveOn); -#endif + if (computerSleeping) { + WakeupComputer(false); // The computer just woke up, so restore the LEDs status = kStatus_USB_Success; } break; diff --git a/right/src/usb_composite_device.h b/right/src/usb_composite_device.h index 34e9509..c443430 100644 --- a/right/src/usb_composite_device.h +++ b/right/src/usb_composite_device.h @@ -33,5 +33,7 @@ //Functions: void InitUsb(void); + bool IsComputerSleeping(void); + void WakeupComputer(bool sendResume); #endif diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 250de4a..8832b55 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -453,5 +453,8 @@ void UpdateUsbReports(void) IsUsbMouseReportSent = false; } + if ((!IsUsbBasicKeyboardReportSent || !IsUsbMediaKeyboardReportSent || !IsUsbSystemKeyboardReportSent || !IsUsbMouseReportSent) && IsComputerSleeping()) + WakeupComputer(true); // Wake up the computer if any key is pressed and the computer is sleeping + Timer_SetCurrentTime(&lastUsbUpdateTime); } From e12e219b4e9fef7cfa2fd33250fd78c88839c0ea Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 19:19:15 +0100 Subject: [PATCH 07/10] Various fixes and improvements (#96) * Global variables shared between an interrupt and the main code should be volatile See: https://www.embedded.com/electronics-blogs/beginner-s-corner/4023801/Introduction-to-the-Volatile-Keyword * There is no reason to change the active report if it has not changed * Declare local functions and variables static This both helps the compiler and the programmer --- right/src/eeprom.c | 4 +- right/src/eeprom.h | 3 +- right/src/init_peripherals.c | 18 +++---- right/src/init_peripherals.h | 4 +- right/src/main.c | 8 +-- right/src/peripherals/adc.c | 2 +- right/src/timer.c | 2 +- right/src/timer.h | 2 +- .../usb_interface_basic_keyboard.c | 4 +- .../usb_interface_basic_keyboard.h | 2 +- .../usb_interface_media_keyboard.c | 4 +- .../usb_interface_media_keyboard.h | 2 +- .../src/usb_interfaces/usb_interface_mouse.c | 4 +- .../src/usb_interfaces/usb_interface_mouse.h | 2 +- .../usb_interface_system_keyboard.c | 4 +- .../usb_interface_system_keyboard.h | 2 +- right/src/usb_report_updater.c | 54 ++++++++++++------- 17 files changed, 69 insertions(+), 52 deletions(-) diff --git a/right/src/eeprom.c b/right/src/eeprom.c index c51181e..5dd0893 100644 --- a/right/src/eeprom.c +++ b/right/src/eeprom.c @@ -6,10 +6,10 @@ #include "config_parser/config_globals.h" #include "buffer.h" -bool IsEepromBusy; +volatile bool IsEepromBusy; static eeprom_operation_t CurrentEepromOperation; static config_buffer_id_t CurrentConfigBufferId; -status_t LastEepromTransferStatus; +static status_t LastEepromTransferStatus; void (*SuccessCallback)(void); static i2c_master_handle_t i2cHandle; diff --git a/right/src/eeprom.h b/right/src/eeprom.h index 7de803a..4ca6ad7 100644 --- a/right/src/eeprom.h +++ b/right/src/eeprom.h @@ -24,8 +24,7 @@ // Variables: - extern bool IsEepromBusy; - extern status_t EepromTransferStatus; + extern volatile bool IsEepromBusy; // Functions: diff --git a/right/src/init_peripherals.c b/right/src/init_peripherals.c index a7fdc5e..44136a3 100644 --- a/right/src/init_peripherals.c +++ b/right/src/init_peripherals.c @@ -18,10 +18,10 @@ #include "bootloader/wormhole.h" bool IsBusPalOn; -uint32_t I2cMainBusRequestedBaudRateBps = I2C_MAIN_BUS_NORMAL_BAUD_RATE; -uint32_t I2cMainBusActualBaudRateBps; +volatile uint32_t I2cMainBusRequestedBaudRateBps = I2C_MAIN_BUS_NORMAL_BAUD_RATE; +volatile uint32_t I2cMainBusActualBaudRateBps; -void initBusPalState(void) { +static void initBusPalState(void) { IsBusPalOn = Wormhole.magicNumber == WORMHOLE_MAGIC_NUMBER && Wormhole.enumerationMode == EnumerationMode_BusPal; if (IsBusPalOn) { Wormhole.magicNumber = 0; @@ -29,7 +29,7 @@ void initBusPalState(void) { } } -void initInterruptPriorities(void) +static void initInterruptPriorities(void) { NVIC_SetPriority(PIT_I2C_WATCHDOG_IRQ_ID, 1); NVIC_SetPriority(PIT_TIMER_IRQ_ID, 2); @@ -40,12 +40,12 @@ void initInterruptPriorities(void) NVIC_SetPriority(USB_IRQ_ID, 3); } -void delay(void) +static void delay(void) { for (volatile uint32_t i=0; i<62; i++); } -void recoverI2c(void) +static void recoverI2c(void) { PORT_SetPinMux(I2C_MAIN_BUS_SDA_PORT, I2C_MAIN_BUS_SDA_PIN, kPORT_MuxAsGpio); PORT_SetPinMux(I2C_MAIN_BUS_SCL_PORT, I2C_MAIN_BUS_SCL_PIN, kPORT_MuxAsGpio); @@ -74,7 +74,7 @@ void recoverI2c(void) delay(); } -void initI2cMainBus(void) +static void initI2cMainBus(void) { CLOCK_EnableClock(I2C_MAIN_BUS_SDA_CLOCK); CLOCK_EnableClock(I2C_MAIN_BUS_SCL_CLOCK); @@ -105,7 +105,7 @@ void ReinitI2cMainBus(void) InitSlaveScheduler(); } -void initI2cEepromBus(void) +static void initI2cEepromBus(void) { port_pin_config_t pinConfig = { .pullSelect = kPORT_PullUp, @@ -126,7 +126,7 @@ void initI2cEepromBus(void) I2C_MasterInit(I2C_EEPROM_BUS_BASEADDR, &masterConfig, sourceClock); } -void initI2c(void) +static void initI2c(void) { initI2cMainBus(); initI2cEepromBus(); diff --git a/right/src/init_peripherals.h b/right/src/init_peripherals.h index 0b0ad61..0d66399 100644 --- a/right/src/init_peripherals.h +++ b/right/src/init_peripherals.h @@ -8,8 +8,8 @@ // Variables: extern bool IsBusPalOn; - extern uint32_t I2cMainBusRequestedBaudRateBps; - extern uint32_t I2cMainBusActualBaudRateBps; + extern volatile uint32_t I2cMainBusRequestedBaudRateBps; + extern volatile uint32_t I2cMainBusActualBaudRateBps; // Functions: diff --git a/right/src/main.c b/right/src/main.c index 60a8c61..2897680 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -11,15 +11,15 @@ #include "peripherals/reset_button.h" #include "usb_report_updater.h" -bool IsEepromInitialized = false; -bool IsConfigInitialized = false; +static bool IsEepromInitialized = false; +static bool IsConfigInitialized = false; -void userConfigurationReadFinished(void) +static void userConfigurationReadFinished(void) { IsEepromInitialized = true; } -void hardwareConfigurationReadFinished(void) +static void hardwareConfigurationReadFinished(void) { EEPROM_LaunchTransfer(EepromOperation_Read, ConfigBufferId_StagingUserConfig, userConfigurationReadFinished); } diff --git a/right/src/peripherals/adc.c b/right/src/peripherals/adc.c index 5803db0..0d79eeb 100644 --- a/right/src/peripherals/adc.c +++ b/right/src/peripherals/adc.c @@ -2,7 +2,7 @@ #include "fsl_port.h" #include "peripherals/adc.h" -adc16_channel_config_t adc16ChannelConfigStruct; +static adc16_channel_config_t adc16ChannelConfigStruct; void ADC_Init(void) { diff --git a/right/src/timer.c b/right/src/timer.c index c997310..ac96d4c 100644 --- a/right/src/timer.c +++ b/right/src/timer.c @@ -1,7 +1,7 @@ #include "fsl_pit.h" #include "timer.h" -uint32_t CurrentTime; +volatile uint32_t CurrentTime; void PIT_TIMER_HANDLER(void) { diff --git a/right/src/timer.h b/right/src/timer.h index 012bb49..69b54bc 100644 --- a/right/src/timer.h +++ b/right/src/timer.h @@ -11,7 +11,7 @@ // Variables: - extern uint32_t CurrentTime; + extern volatile uint32_t CurrentTime; // Functions: diff --git a/right/src/usb_interfaces/usb_interface_basic_keyboard.c b/right/src/usb_interfaces/usb_interface_basic_keyboard.c index 3062b28..f1bef4f 100644 --- a/right/src/usb_interfaces/usb_interface_basic_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_basic_keyboard.c @@ -4,10 +4,10 @@ static usb_basic_keyboard_report_t usbBasicKeyboardReports[2]; uint32_t UsbBasicKeyboardActionCounter; usb_basic_keyboard_report_t* ActiveUsbBasicKeyboardReport = usbBasicKeyboardReports; -bool IsUsbBasicKeyboardReportSent = false; +volatile bool IsUsbBasicKeyboardReportSent = false; static uint8_t usbBasicKeyboardInBuffer[USB_BASIC_KEYBOARD_REPORT_LENGTH]; -usb_basic_keyboard_report_t* getInactiveUsbBasicKeyboardReport(void) +static usb_basic_keyboard_report_t* getInactiveUsbBasicKeyboardReport(void) { return ActiveUsbBasicKeyboardReport == usbBasicKeyboardReports ? usbBasicKeyboardReports+1 : usbBasicKeyboardReports; } diff --git a/right/src/usb_interfaces/usb_interface_basic_keyboard.h b/right/src/usb_interfaces/usb_interface_basic_keyboard.h index f3b1c02..2a7fea0 100644 --- a/right/src/usb_interfaces/usb_interface_basic_keyboard.h +++ b/right/src/usb_interfaces/usb_interface_basic_keyboard.h @@ -31,7 +31,7 @@ // Variables: - extern bool IsUsbBasicKeyboardReportSent; + extern volatile bool IsUsbBasicKeyboardReportSent; extern uint32_t UsbBasicKeyboardActionCounter; extern usb_basic_keyboard_report_t* ActiveUsbBasicKeyboardReport; diff --git a/right/src/usb_interfaces/usb_interface_media_keyboard.c b/right/src/usb_interfaces/usb_interface_media_keyboard.c index 4693dcc..8147734 100644 --- a/right/src/usb_interfaces/usb_interface_media_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_media_keyboard.c @@ -3,9 +3,9 @@ uint32_t UsbMediaKeyboardActionCounter; static usb_media_keyboard_report_t usbMediaKeyboardReports[2]; usb_media_keyboard_report_t* ActiveUsbMediaKeyboardReport = usbMediaKeyboardReports; -bool IsUsbMediaKeyboardReportSent = false; +volatile bool IsUsbMediaKeyboardReportSent = false; -usb_media_keyboard_report_t* getInactiveUsbMediaKeyboardReport(void) +static usb_media_keyboard_report_t* getInactiveUsbMediaKeyboardReport(void) { return ActiveUsbMediaKeyboardReport == usbMediaKeyboardReports ? usbMediaKeyboardReports+1 : usbMediaKeyboardReports; } diff --git a/right/src/usb_interfaces/usb_interface_media_keyboard.h b/right/src/usb_interfaces/usb_interface_media_keyboard.h index 62859d6..8b39fb2 100644 --- a/right/src/usb_interfaces/usb_interface_media_keyboard.h +++ b/right/src/usb_interfaces/usb_interface_media_keyboard.h @@ -28,7 +28,7 @@ // Variables: - extern bool IsUsbMediaKeyboardReportSent; + extern volatile bool IsUsbMediaKeyboardReportSent; extern uint32_t UsbMediaKeyboardActionCounter; extern usb_media_keyboard_report_t* ActiveUsbMediaKeyboardReport; diff --git a/right/src/usb_interfaces/usb_interface_mouse.c b/right/src/usb_interfaces/usb_interface_mouse.c index 27acb64..711e3cd 100644 --- a/right/src/usb_interfaces/usb_interface_mouse.c +++ b/right/src/usb_interfaces/usb_interface_mouse.c @@ -3,9 +3,9 @@ uint32_t UsbMouseActionCounter; static usb_mouse_report_t usbMouseReports[2]; usb_mouse_report_t* ActiveUsbMouseReport = usbMouseReports; -bool IsUsbMouseReportSent = false; +volatile bool IsUsbMouseReportSent = false; -usb_mouse_report_t* getInactiveUsbMouseReport(void) +static usb_mouse_report_t* getInactiveUsbMouseReport(void) { return ActiveUsbMouseReport == usbMouseReports ? usbMouseReports+1 : usbMouseReports; } diff --git a/right/src/usb_interfaces/usb_interface_mouse.h b/right/src/usb_interfaces/usb_interface_mouse.h index e0e1e67..e1a2cc7 100644 --- a/right/src/usb_interfaces/usb_interface_mouse.h +++ b/right/src/usb_interfaces/usb_interface_mouse.h @@ -31,7 +31,7 @@ // Variables: - extern bool IsUsbMouseReportSent; + extern volatile bool IsUsbMouseReportSent; extern uint32_t UsbMouseActionCounter; extern usb_mouse_report_t* ActiveUsbMouseReport; diff --git a/right/src/usb_interfaces/usb_interface_system_keyboard.c b/right/src/usb_interfaces/usb_interface_system_keyboard.c index 7e6e61a..e039069 100644 --- a/right/src/usb_interfaces/usb_interface_system_keyboard.c +++ b/right/src/usb_interfaces/usb_interface_system_keyboard.c @@ -3,9 +3,9 @@ uint32_t UsbSystemKeyboardActionCounter; static usb_system_keyboard_report_t usbSystemKeyboardReports[2]; usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport = usbSystemKeyboardReports; -bool IsUsbSystemKeyboardReportSent = false; +volatile bool IsUsbSystemKeyboardReportSent = false; -usb_system_keyboard_report_t* getInactiveUsbSystemKeyboardReport() +static usb_system_keyboard_report_t* getInactiveUsbSystemKeyboardReport() { return ActiveUsbSystemKeyboardReport == usbSystemKeyboardReports ? usbSystemKeyboardReports+1 : usbSystemKeyboardReports; } diff --git a/right/src/usb_interfaces/usb_interface_system_keyboard.h b/right/src/usb_interfaces/usb_interface_system_keyboard.h index d19d362..0a84353 100644 --- a/right/src/usb_interfaces/usb_interface_system_keyboard.h +++ b/right/src/usb_interfaces/usb_interface_system_keyboard.h @@ -29,7 +29,7 @@ // Variables: - extern bool IsUsbSystemKeyboardReportSent; + extern volatile bool IsUsbSystemKeyboardReportSent; extern uint32_t UsbSystemKeyboardActionCounter; extern usb_system_keyboard_report_t* ActiveUsbSystemKeyboardReport; diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index f4b0277..7fb3eeb 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -17,11 +17,11 @@ #include "config_parser/parse_keymap.h" #include "usb_commands/usb_command_get_debug_buffer.h" -uint32_t UsbReportUpdateTime = 0; +static uint32_t UsbReportUpdateTime = 0; static uint32_t elapsedTime; uint16_t DoubleTapSwitchLayerTimeout = 150; -uint16_t DoubleTapSwitchLayerReleaseTimeout = 100; +static uint16_t DoubleTapSwitchLayerReleaseTimeout = 100; static bool activeMouseStates[ACTIVE_MOUSE_STATES_COUNT]; @@ -53,7 +53,7 @@ mouse_kinetic_state_t MouseScrollState = { .acceleratedSpeed = 50, }; -void processMouseKineticState(mouse_kinetic_state_t *kineticState) +static void processMouseKineticState(mouse_kinetic_state_t *kineticState) { float initialSpeed = kineticState->intMultiplier * kineticState->initialSpeed; float acceleration = kineticState->intMultiplier * kineticState->acceleration; @@ -155,7 +155,7 @@ void processMouseKineticState(mouse_kinetic_state_t *kineticState) kineticState->wasMoveAction = isMoveAction; } -void processMouseActions() +static void processMouseActions() { processMouseKineticState(&MouseMoveState); ActiveUsbMouseReport->x = MouseMoveState.xOut; @@ -193,7 +193,7 @@ static uint8_t basicScancodeIndex = 0; static uint8_t mediaScancodeIndex = 0; static uint8_t systemScancodeIndex = 0; -void applyKeyAction(key_state_t *keyState, key_action_t *action) +static void applyKeyAction(key_state_t *keyState, key_action_t *action) { static key_state_t *doubleTapSwitchLayerKey; static uint32_t doubleTapSwitchLayerStartTime; @@ -265,7 +265,7 @@ static uint8_t secondaryRoleSlotId; static uint8_t secondaryRoleKeyId; static secondary_role_t secondaryRole; -void updateActiveUsbReports(void) +static void updateActiveUsbReports(void) { memset(activeMouseStates, 0, ACTIVE_MOUSE_STATES_COUNT); @@ -369,10 +369,10 @@ void updateActiveUsbReports(void) previousLayer = activeLayer; } -bool UsbBasicKeyboardReportEverSent = false; -bool UsbMediaKeyboardReportEverSent = false; -bool UsbSystemKeyboardReportEverSent = false; -bool UsbMouseReportEverSentEverSent = false; +static bool UsbBasicKeyboardReportEverSent = false; +static bool UsbMediaKeyboardReportEverSent = false; +static bool UsbSystemKeyboardReportEverSent = false; +static bool UsbMouseReportEverSentEverSent = false; uint32_t UsbReportUpdateCounter; static uint32_t lastUsbUpdateTime; @@ -425,15 +425,33 @@ void UpdateUsbReports(void) updateActiveUsbReports(); - SwitchActiveUsbBasicKeyboardReport(); - SwitchActiveUsbMediaKeyboardReport(); - SwitchActiveUsbSystemKeyboardReport(); - SwitchActiveUsbMouseReport(); + static usb_basic_keyboard_report_t last_basic_report = { .scancodes[0] = 0xFF }; + if (memcmp(ActiveUsbBasicKeyboardReport, &last_basic_report, sizeof(usb_basic_keyboard_report_t)) != 0) { + last_basic_report = *ActiveUsbBasicKeyboardReport; + SwitchActiveUsbBasicKeyboardReport(); + IsUsbBasicKeyboardReportSent = false; + } - IsUsbBasicKeyboardReportSent = false; - IsUsbMediaKeyboardReportSent = false; - IsUsbSystemKeyboardReportSent = false; - IsUsbMouseReportSent = false; + static usb_media_keyboard_report_t last_media_report = { .scancodes[0] = 0xFF }; + if (memcmp(ActiveUsbMediaKeyboardReport, &last_media_report, sizeof(usb_media_keyboard_report_t)) != 0) { + last_media_report = *ActiveUsbMediaKeyboardReport; + SwitchActiveUsbMediaKeyboardReport(); + IsUsbMediaKeyboardReportSent = false; + } + + static usb_system_keyboard_report_t last_system_report = { .scancodes[0] = 0xFF }; + if (memcmp(ActiveUsbSystemKeyboardReport, &last_system_report, sizeof(usb_system_keyboard_report_t)) != 0) { + last_system_report = *ActiveUsbSystemKeyboardReport; + SwitchActiveUsbSystemKeyboardReport(); + IsUsbSystemKeyboardReportSent = false; + } + + static usb_mouse_report_t last_mouse_report = { .buttons = 0xFF }; + if (memcmp(ActiveUsbMouseReport, &last_mouse_report, sizeof(usb_mouse_report_t)) != 0) { + last_mouse_report = *ActiveUsbMouseReport; + SwitchActiveUsbMouseReport(); + IsUsbMouseReportSent = false; + } Timer_SetCurrentTime(&lastUsbUpdateTime); } From 5d59540c510fe7efc0399b194ed0d4a35ca9cf3a Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 19:26:04 +0100 Subject: [PATCH 08/10] Updated submodule after https://github.com/UltimateHackingKeyboard/KSDK_2.0_MK22FN512xxx12/pull/1 was merged --- lib/KSDK_2.0_MK22FN512xxx12 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/KSDK_2.0_MK22FN512xxx12 b/lib/KSDK_2.0_MK22FN512xxx12 index 9fbf2bb..7ddde5e 160000 --- a/lib/KSDK_2.0_MK22FN512xxx12 +++ b/lib/KSDK_2.0_MK22FN512xxx12 @@ -1 +1 @@ -Subproject commit 9fbf2bbf085a065a96fb6b2db11d03c3112bf7ba +Subproject commit 7ddde5eadbd33eacc28c9ecee06f5481baf5aa89 From c109a9e202e216469e8b729cf14c5cf342ad0e52 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 19:29:24 +0100 Subject: [PATCH 09/10] The layer keys can now also wake up the computer --- right/src/led_display.c | 2 +- right/src/led_display.h | 3 ++- right/src/usb_composite_device.c | 6 ++++++ right/src/usb_report_updater.c | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/right/src/led_display.c b/right/src/led_display.c index d6fab2b..ed00fd7 100644 --- a/right/src/led_display.c +++ b/right/src/led_display.c @@ -90,7 +90,7 @@ void LedDisplay_SetCurrentKeymapText(void) LedDisplay_SetText(currentKeymap->abbreviationLen, currentKeymap->abbreviation); } -void LedDisplay_SetLayer(uint8_t layerId) +void LedDisplay_SetLayer(layer_id_t layerId) { for (uint8_t i = 13; i <= 45; i += 16) { LedDriverValues[LedDriverId_Left][i] = 0; diff --git a/right/src/led_display.h b/right/src/led_display.h index 2d98423..aa1bbce 100644 --- a/right/src/led_display.h +++ b/right/src/led_display.h @@ -5,6 +5,7 @@ #include #include + #include "layer.h" // Typedefs: @@ -23,7 +24,7 @@ void LedDisplay_SetText(uint8_t length, const char* text); void LedDisplay_SetCurrentKeymapText(void); - void LedDisplay_SetLayer(uint8_t layerId); + void LedDisplay_SetLayer(layer_id_t layerId); void LedDisplay_SetIcon(led_display_icon_t icon, bool isEnabled); bool LedDisplay_GetIcon(led_display_icon_t icon); diff --git a/right/src/usb_composite_device.c b/right/src/usb_composite_device.c index e5fa973..2b43662 100644 --- a/right/src/usb_composite_device.c +++ b/right/src/usb_composite_device.c @@ -187,6 +187,9 @@ static void ComputerIsSleeping(void) { LedSlaveDriver_Init(LedDriverId_Right); LedSlaveDriver_Init(LedDriverId_Left); + // Turn layer LEDs off + LedDisplay_SetLayer(LayerId_Base); + // Clear the text LedDisplay_SetText(0, NULL); #endif @@ -204,6 +207,9 @@ void WakeupComputer(bool sendResume) { LedSlaveDriver_Init(LedDriverId_Right); LedSlaveDriver_Init(LedDriverId_Left); + // Update the active layer + LedDisplay_SetLayer(GetActiveLayer()); + // Restore icon states LedDisplay_SetIcon(LedDisplayIcon_CapsLock, capsLockOn); LedDisplay_SetIcon(LedDisplayIcon_Agent, agentOn); diff --git a/right/src/usb_report_updater.c b/right/src/usb_report_updater.c index 8832b55..206968e 100644 --- a/right/src/usb_report_updater.c +++ b/right/src/usb_report_updater.c @@ -453,7 +453,7 @@ void UpdateUsbReports(void) IsUsbMouseReportSent = false; } - if ((!IsUsbBasicKeyboardReportSent || !IsUsbMediaKeyboardReportSent || !IsUsbSystemKeyboardReportSent || !IsUsbMouseReportSent) && IsComputerSleeping()) + if ((previousLayer != LayerId_Base || !IsUsbBasicKeyboardReportSent || !IsUsbMediaKeyboardReportSent || !IsUsbSystemKeyboardReportSent || !IsUsbMouseReportSent) && IsComputerSleeping()) WakeupComputer(true); // Wake up the computer if any key is pressed and the computer is sleeping Timer_SetCurrentTime(&lastUsbUpdateTime); From ab24e78a5b2c1bee228468f139c694de049153f9 Mon Sep 17 00:00:00 2001 From: Kristian Sloth Lauszus Date: Sun, 11 Mar 2018 20:05:10 +0100 Subject: [PATCH 10/10] Wake up the keyboard if there is any activity on the bus This fixes: https://github.com/UltimateHackingKeyboard/firmware/pull/97#issuecomment-372091423 --- right/src/usb_composite_device.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/right/src/usb_composite_device.c b/right/src/usb_composite_device.c index 2b43662..1a0ddf2 100644 --- a/right/src/usb_composite_device.c +++ b/right/src/usb_composite_device.c @@ -227,6 +227,9 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, return status; } + if (computerSleeping) + WakeupComputer(false); // Wake up the keyboard if there is any activity on the bus + switch (event) { case kUSB_DeviceEventBusReset: UsbCompositeDevice.attach = 0; @@ -239,10 +242,9 @@ static usb_status_t usbDeviceCallback(usb_device_handle handle, uint32_t event, } break; case kUSB_DeviceEventResume: - if (computerSleeping) { - WakeupComputer(false); // The computer just woke up, so restore the LEDs - status = kStatus_USB_Success; - } + // We will just wake up the computer if there is any activity on the bus + // The problem is that the computer won't send a resume event when it boots, so the lights will never come back on + status = kStatus_USB_Success; break; case kUSB_DeviceEventSetConfiguration: UsbCompositeDevice.attach = 1;