diff options
author | Joel Challis <git@zvecr.com> | 2020-05-21 17:59:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-21 17:59:01 +0100 |
commit | 1816ad01d0efcc987b5aa7a833331b0f9d903d4f (patch) | |
tree | 4c70a2dd1903acd1373a2fbf462c10b62190fcfd | |
parent | 9c8f61dfa0dfb3a500ffd12b4a2d1a906a7b669f (diff) | |
download | qmk_firmware-1816ad01d0efcc987b5aa7a833331b0f9d903d4f.tar.gz qmk_firmware-1816ad01d0efcc987b5aa7a833331b0f9d903d4f.zip |
Use LUFA funcs for split_util (#8594)
-rw-r--r-- | quantum/split_common/split_util.c | 65 |
1 files changed, 44 insertions, 21 deletions
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c index fb6a3b85a..6f376ab40 100644 --- a/quantum/split_common/split_util.c +++ b/quantum/split_common/split_util.c | |||
@@ -6,6 +6,14 @@ | |||
6 | #include "transport.h" | 6 | #include "transport.h" |
7 | #include "quantum.h" | 7 | #include "quantum.h" |
8 | 8 | ||
9 | #ifdef PROTOCOL_LUFA | ||
10 | # include <LUFA/Drivers/USB/USB.h> | ||
11 | #endif | ||
12 | |||
13 | #ifdef PROTOCOL_VUSB | ||
14 | # include "usbdrv.h" | ||
15 | #endif | ||
16 | |||
9 | #ifdef EE_HANDS | 17 | #ifdef EE_HANDS |
10 | # include "eeconfig.h" | 18 | # include "eeconfig.h" |
11 | #endif | 19 | #endif |
@@ -22,30 +30,54 @@ | |||
22 | # define SPLIT_USB_TIMEOUT_POLL 10 | 30 | # define SPLIT_USB_TIMEOUT_POLL 10 |
23 | #endif | 31 | #endif |
24 | 32 | ||
33 | #ifdef PROTOCOL_CHIBIOS | ||
34 | # define SPLIT_USB_DETECT // Force this on for now | ||
35 | #endif | ||
36 | |||
25 | volatile bool isLeftHand = true; | 37 | volatile bool isLeftHand = true; |
26 | 38 | ||
27 | bool waitForUsb(void) { | 39 | #if defined(SPLIT_USB_DETECT) |
40 | # if defined(PROTOCOL_LUFA) | ||
41 | static inline bool usbHasActiveConnection(void) { return USB_Device_IsAddressSet(); } | ||
42 | static inline void usbDisable(void) { USB_Disable(); } | ||
43 | # elif defined(PROTOCOL_CHIBIOS) | ||
44 | static inline bool usbHasActiveConnection(void) { usbGetDriverStateI(&USBD1) == USB_ACTIVE; } | ||
45 | static inline void usbDisable(void) { usbStop(&USBD1); } | ||
46 | # elif defined(PROTOCOL_VUSB) | ||
47 | static inline bool usbHasActiveConnection(void) { | ||
48 | usbPoll(); | ||
49 | return usbConfiguration; | ||
50 | } | ||
51 | static inline void usbDisable(void) { usbDeviceDisconnect(); } | ||
52 | # else | ||
53 | static inline bool usbHasActiveConnection(void) { return true; } | ||
54 | static inline void usbDisable(void) {} | ||
55 | # endif | ||
56 | |||
57 | bool usbIsActive(void) { | ||
28 | for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) { | 58 | for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) { |
29 | // This will return true if a USB connection has been established | 59 | // This will return true if a USB connection has been established |
30 | #if defined(__AVR__) | 60 | if (usbHasActiveConnection()) { |
31 | if (UDADDR & _BV(ADDEN)) { | ||
32 | #else | ||
33 | if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) { | ||
34 | #endif | ||
35 | return true; | 61 | return true; |
36 | } | 62 | } |
37 | wait_ms(SPLIT_USB_TIMEOUT_POLL); | 63 | wait_ms(SPLIT_USB_TIMEOUT_POLL); |
38 | } | 64 | } |
39 | 65 | ||
40 | // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow | 66 | // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow |
41 | #if defined(__AVR__) | 67 | usbDisable(); |
42 | (USBCON &= ~(_BV(USBE) | _BV(OTGPADE))); | ||
43 | #else | ||
44 | usbStop(&USBD1); | ||
45 | #endif | ||
46 | 68 | ||
47 | return false; | 69 | return false; |
48 | } | 70 | } |
71 | #elif defined(PROTOCOL_LUFA) | ||
72 | static inline bool usbIsActive(void) { | ||
73 | USB_OTGPAD_On(); // enables VBUS pad | ||
74 | wait_us(5); | ||
75 | |||
76 | return USB_VBUS_GetStatus(); // checks state of VBUS | ||
77 | } | ||
78 | #else | ||
79 | static inline bool usbIsActive(void) { return true; } | ||
80 | #endif | ||
49 | 81 | ||
50 | __attribute__((weak)) bool is_keyboard_left(void) { | 82 | __attribute__((weak)) bool is_keyboard_left(void) { |
51 | #if defined(SPLIT_HAND_PIN) | 83 | #if defined(SPLIT_HAND_PIN) |
@@ -66,16 +98,7 @@ __attribute__((weak)) bool is_keyboard_master(void) { | |||
66 | 98 | ||
67 | // only check once, as this is called often | 99 | // only check once, as this is called often |
68 | if (usbstate == UNKNOWN) { | 100 | if (usbstate == UNKNOWN) { |
69 | #if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS) | 101 | usbstate = usbIsActive() ? MASTER : SLAVE; |
70 | usbstate = waitForUsb() ? MASTER : SLAVE; | ||
71 | #elif defined(__AVR__) | ||
72 | USBCON |= (1 << OTGPADE); // enables VBUS pad | ||
73 | wait_us(5); | ||
74 | |||
75 | usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS | ||
76 | #else | ||
77 | usbstate = MASTER; | ||
78 | #endif | ||
79 | } | 102 | } |
80 | 103 | ||
81 | return (usbstate == MASTER); | 104 | return (usbstate == MASTER); |