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 /quantum | |
| 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
Diffstat (limited to 'quantum')
| -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 |
3 files changed, 29 insertions, 9 deletions
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); | ||
