Skip to content

Commit 5ad3db6

Browse files
committed
Move descriptors back to usb_conf.c, set iInterface to 4, rename macro to USB_DFU_ALTN
Assesses review feedback. I could initially not get the macros to work in usb_conf.c, but they seem to work now even though the changes from pull #38 have been present in the development tree from the start.
1 parent 5aa7684 commit 5ad3db6

6 files changed

Lines changed: 77 additions & 115 deletions

File tree

src/stm32f103/target_stm32f103.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "target.h"
3030
#include "config.h"
3131
#include "backup.h"
32-
#include "usb_descriptor.h"
3332

3433
#ifndef USES_GPIOA
3534
#if (HAVE_USB_PULLUP_CONTROL == 0)
@@ -158,10 +157,6 @@ const usbd_driver* target_usb_init(void) {
158157
return &st_usbfs_v1_usb_driver;
159158
}
160159

161-
const struct usb_config_descriptor* target_usb_descriptor(void) {
162-
return &usb_config;
163-
}
164-
165160
bool target_get_force_bootloader(void) {
166161
bool force = false;
167162
/* Check the RTC backup register */

src/stm32l1/target_stm32l1.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "target.h"
2727
#include "config.h"
2828
#include "backup.h"
29-
#include "usb_descriptor.h"
3029

3130
//#define CMD_FAST_BOOT 0xfa57b007
3231
static const uint32_t CMD_BOOT = 0x544F4F42UL;
@@ -76,11 +75,6 @@ const usbd_driver* target_usb_init(void)
7675
return &st_usbfs_v1_usb_driver;
7776
}
7877

79-
const struct usb_config_descriptor* target_usb_descriptor(void)
80-
{
81-
return &usb_config;
82-
}
83-
8478
/* This implementation will always start in bootloader, unless the app
8579
* has asked it to skipp straight forwards
8680
* You may wish to fill in button handling...

src/target.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
extern void target_clock_setup(void);
2727
extern void target_gpio_setup(void);
2828
extern const usbd_driver* target_usb_init(void);
29-
extern const struct usb_config_descriptor* target_usb_descriptor(void);
3029
extern bool target_get_force_bootloader(void);
3130
extern void target_get_serial_number(char* dest, size_t max_chars);
3231
extern size_t target_get_max_firmware_size(void);

src/usb_conf.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,78 @@ static const struct usb_device_descriptor dev = {
4545
.bNumConfigurations = 1,
4646
};
4747

48+
// Macro to create a dummy (no-op) USB interface descriptor with the given alternate setting
49+
#define ALT_DUMMY(N) { \
50+
.bLength = USB_DT_INTERFACE_SIZE, \
51+
.bDescriptorType = USB_DT_INTERFACE, \
52+
.bInterfaceNumber = INTF_DFU, \
53+
.bAlternateSetting = (N), \
54+
.bNumEndpoints = 0, \
55+
.bInterfaceClass = 0, \
56+
.bInterfaceSubClass = 0, \
57+
.bInterfaceProtocol = 0, \
58+
.iInterface = 0, \
59+
.endpoint = NULL, \
60+
.extra = NULL, \
61+
.extralen = 0, \
62+
},
63+
64+
// Functionality for creating repetitive ALT_DUMMY structs with an increasing count during compile time.
65+
// It doesn't look very nice, but C doesn't allow loops in preprocessor macros, so this needs to be hard-coded.
66+
#define ALT0
67+
#define ALT1 ALT_DUMMY(0)
68+
#define ALT2 ALT1 ALT_DUMMY(1)
69+
#define ALT3 ALT2 ALT_DUMMY(2)
70+
#define ALT4 ALT3 ALT_DUMMY(3)
71+
#define ALT5 ALT4 ALT_DUMMY(4)
72+
#define ALTW(n) ALT##n // Wrapper macro for expansion
73+
#define ALTN(n) ALTW(n)
74+
75+
static const struct usb_interface_descriptor altsettings[] = {
76+
ALTN(USB_DFU_ALTN) // Prepend USB_DFU_ALTN dummy USB interface descriptors to "pad" the real one
77+
{
78+
.bLength = USB_DT_INTERFACE_SIZE,
79+
.bDescriptorType = USB_DT_INTERFACE,
80+
.bInterfaceNumber = INTF_DFU,
81+
.bAlternateSetting = USB_DFU_ALTN,
82+
.bNumEndpoints = 0,
83+
.bInterfaceClass = 0xFE,
84+
.bInterfaceSubClass = 1,
85+
.bInterfaceProtocol = 2,
86+
.iInterface = 4,
87+
88+
.endpoint = NULL,
89+
90+
.extra = &dfu_function,
91+
.extralen = sizeof(dfu_function),
92+
}
93+
};
94+
95+
// Tracking this is mandatory if exposing multiple altsettings
96+
static uint8_t cur_altsetting = 0;
97+
98+
static const struct usb_interface interfaces[] = {
99+
/* DFU interface */
100+
{
101+
.cur_altsetting = &cur_altsetting,
102+
.num_altsetting = USB_DFU_ALTN + 1,
103+
.altsetting = (const struct usb_interface_descriptor*)&altsettings,
104+
}
105+
};
106+
107+
static const struct usb_config_descriptor config = {
108+
.bLength = USB_DT_CONFIGURATION_SIZE,
109+
.bDescriptorType = USB_DT_CONFIGURATION,
110+
.wTotalLength = 0,
111+
.bNumInterfaces = sizeof(interfaces)/sizeof(struct usb_interface),
112+
.bConfigurationValue = 1,
113+
.iConfiguration = 0,
114+
.bmAttributes = 0xC0,
115+
.bMaxPower = 0x32,
116+
117+
.interface = interfaces,
118+
};
119+
48120
static const struct usb_device_capability_descriptor* capabilities[] = {
49121
(const struct usb_device_capability_descriptor*)&webusb_platform,
50122
};
@@ -81,7 +153,7 @@ usbd_device* usb_setup(void) {
81153
int num_strings = sizeof(usb_strings)/sizeof(const char*);
82154

83155
const usbd_driver* driver = target_usb_init();
84-
usbd_device* usbd_dev = usbd_init(driver, &dev, target_usb_descriptor(), &bos,
156+
usbd_device* usbd_dev = usbd_init(driver, &dev, &config, &bos,
85157
usb_strings, num_strings,
86158
usbd_control_buffer, sizeof(usbd_control_buffer));
87159

src/usb_conf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#define USB_PID 0xdb42
3434
#endif
3535

36+
// The DFU interface doesn't have any special alternate setting by default
37+
#ifndef USB_DFU_ALTN
38+
#define USB_DFU_ALTN 0
39+
#endif
3640

3741
#ifndef USB_PRODUCT_STRING
3842

src/usb_descriptor.h

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)