Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boards/etee/etee_dongle_uf2/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

board:
name: etee_dongle_uf2
full_name: eteeDongle
vendor: etee
socs:
- name: nrf52840
2 changes: 1 addition & 1 deletion boards/holyiot/holyiot_21017/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

board:
name: holyiot_21017
full_name: holyiot_21017
full_name: Holyiot-21017
vendor: holyiot
socs:
- name: nrf52840
1 change: 1 addition & 0 deletions boards/kounolab/kounolab_dongle_uf2/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

board:
name: kounolab_dongle_uf2
full_name: Stella
vendor: kounolab
socs:
- name: nrf52820
1 change: 1 addition & 0 deletions boards/nordic/promicro_uf2/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

board:
name: promicro_uf2
full_name: Pro Micro
vendor: nordic
socs:
- name: nrf52840
1 change: 1 addition & 0 deletions boards/slimevr/butterfly_p1/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

board:
name: butterfly_p1_uf2
full_name: Butterfly Receiver P1
vendor: slimevr
socs:
- name: nrf52833
Expand Down
1 change: 1 addition & 0 deletions boards/slimevr/butterfly_p2/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

board:
name: butterfly_p2_uf2
full_name: Butterfly Receiver P2
vendor: slimevr
socs:
- name: nrf52833
Expand Down
1 change: 1 addition & 0 deletions boards/slimevr/butterfly_p3/board.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

board:
name: butterfly_p3
full_name: Butterfly Receiver P3
vendor: slimevr
socs:
- name: nrf52820
2 changes: 1 addition & 1 deletion prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ CONFIG_USB_HID_REPORTS=256

CONFIG_USB_HID_POLL_INTERVAL_MS=1
CONFIG_HID_INTERRUPT_EP_MPS=64
#CONFIG_ENABLE_HID_INT_OUT_EP=y
CONFIG_ENABLE_HID_INT_OUT_EP=y

CONFIG_GPIO=y
CONFIG_PWM=y
Expand Down
64 changes: 63 additions & 1 deletion src/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <zephyr/usb/class/usb_hid.h>

static struct k_work report_send;
static struct k_work report_read;

