Skip to content

Commit c14eebb

Browse files
committed
feature(esp_tinyusb): Added test for usb device class reconfiguration
1 parent 9fbdf0d commit c14eebb

File tree

5 files changed

+264
-21
lines changed

5 files changed

+264
-21
lines changed

device/esp_tinyusb/test_app/main/test_bvalid_sig.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void test_bvalid_sig_umount_cb(void)
6868
dev_umounted++;
6969
}
7070

71-
TEST_CASE("bvalid_signal", "[esp_tinyusb][usb_device]")
71+
TEST_CASE("bvalid_signal", "[usb_device]")
7272
{
7373
unsigned int rounds = DEVICE_DETACH_TEST_ROUNDS;
7474

device/esp_tinyusb/test_app/main/test_descriptors_config.c

Lines changed: 261 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void test_descriptors_config_umount_cb(void)
104104

105105
}
106106

107-
TEST_CASE("descriptors_config_all_default", "[esp_tinyusb][usb_device]")
107+
TEST_CASE("descriptors_config_all_default", "[usb_device]")
108108
{
109109
TEST_ASSERT_EQUAL(true, __test_prep());
110110
// Install TinyUSB driver
@@ -125,7 +125,7 @@ TEST_CASE("descriptors_config_all_default", "[esp_tinyusb][usb_device]")
125125
__test_free();
126126
}
127127

128-
TEST_CASE("descriptors_config_device", "[esp_tinyusb][usb_device]")
128+
TEST_CASE("descriptors_config_device", "[usb_device]")
129129
{
130130
TEST_ASSERT_EQUAL(true, __test_prep());
131131
// Install TinyUSB driver
@@ -146,7 +146,7 @@ TEST_CASE("descriptors_config_device", "[esp_tinyusb][usb_device]")
146146
__test_free();
147147
}
148148

149-
TEST_CASE("descriptors_config_device_and_config", "[esp_tinyusb][usb_device]")
149+
TEST_CASE("descriptors_config_device_and_config", "[usb_device]")
150150
{
151151
TEST_ASSERT_EQUAL(true, __test_prep());
152152
// Install TinyUSB driver
@@ -168,7 +168,7 @@ TEST_CASE("descriptors_config_device_and_config", "[esp_tinyusb][usb_device]")
168168
}
169169

