2 #include "stm32f1xx_hal.h"
12 #define DESC_U16(x) ((x) & 0xff), ((x) >> 8)
16 DESC_STR_MANUFACTURER,
19 DESC_STR_CONFIGURATION,
23 static const byte desc_device[] = {
25 USB_DESC_TYPE_DEVICE, // bDescriptorType
26 DESC_U16(0x0200), // bcdUSB
28 0x00, // bDeviceSubClass
29 0x00, // bDeviceProtocol
30 USB_MAX_EP0_SIZE, // bMaxPacketSize
31 DESC_U16(0x4242), // idVendor
32 DESC_U16(0x0002), // idProduct
33 DESC_U16(0x0200), // bcdDevice
34 DESC_STR_MANUFACTURER, // iManufacturer
35 DESC_STR_PRODUCT, // iProduct
36 DESC_STR_SERIAL, // iSerialNumber
37 USB_NUM_CONFIGURATIONS, // bNumConfigurations
40 static const byte desc_config[] = {
41 // Configuration descriptor
43 USB_DESC_TYPE_CONFIGURATION, // bDescriptorType
46 0x01, // bNumInterfaces
47 0x01, // bConfigurationValue
48 DESC_STR_CONFIGURATION, // iConfiguration
49 0xc0, // bmAttributes: bus-powered, supports remote wakeup
50 0x32, // Max power: 100 mA
51 // Interface descriptor
53 USB_DESC_TYPE_INTERFACE, // bDescriptorType
54 0x00, // bInterfaceNumber
55 0x00, // bAlternateSetting
56 0x02, // bNumEndpoints
57 0xff, // bInterfaceClass: vendor-defined
58 0x00, // bInterfaceSubClass
59 0x00, // nInterfaceProtocol
60 DESC_STR_INTERFACE, // iInterface
61 // End-point descriptor
63 USB_DESC_TYPE_ENDPOINT, // bDescriptorType
64 0x01, // bEndpointAddress
65 USB_EP_TYPE_BULK, // bmAttributes
66 0x40, 0x00, // wMaxPacketSize
67 0x00, // bInterval: unused
68 // End-point descriptor
70 USB_DESC_TYPE_ENDPOINT, // bDescriptorType
71 0x82, // bEndpointAddress
72 USB_EP_TYPE_BULK, // bmAttributes
73 0x40, 0x00, // wMaxPacketSize
74 0x00, // bInterval: unused
77 static const char * const desc_string[] = {
78 NULL, // DESC_STR_NONE
79 "United Computer Wizards", // DESC_STR_MANUFACTURER
80 "Solid State Relays", // DESC_STR_PRODUCT
81 "00000042", // DESC_STR_SERIAL
82 "Default Configuration", // DESC_STR_CONFIGURATION
83 "Default Interface", // DESC_STR_INTERFACE
86 static const byte desc_languages[] = {
88 USB_DESC_TYPE_STRING, // bDescriptorType
89 DESC_U16(1033), // English
96 volatile byte rx_packet_state, tx_packet_state;
98 void usb_dev_reset(struct usb *usb)
100 usb->desc_device = desc_device;
101 usb->desc_device_len = sizeof(desc_device);
102 usb->desc_config = desc_config;
103 usb->desc_config_len = sizeof(desc_config);
104 usb->desc_string = desc_string;
105 usb->desc_string_items = sizeof(desc_string) / sizeof(desc_string[0]);
106 usb->desc_languages = desc_languages;
107 usb->desc_languages_len = sizeof(desc_languages);
110 void usb_dev_configure(struct usb *usb)
112 usb_ep_open(usb, 0x01, USB_EP_TYPE_BULK, 64);
113 usb_ep_open(usb, 0x82, USB_EP_TYPE_BULK, 64);
114 usb_ep_receive(usb, 0x01, rx_packet, 64);
117 void usb_dev_unconfigure(struct usb *usb)
119 usb_ep_close(usb, 0x01);
120 usb_ep_close(usb, 0x82);
123 bool usb_dev_setup_hook(struct usb *usb, struct setup_request *setup)
128 void usb_dev_ctl_recv_done(struct usb *usb)
132 void usb_dev_ctl_send_done(struct usb *usb)
136 void usb_dev_recv_done(struct usb *usb, byte epnum)
140 u32 len = usb_ep_received_size(usb, 0x01);
141 if (len >= 8 && !rx_packet_state)
144 usb_ep_receive(usb, 0x01, rx_packet, 64);
148 void usb_dev_send_done(struct usb *usb, byte epnum)