Browse Source

fix(usb): USB state changes in system thhread.

* Use a `k_work` to send usb status events from the system
  workqueue thread, not the USB work thread.
xmkb
Peter Johanson 3 years ago committed by Pete Johanson
parent
commit
9d34cf561e
  1. 15
      app/src/usb.c

15
app/src/usb.c

@ -19,6 +19,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -19,6 +19,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static enum usb_dc_status_code usb_status = USB_DC_UNKNOWN;
static void raise_usb_status_changed_event(struct k_work *_work) {
ZMK_EVENT_RAISE(new_zmk_usb_conn_state_changed(
(struct zmk_usb_conn_state_changed){.conn_state = zmk_usb_get_conn_state()}));
}
K_WORK_DEFINE(usb_status_notifier_work, raise_usb_status_changed_event);
#ifdef CONFIG_ZMK_USB
static const struct device *hid_dev;
@ -54,14 +61,10 @@ int zmk_usb_hid_send_report(const uint8_t *report, size_t len) { @@ -54,14 +61,10 @@ int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {
#endif /* CONFIG_ZMK_USB */
static void raise_usb_status_changed_event() {
ZMK_EVENT_RAISE(new_zmk_usb_conn_state_changed(
(struct zmk_usb_conn_state_changed){.conn_state = zmk_usb_get_conn_state()}));
}
enum usb_dc_status_code zmk_usb_get_status() { return usb_status; }
enum zmk_usb_conn_state zmk_usb_get_conn_state() {
LOG_DBG("state: %d", usb_status);
switch (usb_status) {
case USB_DC_DISCONNECTED:
case USB_DC_UNKNOWN:
@ -78,7 +81,7 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state() { @@ -78,7 +81,7 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state() {
void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) {
usb_status = status;
raise_usb_status_changed_event();
k_work_submit(&usb_status_notifier_work);
};
static int zmk_usb_init(const struct device *_arg) {

Loading…
Cancel
Save