170170
#if (TUD_OPT_HIGH_SPEED)
171-
TEST_CASE("descriptors_config_device_and_fs_config_only", "[esp_tinyusb][usb_device]")
171+
TEST_CASE("descriptors_config_device_and_fs_config_only", "[usb_device]")
172172
{
173173
TEST_ASSERT_EQUAL(true, __test_prep());
174174
// Install TinyUSB driver
@@ -187,7 +187,7 @@ TEST_CASE("descriptors_config_device_and_fs_config_only", "[esp_tinyusb][usb_dev
187187
__test_free();
188188
}
189189

190-
TEST_CASE("descriptors_config_device_and_hs_config_only", "[esp_tinyusb][usb_device]")
190+
TEST_CASE("descriptors_config_device_and_hs_config_only", "[usb_device]")
191191
{
192192
TEST_ASSERT_EQUAL(true, __test_prep());
193193
// Install TinyUSB driver
@@ -206,7 +206,7 @@ TEST_CASE("descriptors_config_device_and_hs_config_only", "[esp_tinyusb][usb_dev
206206
__test_free();
207207
}
208208

209-
TEST_CASE("descriptors_config_all_configured", "[esp_tinyusb][usb_device]")
209+
TEST_CASE("descriptors_config_all_configured", "[usb_device]")
210210
{
211211
TEST_ASSERT_EQUAL(true, __test_prep());
212212
// Install TinyUSB driver
@@ -226,4 +226,259 @@ TEST_CASE("descriptors_config_all_configured", "[esp_tinyusb][usb_device]")
226226
}
227227
#endif // TUD_OPT_HIGH_SPEED
228228

229+
//
230+
// HID Class device configuration
231+
//
232+
#define TUSB_HID_CONFIG_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_HID * TUD_HID_DESC_LEN)
233+
234+
static const tusb_desc_device_t test_hid_class_dev_desc = {
235+
.bLength = sizeof(tusb_desc_device_t),
236+
.bDescriptorType = TUSB_DESC_DEVICE,
237+
.bcdUSB = 0x0210, // 2.10
238+
.bDeviceClass = TUSB_CLASS_UNSPECIFIED,
239+
.bDeviceSubClass = 0,
240+
.bDeviceProtocol = 0,
241+
.bMaxPacketSize0 = 64,
242+
.idVendor = 0x413C, // Dell Computer Corp
243+
.idProduct = 0x301A, // Dell MS116 Optical Mouse
244+
.bcdDevice = 0x0100, // 1.00
245+
.iManufacturer = 1,
246+
.iProduct = 2,
247+
.iSerialNumber = 3,
248+
.bNumConfigurations = 1,
249+
};
250+
251+
const uint8_t test_hid_report_descriptor[] = {
252+
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(HID_ITF_PROTOCOL_KEYBOARD)),
253+
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(HID_ITF_PROTOCOL_MOUSE))
254+
};
255+
256+
const char *test_hid_string_descriptor[5] = {
257+
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
258+
"Espressif", // 1: Manufacturer
259+
"USB Test HID Class", // 2: Product
260+
"123456", // 3: Serials, should use chip ID
261+
"Test HID Interface", // 4: HID
262+
};
263+
264+
static const uint8_t test_hid_fs_configuration_descriptor[] = {
265+
// Configuration number, interface count, string index, total length, attribute, power in mA
266+
TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUSB_HID_CONFIG_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
267+
TUD_HID_DESCRIPTOR(0, 4, false, sizeof(test_hid_report_descriptor), 0x81, 16, 10),
268+
};
269+
270+
271+
#if (TUD_OPT_HIGH_SPEED)
272+
static uint8_t const test_hid_hs_configuration_descriptor[] = {
273+
// Config number, interface count, string index, total length, attribute, power in mA
274+
// Configuration number, interface count, string index, total length, attribute, power in mA
275+
TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUSB_HID_CONFIG_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
276+
277+
// Interface number, string index, boot protocol, report descriptor len, EP In address, size & polling interval
278+
TUD_HID_DESCRIPTOR(0, 4, false, sizeof(test_hid_report_descriptor), 0x81, 16, 10),
279+
};
280+
#endif // TUD_OPT_HIGH_SPEED
281+
282+
uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance)
283+
{
284+
return test_hid_report_descriptor;
285+
}
286+
287+
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t *buffer, uint16_t reqlen)
288+
{
289+
return 0;
290+
}
291+
292+
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize)
293+
{
294+
295+
}
296+
297+
//
298+
// CDC Class 1x Interface
299+
//
300+
#define TUSB_CDC_CONFIG_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + 1 * TUD_CDC_DESC_LEN)
301+
302+
const tusb_desc_device_t test_cdc_class_dev_desc = {
303+
.bLength = sizeof(tusb_desc_device_t),
304+
.bDescriptorType = TUSB_DESC_DEVICE,
305+
.bcdUSB = 0x0200,
306+
.bDeviceClass = TUSB_CLASS_MISC,
307+
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
308+
.bDeviceProtocol = MISC_PROTOCOL_IAD,
309+
.bMaxPacketSize0 = 64,
310+
.idVendor = 0x0001,
311+
.idProduct = 0x0002,
312+
.bcdDevice = 0x0000,
313+
.iManufacturer = 0x01,
314+
.iProduct = 0x02,
315+
.iSerialNumber = 0x03,
316+
.bNumConfigurations = 0x01
317+
};
318+
319+
const char *test_cdc_string_descriptor[5] = {
320+
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
321+
"Espressif", // 1: Manufacturer
322+
"USB Test CDC Class", // 2: Product
323+
"123456", // 3: Serials, should use chip ID
324+
"Test CDC Interface", // 4: CDC
325+
};
326+
327+
static const uint8_t test_cdc_fs_configuration_descriptor[] = {
328+
// Configuration number, interface count, string index, total length, attribute, power in mA
329+
TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUSB_CDC_CONFIG_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
330+
TUD_CDC_DESCRIPTOR(0, 4, 0x81, 8, 0x02, 0x82, 64),
331+
};
332+
333+
//
334+
// MSC Class
335+
//
336+
#define TUSB_MSC_CONFIG_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN)
337+
338+
339+
const tusb_desc_device_t test_msc_class_dev_desc = {
340+
.bLength = sizeof(tusb_desc_device_t),
341+
.bDescriptorType = TUSB_DESC_DEVICE,
342+
.bcdUSB = 0x0210, // 2.10
343+
.bDeviceClass = TUSB_CLASS_MSC,
344+
.bDeviceSubClass = 0,
345+
.bDeviceProtocol = 0,
346+
.bMaxPacketSize0 = 64,
347+
.idVendor = 0x0781, // SanDisk Corp
348+
.idProduct = 0x5595,
349+
.bcdDevice = 0x0100, // 1.00
350+
.iManufacturer = 0x01,
351+
.iProduct = 0x02,
352+
.iSerialNumber = 0x03,
353+
.bNumConfigurations = 0x01
354+
};
355+
356+
const char *test_msc_string_descriptor[4] = {
357+
(char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
358+
"Espressif", // 1: Manufacturer
359+
"USB Test MSC Class", // 2: Product
360+
NULL, // 3: NULL
361+
};
362+
363+
static const uint8_t test_msc_fs_configuration_descriptor[] = {
364+
// Configuration number, interface count, string index, total length, attribute, power in mA
365+
TUD_CONFIG_DESCRIPTOR(1, 1, 0, TUSB_MSC_CONFIG_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
366+
TUD_MSC_DESCRIPTOR(1, 0, 0x01, 0x81, 64),
367+
};
368+
369+
bool tud_msc_test_unit_ready_cb(uint8_t lun)
370+
{
371+
(void) lun;
372+
// Our device doesn't have any media for testing purpose
373+
return false;
374+
}
375+
376+
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
377+
{
378+
(void) lun;
379+
*block_count = 0;
380+
*block_size = 0;
381+
}
382+
383+
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
384+
{
385+
(void) lun;
386+
const char vid[] = "USB";
387+
const char pid[] = "MSC Class";
388+
const char rev[] = "0.1";
389+
390+
memcpy(vendor_id, vid, strlen(vid));
391+
memcpy(product_id, pid, strlen(pid));
392+
memcpy(product_rev, rev, strlen(rev));
393+
}
394+
395+
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
396+
{
397+
return 0;
398+
}
399+
400+
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
401+
{
402+
return 0;
403+
}
404+
405+
int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
406+
{
407+
return 0;
408+
}
409+
410+
//
411+
// Configuration array
412+
//
413+
414+
enum {
415+
TEST_CFG_HID_CLASS = 0,
416+
TEST_CFG_CDC_CLASS,
417+
TEST_CFG_MSC_CLASS,
418+
};
419+
420+
const tinyusb_config_t tusb_cfg[] = {
421+
// HID Class
422+
{
423+
.external_phy = false,
424+
.device_descriptor = &test_hid_class_dev_desc,
425+
.string_descriptor = test_hid_string_descriptor,
426+
.string_descriptor_count = sizeof(test_hid_string_descriptor) / sizeof(test_hid_string_descriptor[0]),
427+
.configuration_descriptor = test_hid_fs_configuration_descriptor,
428+
#if (TUD_OPT_HIGH_SPEED)
429+
.hs_configuration_descriptor = test_hid_hs_configuration_descriptor,
430+
#endif // TUD_OPT_HIGH_SPEED
431+
},
432+
// CDC Class
433+
{
434+
.external_phy = false,
435+
.device_descriptor = &test_cdc_class_dev_desc,
436+
.string_descriptor = test_cdc_string_descriptor,
437+
.string_descriptor_count = sizeof(test_cdc_string_descriptor) / sizeof(test_cdc_string_descriptor[0]),
438+
.configuration_descriptor = test_cdc_fs_configuration_descriptor,
439+
#if (TUD_OPT_HIGH_SPEED)
440+
.hs_configuration_descriptor = test_cdc_hs_configuration_descriptor,
441+
#endif // TUD_OPT_HIGH_SPEED
442+
},
443+
// MSC Class
444+
{
445+
.external_phy = false,
446+
.device_descriptor = &test_msc_class_dev_desc,
447+
.string_descriptor = test_msc_string_descriptor,
448+
.string_descriptor_count = sizeof(test_msc_string_descriptor) / sizeof(test_msc_string_descriptor[0]),
449+
.configuration_descriptor = test_msc_fs_configuration_descriptor,
450+
#if (TUD_OPT_HIGH_SPEED)
451+
.hs_configuration_descriptor = test_msc_hs_configuration_descriptor,
452+
#endif // TUD_OPT_HIGH_SPEED
453+
},
454+
};
455+
456+
457+
TEST_CASE("device_class_reconfiguration", "[usb_device]")
458+
{
459+
TEST_ASSERT_EQUAL(true, __test_prep());
460+
// Install HID Class only
461+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_install(&tusb_cfg[TEST_CFG_HID_CLASS]));
462+
// Wait for mounted callback
463+
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
464+
// Cleanup
465+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
466+
467+
// Install CDC Class only
468+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_install(&tusb_cfg[TEST_CFG_CDC_CLASS]));
469+
// Wait for mounted callback
470+
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
471+
// Cleanup
472+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
473+
474+
// Install MSC Class only
475+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_install(&tusb_cfg[TEST_CFG_MSC_CLASS]));
476+
// Wait for mounted callback
477+
TEST_ASSERT_EQUAL(ESP_OK, __test_wait_conn());
478+
// Cleanup
479+
TEST_ASSERT_EQUAL(ESP_OK, tinyusb_driver_uninstall());
480+
481+
__test_free();
482+
}
483+
229484
#endif // SOC_USB_OTG_SUPPORTED

device/esp_tinyusb/test_app/pytest_usb_device.py

Whitespace-only changes.

device/esp_tinyusb/test_app/pytest_usb_device_esp_tinyusb.py

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

device/esp_tinyusb/test_app/sdkconfig.defaults

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Configure TinyUSB, it will be used to mock USB devices
22
CONFIG_TINYUSB=y
3-
CONFIG_TINYUSB_MSC_ENABLED=n
3+
CONFIG_TINYUSB_MSC_ENABLED=y
44
CONFIG_TINYUSB_CDC_ENABLED=y
55
CONFIG_TINYUSB_CDC_COUNT=2
6-
CONFIG_TINYUSB_HID_COUNT=0
6+
CONFIG_TINYUSB_HID_COUNT=1
77

88
# Disable watchdogs, they'd get triggered during unity interactive menu
99
CONFIG_ESP_INT_WDT=n

0 commit comments

Comments
 (0)