aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--quantum/split_common/split_util.c70
-rw-r--r--tmk_core/common.mk1
-rw-r--r--tmk_core/common/chibios/chibios_config.h2
-rw-r--r--tmk_core/common/usb_util.c29
-rw-r--r--tmk_core/common/usb_util.h22
-rw-r--r--tmk_core/protocol/chibios.mk1
-rw-r--r--tmk_core/protocol/chibios/usb_util.c21
-rw-r--r--tmk_core/protocol/lufa.mk1
-rw-r--r--tmk_core/protocol/lufa/usb_util.c34
-rw-r--r--tmk_core/protocol/vusb.mk1
-rw-r--r--tmk_core/protocol/vusb/usb_util.c24
11 files changed, 160 insertions, 46 deletions
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c
index 2ae44e6e1..9e75e19ce 100644
--- a/quantum/split_common/split_util.c
+++ b/quantum/split_common/split_util.c
@@ -1,3 +1,18 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
1#include "split_util.h" 16#include "split_util.h"
2#include "matrix.h" 17#include "matrix.h"
3#include "keyboard.h" 18#include "keyboard.h"
@@ -6,14 +21,7 @@
6#include "transport.h" 21#include "transport.h"
7#include "quantum.h" 22#include "quantum.h"
8#include "wait.h" 23#include "wait.h"
9 24#include "usb_util.h"
10#ifdef PROTOCOL_LUFA
11# include <LUFA/Drivers/USB/USB.h>
12#endif
13
14#ifdef PROTOCOL_VUSB
15# include <usbdrv/usbdrv.h>
16#endif
17 25
18#ifdef EE_HANDS 26#ifdef EE_HANDS
19# include "eeconfig.h" 27# include "eeconfig.h"
@@ -31,56 +39,21 @@
31# define SPLIT_USB_TIMEOUT_POLL 10 39# define SPLIT_USB_TIMEOUT_POLL 10
32#endif 40#endif
33 41
34#ifdef PROTOCOL_CHIBIOS
35# define SPLIT_USB_DETECT // Force this on for now
36#endif
37
38volatile bool isLeftHand = true; 42volatile bool isLeftHand = true;
39 43
40#if defined(SPLIT_USB_DETECT) 44#if defined(SPLIT_USB_DETECT)
41# if defined(PROTOCOL_LUFA) 45static bool usbIsActive(void) {
42static inline bool usbHasActiveConnection(void) { return USB_Device_IsAddressSet(); }
43static inline void usbDisable(void) {
44 USB_Disable();
45 USB_DeviceState = DEVICE_STATE_Unattached;
46}
47# elif defined(PROTOCOL_CHIBIOS)
48static inline bool usbHasActiveConnection(void) { return usbGetDriverStateI(&USBD1) == USB_ACTIVE; }
49static inline void usbDisable(void) { usbStop(&USBD1); }
50# elif defined(PROTOCOL_VUSB)
51static inline bool usbHasActiveConnection(void) {
52 usbPoll();
53 return usbConfiguration;
54}
55static inline void usbDisable(void) { usbDeviceDisconnect(); }
56# else
57static inline bool usbHasActiveConnection(void) { return true; }
58static inline void usbDisable(void) {}
59# endif
60
61bool usbIsActive(void) {
62 for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) { 46 for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / SPLIT_USB_TIMEOUT_POLL); i++) {
63 // This will return true if a USB connection has been established 47 // This will return true if a USB connection has been established
64 if (usbHasActiveConnection()) { 48 if (usb_connected_state()) {
65 return true; 49 return true;
66 } 50 }
67 wait_ms(SPLIT_USB_TIMEOUT_POLL); 51 wait_ms(SPLIT_USB_TIMEOUT_POLL);
68 } 52 }
69
70 // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
71 usbDisable();
72
73 return false; 53 return false;
74} 54}
75#elif defined(PROTOCOL_LUFA) && defined(OTGPADE)
76static inline bool usbIsActive(void) {
77 USB_OTGPAD_On(); // enables VBUS pad
78 wait_us(5);
79
80 return USB_VBUS_GetStatus(); // checks state of VBUS
81}
82#else 55#else
83static inline bool usbIsActive(void) { return true; } 56static inline bool usbIsActive(void) { return usb_vbus_state(); }
84#endif 57#endif
85 58
86#ifdef SPLIT_HAND_MATRIX_GRID 59#ifdef SPLIT_HAND_MATRIX_GRID
@@ -126,6 +99,11 @@ __attribute__((weak)) bool is_keyboard_master(void) {
126 // only check once, as this is called often 99 // only check once, as this is called often
127 if (usbstate == UNKNOWN) { 100 if (usbstate == UNKNOWN) {
128 usbstate = usbIsActive() ? MASTER : SLAVE; 101 usbstate = usbIsActive() ? MASTER : SLAVE;
102
103 // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
104 if (usbstate == SLAVE) {
105 usb_disable();
106 }
129 } 107 }
130 108
131 return (usbstate == MASTER); 109 return (usbstate == MASTER);
diff --git a/tmk_core/common.mk b/tmk_core/common.mk
index c2fc522ce..2f8f81126 100644
--- a/tmk_core/common.mk
+++ b/tmk_core/common.mk
@@ -12,6 +12,7 @@ TMK_COMMON_SRC += $(COMMON_DIR)/host.c \
12 $(COMMON_DIR)/sendchar_null.c \ 12 $(COMMON_DIR)/sendchar_null.c \
13 $(COMMON_DIR)/eeconfig.c \ 13 $(COMMON_DIR)/eeconfig.c \
14 $(COMMON_DIR)/report.c \ 14 $(COMMON_DIR)/report.c \
15 $(COMMON_DIR)/usb_util.c \
15 $(PLATFORM_COMMON_DIR)/suspend.c \ 16 $(PLATFORM_COMMON_DIR)/suspend.c \
16 $(PLATFORM_COMMON_DIR)/timer.c \ 17 $(PLATFORM_COMMON_DIR)/timer.c \
17 $(COMMON_DIR)/sync_timer.c \ 18 $(COMMON_DIR)/sync_timer.c \
diff --git a/tmk_core/common/chibios/chibios_config.h b/tmk_core/common/chibios/chibios_config.h
index b4d96465d..bebf026de 100644
--- a/tmk_core/common/chibios/chibios_config.h
+++ b/tmk_core/common/chibios/chibios_config.h
@@ -15,6 +15,8 @@
15 */ 15 */
16#pragma once 16#pragma once
17 17
18#define SPLIT_USB_DETECT // Force this on for now
19
18#if defined(STM32F1XX) 20#if defined(STM32F1XX)
19# define USE_GPIOV1 21# define USE_GPIOV1
20#endif 22#endif
diff --git a/tmk_core/common/usb_util.c b/tmk_core/common/usb_util.c
new file mode 100644
index 000000000..e4c50fcb1
--- /dev/null
+++ b/tmk_core/common/usb_util.c
@@ -0,0 +1,29 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include "usb_util.h"
17#include "wait.h"
18
19__attribute__((weak)) void usb_disable(void) {}
20__attribute__((weak)) bool usb_connected_state(void) { return true; }
21__attribute__((weak)) bool usb_vbus_state(void) {
22#ifdef USB_VBUS_PIN
23 setPinInput(USB_VBUS_PIN);
24 wait_us(5);
25 return readPin(USB_VBUS_PIN);
26#else
27 return true;
28#endif
29}
diff --git a/tmk_core/common/usb_util.h b/tmk_core/common/usb_util.h
new file mode 100644
index 000000000..4ebedb1e7
--- /dev/null
+++ b/tmk_core/common/usb_util.h
@@ -0,0 +1,22 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#pragma once
17
18#include <stdbool.h>
19
20void usb_disable(void);
21bool usb_connected_state(void);
22bool usb_vbus_state(void);
diff --git a/tmk_core/protocol/chibios.mk b/tmk_core/protocol/chibios.mk
index 80554abb3..d01697835 100644
--- a/tmk_core/protocol/chibios.mk
+++ b/tmk_core/protocol/chibios.mk
@@ -6,6 +6,7 @@ SRC += $(CHIBIOS_DIR)/usb_main.c
6SRC += $(CHIBIOS_DIR)/main.c 6SRC += $(CHIBIOS_DIR)/main.c
7SRC += usb_descriptor.c 7SRC += usb_descriptor.c
8SRC += $(CHIBIOS_DIR)/usb_driver.c 8SRC += $(CHIBIOS_DIR)/usb_driver.c
9SRC += $(CHIBIOS_DIR)/usb_util.c
9SRC += $(LIBSRC) 10SRC += $(LIBSRC)
10 11
11VPATH += $(TMK_PATH)/$(PROTOCOL_DIR) 12VPATH += $(TMK_PATH)/$(PROTOCOL_DIR)
diff --git a/tmk_core/protocol/chibios/usb_util.c b/tmk_core/protocol/chibios/usb_util.c
new file mode 100644
index 000000000..5945e8a8d
--- /dev/null
+++ b/tmk_core/protocol/chibios/usb_util.c
@@ -0,0 +1,21 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include <hal.h>
17#include "usb_util.h"
18
19void usb_disable(void) { usbStop(&USBD1); }
20
21bool usb_connected_state(void) { return usbGetDriverStateI(&USBD1) == USB_ACTIVE; }
diff --git a/tmk_core/protocol/lufa.mk b/tmk_core/protocol/lufa.mk
index 1cc1fa04e..9d9fb728b 100644
--- a/tmk_core/protocol/lufa.mk
+++ b/tmk_core/protocol/lufa.mk
@@ -44,6 +44,7 @@ ifeq ($(strip $(VIRTSER_ENABLE)), yes)
44endif 44endif
45 45
46SRC += $(LUFA_SRC) 46SRC += $(LUFA_SRC)
47SRC += $(LUFA_DIR)/usb_util.c
47 48
48# Search Path 49# Search Path
49VPATH += $(TMK_PATH)/$(LUFA_DIR) 50VPATH += $(TMK_PATH)/$(LUFA_DIR)
diff --git a/tmk_core/protocol/lufa/usb_util.c b/tmk_core/protocol/lufa/usb_util.c
new file mode 100644
index 000000000..9e943a21b
--- /dev/null
+++ b/tmk_core/protocol/lufa/usb_util.c
@@ -0,0 +1,34 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include <LUFA/Drivers/USB/USB.h>
17#include "usb_util.h"
18#include "wait.h"
19
20void usb_disable(void) {
21 USB_Disable();
22 USB_DeviceState = DEVICE_STATE_Unattached;
23}
24
25bool usb_connected_state(void) { return USB_Device_IsAddressSet(); }
26
27#if defined(OTGPADE)
28bool usb_vbus_state(void) {
29 USB_OTGPAD_On(); // enables VBUS pad
30 wait_us(5);
31
32 return USB_VBUS_GetStatus(); // checks state of VBUS
33}
34#endif
diff --git a/tmk_core/protocol/vusb.mk b/tmk_core/protocol/vusb.mk
index 1de600308..e4d013b38 100644
--- a/tmk_core/protocol/vusb.mk
+++ b/tmk_core/protocol/vusb.mk
@@ -5,6 +5,7 @@ VUSB_PATH = $(LIB_PATH)/vusb
5 5
6SRC += $(VUSB_DIR)/main.c \ 6SRC += $(VUSB_DIR)/main.c \
7 $(VUSB_DIR)/vusb.c \ 7 $(VUSB_DIR)/vusb.c \
8 $(VUSB_DIR)/usb_util.c \
8 $(VUSB_PATH)/usbdrv/usbdrv.c \ 9 $(VUSB_PATH)/usbdrv/usbdrv.c \
9 $(VUSB_PATH)/usbdrv/usbdrvasm.S \ 10 $(VUSB_PATH)/usbdrv/usbdrvasm.S \
10 $(VUSB_PATH)/usbdrv/oddebug.c 11 $(VUSB_PATH)/usbdrv/oddebug.c
diff --git a/tmk_core/protocol/vusb/usb_util.c b/tmk_core/protocol/vusb/usb_util.c
new file mode 100644
index 000000000..602854dbe
--- /dev/null
+++ b/tmk_core/protocol/vusb/usb_util.c
@@ -0,0 +1,24 @@
1/* Copyright 2021 QMK
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#include <usbdrv/usbdrv.h>
17#include "usb_util.h"
18
19void usb_disable(void) { usbDeviceDisconnect(); }
20
21bool usb_connected_state(void) {
22 usbPoll();
23 return usbConfiguration;
24}