]> mj.ucw.cz Git - home-hw.git/blob - ssr/Src/usbdev.c
SSR: USB descriptors
[home-hw.git] / ssr / Src / usbdev.c
1 #include "stm32f1xx.h"
2 #include "stm32f1xx_hal.h"
3
4 #include "util.h"
5 #include "usb.h"
6 #include "app.h"
7
8 #include <string.h>
9
10 /*** Descriptors ***/
11
12 #define DESC_U16(x) ((x) & 0xff), ((x) >> 8)
13
14 enum desc_string {
15   DESC_STR_NONE = 0,
16   DESC_STR_MANUFACTURER,
17   DESC_STR_PRODUCT,
18   DESC_STR_SERIAL,
19   DESC_STR_CONFIGURATION,
20   DESC_STR_INTERFACE,
21 };
22
23 static const byte desc_device[] = {
24   18,                           // bLength
25   USB_DESC_TYPE_DEVICE,         // bDescriptorType
26   DESC_U16(0x0200),             // bcdUSB
27   0x00,                         // bDeviceClass
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
38 };
39
40 static const byte desc_config[] = {
41   // Configuration descriptor
42   9,                            // bLength
43   USB_DESC_TYPE_CONFIGURATION,  // bDescriptorType
44   32,                           // wTotalLength
45   0,
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
52   9,                            // bLength
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
62   7,                            // bLength
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
69   7,                            // bLength
70   USB_DESC_TYPE_ENDPOINT,       // bDescriptorType
71   0x82,                         // bEndpointAddress
72   USB_EP_TYPE_BULK,             // bmAttributes
73   0x40, 0x00,                   // wMaxPacketSize
74   0x00,                         // bInterval: unused
75 };
76
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
84 };
85
86 static const byte desc_languages[] = {
87   4,                            // bLength
88   USB_DESC_TYPE_STRING,         // bDescriptorType
89   DESC_U16(1033),               // English
90 };
91
92 /*** Callbacks ***/
93
94 byte rx_packet[64];
95 byte tx_packet[64];
96 volatile byte rx_packet_state, tx_packet_state;
97
98 void usb_dev_reset(struct usb *usb)
99 {
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);
108 }
109
110 void usb_dev_configure(struct usb *usb)
111 {
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);
115 }
116
117 void usb_dev_unconfigure(struct usb *usb)
118 {
119   usb_ep_close(usb, 0x01);
120   usb_ep_close(usb, 0x82);
121 }
122
123 bool usb_dev_setup_hook(struct usb *usb, struct setup_request *setup)
124 {
125   return false;
126 }
127
128 void usb_dev_ctl_recv_done(struct usb *usb)
129 {
130 }
131
132 void usb_dev_ctl_send_done(struct usb *usb)
133 {
134 }
135
136 void usb_dev_recv_done(struct usb *usb, byte epnum)
137 {
138   if (epnum == 0x01)
139     {
140       u32 len = usb_ep_received_size(usb, 0x01);
141       if (len >= 8 && !rx_packet_state)
142         rx_packet_state = 1;
143       else
144         usb_ep_receive(usb, 0x01, rx_packet, 64);
145     }
146 }
147
148 void usb_dev_send_done(struct usb *usb, byte epnum)
149 {
150   tx_packet_state = 0;
151 }