Port SPLIT_USB_DETECT to crkbd (#7195)

This commit is contained in:
Joel Challis 2019-11-16 00:07:21 +00:00 committed by GitHub
parent 0f0c73f14a
commit b94f6cb116
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 24 deletions

View File

@ -133,9 +133,8 @@ void rx_led_off(void)
void matrix_init(void) void matrix_init(void)
{ {
debug_enable = true; split_keyboard_setup();
debug_matrix = true;
debug_mouse = true;
// initialize row and col // initialize row and col
unselect_rows(); unselect_rows();
init_cols(); init_cols();

View File

@ -7,6 +7,11 @@
#include "split_util.h" #include "split_util.h"
#include "matrix.h" #include "matrix.h"
#include "keyboard.h" #include "keyboard.h"
#include "wait.h"
#ifdef EE_HANDS
# include "eeconfig.h"
#endif
#ifdef USE_MATRIX_I2C #ifdef USE_MATRIX_I2C
# include "i2c.h" # include "i2c.h"
@ -14,19 +19,59 @@
# include "split_scomm.h" # include "split_scomm.h"
#endif #endif
#ifndef SPLIT_USB_TIMEOUT
# define SPLIT_USB_TIMEOUT 2500
#endif
volatile bool isLeftHand = true; volatile bool isLeftHand = true;
static void setup_handedness(void) { bool waitForUsb(void) {
#ifdef EE_HANDS for (uint8_t i = 0; i < (SPLIT_USB_TIMEOUT / 100); i++) {
isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS); // This will return true of a USB connection has been established
#else if (UDADDR & _BV(ADDEN)) {
// I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c return true;
#if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT) }
isLeftHand = !has_usb(); wait_ms(100);
#else }
isLeftHand = has_usb();
#endif // Avoid NO_USB_STARTUP_CHECK - Disable USB as the previous checks seem to enable it somehow
#endif (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) { static void keyboard_master_setup(void) {
@ -47,14 +92,9 @@ static void keyboard_slave_setup(void) {
#endif #endif
} }
bool has_usb(void) { // this code runs before the usb and keyboard is initialized
USBCON |= (1 << OTGPADE); //enables VBUS pad
_delay_us(5);
return (USBSTA & (1<<VBUS)); //checks state of VBUS
}
void split_keyboard_setup(void) { void split_keyboard_setup(void) {
setup_handedness(); isLeftHand = is_keyboard_left();
if (has_usb()) { if (has_usb()) {
keyboard_master_setup(); keyboard_master_setup();
@ -64,7 +104,7 @@ void split_keyboard_setup(void) {
sei(); sei();
} }
// this code runs before the usb and keyboard is initialized // backwards compat
void matrix_setup(void) { bool has_usb(void) {
split_keyboard_setup(); return is_keyboard_master();
} }