Skip to content

Commit 4e6214c

Browse files
committed
clean up USB interface driver
1 parent 3d10419 commit 4e6214c

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

firmware/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ config JABI_SERIAL
66
help
77
must be non-empty
88

9+
config JABI_USB_MPS
10+
int "max packet size of USB bulk transfers"
11+
default 512 if USB_DC_HAS_HS_SUPPORT
12+
default 64
13+
914
config JABI_CAN_BUFFER_SIZE
1015
int "CAN buffer size"
1116
default 32

firmware/src/interfaces/usb.c

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,21 @@
11
#include <zephyr/sys/byteorder.h>
22
#include <zephyr/usb/usb_device.h>
3+
#include <usb_descriptor.h>
34
#include <jabi.h>
45

56
#include <zephyr/logging/log.h>
67
LOG_MODULE_REGISTER(iface_usb, CONFIG_LOG_DEFAULT_LEVEL);
78

89
#if DT_PROP(JABI_IFACE_NODE, usb)
910

10-
#define JABI_IF_STR "JABI USB"
11+
/* USB configuration */
12+
#define JABI_IF_STR "JABI USB" // host will search for this
1113

12-
#define AUTO_EP_IN 0x80
13-
#define AUTO_EP_OUT 0x00
14-
15-
#ifdef CONFIG_USB_DC_HAS_HS_SUPPORT
16-
#define MPS 512
17-
#else
18-
#define MPS 64
19-
#endif // IS_ENABLED(CONFIG_USB_DC_HAS_HS_SUPPORT)
20-
21-
#define UTF16LE_LEN(s) (sizeof(s) * 2 - 2)
22-
23-
extern int usb_get_str_descriptor_idx(void *ptr);
24-
25-
struct usb_jabi_config {
14+
USBD_CLASS_DESCR_DEFINE(primary, 0) struct {
2615
struct usb_if_descriptor if0;
2716
struct usb_ep_descriptor if0_out_ep;
2817
struct usb_ep_descriptor if0_in_ep;
29-
} __packed;
30-
31-
struct usb_jabi_str {
32-
uint8_t bLength;
33-
uint8_t bDescriptorType;
34-
uint8_t bString[UTF16LE_LEN(JABI_IF_STR)];
35-
} __packed;
36-
37-
USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_jabi_config usb_cfg = {
18+
} __packed usb_if_desc = {
3819
.if0 = {
3920
.bLength = sizeof(struct usb_if_descriptor),
4021
.bDescriptorType = USB_DESC_INTERFACE,
@@ -46,28 +27,30 @@ USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_jabi_config usb_cfg = {
4627
.bInterfaceProtocol = 0,
4728
.iInterface = 0,
4829
},
49-
5030
.if0_out_ep = {
5131
.bLength = sizeof(struct usb_ep_descriptor),
5232
.bDescriptorType = USB_DESC_ENDPOINT,
5333
.bEndpointAddress = AUTO_EP_OUT,
5434
.bmAttributes = USB_DC_EP_BULK,
55-
.wMaxPacketSize = sys_cpu_to_le16(MPS),
35+
.wMaxPacketSize = sys_cpu_to_le16(CONFIG_JABI_USB_MPS),
5636
.bInterval = 0x00,
5737
},
58-
5938
.if0_in_ep = {
6039
.bLength = sizeof(struct usb_ep_descriptor),
6140
.bDescriptorType = USB_DESC_ENDPOINT,
6241
.bEndpointAddress = AUTO_EP_IN,
6342
.bmAttributes = USB_DC_EP_BULK,
64-
.wMaxPacketSize = sys_cpu_to_le16(MPS),
43+
.wMaxPacketSize = sys_cpu_to_le16(CONFIG_JABI_USB_MPS),
6544
.bInterval = 0x00,
6645
},
6746
};
6847

69-
USBD_STRING_DESCR_USER_DEFINE(primary) struct usb_jabi_str usb_if_str = {
70-
.bLength = (UTF16LE_LEN(JABI_IF_STR) + 2),
48+
USBD_STRING_DESCR_USER_DEFINE(primary) struct {
49+
uint8_t bLength;
50+
uint8_t bDescriptorType;
51+
uint8_t bString[USB_BSTRING_LENGTH(JABI_IF_STR)];
52+
} __packed usb_str_desc = {
53+
.bLength = USB_STRING_DESCRIPTOR_LENGTH(JABI_IF_STR),
7154
.bDescriptorType = USB_DESC_STRING,
7255
.bString = JABI_IF_STR,
7356
};
@@ -83,19 +66,16 @@ static struct usb_ep_cfg_data ep_cfg[] = {
8366
},
8467
};
8568

86-
#define JABI_EP_OUT (ep_cfg[0].ep_addr)
87-
#define JABI_EP_IN (ep_cfg[1].ep_addr)
88-
8969
static void usb_jabi_interface_config(struct usb_desc_header *head,
9070
uint8_t bInterfaceNumber) {
9171
ARG_UNUSED(head);
92-
usb_cfg.if0.bInterfaceNumber = bInterfaceNumber;
93-
usb_cfg.if0.iInterface = usb_get_str_descriptor_idx(&usb_if_str);
72+
usb_if_desc.if0.bInterfaceNumber = bInterfaceNumber;
73+
usb_if_desc.if0.iInterface = usb_get_str_descriptor_idx(&usb_str_desc);
9474
}
9575

9676
USBD_DEFINE_CFG_DATA(usb_config) = {
9777
.usb_device_description = NULL,
98-
.interface_descriptor = &usb_cfg.if0,
78+
.interface_descriptor = &usb_if_desc.if0,
9979
.interface_config = usb_jabi_interface_config,
10080
.cb_usb_status = NULL,
10181
.interface = {
@@ -107,14 +87,15 @@ USBD_DEFINE_CFG_DATA(usb_config) = {
10787
.endpoint = ep_cfg,
10888
};
10989

90+
/* JABI API implementation */
11091
static int usb_init() {
11192
return 0;
11293
}
11394

11495
static void usb_get_req(iface_req_t *req) {
11596
while (1) { // loop until packet received
11697
req->payload_len = 0;
117-
int len = usb_transfer_sync(JABI_EP_OUT, (uint8_t*) req,
98+
int len = usb_transfer_sync(ep_cfg[0].ep_addr, (uint8_t*) req,
11899
sizeof(iface_req_t), USB_TRANS_READ);
119100
if (len < 0) {
120101
LOG_ERR("transfer failed");
@@ -136,7 +117,7 @@ static void usb_send_resp(iface_resp_t *resp) {
136117
}
137118
iface_resp_to_le(resp);
138119
size_t expect_len = IFACE_RESP_HDR_SIZE + resp->payload_len;
139-
int len = usb_transfer_sync(JABI_EP_IN, (uint8_t*) resp,
120+
int len = usb_transfer_sync(ep_cfg[1].ep_addr, (uint8_t*) resp,
140121
expect_len, USB_TRANS_WRITE);
141122
if (len < 0 || len != expect_len) {
142123
LOG_ERR("failed to send response %d", len);

0 commit comments

Comments
 (0)