Browse Source

fix(usb): Split HID from core USB, logging fix.

* Split core USB init from USB HID init.
* Tweak logging to avoid "log loop" causing
  spurious buffer messages on startup.
xmkb
Peter Johanson 3 years ago committed by Pete Johanson
parent
commit
40cd8da743
  1. 1
      app/CMakeLists.txt
  2. 7
      app/Kconfig
  3. 4
      app/include/zmk/usb.h
  4. 9
      app/include/zmk/usb_hid.h
  5. 2
      app/src/endpoints.c
  6. 48
      app/src/usb.c
  7. 64
      app/src/usb_hid.c

1
app/CMakeLists.txt

@ -78,6 +78,7 @@ if (CONFIG_ZMK_SPLIT_BLE AND CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL) @@ -78,6 +78,7 @@ if (CONFIG_ZMK_SPLIT_BLE AND CONFIG_ZMK_SPLIT_BLE_ROLE_CENTRAL)
target_sources(app PRIVATE src/split/bluetooth/central.c)
endif()
target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c)
target_sources_ifdef(CONFIG_ZMK_USB app PRIVATE src/usb_hid.c)
target_sources_ifdef(CONFIG_ZMK_BLE app PRIVATE src/hog.c)
target_sources_ifdef(CONFIG_ZMK_RGB_UNDERGLOW app PRIVATE src/rgb_underglow.c)
target_sources_ifdef(CONFIG_ZMK_BACKLIGHT app PRIVATE src/backlight.c)

7
app/Kconfig

@ -477,6 +477,10 @@ if ZMK_USB_LOGGING @@ -477,6 +477,10 @@ if ZMK_USB_LOGGING
config ZMK_LOG_LEVEL
default 4
# We do this to avoid log loop where logging to USB generates more log messages.
config USB_CDC_ACM_LOG_LEVEL
default 1
config USB_CDC_ACM_RINGBUF_SIZE
default 1024
@ -486,6 +490,9 @@ config LOG_BUFFER_SIZE @@ -486,6 +490,9 @@ config LOG_BUFFER_SIZE
config LOG_STRDUP_BUF_COUNT
default 16
config LOG_PROCESS_THREAD_STARTUP_DELAY_MS
default 1000
#ZMK_USB_LOGGING
endif

4
app/include/zmk/usb.h

@ -23,7 +23,3 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state(); @@ -23,7 +23,3 @@ enum zmk_usb_conn_state zmk_usb_get_conn_state();
static inline bool zmk_usb_is_powered() { return zmk_usb_get_conn_state() != ZMK_USB_CONN_NONE; }
static inline bool zmk_usb_is_hid_ready() { return zmk_usb_get_conn_state() == ZMK_USB_CONN_HID; }
#ifdef CONFIG_ZMK_USB
int zmk_usb_hid_send_report(const uint8_t *report, size_t len);
#endif /* CONFIG_ZMK_USB */

9
app/include/zmk/usb_hid.h

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#pragma once
int zmk_usb_hid_send_report(const uint8_t *report, size_t len);

2
app/src/endpoints.c

@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
#include <zmk/endpoints.h>
#include <zmk/hid.h>
#include <dt-bindings/zmk/hid_usage_pages.h>
#include <zmk/usb.h>
#include <zmk/usb_hid.h>
#include <zmk/hog.h>
#include <zmk/event_manager.h>
#include <zmk/events/ble_active_profile_changed.h>

48
app/src/usb.c

@ -26,41 +26,6 @@ static void raise_usb_status_changed_event(struct k_work *_work) { @@ -26,41 +26,6 @@ static void raise_usb_status_changed_event(struct k_work *_work) {
K_WORK_DEFINE(usb_status_notifier_work, raise_usb_status_changed_event);
#ifdef CONFIG_ZMK_USB
static const struct device *hid_dev;
static K_SEM_DEFINE(hid_sem, 1, 1);
static void in_ready_cb(const struct device *dev) { k_sem_give(&hid_sem); }
static const struct hid_ops ops = {
.int_in_ready = in_ready_cb,
};
int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {
switch (usb_status) {
case USB_DC_SUSPEND:
return usb_wakeup_request();
case USB_DC_ERROR:
case USB_DC_RESET:
case USB_DC_DISCONNECTED:
case USB_DC_UNKNOWN:
return -ENODEV;
default:
k_sem_take(&hid_sem, K_MSEC(30));
int err = hid_int_ep_write(hid_dev, report, len, NULL);
if (err) {
k_sem_give(&hid_sem);
}
return err;
}
}
#endif /* CONFIG_ZMK_USB */
enum usb_dc_status_code zmk_usb_get_status() { return usb_status; }
enum zmk_usb_conn_state zmk_usb_get_conn_state() {
@ -87,19 +52,6 @@ void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) { @@ -87,19 +52,6 @@ void usb_status_cb(enum usb_dc_status_code status, const uint8_t *params) {
static int zmk_usb_init(const struct device *_arg) {
int usb_enable_ret;
#ifdef CONFIG_ZMK_USB
hid_dev = device_get_binding("HID_0");
if (hid_dev == NULL) {
LOG_ERR("Unable to locate HID device");
return -EINVAL;
}
usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops);
usb_hid_init(hid_dev);
#endif /* CONFIG_ZMK_USB */
usb_enable_ret = usb_enable(usb_status_cb);
if (usb_enable_ret != 0) {

64
app/src/usb_hid.c

@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include <device.h>
#include <init.h>
#include <usb/usb_device.h>
#include <usb/class/usb_hid.h>
#include <zmk/usb.h>
#include <zmk/hid.h>
#include <zmk/keymap.h>
#include <zmk/event_manager.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static const struct device *hid_dev;
static K_SEM_DEFINE(hid_sem, 1, 1);
static void in_ready_cb(const struct device *dev) { k_sem_give(&hid_sem); }
static const struct hid_ops ops = {
.int_in_ready = in_ready_cb,
};
int zmk_usb_hid_send_report(const uint8_t *report, size_t len) {
switch (zmk_usb_get_status()) {
case USB_DC_SUSPEND:
return usb_wakeup_request();
case USB_DC_ERROR:
case USB_DC_RESET:
case USB_DC_DISCONNECTED:
case USB_DC_UNKNOWN:
return -ENODEV;
default:
k_sem_take(&hid_sem, K_MSEC(30));
int err = hid_int_ep_write(hid_dev, report, len, NULL);
if (err) {
k_sem_give(&hid_sem);
}
return err;
}
}
static int zmk_usb_hid_init(const struct device *_arg) {
hid_dev = device_get_binding("HID_0");
if (hid_dev == NULL) {
LOG_ERR("Unable to locate HID device");
return -EINVAL;
}
usb_hid_register_device(hid_dev, zmk_hid_report_desc, sizeof(zmk_hid_report_desc), &ops);
usb_hid_init(hid_dev);
return 0;
}
SYS_INIT(zmk_usb_hid_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
Loading…
Cancel
Save