From 4962b743d2123bc0c00330b9e1a2ce687dad5414 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sat, 8 Feb 2020 03:03:51 +0000 Subject: [Keyboard] Port SPLIT_USB_DETECT to lily58 (#8107) * remove unused files * Port SPLIT_USB_DETECT to lily58 --- keyboards/lily58/rev1/matrix.c | 5 +-- keyboards/lily58/rev1/split_util.c | 76 ++++++++++++++++++++++++++------------ 2 files changed, 55 insertions(+), 26 deletions(-) (limited to 'keyboards/lily58/rev1') diff --git a/keyboards/lily58/rev1/matrix.c b/keyboards/lily58/rev1/matrix.c index 718cc57448..eaff974e5d 100755 --- a/keyboards/lily58/rev1/matrix.c +++ b/keyboards/lily58/rev1/matrix.c @@ -95,9 +95,8 @@ uint8_t matrix_cols(void) void matrix_init(void) { - debug_enable = true; - debug_matrix = true; - debug_mouse = true; + split_keyboard_setup(); + // initialize row and col unselect_rows(); init_cols(); diff --git a/keyboards/lily58/rev1/split_util.c b/keyboards/lily58/rev1/split_util.c index e1ff8b4379..316c1c389e 100755 --- a/keyboards/lily58/rev1/split_util.c +++ b/keyboards/lily58/rev1/split_util.c @@ -7,6 +7,7 @@ #include "split_util.h" #include "matrix.h" #include "keyboard.h" +#include "wait.h" #ifdef USE_MATRIX_I2C # include "i2c.h" @@ -14,19 +15,59 @@ # include "split_scomm.h" #endif +#ifndef SPLIT_USB_TIMEOUT +# define SPLIT_USB_TIMEOUT 2500 +#endif + volatile bool isLeftHand = true; -static void setup_handedness(void) { - #ifdef EE_HANDS - isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); - #else - // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c - #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT) - isLeftHand = !has_usb(); - #else - isLeftHand = has_usb(); - #endif - #endif +bool waitForUsb(void) { + for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) { + // This will return true of a USB connection has been established + if (UDADDR & _BV(ADDEN)) { + return true; + } + wait_ms(100); + } + + // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow + (USBCON &= ~(_BV(USBE) | _BV(OTGPADE))); + + return false; +} + +__attribute__((weak)) bool is_keyboard_left(void) { +#if defined(SPLIT_HAND_PIN) + // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand + setPinInput(SPLIT_HAND_PIN); + return readPin(SPLIT_HAND_PIN); +#elif defined(EE_HANDS) + return eeconfig_read_handedness(); +#elif defined(MASTER_RIGHT) + return !has_usb(); +#endif + + return has_usb(); +} + +__attribute__((weak)) bool has_usb(void) { + static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN; + + // only check once, as this is called often + if (usbstate == UNKNOWN) { +#if defined(SPLIT_USB_DETECT) + usbstate = waitForUsb() ? MASTER : SLAVE; +#elif defined(__AVR__) + USBCON |= (1 << OTGPADE); // enables VBUS pad + wait_us(5); + + usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS +#else + usbstate = MASTER; +#endif + } + + return (usbstate == MASTER); } static void keyboard_master_setup(void) { @@ -47,14 +88,8 @@ static void keyboard_slave_setup(void) { #endif } -bool has_usb(void) { - USBCON |= (1 << OTGPADE); //enables VBUS pad - _delay_us(5); - return (USBSTA & (1<