static struct tracker_report {
uint8_t data[16];
Expand All @@ -43,18 +44,24 @@ atomic_t report_read_index = 0;
static bool configured;
static const struct device *hdev;
static ATOMIC_DEFINE(hid_ep_in_busy, 1);
static ATOMIC_DEFINE(hid_ep_out_busy, 1);

#define HID_EP_BUSY_FLAG 0
#define REPORT_PERIOD K_MSEC(1) // streaming reports
#define REPORT_PERIOD K_MSEC(1) // streaming reports // TODO: could it be shorter/reduce latency?
#define POLL_PERIOD K_MSEC(1) // streaming reports // TODO: could it be shorter/reduce latency?
#define HID_EP_REPORT_COUNT 4

struct tracker_report ep_report_buffer[HID_EP_REPORT_COUNT];
uint8_t ep_read_buffer[256]; // TODO: no struct // TODO: is possible to read >64 bytes, e.g. a delayed read?

LOG_MODULE_REGISTER(hid_event, LOG_LEVEL_INF);

static void report_event_handler(struct k_timer *dummy);
static K_TIMER_DEFINE(event_timer, report_event_handler, NULL);

static void report_read_handler(struct k_timer *dummy);
static K_TIMER_DEFINE(read_timer, report_read_handler, NULL);

static const uint8_t hid_report_desc[] = {
HID_USAGE_PAGE(HID_USAGE_GEN_DESKTOP),
HID_USAGE(HID_USAGE_GEN_DESKTOP_UNDEFINED),
Expand All @@ -63,6 +70,10 @@ static const uint8_t hid_report_desc[] = {
HID_REPORT_SIZE(8),
HID_REPORT_COUNT(64),
HID_INPUT(0x02),
HID_USAGE(HID_USAGE_GEN_DESKTOP_UNDEFINED),
HID_REPORT_SIZE(8),
HID_REPORT_COUNT(64),
HID_OUTPUT(0x02),
HID_END_COLLECTION,
};

Expand Down Expand Up @@ -151,12 +162,52 @@ static void hid_dropped_reports_logging(void)

K_THREAD_DEFINE(hid_dropped_reports_logging_thread, 256, hid_dropped_reports_logging, NULL, NULL, NULL, 6, 0, 0);

static void read_report(struct k_work *work)
{
if (!usb_enabled) return;

int ret, read;

if (!atomic_test_and_set_bit(hid_ep_out_busy, HID_EP_BUSY_FLAG)) {
ret = hid_int_ep_read(hdev, (uint8_t *)ep_read_buffer, sizeof(ep_read_buffer), &read);

if (ret != 0) {
LOG_ERR("hid_int_ep_read: %d", ret);
} else {
LOG_INF("hid_int_ep_read: %d", read);
// do something here
LOG_INF("%016llX%016llX%016llX%016llX%016llX%016llX%016llX%016llX",
*(uint64_t *)(ep_read_buffer + 56),
*(uint64_t *)(ep_read_buffer + 48),
*(uint64_t *)(ep_read_buffer + 40),
*(uint64_t *)(ep_read_buffer + 32),
*(uint64_t *)(ep_read_buffer + 24),
*(uint64_t *)(ep_read_buffer + 16),
*(uint64_t *)(ep_read_buffer + 8),
*(uint64_t *)ep_read_buffer
);
}
} else { // busy with what
//LOG_DBG("HID OUT endpoint busy");
}
}

static void int_in_ready_cb(const struct device *dev)
{
ARG_UNUSED(dev);
if (!atomic_test_and_clear_bit(hid_ep_in_busy, HID_EP_BUSY_FLAG)) {
LOG_WRN("IN endpoint callback without preceding buffer write");
}
// TODO: can probably immediately write report from here
}

static void int_out_ready_cb(const struct device *dev)
{
ARG_UNUSED(dev);
if (!atomic_test_and_clear_bit(hid_ep_out_busy, HID_EP_BUSY_FLAG)) {
LOG_WRN("OUT endpoint callback without preceding buffer write");
}
// TODO: can probably immediately read report from here
}

/*
Expand All @@ -176,6 +227,12 @@ static void report_event_handler(struct k_timer *dummy)
k_work_submit(&report_send);
}

static void report_read_handler(struct k_timer *dummy)
{
if (usb_enabled)
k_work_submit(&report_read);
}

static void protocol_cb(const struct device *dev, uint8_t protocol)
{
LOG_INF("New protocol: %s", protocol == HID_PROTOCOL_BOOT ?
Expand All @@ -184,6 +241,7 @@ static void protocol_cb(const struct device *dev, uint8_t protocol)

static const struct hid_ops ops = {
.int_in_ready = int_in_ready_cb,
.int_out_ready = int_out_ready_cb,
.on_idle = on_idle_cb,
.protocol_change = protocol_cb,
};
Expand Down Expand Up @@ -230,6 +288,9 @@ static int composite_pre_init()
atomic_set_bit(hid_ep_in_busy, HID_EP_BUSY_FLAG);
k_timer_start(&event_timer, REPORT_PERIOD, REPORT_PERIOD);

atomic_set_bit(hid_ep_out_busy, HID_EP_BUSY_FLAG);
k_timer_start(&read_timer, POLL_PERIOD, POLL_PERIOD);

if (usb_hid_set_proto_code(hdev, HID_BOOT_IFACE_CODE_NONE)) {
LOG_WRN("Failed to set Protocol Code");
}
Expand All @@ -243,6 +304,7 @@ void usb_init_thread(void)
{
usb_enable(status_cb);
k_work_init(&report_send, send_report);
k_work_init(&report_read, read_report);
usb_enabled = true;
}

Expand Down
Loading
Loading