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

update chibios os usb for the otg driver #8893

Merged
merged 10 commits into from
Nov 10, 2020
5 changes: 5 additions & 0 deletions common_features.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ QUANTUM_SRC += \
$(QUANTUM_DIR)/keymap_common.c \
$(QUANTUM_DIR)/keycode_config.c

ifeq ($(strip $(DEBUG_MATRIX_SCAN_RATE_ENABLE)), yes)
OPT_DEFS += -DDEBUG_MATRIX_SCAN_RATE
CONSOLE_ENABLE = yes
endif

ifeq ($(strip $(API_SYSEX_ENABLE)), yes)
OPT_DEFS += -DAPI_SYSEX_ENABLE
OPT_DEFS += -DAPI_ENABLE
Expand Down
31 changes: 26 additions & 5 deletions tmk_core/common/action_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,32 @@ void set_macro_mods(uint8_t mods) { macro_mods = mods; }
void clear_macro_mods(void) { macro_mods = 0; }

#ifndef NO_ACTION_ONESHOT
/** \brief get oneshot mods
*
* FIXME: needs doc
*/
uint8_t get_oneshot_mods(void) { return oneshot_mods; }

void add_oneshot_mods(uint8_t mods) {
if ((oneshot_mods & mods) != mods) {
# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
oneshot_time = timer_read();
# endif
oneshot_mods |= mods;
oneshot_mods_changed_kb(mods);
}
}

void del_oneshot_mods(uint8_t mods) {
if (oneshot_mods & mods) {
oneshot_mods &= ~mods;
# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
oneshot_time = oneshot_mods ? timer_read() : 0;
# endif
oneshot_mods_changed_kb(oneshot_mods);
}
}

/** \brief set oneshot mods
*
* FIXME: needs doc
Expand All @@ -316,11 +342,6 @@ void clear_oneshot_mods(void) {
oneshot_mods_changed_kb(oneshot_mods);
}
}
/** \brief get oneshot mods
*
* FIXME: needs doc
*/
uint8_t get_oneshot_mods(void) { return oneshot_mods; }
#endif

/** \brief Called when the one shot modifiers have been changed.
Expand Down
7 changes: 3 additions & 4 deletions tmk_core/common/action_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ void set_macro_mods(uint8_t mods);
void clear_macro_mods(void);

/* oneshot modifier */
void set_oneshot_mods(uint8_t mods);
uint8_t get_oneshot_mods(void);
void add_oneshot_mods(uint8_t mods);
void del_oneshot_mods(uint8_t mods);
void set_oneshot_mods(uint8_t mods);
void clear_oneshot_mods(void);
void oneshot_toggle(void);
void oneshot_enable(void);
void oneshot_disable(void);
bool has_oneshot_mods_timed_out(void);

uint8_t get_oneshot_locked_mods(void);
Expand Down
4 changes: 4 additions & 0 deletions tmk_core/common/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ void keyboard_init(void) {
dip_switch_init();
#endif

#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
debug_enable = true;
#endif

keyboard_post_init_kb(); /* Always keep this last */
}

Expand Down
74 changes: 74 additions & 0 deletions tmk_core/protocol/chibios/usb_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ static const USBEndpointConfig shared_ep_config = {
};
#endif

