Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port SPLIT_USB_DETECT to helix/rev2 #7385

Merged
merged 3 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions keyboards/helix/rev2/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ void matrix_init(void)
debug_enable = true;
debug_matrix = true;
debug_mouse = true;
split_keyboard_setup();

// initialize row and col
unselect_rows();
init_cols();
Expand Down
78 changes: 59 additions & 19 deletions keyboards/helix/rev2/split_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,72 @@
#include "split_util.h"
#include "matrix.h"
#include "keyboard.h"
#include "wait.h"

#ifdef USE_MATRIX_I2C
# include "i2c.h"
#else
# include "split_scomm.h"
#endif

#ifdef EE_HANDS
# include "eeconfig.h"
#endif

#ifndef SPLIT_USB_TIMEOUT
#define SPLIT_USB_TIMEOUT 2500
#endif

volatile bool isLeftHand = true;

static void setup_handedness(void) {
#ifdef EE_HANDS
isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
#else
// I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
#if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
isLeftHand = !has_usb();
#else
isLeftHand = has_usb();
#endif
#endif
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 (UDADDR & _BV(ADDEN)) {
return true;
}
wait_ms(100);
}

// Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
(USBCON &= ~(_BV(USBE) | _BV(OTGPADE)));

return false;
}


__attribute__((weak)) bool is_keyboard_left(void) {
#if defined(SPLIT_HAND_PIN)
// Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
setPinInput(SPLIT_HAND_PIN);
return readPin(SPLIT_HAND_PIN);
#elif defined(EE_HANDS)
return eeconfig_read_handedness();
#elif defined(MASTER_RIGHT)
return !is_keyboard_master();
#endif

return is_keyboard_master();
}

__attribute__((weak)) bool is_keyboard_master(void) {
static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;

// only check once, as this is called often
if (usbstate == UNKNOWN) {
#if defined(SPLIT_USB_DETECT)
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);
}

static void keyboard_master_setup(void) {
Expand All @@ -48,13 +94,11 @@ static void keyboard_slave_setup(void) {
}

bool has_usb(void) {
USBCON |= (1 << OTGPADE); //enables VBUS pad
_delay_us(5);
return (USBSTA & (1<<VBUS)); //checks state of VBUS
return is_keyboard_master();
}

void split_keyboard_setup(void) {
setup_handedness();
isLeftHand = is_keyboard_left();

if (has_usb()) {
keyboard_master_setup();
Expand All @@ -64,7 +108,3 @@ void split_keyboard_setup(void) {
sei();
}

// this code runs before the usb and keyboard is initialized
void matrix_setup(void) {
split_keyboard_setup();
}