Skip to content

Commit 93b657b

Browse files
committed
f4-discovery usb_cdcacm cleanup
Clean up of f4-discovery usb_cdcacm sample. - Named endpoints - Named result codes - Data buffer made static instead of allocated on stack - Used sizeof-based values where possible
1 parent ec1d59e commit 93b657b

File tree

1 file changed

+37
-26
lines changed
  • examples/stm32/f4/stm32f4-discovery/usb_cdcacm

1 file changed

+37
-26
lines changed

examples/stm32/f4/stm32f4-discovery/usb_cdcacm/cdcacm.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
#include <libopencm3/usb/cdc.h>
2525
#include <libopencm3/cm3/scb.h>
2626

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+
2737
static const struct usb_device_descriptor dev = {
2838
.bLength = USB_DT_DEVICE_SIZE,
2939
.bDescriptorType = USB_DT_DEVICE,
@@ -49,7 +59,7 @@ static const struct usb_device_descriptor dev = {
4959
static const struct usb_endpoint_descriptor comm_endp[] = {{
5060
.bLength = USB_DT_ENDPOINT_SIZE,
5161
.bDescriptorType = USB_DT_ENDPOINT,
52-
.bEndpointAddress = 0x83,
62+
.bEndpointAddress = COMM_IN_EP,
5363
.bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
5464
.wMaxPacketSize = 16,
5565
.bInterval = 255,
@@ -58,16 +68,16 @@ static const struct usb_endpoint_descriptor comm_endp[] = {{
5868
static const struct usb_endpoint_descriptor data_endp[] = {{
5969
.bLength = USB_DT_ENDPOINT_SIZE,
6070
.bDescriptorType = USB_DT_ENDPOINT,
61-
.bEndpointAddress = 0x01,
71+
.bEndpointAddress = DATA_OUT_EP,
6272
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
63-
.wMaxPacketSize = 64,
73+
.wMaxPacketSize = sizeof(data_buf),
6474
.bInterval = 1,
6575
}, {
6676
.bLength = USB_DT_ENDPOINT_SIZE,
6777
.bDescriptorType = USB_DT_ENDPOINT,
68-
.bEndpointAddress = 0x82,
78+
.bEndpointAddress = DATA_IN_EP,
6979
.bmAttributes = USB_ENDPOINT_ATTR_BULK,
70-
.wMaxPacketSize = 64,
80+
.wMaxPacketSize = sizeof(data_buf),
7181
.bInterval = 1,
7282
} };
7383

@@ -111,7 +121,7 @@ static const struct usb_interface_descriptor comm_iface[] = {{
111121
.bDescriptorType = USB_DT_INTERFACE,
112122
.bInterfaceNumber = 0,
113123
.bAlternateSetting = 0,
114-
.bNumEndpoints = 1,
124+
.bNumEndpoints = sizeof(comm_endp)/sizeof(comm_endp[0]),
115125
.bInterfaceClass = USB_CLASS_CDC,
116126
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
117127
.bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
@@ -128,7 +138,7 @@ static const struct usb_interface_descriptor data_iface[] = {{
128138
.bDescriptorType = USB_DT_INTERFACE,
129139
.bInterfaceNumber = 1,
130140
.bAlternateSetting = 0,
131-
.bNumEndpoints = 2,
141+
.bNumEndpoints = sizeof(data_endp)/sizeof(data_endp[0]),
132142
.bInterfaceClass = USB_CLASS_DATA,
133143
.bInterfaceSubClass = 0,
134144
.bInterfaceProtocol = 0,
@@ -149,11 +159,12 @@ static const struct usb_config_descriptor config = {
149159
.bLength = USB_DT_CONFIGURATION_SIZE,
150160
.bDescriptorType = USB_DT_CONFIGURATION,
151161
.wTotalLength = 0,
152-
.bNumInterfaces = 2,
162+
.bNumInterfaces = sizeof(ifaces)/sizeof(ifaces[0]),
153163
.bConfigurationValue = 1,
154164
.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
157168

158169
.interface = ifaces,
159170
};
@@ -164,9 +175,6 @@ static const char * usb_strings[] = {
164175
"DEMO",
165176
};
166177

167-
/* Buffer to be used for control requests. */
168-
uint8_t usbd_control_buffer[128];
169-
170178
static int cdcacm_control_request(usbd_device *usbd_dev,
171179
struct usb_setup_data *req, uint8_t **buf, uint16_t *len,
172180
void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
@@ -182,38 +190,41 @@ static int cdcacm_control_request(usbd_device *usbd_dev,
182190
* even though it's optional in the CDC spec, and we don't
183191
* advertise it in the ACM functional descriptor.
184192
*/
185-
return 1;
193+
return USBD_REQ_HANDLED;
186194
}
187195
case USB_CDC_REQ_SET_LINE_CODING:
188196
if (*len < sizeof(struct usb_cdc_line_coding)) {
189-
return 0;
197+
return USBD_REQ_NOTSUPP;
190198
}
191199

192-
return 1;
200+
return USBD_REQ_HANDLED;
193201
}
194-
return 0;
202+
return USBD_REQ_NOTSUPP;
195203
}
196204

197205
static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
198206
{
199207
(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));
203211

204212
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);
206215
}
207216
}
208217

209218
static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
210219
{
211220
(void)wValue;
212221

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);
217228

218229
usbd_register_control_callback(
219230
usbd_dev,
@@ -236,7 +247,7 @@ int main(void)
236247
gpio_set_af(GPIOA, GPIO_AF10, GPIO9 | GPIO11 | GPIO12);
237248

238249
usbd_dev = usbd_init(&otgfs_usb_driver, &dev, &config,
239-
usb_strings, 3,
250+
usb_strings, sizeof(usb_strings)/sizeof(usb_strings[0]),
240251
usbd_control_buffer, sizeof(usbd_control_buffer));
241252

242253
usbd_register_set_config_callback(usbd_dev, cdcacm_set_config);

0 commit comments

Comments
 (0)