From c41e48a0ab0712d2667feb6b5dd8a4d5491cfcc5 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 22 Mar 2019 22:15:26 +0900 Subject: [PATCH 01/16] core: Fix uart.c for ATmega32U4 --- tmk_core/common/{ => avr}/uart.c | 54 +++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) rename tmk_core/common/{ => avr}/uart.c (69%) diff --git a/tmk_core/common/uart.c b/tmk_core/common/avr/uart.c similarity index 69% rename from tmk_core/common/uart.c rename to tmk_core/common/avr/uart.c index c17649b0..d9d42390 100644 --- a/tmk_core/common/uart.c +++ b/tmk_core/common/avr/uart.c @@ -1,4 +1,3 @@ -// TODO: Teensy support(ATMega32u4/AT90USB128) // Fixed for Arduino Duemilanove ATmega168p by Jun Wako /* UART Example for Teensy USB Development Board * http://www.pjrc.com/teensy/ @@ -32,6 +31,39 @@ #include "uart.h" +#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__) +# define UDRn UDR0 +# define UBRRn UBRR0 +# define UCSRnA UCSR0A +# define UCSRnB UCSR0B +# define UCSRnC UCSR0C +# define U2Xn U2X0 +# define RXENn RXEN0 +# define TXENn TXEN0 +# define RXCIEn RXCIE0 +# define UCSZn1 UCSZ01 +# define UCSZn0 UCSZ00 +# define UDRIEn UDRIE0 +# define UDRE_vect USART_UDRE_vect +# define RX_vect USART_RX_vect +#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U2__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) +# define UDRn UDR1 +# define UBRRn UBRR1 +# define UCSRnA UCSR1A +# define UCSRnB UCSR1B +# define UCSRnC UCSR1C +# define U2Xn U2X1 +# define RXENn RXEN1 +# define TXENn TXEN1 +# define RXCIEn RXCIE1 +# define UCSZn1 UCSZ11 +# define UCSZn0 UCSZ10 +# define UDRIEn UDRIE1 +# define UDRE_vect USART1_UDRE_vect +# define RX_vect USART1_RX_vect +#endif + + // These buffers may be any size from 2 to 256 bytes. #define RX_BUFFER_SIZE 64 #define TX_BUFFER_SIZE 40 @@ -47,10 +79,10 @@ static volatile uint8_t rx_buffer_tail; void uart_init(uint32_t baud) { cli(); - UBRR0 = (F_CPU / 4 / baud - 1) / 2; - UCSR0A = (1<= TX_BUFFER_SIZE) i = 0; - UDR0 = tx_buffer[i]; + UDRn = tx_buffer[i]; tx_buffer_tail = i; } } // Receive Interrupt -ISR(USART_RX_vect) +ISR(RX_vect) { uint8_t c, i; - c = UDR0; + c = UDRn; i = rx_buffer_head + 1; if (i >= RX_BUFFER_SIZE) i = 0; if (i != rx_buffer_tail) { From e23520662dfac30c38c41c87f5593c21f1abfb1d Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 26 Mar 2019 16:09:45 +0900 Subject: [PATCH 02/16] lufa: Add debug print support with UART --- tmk_core/common.mk | 8 ++++++-- tmk_core/protocol/lufa.mk | 13 +++++++++++++ tmk_core/protocol/lufa/lufa.c | 29 +++++++++++++++++++---------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/tmk_core/common.mk b/tmk_core/common.mk index 14951ec8..06507ecf 100644 --- a/tmk_core/common.mk +++ b/tmk_core/common.mk @@ -50,8 +50,12 @@ endif ifeq (yes,$(strip $(CONSOLE_ENABLE))) OPT_DEFS += -DCONSOLE_ENABLE else - OPT_DEFS += -DNO_PRINT - OPT_DEFS += -DNO_DEBUG + # Remove print functions when console is disabled and + # no other print method like UART is available + ifneq (yes, $(strip $(DEBUG_PRINT_AVAILABLE))) + OPT_DEFS += -DNO_PRINT + OPT_DEFS += -DNO_DEBUG + endif endif ifeq (yes,$(strip $(COMMAND_ENABLE))) diff --git a/tmk_core/protocol/lufa.mk b/tmk_core/protocol/lufa.mk index 1b529bde..19fc80d4 100644 --- a/tmk_core/protocol/lufa.mk +++ b/tmk_core/protocol/lufa.mk @@ -49,7 +49,20 @@ OPT_DEFS += $(LUFA_OPTS) # This indicates using LUFA stack OPT_DEFS += -DPROTOCOL_LUFA +ifeq (yes,$(strip $(LUFA_DEBUG))) + LUFA_OPTS += -DLUFA_DEBUG +endif + ifeq (yes,$(strip $(LUFA_DEBUG_SUART))) SRC += common/avr/suart.S LUFA_OPTS += -DLUFA_DEBUG_SUART + # Keep print/debug lines when disabling HID console. See common.mk. + DEBUG_PRINT_AVAILABLE = yes +endif + +ifeq (yes,$(strip $(LUFA_DEBUG_UART))) + SRC += common/avr/uart.c + LUFA_OPTS += -DLUFA_DEBUG_UART + # Keep print/debug lines when disabling HID console. See common.mk. + DEBUG_PRINT_AVAILABLE = yes endif diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index b5d0b50e..ffa5cfbd 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -56,6 +56,10 @@ #include "avr/suart.h" #endif +#ifdef LUFA_DEBUG_UART +#include "uart.h" +#endif + #include "matrix.h" #include "descriptor.h" #include "lufa.h" @@ -218,7 +222,9 @@ static void console_task(void) */ void EVENT_USB_Device_Connect(void) { +#ifdef LUFA_DEBUG print("[C]"); +#endif /* For battery powered device */ if (!USB_IsInitialized) { USB_Disable(); @@ -229,7 +235,9 @@ void EVENT_USB_Device_Connect(void) void EVENT_USB_Device_Disconnect(void) { +#ifdef LUFA_DEBUG print("[D]"); +#endif /* For battery powered device */ USB_IsInitialized = false; /* TODO: This doesn't work. After several plug in/outs can not be enumerated. @@ -575,25 +583,22 @@ static void send_consumer(uint16_t data) /******************************************************************************* * sendchar ******************************************************************************/ -#ifdef CONSOLE_ENABLE int8_t sendchar(uint8_t c) { #ifdef LUFA_DEBUG_SUART xmit(c); #endif - bool r = console_putc(c); - return (r ? 0 : -1); -} -#else -int8_t sendchar(uint8_t c) -{ - #ifdef LUFA_DEBUG_SUART - xmit(c); + #ifdef LUFA_DEBUG_UART + uart_putchar(c); #endif + + #ifdef CONSOLE_ENABLE + console_putc(c); + #endif + return 0; } -#endif /******************************************************************************* @@ -629,6 +634,10 @@ int main(void) SUART_OUT_PORT |= (1< Date: Sun, 31 Mar 2019 14:42:19 +0900 Subject: [PATCH 03/16] lufa: This prevents resume when debug --- tmk_core/protocol/lufa/lufa.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index ffa5cfbd..400011d1 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -669,9 +669,6 @@ int main(void) print("\nKeyboard start.\n"); while (1) { while (USB_DeviceState == DEVICE_STATE_Suspended) { -#ifdef LUFA_DEBUG - print("[s]"); -#endif hook_usb_suspend_loop(); } From 527bdbbcbddc51b22c4fb210889998d550544189 Mon Sep 17 00:00:00 2001 From: tmk Date: Sun, 21 Apr 2019 09:19:22 +0900 Subject: [PATCH 04/16] usb_usb: Add debug print for low level --- converter/usb_usb/Makefile.debug | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/converter/usb_usb/Makefile.debug b/converter/usb_usb/Makefile.debug index e229cd78..830995b4 100644 --- a/converter/usb_usb/Makefile.debug +++ b/converter/usb_usb/Makefile.debug @@ -1,6 +1,20 @@ TARGET = usb_usb_debug -UNIMAP_ENABLE = yes + +#UNIMAP_ENABLE = yes #KEYMAP_SECTION_ENABLE = yes -#LUFA_DEBUG = yes -OPT_DEFS += -DDEBUG_USB_HOST + +# LUFA debug print +# This may prevent USB enumeration and keyboard init +LUFA_DEBUG = yes +# Select one of outputs for debug print +LUFA_DEBUG_UART = yes +#LUFA_DEBUG_SUART = yes + +# USB_Host_Shield_2.0 debug print +# This may prevent USB enumeration and keyboard init +#OPT_DEFS += -DDEBUG_USB_HOST + +CONSOLE_ENABLE = yes +MOUSEKEY_ENABLE = no +EXTRAKEY_ENABLE = no include Makefile From 292cc939ec6bea70c4d9b7b2130c35efd1545fe8 Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 26 Apr 2019 09:17:50 +0900 Subject: [PATCH 05/16] lufa: Fix for UART debug print --- tmk_core/protocol/lufa/lufa.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 400011d1..eade7d1b 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -713,7 +713,10 @@ void hook_usb_suspend_entry(void) __attribute__((weak)) void hook_usb_suspend_loop(void) { +#ifndef LUFA_DEBUG_UART + // This corrupts debug print when suspend suspend_power_down(); +#endif if (USB_Device_RemoteWakeupEnabled && suspend_wakeup_condition()) { USB_Device_SendRemoteWakeup(); } From 55443fabb731459e21b45781c6d951edac5d75f4 Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 2 May 2019 20:57:48 +0900 Subject: [PATCH 06/16] core: Avoid deadlock when uart.c is used in ISR --- tmk_core/common/avr/uart.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tmk_core/common/avr/uart.c b/tmk_core/common/avr/uart.c index d9d42390..0fec705f 100644 --- a/tmk_core/common/avr/uart.c +++ b/tmk_core/common/avr/uart.c @@ -66,7 +66,7 @@ // These buffers may be any size from 2 to 256 bytes. #define RX_BUFFER_SIZE 64 -#define TX_BUFFER_SIZE 40 +#define TX_BUFFER_SIZE 256 static volatile uint8_t tx_buffer[TX_BUFFER_SIZE]; static volatile uint8_t tx_buffer_head; @@ -95,6 +95,8 @@ void uart_putchar(uint8_t c) i = tx_buffer_head + 1; if (i >= TX_BUFFER_SIZE) i = 0; + // return immediately to avoid deadlock when interrupt is disabled(called from ISR) + if (tx_buffer_tail == i && (SREG & (1< Date: Mon, 6 May 2019 16:27:46 +0900 Subject: [PATCH 07/16] lufa: Fix Change debug print message --- tmk_core/protocol/lufa/lufa.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index eade7d1b..991c2719 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -642,7 +642,7 @@ int main(void) print_set_sendchar(sendchar); host_set_driver(&lufa_driver); - print("Keyboard init.\n"); + print("\n\nKeyboard init.\n"); hook_early_init(); keyboard_setup(); setup_usb(); @@ -663,6 +663,7 @@ int main(void) #endif matrix_scan(); } + print("\nUSB configured.\n"); hook_late_init(); From 993a9b02f77eda8b06cf096f345ad3f35fafc7ba Mon Sep 17 00:00:00 2001 From: tmk Date: Fri, 29 Mar 2019 15:45:20 +0900 Subject: [PATCH 08/16] lufa: Fix for freeze at re/boot time problem Calling led_set() in ISR can cause the problem. With converter especially, led_set() can take long time and USB can be stuck in the end. USB-USB converter freezes occasionally when computer power up or reboot. https://geekhack.org/index.php?topic=69169.msg2740179#msg2740179 This is also related to suspend/wakeup issue #386. --- tmk_core/protocol/lufa/lufa.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 991c2719..48034dbe 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -697,12 +697,12 @@ static uint8_t _led_stats = 0; __attribute__((weak)) void hook_usb_suspend_entry(void) { - // Turn LED off to save power - // Set 0 with putting aside status before suspend and restore - // it after wakeup, then LED is updated at keyboard_task() in main loop + // Turn off LED to save power and keep its status to resotre it later. + // LED status will be updated by keyboard_task() in main loop hopefully. _led_stats = keyboard_led_stats; keyboard_led_stats = 0; - led_set(keyboard_led_stats); + + // Calling long task here can prevent USB state transition matrix_clear(); clear_keyboard(); @@ -731,10 +731,8 @@ void hook_usb_wakeup(void) sleep_led_disable(); #endif - // Restore LED status - // BIOS/grub won't recognize/enumerate if led_set() takes long(around 40ms?) - // Converters fall into the case and miss wakeup event(timeout to reply?) in the end. - //led_set(host_keyboard_leds()); - // Instead, restore stats and update at keyboard_task() in main loop + // Restore LED status and update at keyboard_task() in main loop keyboard_led_stats = _led_stats; + + // Calling long task here can prevent USB state transition } From 4e83400fc6f67a0559cf343ba5846b505c35fd30 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 6 May 2019 23:01:18 +0900 Subject: [PATCH 09/16] usb_hid: Change to USB_Host_Shield_2.0 of tmk repo - Fix SOF/Keep Alive start timing - Disable bus detection during settling after attach - Remove keyboard LED blinking at configuring --- .gitmodules | 6 +++--- tmk_core/protocol/usb_hid.mk | 2 +- tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git | 1 - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk | 1 + 4 files changed, 5 insertions(+), 5 deletions(-) delete mode 160000 tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git create mode 160000 tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk diff --git a/.gitmodules b/.gitmodules index 3ae861fb..c688aa84 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git"] - path = tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git - url = https://github.com/felis/USB_Host_Shield_2.0.git +[submodule "tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk"] + path = tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk + url = https://github.com/tmk/USB_Host_Shield_2.0.git diff --git a/tmk_core/protocol/usb_hid.mk b/tmk_core/protocol/usb_hid.mk index 495c4bee..bba4292f 100644 --- a/tmk_core/protocol/usb_hid.mk +++ b/tmk_core/protocol/usb_hid.mk @@ -4,7 +4,7 @@ USB_HID_DIR = protocol/usb_hid # # USB Host Shield # -USB_HOST_SHIELD_DIR = $(USB_HID_DIR)/USB_Host_Shield_2.0-git +USB_HOST_SHIELD_DIR = $(USB_HID_DIR)/USB_Host_Shield_2.0-tmk USB_HOST_SHIELD_SRC = \ $(USB_HOST_SHIELD_DIR)/Usb.cpp \ $(USB_HOST_SHIELD_DIR)/usbhid.cpp \ diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git deleted file mode 160000 index ed08df7e..00000000 --- a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-git +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ed08df7e68af06a5612d938b1bbf55cc4458518a diff --git a/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk new file mode 160000 index 00000000..48b2a095 --- /dev/null +++ b/tmk_core/protocol/usb_hid/USB_Host_Shield_2.0-tmk @@ -0,0 +1 @@ +Subproject commit 48b2a0950496c7b8ba1bb6ddb937b6f00a6de781 From dd7b75040a7f3cc62fe58d6d4b3cc2a331d487b3 Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 6 May 2019 01:52:25 +0900 Subject: [PATCH 10/16] lufa:usb_usb: matrix_scan() is no longer needed This is due to SOF timing fix of USB_Host_Shield_2.0. The matrix_scan() was needed for usb_usb converter to recognize FC660C at startup. --- tmk_core/protocol/lufa/lufa.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 48034dbe..aecaa050 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -661,7 +661,6 @@ int main(void) #else USB_USBTask(); #endif - matrix_scan(); } print("\nUSB configured.\n"); From 7c228967a405b04d58bb06e619de8d1d7bb3c15b Mon Sep 17 00:00:00 2001 From: tmk Date: Mon, 6 May 2019 12:15:37 +0900 Subject: [PATCH 11/16] usb_usb: Order of init prevents uneeded bus reset Device classes are registered to array of config driver pool and tried in that order until proper class is found in configuration process. If tried driver cannot handle device, bus reset is issued to the device. --- converter/usb_usb/usb_usb.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/converter/usb_usb/usb_usb.cpp b/converter/usb_usb/usb_usb.cpp index 086c90ae..75750911 100644 --- a/converter/usb_usb/usb_usb.cpp +++ b/converter/usb_usb/usb_usb.cpp @@ -71,8 +71,6 @@ static bool matrix_is_mod =false; * This supports two cascaded hubs and four keyboards */ USB usb_host; -USBHub hub1(&usb_host); -USBHub hub2(&usb_host); HIDBoot kbd1(&usb_host); HIDBoot kbd2(&usb_host); HIDBoot kbd3(&usb_host); @@ -81,6 +79,8 @@ KBDReportParser kbd_parser1; KBDReportParser kbd_parser2; KBDReportParser kbd_parser3; KBDReportParser kbd_parser4; +USBHub hub1(&usb_host); +USBHub hub2(&usb_host); uint8_t matrix_rows(void) { return MATRIX_ROWS; } From 2fd396b8ca15592729f2ab84fce474bf987deb11 Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 8 May 2019 12:53:15 +0900 Subject: [PATCH 12/16] usb_usb: Add option for disabling suspend power down --- converter/usb_usb/config.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/converter/usb_usb/config.h b/converter/usb_usb/config.h index 87da4a45..b2e5a9a3 100644 --- a/converter/usb_usb/config.h +++ b/converter/usb_usb/config.h @@ -36,4 +36,8 @@ along with this program. If not, see . /* key combination for command */ #define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) +// Disable power saving in USB suspend loop but remote wakeup is still valid. +// This allows keep USB::Task() going during suspend without power down time delay. +//#define NO_SUSPEND_POWER_DOWN + #endif From d8e304e141723f950ea610c1b3a3fe6f85a535e9 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 7 May 2019 11:09:25 +0900 Subject: [PATCH 13/16] lufa: Startup and suspend loop can be disabled Startup wait loop is originally intended to start keyboard task loop and after console output endpoint becomes available. But now that console output is buffered when it is not available and you don't have to wait for it to be ready. You can disable the startup wait loop by defining NO_WAIT_FOR_USB_CONFIGURED in config.h Suspend loop is used for power saving by making tasks stop while USB bus is under suspend status. But this may cause problem on some devices like converter that must keep doing its task to retain communication with keyboard. The suspend loop can block its task for around 15-17ms. You can disable the suspend loop by defining NO_USB_SUSPEND_LOOP in config.h. --- tmk_core/protocol/lufa/lufa.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index aecaa050..ce15ec73 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -654,6 +654,7 @@ int main(void) keyboard_init(); +#ifndef NO_USB_STARTUP_WAIT_LOOP /* wait for USB startup */ while (USB_DeviceState != DEVICE_STATE_Configured) { #if defined(INTERRUPT_CONTROL_ENDPOINT) @@ -663,14 +664,17 @@ int main(void) #endif } print("\nUSB configured.\n"); +#endif hook_late_init(); print("\nKeyboard start.\n"); while (1) { +#ifndef NO_USB_SUSPEND_LOOP while (USB_DeviceState == DEVICE_STATE_Suspended) { hook_usb_suspend_loop(); } +#endif keyboard_task(); From 00bba0fdb5ced3156879ecbda83955881d974d48 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 7 May 2019 12:01:59 +0900 Subject: [PATCH 14/16] usb_usb: Add options for disabling blocking loops USB startup wait loop delays UHS2 Task() starting for a while like 200-600ms and USB suspend loop blocks the Task() while power saving like 15-17ms. These loops may cause keyboard enumeration failure, perhaps. Not confirmed it yet though. --- converter/usb_usb/config.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/converter/usb_usb/config.h b/converter/usb_usb/config.h index b2e5a9a3..7b80fc21 100644 --- a/converter/usb_usb/config.h +++ b/converter/usb_usb/config.h @@ -36,8 +36,17 @@ along with this program. If not, see . /* key combination for command */ #define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) + // Disable power saving in USB suspend loop but remote wakeup is still valid. // This allows keep USB::Task() going during suspend without power down time delay. //#define NO_SUSPEND_POWER_DOWN + +// Disable USB startup wait, which can delays starting UHS2 Task() for 350-600ms. +//#define NO_USB_STARTUP_WAIT_LOOP + +// Disable USB suspend loop, which blocks UHS2 Task() while power saving. +// Note that this also disables power saving and remote wakeup from keyboard completely. +//#define NO_USB_SUSPEND_LOOP + #endif From f3e498590cefe28dba4419aa9525655e747e01dd Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 8 May 2019 11:44:56 +0900 Subject: [PATCH 15/16] core: Add hook_usb_startup_wait_loop --- tmk_core/common/hook.h | 11 +++++++++++ tmk_core/common/suspend.h | 8 ++++++++ tmk_core/doc/hook.txt | 2 ++ tmk_core/protocol/lufa/lufa.c | 4 ++++ 4 files changed, 25 insertions(+) diff --git a/tmk_core/common/hook.h b/tmk_core/common/hook.h index 56fe5677..140e2779 100644 --- a/tmk_core/common/hook.h +++ b/tmk_core/common/hook.h @@ -21,6 +21,10 @@ along with this program. If not, see . #include "keyboard.h" #include "led.h" +#ifdef __cplusplus +extern "C" { +#endif + /* ------------------------------------- * Protocol hooks * ------------------------------------- */ @@ -47,6 +51,10 @@ void hook_usb_suspend_loop(void); * the "normal" indicator LED status by default. */ void hook_usb_wakeup(void); +/* Called repeatedly until getting to CONFIGURED state */ +/* Default behaviour: do nothing. */ +void hook_usb_startup_wait_loop(void); + /* ------------------------------------- * Keyboard hooks @@ -76,5 +84,8 @@ void hook_keyboard_leds_change(uint8_t led_status); /* Default behaviour: do nothing. */ void hook_bootmagic(void); +#ifdef __cplusplus +} +#endif #endif /* _HOOKS_H_ */ diff --git a/tmk_core/common/suspend.h b/tmk_core/common/suspend.h index 80617a82..f0a668cf 100644 --- a/tmk_core/common/suspend.h +++ b/tmk_core/common/suspend.h @@ -5,9 +5,17 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + void suspend_idle(uint8_t timeout); void suspend_power_down(void); bool suspend_wakeup_condition(void); void suspend_wakeup_init(void); +#ifdef __cplusplus +} +#endif + #endif diff --git a/tmk_core/doc/hook.txt b/tmk_core/doc/hook.txt index 1689034e..acd77cce 100644 --- a/tmk_core/doc/hook.txt +++ b/tmk_core/doc/hook.txt @@ -9,6 +9,8 @@ Hook function | Timing `hook_early_init(void)` | Early in the boot process, before the matrix is initialized and before a connection is made with the host. Thus, this hook has access to very few parameters, but it is a good place to define any custom parameters needed by other early processes. `hook_late_init(void)` | Near the end of the boot process, after Boot Magic has run and LEDs have been initialized. `hook_bootmagic(void)` | During the Boot Magic window, after EEPROM and Bootloader checks are made, but before any other built-in Boot Magic checks are made. +`hook_usb_startup_wait_loop(void)` | Continuously, until the device gets ready and into USB configured state. + `hook_usb_wakeup(void)` | When the device wakes up from USB suspend state. `hook_usb_suspend_entry(void)` | When the device enters USB suspend state. `hook_usb_suspend_loop(void)` | Continuously, while the device is in USB suspend state. *Default action:* power down and periodically check the matrix, causing wakeup if needed. diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index ce15ec73..9dadd572 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -662,6 +662,7 @@ int main(void) #else USB_USBTask(); #endif + hook_usb_startup_wait_loop(); } print("\nUSB configured.\n"); #endif @@ -739,3 +740,6 @@ void hook_usb_wakeup(void) // Calling long task here can prevent USB state transition } + +__attribute__((weak)) +void hook_usb_startup_wait_loop(void) {} From 4880e2b661ac255c35c765cd6595ab9b1136ef2f Mon Sep 17 00:00:00 2001 From: tmk Date: Wed, 8 May 2019 11:48:47 +0900 Subject: [PATCH 16/16] usb_usb: Override startup and suspend hook --- converter/usb_usb/usb_usb.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/converter/usb_usb/usb_usb.cpp b/converter/usb_usb/usb_usb.cpp index 75750911..50d2578e 100644 --- a/converter/usb_usb/usb_usb.cpp +++ b/converter/usb_usb/usb_usb.cpp @@ -35,6 +35,10 @@ along with this program. If not, see . #include "host.h" #include "keyboard.h" +#include "hook.h" +#include "suspend.h" +#include "lufa.h" + /* KEY CODE to Matrix * @@ -233,3 +237,28 @@ void led_set(uint8_t usb_led) if (kbd3.isReady()) kbd3.SetReport(0, 0, 2, 0, 1, &usb_led); if (kbd4.isReady()) kbd4.SetReport(0, 0, 2, 0, 1, &usb_led); } + +// We need to keep doing UHS2 USB::Task() to initialize keyboard +// even before USB is not configured. +void hook_usb_startup_wait_loop(void) +{ + matrix_scan(); +} + +// We need to keep doing UHS2 USB::Task() to initialize keyboard +// even during USB bus is suspended and remote wakeup is not enabled yet on LUFA side. +// This situation can happen just after pluging converter into USB port. +void hook_usb_suspend_loop(void) +{ +#ifndef LUFA_DEBUG_UART + // This corrupts debug print when suspend + suspend_power_down(); +#endif + if (USB_Device_RemoteWakeupEnabled) { + if (suspend_wakeup_condition()) { + USB_Device_SendRemoteWakeup(); + } + } else { + matrix_scan(); + } +}