24
24
#include <libopencm3/usb/cdc.h>
25
25
#include <libopencm3/cm3/scb.h>
26
26
27
+ #define COMM_IN_EP 0x83
28
+ #define DATA_IN_EP 0x82
29
+ #define DATA_OUT_EP 0x01
30
+
31
+ /* Buffer to be used for control requests. */
32
+ static uint8_t usbd_control_buffer [128 ];
33
+
34
+ /* Data I/O buffer */
35
+ static char data_buf [64 ];
36
+
27
37
static const struct usb_device_descriptor dev = {
28
38
.bLength = USB_DT_DEVICE_SIZE ,
29
39
.bDescriptorType = USB_DT_DEVICE ,
@@ -49,7 +59,7 @@ static const struct usb_device_descriptor dev = {
49
59
static const struct usb_endpoint_descriptor comm_endp [] = {{
50
60
.bLength = USB_DT_ENDPOINT_SIZE ,
51
61
.bDescriptorType = USB_DT_ENDPOINT ,
52
- .bEndpointAddress = 0x83 ,
62
+ .bEndpointAddress = COMM_IN_EP ,
53
63
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT ,
54
64
.wMaxPacketSize = 16 ,
55
65
.bInterval = 255 ,
@@ -58,16 +68,16 @@ static const struct usb_endpoint_descriptor comm_endp[] = {{
58
68
static const struct usb_endpoint_descriptor data_endp [] = {{
59
69
.bLength = USB_DT_ENDPOINT_SIZE ,
60
70
.bDescriptorType = USB_DT_ENDPOINT ,
61
- .bEndpointAddress = 0x01 ,
71
+ .bEndpointAddress = DATA_OUT_EP ,
62
72
.bmAttributes = USB_ENDPOINT_ATTR_BULK ,
63
- .wMaxPacketSize = 64 ,
73
+ .wMaxPacketSize = sizeof ( data_buf ) ,
64
74
.bInterval = 1 ,
65
75
}, {
66
76
.bLength = USB_DT_ENDPOINT_SIZE ,
67
77
.bDescriptorType = USB_DT_ENDPOINT ,
68
- .bEndpointAddress = 0x82 ,
78
+ .bEndpointAddress = DATA_IN_EP ,
69
79
.bmAttributes = USB_ENDPOINT_ATTR_BULK ,
70
- .wMaxPacketSize = 64 ,
80
+ .wMaxPacketSize = sizeof ( data_buf ) ,
71
81
.bInterval = 1 ,
72
82
} };
73
83
@@ -111,7 +121,7 @@ static const struct usb_interface_descriptor comm_iface[] = {{
111
121
.bDescriptorType = USB_DT_INTERFACE ,
112
122
.bInterfaceNumber = 0 ,
113
123
.bAlternateSetting = 0 ,
114
- .bNumEndpoints = 1 ,
124
+ .bNumEndpoints = sizeof ( comm_endp )/ sizeof ( comm_endp [ 0 ]) ,
115
125
.bInterfaceClass = USB_CLASS_CDC ,
116
126
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM ,
117
127
.bInterfaceProtocol = USB_CDC_PROTOCOL_AT ,
@@ -128,7 +138,7 @@ static const struct usb_interface_descriptor data_iface[] = {{
128
138
.bDescriptorType = USB_DT_INTERFACE ,
129
139
.bInterfaceNumber = 1 ,
130
140
.bAlternateSetting = 0 ,
131
- .bNumEndpoints = 2 ,
141
+ .bNumEndpoints = sizeof ( data_endp )/ sizeof ( data_endp [ 0 ]) ,
132
142
.bInterfaceClass = USB_CLASS_DATA ,
133
143
.bInterfaceSubClass = 0 ,
134
144
.bInterfaceProtocol = 0 ,
@@ -149,11 +159,12 @@ static const struct usb_config_descriptor config = {
149
159
.bLength = USB_DT_CONFIGURATION_SIZE ,
150
160
.bDescriptorType = USB_DT_CONFIGURATION ,
151
161
.wTotalLength = 0 ,
152
- .bNumInterfaces = 2 ,
162
+ .bNumInterfaces = sizeof ( ifaces )/ sizeof ( ifaces [ 0 ]) ,
153
163
.bConfigurationValue = 1 ,
154
164
.iConfiguration = 0 ,
155
- .bmAttributes = 0x80 ,
156
- .bMaxPower = 0x32 ,
165
+ .bmAttributes = 0x80 , // D7 is reserved and must be set to one for
166
+ // historical reasons (Table 9-10)
167
+ .bMaxPower = 0x32 , // 100mA
157
168
158
169
.interface = ifaces ,
159
170
};
@@ -164,9 +175,6 @@ static const char * usb_strings[] = {
164
175
"DEMO" ,
165
176
};
166
177
167
- /* Buffer to be used for control requests. */
168
- uint8_t usbd_control_buffer [128 ];
169
-
170
178
static int cdcacm_control_request (usbd_device * usbd_dev ,
171
179
struct usb_setup_data * req , uint8_t * * buf , uint16_t * len ,
172
180
void (* * complete )(usbd_device * usbd_dev , struct usb_setup_data * req ))
@@ -182,38 +190,41 @@ static int cdcacm_control_request(usbd_device *usbd_dev,
182
190
* even though it's optional in the CDC spec, and we don't
183
191
* advertise it in the ACM functional descriptor.
184
192
*/
185
- return 1 ;
193
+ return USBD_REQ_HANDLED ;
186
194
}
187
195
case USB_CDC_REQ_SET_LINE_CODING :
188
196
if (* len < sizeof (struct usb_cdc_line_coding )) {
189
- return 0 ;
197
+ return USBD_REQ_NOTSUPP ;
190
198
}
191
199
192
- return 1 ;
200
+ return USBD_REQ_HANDLED ;
193
201
}
194
- return 0 ;
202
+ return USBD_REQ_NOTSUPP ;
195
203
}
196
204
197
205
static void cdcacm_data_rx_cb (usbd_device * usbd_dev , uint8_t ep )
198
206
{
199
207
(void )ep ;
200
-
201
- char buf [ 64 ];
202
- int len = usbd_ep_read_packet ( usbd_dev , 0x01 , buf , 64 );
208
+
209
+ size_t len = usbd_ep_read_packet ( usbd_dev , DATA_OUT_EP , data_buf ,
210
+ sizeof ( data_buf ) );
203
211
204
212
if (len ) {
205
- while (usbd_ep_write_packet (usbd_dev , 0x82 , buf , len ) == 0 );
213
+ while (usbd_ep_write_packet (usbd_dev , DATA_IN_EP ,
214
+ data_buf , len ) == 0 );
206
215
}
207
216
}
208
217
209
218
static void cdcacm_set_config (usbd_device * usbd_dev , uint16_t wValue )
210
219
{
211
220
(void )wValue ;
212
221
213
- usbd_ep_setup (usbd_dev , 0x01 , USB_ENDPOINT_ATTR_BULK , 64 ,
214
- cdcacm_data_rx_cb );
215
- usbd_ep_setup (usbd_dev , 0x82 , USB_ENDPOINT_ATTR_BULK , 64 , NULL );
216
- usbd_ep_setup (usbd_dev , 0x83 , USB_ENDPOINT_ATTR_INTERRUPT , 16 , NULL );
222
+ usbd_ep_setup (usbd_dev , DATA_OUT_EP , USB_ENDPOINT_ATTR_BULK ,
223
+ sizeof (data_buf ), cdcacm_data_rx_cb );
224
+ usbd_ep_setup (usbd_dev , DATA_IN_EP , USB_ENDPOINT_ATTR_BULK ,
225
+ sizeof (data_buf ), NULL );
226
+ usbd_ep_setup (usbd_dev , COMM_IN_EP , USB_ENDPOINT_ATTR_INTERRUPT ,
227
+ 16 , NULL );
217
228
218
229
usbd_register_control_callback (
219
230
usbd_dev ,
@@ -236,7 +247,7 @@ int main(void)
236
247
gpio_set_af (GPIOA , GPIO_AF10 , GPIO9 | GPIO11 | GPIO12 );
237
248
238
249
usbd_dev = usbd_init (& otgfs_usb_driver , & dev , & config ,
239
- usb_strings , 3 ,
250
+ usb_strings , sizeof ( usb_strings )/ sizeof ( usb_strings [ 0 ]) ,
240
251
usbd_control_buffer , sizeof (usbd_control_buffer ));
241
252
242
253
usbd_register_set_config_callback (usbd_dev , cdcacm_set_config );
0 commit comments