Skip to content

Commit

Permalink
Initial split refactor to allow usb master detection
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr committed Jul 26, 2019
1 parent 1ab008e commit d676815
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
4 changes: 1 addition & 3 deletions quantum/split_common/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
#endif

void matrix_init(void) {
debug_enable = true;
debug_matrix = true;
debug_mouse = true;
keyboard_split_setup();

// Set pinout for right half if pinout for that half is defined
if (!isLeftHand) {
Expand Down
33 changes: 27 additions & 6 deletions quantum/split_common/split_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@
#include "rgblight.h"
#endif

#ifndef SPLIT_USB_TIMEOUT
#define SPLIT_USB_TIMEOUT 2500
#endif

volatile bool isLeftHand = true;

bool waitForUsb(void) {
for(uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) {
// This will return true of a USB connection has been established
#if defined(__AVR__)
if (UDADDR & _BV(ADDEN)) {
#else
if (usbGetDriverStateI(&USBD1) == USB_ACTIVE) {
#endif
return true;
}
wait_ms(100);
}
return false;
}

__attribute__((weak))
bool is_keyboard_left(void) {
#if defined(SPLIT_HAND_PIN)
Expand All @@ -35,22 +54,24 @@ bool is_keyboard_left(void) {
__attribute__((weak))
bool is_keyboard_master(void)
{
#ifdef __AVR__
static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;

// only check once, as this is called often
if (usbstate == UNKNOWN)
{
#if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS)
usbstate = waitForUsb() ? MASTER : SLAVE;
#elif defined(__AVR__)
USBCON |= (1 << OTGPADE); // enables VBUS pad
wait_us(5);

usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
#else
usbstate = MASTER;
#endif
}

return (usbstate == MASTER);
#else
return true;
#endif
}

static void keyboard_master_setup(void) {
Expand All @@ -67,8 +88,8 @@ static void keyboard_slave_setup(void)
transport_slave_init();
}

// this code runs before the usb and keyboard is initialized
void matrix_setup(void)
// this code runs before the keyboard is fully initialized
void keyboard_split_setup(void)
{
isLeftHand = is_keyboard_left();

Expand Down
1 change: 1 addition & 0 deletions quantum/split_common/split_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
extern volatile bool isLeftHand;

void matrix_master_OLED_init (void);
void keyboard_split_setup(void);

0 comments on commit d676815

Please sign in to comment.