summaryrefslogtreecommitdiff
path: root/app/src/usb.c
diff options
context:
space:
mode:
authorPeter Johanson <peter@peterjohanson.com>2021-08-02 17:00:56 -0400
committerPete Johanson <peter@peterjohanson.com>2021-08-03 00:19:05 -0400
commit9d34cf561e11682840e10283f0a6947547a649b5 (patch)
tree1ef6318969e40cf2535522085c5264526175c763 /app/src/usb.c
parentcf5c56c4f1023c3d6b3911e2f57724e54dfd6842 (diff)
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.
Diffstat (limited to 'app/src/usb.c')
-rw-r--r--app/src/usb.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/app/src/usb.c b/app/src/usb.c
index c2f61a5..2f0fa43 100644
--- a/app/src/usb.c
+++ b/app/src/usb.c
@@ -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) {
#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() {
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) {