#if STM32_USB_USE_OTG1
typedef struct {
size_t queue_capacity_in;
size_t queue_capacity_out;
USBInEndpointState in_ep_state;
USBOutEndpointState out_ep_state;
USBInEndpointState int_ep_state;
USBEndpointConfig inout_ep_config;
USBEndpointConfig int_ep_config;
const QMKUSBConfig config;
QMKUSBDriver driver;
} usb_driver_config_t;
#else
typedef struct {
size_t queue_capacity_in;
size_t queue_capacity_out;
Expand All @@ -177,7 +190,54 @@ typedef struct {
const QMKUSBConfig config;
QMKUSBDriver driver;
} usb_driver_config_t;
#endif

#if STM32_USB_USE_OTG1
/* Reusable initialization structure - see USBEndpointConfig comment at top of file */
#define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \
{ \
.queue_capacity_in = stream##_IN_CAPACITY, .queue_capacity_out = stream##_OUT_CAPACITY, \
.inout_ep_config = \
{ \
stream##_IN_MODE, /* Interrupt EP */ \
NULL, /* SETUP packet notification callback */ \
qmkusbDataTransmitted, /* IN notification callback */ \
qmkusbDataReceived, /* OUT notification callback */ \
stream##_EPSIZE, /* IN maximum packet size */ \
stream##_EPSIZE, /* OUT maximum packet size */ \
NULL, /* IN Endpoint state */ \
NULL, /* OUT endpoint state */ \
2, /* IN multiplier */ \
NULL /* SETUP buffer (not a SETUP endpoint) */ \
}, \
.int_ep_config = \
{ \
USB_EP_MODE_TYPE_INTR, /* Interrupt EP */ \
NULL, /* SETUP packet notification callback */ \
qmkusbInterruptTransmitted, /* IN notification callback */ \
NULL, /* OUT notification callback */ \
CDC_NOTIFICATION_EPSIZE, /* IN maximum packet size */ \
0, /* OUT maximum packet size */ \
NULL, /* IN Endpoint state */ \
NULL, /* OUT endpoint state */ \
2, /* IN multiplier */ \
NULL, /* SETUP buffer (not a SETUP endpoint) */ \
}, \
.config = { \
.usbp = &USB_DRIVER, \
.bulk_in = stream##_IN_EPNUM, \
.bulk_out = stream##_OUT_EPNUM, \
.int_in = notification, \
.in_buffers = stream##_IN_CAPACITY, \
.out_buffers = stream##_OUT_CAPACITY, \
.in_size = stream##_EPSIZE, \
.out_size = stream##_EPSIZE, \
.fixed_size = fixedsize, \
.ib = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_IN_CAPACITY, stream##_EPSIZE)]){}, \
.ob = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \
} \
}
#else
/* Reusable initialization structure - see USBEndpointConfig comment at top of file */
#define QMK_USB_DRIVER_CONFIG(stream, notification, fixedsize) \
{ \
Expand Down Expand Up @@ -235,6 +295,7 @@ typedef struct {
.ob = (__attribute__((aligned(4))) uint8_t[BQ_BUFFER_SIZE(stream##_OUT_CAPACITY, stream##_EPSIZE)]){}, \
} \
}
#endif

typedef struct {
union {
Expand Down Expand Up @@ -327,8 +388,12 @@ static void usb_event_cb(USBDriver *usbp, usbevent_t event) {
usbInitEndpointI(usbp, SHARED_IN_EPNUM, &shared_ep_config);
#endif
for (int i = 0; i < NUM_USB_DRIVERS; i++) {
#if STM32_USB_USE_OTG1
usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].inout_ep_config);
#else
usbInitEndpointI(usbp, drivers.array[i].config.bulk_in, &drivers.array[i].in_ep_config);
usbInitEndpointI(usbp, drivers.array[i].config.bulk_out, &drivers.array[i].out_ep_config);
#endif
if (drivers.array[i].config.int_in) {
usbInitEndpointI(usbp, drivers.array[i].config.int_in, &drivers.array[i].int_ep_config);
}
Expand Down Expand Up @@ -553,12 +618,21 @@ static const USBConfig usbcfg = {
*/
void init_usb_driver(USBDriver *usbp) {
for (int i = 0; i < NUM_USB_DRIVERS; i++) {
#if STM32_USB_USE_OTG1
QMKUSBDriver *driver = &drivers.array[i].driver;
drivers.array[i].inout_ep_config.in_state = &drivers.array[i].in_ep_state;
drivers.array[i].inout_ep_config.out_state = &drivers.array[i].out_ep_state;
drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state;
qmkusbObjectInit(driver, &drivers.array[i].config);
qmkusbStart(driver, &drivers.array[i].config);
#else
QMKUSBDriver *driver = &drivers.array[i].driver;
drivers.array[i].in_ep_config.in_state = &drivers.array[i].in_ep_state;
drivers.array[i].out_ep_config.out_state = &drivers.array[i].out_ep_state;
drivers.array[i].int_ep_config.in_state = &drivers.array[i].int_ep_state;
qmkusbObjectInit(driver, &drivers.array[i].config);
qmkusbStart(driver, &drivers.array[i].config);
#endif
}

/*
Expand Down
20 changes: 20 additions & 0 deletions tmk_core/protocol/usb_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ enum usb_endpoints {

#ifdef RAW_ENABLE
RAW_IN_EPNUM = NEXT_EPNUM,
#if STM32_USB_USE_OTG1
#define RAW_OUT_EPNUM RAW_IN_EPNUM
#else
RAW_OUT_EPNUM = NEXT_EPNUM,
#endif
#endif

#ifdef SHARED_EP_ENABLE
Expand All @@ -219,25 +223,41 @@ enum usb_endpoints {
// ChibiOS has enough memory and descriptor to actually enable the endpoint
// It could use the same endpoint numbers, as that's supported by ChibiOS
// But the QMK code currently assumes that the endpoint numbers are different
#if STM32_USB_USE_OTG1
#define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM
#else
CONSOLE_OUT_EPNUM = NEXT_EPNUM,
#endif
# else
# define CONSOLE_OUT_EPNUM CONSOLE_IN_EPNUM
# endif
#endif

#ifdef MIDI_ENABLE
MIDI_STREAM_IN_EPNUM = NEXT_EPNUM,
#if STM32_USB_USE_OTG1
#define MIDI_STREAM_OUT_EPNUM MIDI_STREAM_IN_EPNUM
#else
MIDI_STREAM_OUT_EPNUM = NEXT_EPNUM,
#endif
#endif

#ifdef VIRTSER_ENABLE
CDC_NOTIFICATION_EPNUM = NEXT_EPNUM,
CDC_IN_EPNUM = NEXT_EPNUM,
#if STM32_USB_USE_OTG1
#define CDC_OUT_EPNUM CDC_IN_EPNUM
#else
CDC_OUT_EPNUM = NEXT_EPNUM,
#endif
#endif
#ifdef JOYSTICK_ENABLE
JOYSTICK_IN_EPNUM = NEXT_EPNUM,
#if STM32_USB_USE_OTG1
JOYSTICK_OUT_EPNUM = JOYSTICK_IN_EPNUM,
#else
JOYSTICK_OUT_EPNUM = NEXT_EPNUM,
#endif
#endif
};

Expand Down