diff options
author | Joel Challis <git@zvecr.com> | 2019-10-12 04:25:43 +0100 |
---|---|---|
committer | Danny <nooges@users.noreply.github.com> | 2019-10-11 23:25:43 -0400 |
commit | 76378d74f522f53eebc05907d44c839455a0336b (patch) | |
tree | beb899f965a3f20f1627b21972031bae3aa2bc27 | |
parent | 64c075ed2c2e2ddb2da9321bf17ed911d758a9b2 (diff) | |
download | qmk_firmware-76378d74f522f53eebc05907d44c839455a0336b.tar.gz qmk_firmware-76378d74f522f53eebc05907d44c839455a0336b.zip |
ARM split - detect USB to select master/slave (#6424)
* Initial split refactor to allow usb master detection
* Add split USB detect docs
* Add SPLIT_USB_DETECT demo mode limitation
* fix rebase issues
* clang-format
-rw-r--r-- | docs/config_options.md | 8 | ||||
-rw-r--r-- | docs/feature_split_keyboard.md | 12 | ||||
-rw-r--r-- | quantum/split_common/matrix.c | 4 | ||||
-rw-r--r-- | quantum/split_common/split_util.c | 33 | ||||
-rw-r--r-- | quantum/split_common/split_util.h | 1 |
5 files changed, 49 insertions, 9 deletions
diff --git a/docs/config_options.md b/docs/config_options.md index 9e43f47c2..ec3d1a1c8 100644 --- a/docs/config_options.md +++ b/docs/config_options.md | |||
@@ -267,6 +267,14 @@ There are a few different ways to set handedness for split keyboards (listed in | |||
267 | * 4: about 26kbps | 267 | * 4: about 26kbps |
268 | * 5: about 20kbps | 268 | * 5: about 20kbps |
269 | 269 | ||
270 | * `#define SPLIT_USB_DETECT` | ||
271 | * Detect (with timeout) USB connection when delegating master/slave | ||
272 | * Default behavior for ARM | ||
273 | * Required for AVR Teensy | ||
274 | |||
275 | * `#define SPLIT_USB_TIMEOUT 2500` | ||
276 | * Maximum timeout when detecting master/slave when using `SPLIT_USB_DETECT` | ||
277 | |||
270 | # The `rules.mk` File | 278 | # The `rules.mk` File |
271 | 279 | ||
272 | This is a [make](https://www.gnu.org/software/make/manual/make.html) file that is included by the top-level `Makefile`. It is used to set some information about the MCU that we will be compiling for as well as enabling and disabling certain features. | 280 | This is a [make](https://www.gnu.org/software/make/manual/make.html) file that is included by the top-level `Makefile`. It is used to set some information about the MCU that we will be compiling for as well as enabling and disabling certain features. |
diff --git a/docs/feature_split_keyboard.md b/docs/feature_split_keyboard.md index 42dd838d0..efc65a4c7 100644 --- a/docs/feature_split_keyboard.md +++ b/docs/feature_split_keyboard.md | |||
@@ -190,6 +190,18 @@ This sets how many LEDs are directly connected to each controller. The first nu | |||
190 | ?> This setting implies that `RGBLIGHT_SPLIT` is enabled, and will forcibly enable it, if it's not. | 190 | ?> This setting implies that `RGBLIGHT_SPLIT` is enabled, and will forcibly enable it, if it's not. |
191 | 191 | ||
192 | 192 | ||
193 | ```c | ||
194 | #define SPLIT_USB_DETECT | ||
195 | ``` | ||
196 | This option changes the startup behavior to detect an active USB connection when delegating master/slave. If this operation times out, then the half is assume to be a slave. This is the default behavior for ARM, and required for AVR Teensy boards (due to hardware limitations). | ||
197 | |||
198 | ?> This setting will stop the ability to demo using battery packs. | ||
199 | |||
200 | ```c | ||
201 | #define SPLIT_USB_TIMEOUT 2500 | ||
202 | ``` | ||
203 | This sets the maximum timeout when detecting master/slave when using `SPLIT_USB_DETECT`. | ||
204 | |||
193 | ## Additional Resources | 205 | ## Additional Resources |
194 | 206 | ||
195 | Nicinabox has a [very nice and detailed guide](https://github.com/nicinabox/lets-split-guide) for the Let's Split keyboard, that covers most everything you need to know, including troubleshooting information. | 207 | Nicinabox has a [very nice and detailed guide](https://github.com/nicinabox/lets-split-guide) for the Let's Split keyboard, that covers most everything you need to know, including troubleshooting information. |
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 313f7830b..7176d0cc4 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c | |||
@@ -246,9 +246,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) | |||
246 | #endif | 246 | #endif |
247 | 247 | ||
248 | void matrix_init(void) { | 248 | void matrix_init(void) { |
249 | debug_enable = true; | 249 | keyboard_split_setup(); |
250 | debug_matrix = true; | ||
251 | debug_mouse = true; | ||
252 | 250 | ||
253 | // Set pinout for right half if pinout for that half is defined | 251 | // Set pinout for right half if pinout for that half is defined |
254 | if (!isLeftHand) { | 252 | if (!isLeftHand) { |
diff --git a/quantum/split_common/split_util.c b/quantum/split_common/split_util.c index d16a98977..8983861bc 100644 --- a/quantum/split_common/split_util.c +++ b/quantum/split_common/split_util.c | |||
@@ -14,8 +14,27 @@ | |||
14 | # include "rgblight.h" | 14 | # include "rgblight.h" |
15 | #endif | 15 | #endif |
16 | 16 | ||
17 | #ifndef SPLIT_USB_TIMEOUT | ||
18 | # define SPLIT_USB_TIMEOUT 2500 | ||
19 | #endif | ||
20 | |||
17 | volatile bool isLeftHand = true; | 21 | volatile bool isLeftHand = true; |
18 | 22 | ||
23 | bool waitForUsb(void) { | ||
24 | for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) { | ||
25 | // This will return true of a USB connection has been established | ||
26 | #if defined(__AVR__) | ||
27 | if (UDADDR & _BV(ADDEN)) { | ||
28 | #else | ||
29 | if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) { | ||
30 | #endif | ||
31 | return true; | ||
32 | } | ||
33 | wait_ms(100); | ||
34 | } | ||
35 | return false; | ||
36 | } | ||
37 | |||
19 | __attribute__((weak)) bool is_keyboard_left(void) { | 38 | __attribute__((weak)) bool is_keyboard_left(void) { |
20 | #if defined(SPLIT_HAND_PIN) | 39 | #if defined(SPLIT_HAND_PIN) |
21 | // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand | 40 | // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand |
@@ -31,21 +50,23 @@ __attribute__((weak)) bool is_keyboard_left(void) { | |||
31 | } | 50 | } |
32 | 51 | ||
33 | __attribute__((weak)) bool is_keyboard_master(void) { | 52 | __attribute__((weak)) bool is_keyboard_master(void) { |
34 | #ifdef __AVR__ | ||
35 | static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN; | 53 | static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN; |
36 | 54 | ||
37 | // only check once, as this is called often | 55 | // only check once, as this is called often |
38 | if (usbstate == UNKNOWN) { | 56 | if (usbstate == UNKNOWN) { |
57 | #if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS) | ||
58 | usbstate = waitForUsb() ? MASTER : SLAVE; | ||
59 | #elif defined(__AVR__) | ||
39 | USBCON |= (1 << OTGPADE); // enables VBUS pad | 60 | USBCON |= (1 << OTGPADE); // enables VBUS pad |
40 | wait_us(5); | 61 | wait_us(5); |
41 | 62 | ||
42 | usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS | 63 | usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS |
64 | #else | ||
65 | usbstate = MASTER; | ||
66 | #endif | ||
43 | } | 67 | } |
44 | 68 | ||
45 | return (usbstate == MASTER); | 69 | return (usbstate == MASTER); |
46 | #else | ||
47 | return true; | ||
48 | #endif | ||
49 | } | 70 | } |
50 | 71 | ||
51 | static void keyboard_master_setup(void) { | 72 | static void keyboard_master_setup(void) { |
@@ -59,8 +80,8 @@ static void keyboard_master_setup(void) { | |||
59 | 80 | ||
60 | static void keyboard_slave_setup(void) { transport_slave_init(); } | 81 | static void keyboard_slave_setup(void) { transport_slave_init(); } |
61 | 82 | ||
62 | // this code runs before the usb and keyboard is initialized | 83 | // this code runs before the keyboard is fully initialized |
63 | void matrix_setup(void) { | 84 | void keyboard_split_setup(void) { |
64 | isLeftHand = is_keyboard_left(); | 85 | isLeftHand = is_keyboard_left(); |
65 | 86 | ||
66 | #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) | 87 | #if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) |
diff --git a/quantum/split_common/split_util.h b/quantum/split_common/split_util.h index f41c77605..5d9c52340 100644 --- a/quantum/split_common/split_util.h +++ b/quantum/split_common/split_util.h | |||
@@ -8,3 +8,4 @@ | |||
8 | extern volatile bool isLeftHand; | 8 | extern volatile bool isLeftHand; |
9 | 9 | ||
10 | void matrix_master_OLED_init(void); | 10 | void matrix_master_OLED_init(void); |
11 | void keyboard_split_setup(void); | ||