diff options
author | Joel Challis <git@zvecr.com> | 2019-11-16 00:07:21 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-16 00:07:21 +0000 |
commit | b94f6cb116f1628f6d14ff3b1365bcbae6792dea (patch) | |
tree | fb717e936be5bf344733771674e236f4bd00bc0f | |
parent | 0f0c73f14a8ffb83a79b51582c0e7465c2411749 (diff) | |
download | qmk_firmware-b94f6cb116f1628f6d14ff3b1365bcbae6792dea.tar.gz qmk_firmware-b94f6cb116f1628f6d14ff3b1365bcbae6792dea.zip |
Port SPLIT_USB_DETECT to crkbd (#7195)
-rw-r--r-- | keyboards/crkbd/rev1/matrix.c | 5 | ||||
-rw-r--r-- | keyboards/crkbd/rev1/split_util.c | 82 |
2 files changed, 63 insertions, 24 deletions
diff --git a/keyboards/crkbd/rev1/matrix.c b/keyboards/crkbd/rev1/matrix.c index dd93506db..9d86879d6 100644 --- a/keyboards/crkbd/rev1/matrix.c +++ b/keyboards/crkbd/rev1/matrix.c | |||
@@ -133,9 +133,8 @@ void rx_led_off(void) | |||
133 | 133 | ||
134 | void matrix_init(void) | 134 | void matrix_init(void) |
135 | { | 135 | { |
136 | debug_enable = true; | 136 | split_keyboard_setup(); |
137 | debug_matrix = true; | 137 | |
138 | debug_mouse = true; | ||
139 | // initialize row and col | 138 | // initialize row and col |
140 | unselect_rows(); | 139 | unselect_rows(); |
141 | init_cols(); | 140 | init_cols(); |
diff --git a/keyboards/crkbd/rev1/split_util.c b/keyboards/crkbd/rev1/split_util.c index e1ff8b437..c0d44af29 100644 --- a/keyboards/crkbd/rev1/split_util.c +++ b/keyboards/crkbd/rev1/split_util.c | |||
@@ -7,6 +7,11 @@ | |||
7 | #include "split_util.h" | 7 | #include "split_util.h" |
8 | #include "matrix.h" | 8 | #include "matrix.h" |
9 | #include "keyboard.h" | 9 | #include "keyboard.h" |
10 | #include "wait.h" | ||
11 | |||
12 | #ifdef EE_HANDS | ||
13 | # include "eeconfig.h" | ||
14 | #endif | ||
10 | 15 | ||
11 | #ifdef USE_MATRIX_I2C | 16 | #ifdef USE_MATRIX_I2C |
12 | # include "i2c.h" | 17 | # include "i2c.h" |
@@ -14,19 +19,59 @@ | |||
14 | # include "split_scomm.h" | 19 | # include "split_scomm.h" |
15 | #endif | 20 | #endif |
16 | 21 | ||
22 | #ifndef SPLIT_USB_TIMEOUT | ||
23 | # define SPLIT_USB_TIMEOUT 2500 | ||
24 | #endif | ||
25 | |||
17 | volatile bool isLeftHand = true; | 26 | volatile bool isLeftHand = true; |
18 | 27 | ||
19 | static void setup_handedness(void) { | 28 | bool waitForUsb(void) { |
20 | #ifdef EE_HANDS | 29 | for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) { |
21 | isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); | 30 | // This will return true of a USB connection has been established |
22 | #else | 31 | if (UDADDR & _BV(ADDEN)) { |
23 | // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c | 32 | return true; |
24 | #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT) | 33 | } |
25 | isLeftHand = !has_usb(); | 34 | wait_ms(100); |
26 | #else | 35 | } |
27 | isLeftHand = has_usb(); | 36 | |
28 | #endif | 37 | // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow |
29 | #endif | 38 | (USBCON &= ~(_BV(USBE) | _BV(OTGPADE))); |
39 | |||
40 | return false; | ||
41 | } | ||
42 | |||
43 | __attribute__((weak)) bool is_keyboard_left(void) { | ||
44 | #if defined(SPLIT_HAND_PIN) | ||
45 | // Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand | ||
46 | setPinInput(SPLIT_HAND_PIN); | ||
47 | return readPin(SPLIT_HAND_PIN); | ||
48 | #elif defined(EE_HANDS) | ||
49 | return eeconfig_read_handedness(); | ||
50 | #elif defined(MASTER_RIGHT) | ||
51 | return !is_keyboard_master(); | ||
52 | #endif | ||
53 | |||
54 | return is_keyboard_master(); | ||
55 | } | ||
56 | |||
57 | __attribute__((weak)) bool is_keyboard_master(void) { | ||
58 | static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN; | ||
59 | |||
60 | // only check once, as this is called often | ||
61 | if (usbstate == UNKNOWN) { | ||
62 | #if defined(SPLIT_USB_DETECT) | ||
63 | usbstate = waitForUsb() ? MASTER : SLAVE; | ||
64 | #elif defined(__AVR__) | ||
65 | USBCON |= (1 << OTGPADE); // enables VBUS pad | ||
66 | wait_us(5); | ||
67 | |||
68 | usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS | ||
69 | #else | ||
70 | usbstate = MASTER; | ||
71 | #endif | ||
72 | } | ||
73 | |||
74 | return (usbstate == MASTER); | ||
30 | } | 75 | } |
31 | 76 | ||
32 | static void keyboard_master_setup(void) { | 77 | static void keyboard_master_setup(void) { |
@@ -47,14 +92,9 @@ static void keyboard_slave_setup(void) { | |||
47 | #endif | 92 | #endif |
48 | } | 93 | } |
49 | 94 | ||
50 | bool has_usb(void) { | 95 | // this code runs before the usb and keyboard is initialized |
51 | USBCON |= (1 << OTGPADE); //enables VBUS pad | ||
52 | _delay_us(5); | ||
53 | return (USBSTA & (1<<VBUS)); //checks state of VBUS | ||
54 | } | ||
55 | |||
56 | void split_keyboard_setup(void) { | 96 | void split_keyboard_setup(void) { |
57 | setup_handedness(); | 97 | isLeftHand = is_keyboard_left(); |
58 | 98 | ||
59 | if (has_usb()) { | 99 | if (has_usb()) { |
60 | keyboard_master_setup(); | 100 | keyboard_master_setup(); |
@@ -64,7 +104,7 @@ void split_keyboard_setup(void) { | |||
64 | sei(); | 104 | sei(); |
65 | } | 105 | } |
66 | 106 | ||
67 | // this code runs before the usb and keyboard is initialized | 107 | // backwards compat |
68 | void matrix_setup(void) { | 108 | bool has_usb(void) { |
69 | split_keyboard_setup(); | 109 | return is_keyboard_master(); |
70 | } | 110 | } |