summaryrefslogtreecommitdiff
path: root/keyboards/helix/rev2/split_util.c
diff options
context:
space:
mode:
authorEric Bunton <stormbard@users.noreply.github.com>2019-12-07 07:17:13 -0500
committerJoel Challis <git@zvecr.com>2019-12-07 12:17:13 +0000
commit36a6e269bfff8b557a92e3c5e7a4a104d0dfe5f7 (patch)
tree034ef402f0b3b45767111450aab9dcbd5ec731c8 /keyboards/helix/rev2/split_util.c
parente8efc46e744f1a6188b2a98abf2e281f65e9f308 (diff)
Port SPLIT_USB_DETECT to helix/rev2 (#7385)
* Port SPLIT_USB_DETECT to helix/rev2 * Remove debug toggles. * Rename is_keyboard_master to has_usb in split_util
Diffstat (limited to 'keyboards/helix/rev2/split_util.c')
-rw-r--r--keyboards/helix/rev2/split_util.c80
1 files changed, 58 insertions, 22 deletions
diff --git a/keyboards/helix/rev2/split_util.c b/keyboards/helix/rev2/split_util.c
index e1ff8b4379..89df43e277 100644
--- a/keyboards/helix/rev2/split_util.c
+++ b/keyboards/helix/rev2/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,64 @@
# include "split_scomm.h"
#endif
+#ifdef EE_HANDS
+# include "eeconfig.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 +93,8 @@ static void keyboard_slave_setup(void) {
#endif
}
-bool has_usb(void) {
- USBCON |= (1 << OTGPADE); //enables VBUS pad
- _delay_us(5);
- return (USBSTA & (1<<VBUS)); //checks state of VBUS
-}
-
void split_keyboard_setup(void) {
- setup_handedness();
+ isLeftHand = is_keyboard_left();
if (has_usb()) {
keyboard_master_setup();
@@ -64,7 +104,3 @@ void split_keyboard_setup(void) {
sei();
}
-// this code runs before the usb and keyboard is initialized
-void matrix_setup(void) {
- split_keyboard_setup();
-}