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
This commit is contained in:
committed by
Florian Didron
parent
a15119dc6f
commit
80f8ad232b
@@ -246,9 +246,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
void matrix_init(void) {
|
void matrix_init(void) {
|
||||||
debug_enable = true;
|
keyboard_split_setup();
|
||||||
debug_matrix = true;
|
|
||||||
debug_mouse = true;
|
|
||||||
|
|
||||||
// Set pinout for right half if pinout for that half is defined
|
// Set pinout for right half if pinout for that half is defined
|
||||||
if (!isLeftHand) {
|
if (!isLeftHand) {
|
||||||
|
|||||||
@@ -14,8 +14,27 @@
|
|||||||
# include "rgblight.h"
|
# include "rgblight.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef SPLIT_USB_TIMEOUT
|
||||||
|
# define SPLIT_USB_TIMEOUT 2500
|
||||||
|
#endif
|
||||||
|
|
||||||
volatile bool isLeftHand = true;
|
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) {
|
__attribute__((weak)) bool is_keyboard_left(void) {
|
||||||
#if defined(SPLIT_HAND_PIN)
|
#if defined(SPLIT_HAND_PIN)
|
||||||
// Test pin SPLIT_HAND_PIN for High/Low, if low it's right hand
|
// 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) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((weak)) bool is_keyboard_master(void) {
|
__attribute__((weak)) bool is_keyboard_master(void) {
|
||||||
#ifdef __AVR__
|
|
||||||
static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
|
static enum { UNKNOWN, MASTER, SLAVE } usbstate = UNKNOWN;
|
||||||
|
|
||||||
// only check once, as this is called often
|
// only check once, as this is called often
|
||||||
if (usbstate == UNKNOWN) {
|
if (usbstate == UNKNOWN) {
|
||||||
|
#if defined(SPLIT_USB_DETECT) || defined(PROTOCOL_CHIBIOS)
|
||||||
|
usbstate = waitForUsb() ? MASTER : SLAVE;
|
||||||
|
#elif defined(__AVR__)
|
||||||
USBCON |= (1 << OTGPADE); // enables VBUS pad
|
USBCON |= (1 << OTGPADE); // enables VBUS pad
|
||||||
wait_us(5);
|
wait_us(5);
|
||||||
|
|
||||||
usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
|
usbstate = (USBSTA & (1 << VBUS)) ? MASTER : SLAVE; // checks state of VBUS
|
||||||
|
#else
|
||||||
|
usbstate = MASTER;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return (usbstate == MASTER);
|
return (usbstate == MASTER);
|
||||||
#else
|
|
||||||
return true;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void keyboard_master_setup(void) {
|
static void keyboard_master_setup(void) {
|
||||||
@@ -59,8 +80,8 @@ static void keyboard_master_setup(void) {
|
|||||||
|
|
||||||
static void keyboard_slave_setup(void) { transport_slave_init(); }
|
static void keyboard_slave_setup(void) { transport_slave_init(); }
|
||||||
|
|
||||||
// this code runs before the usb and keyboard is initialized
|
// this code runs before the keyboard is fully initialized
|
||||||
void matrix_setup(void) {
|
void keyboard_split_setup(void) {
|
||||||
isLeftHand = is_keyboard_left();
|
isLeftHand = is_keyboard_left();
|
||||||
|
|
||||||
#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
|
#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT)
|
||||||
|
|||||||
@@ -8,3 +8,4 @@
|
|||||||
extern volatile bool isLeftHand;
|
extern volatile bool isLeftHand;
|
||||||
|
|
||||||
void matrix_master_OLED_init(void);
|
void matrix_master_OLED_init(void);
|
||||||
|
void keyboard_split_setup(void);
|
||||||
|
|||||||
Reference in New Issue
Block a user