|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/kernel.h> |
| 8 | +#include <zephyr/init.h> |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/usb/usb_device.h> |
| 11 | +#include <zephyr/usb/class/usb_hid.h> |
| 12 | +#include <zephyr/net/buf.h> |
| 13 | + |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | +LOG_MODULE_REGISTER(dap_hid, LOG_LEVEL_INF); |
| 16 | + |
| 17 | +#include <cmsis_dap.h> |
| 18 | + |
| 19 | +#define DAP_PACKET_SIZE CONFIG_HID_INTERRUPT_EP_MPS |
| 20 | + |
| 21 | +#if (DAP_PACKET_SIZE < 64U) |
| 22 | +#error "Minimum packet size is 64" |
| 23 | +#endif |
| 24 | +#if (DAP_PACKET_SIZE > 32768U) |
| 25 | +#error "Maximum packet size is 32768" |
| 26 | +#endif |
| 27 | + |
| 28 | +const static struct device *hid0_dev; |
| 29 | + |
| 30 | +NET_BUF_POOL_FIXED_DEFINE(ep_out_pool, CONFIG_CMSIS_DAP_PACKET_COUNT, |
| 31 | + DAP_PACKET_SIZE, 0, NULL); |
| 32 | + |
| 33 | +K_SEM_DEFINE(hid_epin_sem, 0, 1); |
| 34 | +K_FIFO_DEFINE(ep_out_queue); |
| 35 | + |
| 36 | +static const uint8_t hid_report_desc[] = { |
| 37 | + HID_ITEM(HID_ITEM_TAG_USAGE_PAGE, HID_ITEM_TYPE_GLOBAL, 2), 0x00, 0xFF, |
| 38 | + HID_USAGE(HID_USAGE_GEN_DESKTOP_POINTER), |
| 39 | + HID_COLLECTION(HID_COLLECTION_APPLICATION), |
| 40 | + HID_LOGICAL_MIN8(0x00), |
| 41 | + HID_LOGICAL_MAX16(0xFF, 0x00), |
| 42 | + HID_REPORT_SIZE(8), |
| 43 | + HID_REPORT_COUNT(64), |
| 44 | + HID_USAGE(HID_USAGE_GEN_DESKTOP_POINTER), |
| 45 | + HID_INPUT(0x02), |
| 46 | + HID_REPORT_COUNT(64), |
| 47 | + HID_USAGE(HID_USAGE_GEN_DESKTOP_POINTER), |
| 48 | + HID_OUTPUT(0x02), |
| 49 | + HID_REPORT_COUNT(0x01), |
| 50 | + HID_USAGE(HID_USAGE_GEN_DESKTOP_POINTER), |
| 51 | + HID_FEATURE(0x02), |
| 52 | + HID_END_COLLECTION, |
| 53 | +}; |
| 54 | + |
| 55 | +static void int_in_ready_cb(const struct device *dev) |
| 56 | +{ |
| 57 | + ARG_UNUSED(dev); |
| 58 | + |
| 59 | + k_sem_give(&hid_epin_sem); |
| 60 | +} |
| 61 | + |
| 62 | +void int_out_ready_cb(const struct device *dev) |
| 63 | +{ |
| 64 | + struct net_buf *buf; |
| 65 | + size_t len = 0; |
| 66 | + |
| 67 | + buf = net_buf_alloc(&ep_out_pool, K_FOREVER); |
| 68 | + hid_int_ep_read(dev, buf->data, buf->size, &len); |
| 69 | + net_buf_add(buf, len); |
| 70 | + |
| 71 | + if (len == 0) { |
| 72 | + LOG_WRN("drop empty packet"); |
| 73 | + net_buf_unref(buf); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + k_fifo_put(&ep_out_queue, buf); |
| 78 | +} |
| 79 | + |
| 80 | +static const struct hid_ops ops = { |
| 81 | + .int_in_ready = int_in_ready_cb, |
| 82 | + .int_out_ready = int_out_ready_cb, |
| 83 | +}; |
| 84 | + |
| 85 | +int main(void) |
| 86 | +{ |
| 87 | + const struct device *const swd_dev = DEVICE_DT_GET_ONE(zephyr_swdp_gpio); |
| 88 | + uint8_t response_buf[DAP_PACKET_SIZE]; |
| 89 | + int ret; |
| 90 | + |
| 91 | + if (!device_is_ready(swd_dev)) { |
| 92 | + LOG_ERR("SWD device is not ready"); |
| 93 | + return -ENODEV; |
| 94 | + } |
| 95 | + |
| 96 | + ret = dap_setup(swd_dev); |
| 97 | + if (ret) { |
| 98 | + LOG_ERR("Failed to initialize DAP controller (%d)", ret); |
| 99 | + return ret; |
| 100 | + } |
| 101 | + |
| 102 | + ret = usb_enable(NULL); |
| 103 | + if (ret) { |
| 104 | + LOG_ERR("Failed to enable USB (%d)", ret); |
| 105 | + return ret; |
| 106 | + } |
| 107 | + |
| 108 | + while (1) { |
| 109 | + struct net_buf *buf; |
| 110 | + size_t len; |
| 111 | + |
| 112 | + buf = k_fifo_get(&ep_out_queue, K_FOREVER); |
| 113 | + |
| 114 | + len = dap_execute_cmd(buf->data, response_buf); |
| 115 | + LOG_DBG("response length %u", len); |
| 116 | + net_buf_unref(buf); |
| 117 | + |
| 118 | + if (hid_int_ep_write(hid0_dev, response_buf, len, NULL)) { |
| 119 | + LOG_ERR("Failed to send a response"); |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + k_sem_take(&hid_epin_sem, K_FOREVER); |
| 124 | + } |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
| 129 | +static int hid_dap_preinit(void) |
| 130 | +{ |
| 131 | + hid0_dev = device_get_binding("HID_0"); |
| 132 | + if (hid0_dev == NULL) { |
| 133 | + LOG_ERR("Cannot get HID_0"); |
| 134 | + return -ENODEV; |
| 135 | + } |
| 136 | + |
| 137 | + usb_hid_register_device(hid0_dev, hid_report_desc, |
| 138 | + sizeof(hid_report_desc), &ops); |
| 139 | + |
| 140 | + return usb_hid_init(hid0_dev); |
| 141 | +} |
| 142 | + |
| 143 | +SYS_INIT(hid_dap_preinit, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); |
0 commit comments