-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
271 lines (231 loc) · 9.59 KB
/
Copy pathmain.c
File metadata and controls
271 lines (231 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright (c) Rob Moran 2026 <github@thegecko.org>
*/
#include <string.h>
#include <stdint.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/usbstd.h>
#define BULK_EP_MAXPACKET 64
#define CONTROL_EP_PACKET 16
#define WCID_VENDOR_CODE 0x37
void register_wcid_desc(usbd_device *usb_dev);
static enum usbd_request_return_codes msft_string_desc(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete);
static enum usbd_request_return_codes msft_feature_desc(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, usbd_control_complete_callback *complete);
// Microsoft WCID string descriptor (string index 0xee)
static const uint8_t msft_sig_desc[] = {
0x12,
USB_DT_STRING,
'M', 0, 'S', 0, 'F', 0, 'T', 0,
'1', 0, '0', 0, '0', 0,
WCID_VENDOR_CODE,
0
};
// Microsoft WCID feature descriptor (index 0x0004)
static const uint8_t wcid_feature_desc[] = {
0x28, 0x00, 0x00, 0x00, // length = 40 bytes
0x00, 0x01, // version 1.0 (in BCD)
0x04, 0x00, // compatibility descriptor index 0x0004
0x01, // number of sections
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved (7 bytes)
0x00, // interface number 0
0x01, // reserved
0x57, 0x49, 0x4E, 0x55, 0x53, 0x42, 0x00, 0x00, // Compatible ID "WINUSB\0\0"
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Subcompatible ID (unused)
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // reserved 6 bytes
};
static const struct usb_device_descriptor dev = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0110, // USB 1.10
.bDeviceClass = 0x00, // Use class information in the Interface Descriptors
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = BULK_EP_MAXPACKET,
.idVendor = 0x59e3,
.idProduct = 0x0a23,
.bcdDevice = 0x0110, // Version
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
.bNumConfigurations = 1,
};
static const struct usb_endpoint_descriptor endp_bulk[] = {{
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_REQ_TYPE_IN | 1,
.bmAttributes = USB_ENDPOINT_ATTR_BULK | USB_ENDPOINT_ATTR_NOSYNC | USB_ENDPOINT_ATTR_DATA,
.wMaxPacketSize = BULK_EP_MAXPACKET,
.bInterval = 0,
}, {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_REQ_TYPE_OUT | 2,
.bmAttributes = USB_ENDPOINT_ATTR_BULK | USB_ENDPOINT_ATTR_NOSYNC | USB_ENDPOINT_ATTR_DATA,
.wMaxPacketSize = BULK_EP_MAXPACKET,
.bInterval = 0,
}, {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_REQ_TYPE_IN | 3,
.bmAttributes = USB_ENDPOINT_ATTR_BULK | USB_ENDPOINT_ATTR_NOSYNC | USB_ENDPOINT_ATTR_DATA,
.wMaxPacketSize = BULK_EP_MAXPACKET,
.bInterval = 0,
}, {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_REQ_TYPE_OUT | 4,
.bmAttributes = USB_ENDPOINT_ATTR_BULK | USB_ENDPOINT_ATTR_NOSYNC | USB_ENDPOINT_ATTR_DATA,
.wMaxPacketSize = BULK_EP_MAXPACKET,
.bInterval = 0,
}, {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_REQ_TYPE_IN | 5,
.bmAttributes = USB_ENDPOINT_ATTR_BULK | USB_ENDPOINT_ATTR_NOSYNC | USB_ENDPOINT_ATTR_DATA,
.wMaxPacketSize = BULK_EP_MAXPACKET,
.bInterval = 0,
}, {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_REQ_TYPE_OUT | 6,
.bmAttributes = USB_ENDPOINT_ATTR_BULK | USB_ENDPOINT_ATTR_NOSYNC | USB_ENDPOINT_ATTR_DATA,
.wMaxPacketSize = BULK_EP_MAXPACKET,
.bInterval = 0,
}};
static const struct usb_interface_descriptor data_iface[] = {{
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bAlternateSetting = 0,
.bNumEndpoints = 6,
.bInterfaceClass = USB_CLASS_VENDOR,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 0,
.endpoint = endp_bulk,
}};
static const struct usb_interface ifaces[] = {{
.num_altsetting = 1,
.altsetting = data_iface,
}};
static const struct usb_config_descriptor config = {
.bLength = USB_DT_CONFIGURATION_SIZE,
.bDescriptorType = USB_DT_CONFIGURATION,
.wTotalLength = 0,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = USB_CONFIG_ATTR_DEFAULT,
.bMaxPower = 500 >> 1,
.interface = ifaces,
};
static const char *usb_strings[] = {
"Nonolith Labs",
"STM32F103 Test Device",
"TEST_DEVICE",
};
static usbd_device *test_dev;
static uint8_t control_buf[CONTROL_EP_PACKET];
static uint8_t poll_buf[BULK_EP_MAXPACKET];
static uint8_t bulk_buf[BULK_EP_MAXPACKET];
static volatile int ep_pending_len = 0;
static void ep_out_cb(usbd_device *usbd_dev, uint8_t ep) {
(void)ep;
int len = usbd_ep_read_packet(usbd_dev, USB_REQ_TYPE_OUT | 4, bulk_buf, sizeof(bulk_buf));
if (len > 0) {
ep_pending_len = len;
}
}
static enum usbd_request_return_codes control_callback(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) {
(void)usbd_dev;
(void)complete;
switch(req->bRequest) {
case 0x81: {
if (req->bmRequestType & USB_REQ_TYPE_IN) {
memcpy(*buf, control_buf, CONTROL_EP_PACKET);
*len = CONTROL_EP_PACKET;
} else {
if (*len > CONTROL_EP_PACKET) *len = CONTROL_EP_PACKET;
memcpy(control_buf, *buf, *len);
}
return USBD_REQ_HANDLED;
}
}
return USBD_REQ_NOTSUPP;
}
// Registers additional control request handlers to implement
// See https://github.com/pbatard/libwdi/wiki/WCID-Devices
void register_wcid_desc(usbd_device *usb_dev) {
usbd_register_control_callback(usb_dev, USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_DEVICE, USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, msft_string_desc);
usbd_register_control_callback(usb_dev, USB_REQ_TYPE_VENDOR, USB_REQ_TYPE_TYPE, msft_feature_desc);
}
static enum usbd_request_return_codes msft_string_desc(__attribute__((unused)) usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len, __attribute__((unused)) usbd_control_complete_callback *complete) {
// 0x03: descriptor type string
// 0xee: Microsoft WCID string index
if (req->bRequest == USB_REQ_GET_DESCRIPTOR && req->wValue == 0x03ee)
{
*buf = (uint8_t *)msft_sig_desc;
if (*len > msft_sig_desc[0])
*len = msft_sig_desc[0];
return USBD_REQ_HANDLED;
}
return USBD_REQ_NEXT_CALLBACK;
}
static enum usbd_request_return_codes msft_feature_desc(__attribute__((unused)) usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len, __attribute__((unused)) usbd_control_complete_callback *complete) {
// 0x0004: Microsoft WCID index for feature descriptor
if (req->bRequest == WCID_VENDOR_CODE && req->wIndex == 0x0004)
{
*buf = (uint8_t *)wcid_feature_desc;
if (*len > wcid_feature_desc[0])
*len = wcid_feature_desc[0];
return USBD_REQ_HANDLED;
}
return USBD_REQ_NEXT_CALLBACK;
}
static void config_callback(usbd_device *usbd_dev, uint16_t wValue) {
(void)wValue;
// For polling
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_IN | 1, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 2, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
// For timeouts
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_IN | 3, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 4, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, ep_out_cb);
// For echoing back data received
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_IN | 5, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 6, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
// Control requests
usbd_register_control_callback(usbd_dev, USB_REQ_TYPE_VENDOR, USB_REQ_TYPE_TYPE, control_callback);
// NAK endpoint 6 so it times out
usbd_ep_nak_set(usbd_dev, USB_REQ_TYPE_OUT | 6, 1);
// SysTick interrupt every N clock pulses: set reload to N-1
systick_set_reload(99999);
systick_interrupt_enable();
systick_counter_enable();
register_wcid_desc(usbd_dev);
}
int main(void) {
rcc_clock_setup_pll(&rcc_hsi_configs[RCC_CLOCK_HSI_48MHZ]);
uint8_t usbd_control_buffer[128];
test_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(test_dev, config_callback);
register_wcid_desc(test_dev);
while (1) {
usbd_poll(test_dev);
if (ep_pending_len > 0) {
int wrote = usbd_ep_write_packet(test_dev, USB_REQ_TYPE_IN | 3, bulk_buf, ep_pending_len);
if (wrote > 0) {
ep_pending_len = 0;
}
}
}
}
void sys_tick_handler(void) {
usbd_ep_write_packet(test_dev, USB_REQ_TYPE_IN | 1, poll_buf, BULK_EP_MAXPACKET);
usbd_ep_read_packet(test_dev, USB_REQ_TYPE_OUT | 2, poll_buf, BULK_EP_MAXPACKET);
}