@@ -31,8 +31,6 @@ extern "C" {
31
31
#include " r_usb_basic_api.h"
32
32
#include " r_usb_pcdc_api.h"
33
33
34
- #define USBD_ITF_CDC (0 ) // needs 2 interfaces
35
-
36
34
#ifndef USBD_CDC_EP_CMD
37
35
#define USBD_CDC_EP_CMD (0x81 )
38
36
#endif
@@ -121,45 +119,73 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
121
119
void __SetupUSBDescriptor () {
122
120
if (!usbd_desc_cfg) {
123
121
124
- uint8_t interface_count = (__USBInstallSerial ? 3 : 0 ) + (__USBGetHIDReport ? 1 : 0 ) + (__USBInstallMSD ? 1 : 0 );
122
+ // This tracks the next (0 based) interface number to assign, incremented after each
123
+ // interface descriptor is created. After all interface descriptors have been created,
124
+ // it represents the (1 based) number of interface descriptors that have been defined.
125
+ uint8_t interface_count = 0 ;
125
126
126
- uint8_t cdc_desc[TUD_CDC_DESC_LEN + TUD_DFU_RT_DESC_LEN] = {
127
+ /*
128
+ * ----- CDC
129
+ */
130
+ bool install_CDC = __USBInstallSerial;
131
+ uint8_t cdc_desc[TUD_CDC_DESC_LEN] = {
127
132
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
128
- TUD_CDC_DESCRIPTOR (USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
129
- TUD_DFU_RT_DESCRIPTOR (USBD_ITF_CDC+2 , USBD_STR_DFU_RT, 0x0d , 1000 , 4096 ),
133
+ TUD_CDC_DESCRIPTOR (interface_count, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE)
130
134
};
135
+ interface_count += (install_CDC ? 2 : 0 );
131
136
132
137
/*
133
- * ----- HID
134
- */
138
+ * ----- DFU
139
+ */
140
+ bool install_DFU = false ;
141
+ #if CFG_TUD_DFU_RUNTIME
142
+ install_DFU = __USBInstallSerial;
143
+ uint8_t dfu_desc[TUD_DFU_RT_DESC_LEN] = {
144
+ // Interface number, string index, attribute, timeout, xfer size
145
+ TUD_DFU_RT_DESCRIPTOR (interface_count, USBD_STR_DFU_RT, 0x0d , 1000 , 4096 )
146
+ };
147
+ interface_count += (install_DFU ? 1 : 0 );
148
+ #else
149
+ uint8_t dfu_desc[0 ] = {};
150
+ #endif
135
151
152
+ /*
153
+ * ----- HID
154
+ */
155
+ bool install_HID = false ;
136
156
size_t hid_report_len = 0 ;
137
157
if (__USBGetHIDReport) {
158
+ install_HID = true ;
138
159
__USBGetHIDReport (&hid_report_len);
139
160
}
140
- uint8_t hid_itf = __USBInstallSerial ? 3 : 0 ;
141
161
uint8_t hid_desc[TUD_HID_DESC_LEN] = {
142
162
// Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
143
- TUD_HID_DESCRIPTOR (hid_itf , 0 , HID_ITF_PROTOCOL_NONE, hid_report_len, USBD_HID_EP, CFG_TUD_HID_EP_BUFSIZE, 10 )
163
+ TUD_HID_DESCRIPTOR (interface_count , 0 , HID_ITF_PROTOCOL_NONE, hid_report_len, USBD_HID_EP, CFG_TUD_HID_EP_BUFSIZE, 10 )
144
164
};
165
+ interface_count += (install_HID ? 1 : 0 );
145
166
146
167
/*
147
168
* ----- MASS STORAGE DEVICE
148
- */
149
-
169
+ */
170
+ bool install_MSD = false ;
150
171
#if CFG_TUD_MSC
151
- uint8_t msd_itf = (__USBInstallSerial ? 3 : 0 ) + (__USBGetHIDReport ? 1 : 0 ) ;
172
+ install_MSD = __USBInstallMSD ;
152
173
uint8_t msd_desc[TUD_MSC_DESC_LEN] = {
153
174
// Interface number, string index, EP Out & EP In address, EP size
154
- TUD_MSC_DESCRIPTOR (msd_itf , 0 , USBD_MSD_EP_OUT, USBD_MSD_EP_IN, USBD_MSD_IN_OUT_SIZE)
175
+ TUD_MSC_DESCRIPTOR (interface_count , 0 , USBD_MSD_EP_OUT, USBD_MSD_EP_IN, USBD_MSD_IN_OUT_SIZE)
155
176
};
177
+ interface_count += (install_MSD ? 1 : 0 );
156
178
#else
157
179
uint8_t msd_desc[0 ] = {};
158
180
#endif
159
181
160
-
161
- int usbd_desc_len = TUD_CONFIG_DESC_LEN + (__USBInstallSerial ? sizeof (cdc_desc) : 0 ) + (__USBGetHIDReport ? sizeof (hid_desc) : 0 ) + (__USBInstallMSD ? sizeof (msd_desc) : 0 );
162
-
182
+ // Create configuration descriptor
183
+ int usbd_desc_len =
184
+ TUD_CONFIG_DESC_LEN
185
+ + (install_CDC ? sizeof (cdc_desc) : 0 )
186
+ + (install_DFU ? sizeof (dfu_desc) : 0 )
187
+ + (install_HID ? sizeof (hid_desc) : 0 )
188
+ + (install_MSD ? sizeof (msd_desc) : 0 );
163
189
uint8_t tud_cfg_desc[TUD_CONFIG_DESC_LEN] = {
164
190
// Config number, interface count, string index, total length, attribute, power in mA
165
191
TUD_CONFIG_DESCRIPTOR (1 , interface_count, USBD_STR_0, usbd_desc_len, TUSB_DESC_CONFIG_ATT_SELF_POWERED, 500 )
@@ -172,15 +198,19 @@ void __SetupUSBDescriptor() {
172
198
uint8_t *ptr = usbd_desc_cfg;
173
199
memcpy (ptr, tud_cfg_desc, sizeof (tud_cfg_desc));
174
200
ptr += sizeof (tud_cfg_desc);
175
- if (__USBInstallSerial ) {
201
+ if (install_CDC ) {
176
202
memcpy (ptr, cdc_desc, sizeof (cdc_desc));
177
203
ptr += sizeof (cdc_desc);
178
204
}
179
- if (__USBGetHIDReport) {
205
+ if (install_DFU) {
206
+ memcpy (ptr, dfu_desc, sizeof (dfu_desc));
207
+ ptr += sizeof (dfu_desc);
208
+ }
209
+ if (install_HID) {
180
210
memcpy (ptr, hid_desc, sizeof (hid_desc));
181
211
ptr += sizeof (hid_desc);
182
212
}
183
- if (__USBInstallMSD ) {
213
+ if (install_MSD ) {
184
214
memcpy (ptr, msd_desc, sizeof (msd_desc));
185
215
ptr += sizeof (msd_desc);
186
216
}
0 commit comments