Skip to content

Commit c0bbe3b

Browse files
committed
Enhanced test devices
1 parent a02b612 commit c0bbe3b

File tree

17 files changed

+120
-41
lines changed

17 files changed

+120
-41
lines changed

java-does-usb/src/test/java/net/codecrete/usb/ControlTransferTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.Test;
1313

1414
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
1516

1617
/**
1718
* Tests control transfers
@@ -38,4 +39,17 @@ void storeValueInDataStage_canBeRetrieved() {
3839
var retrievedValue = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
3940
assertArrayEquals(sentValue, retrievedValue);
4041
}
42+
43+
@Test
44+
void interfaceNumber_canBeRetrieved() {
45+
var response = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) interfaceNumber), 1);
46+
assertEquals(interfaceNumber, response[0] & 0xff);
47+
48+
if (isCompositeDevce()) {
49+
testDevice.claimInterface(2);
50+
response = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) 2), 1);
51+
assertEquals(2, response[0] & 0xff);
52+
testDevice.releaseInterface(2);
53+
}
54+
}
4155
}

java-does-usb/src/test/java/net/codecrete/usb/DescriptionTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ void deviceInfo_isCorrect() {
2424
assertEquals(isLoopbackDevice() ? "Loopback" : "Composite", testDevice.product());
2525
assertEquals(12, testDevice.serialNumber().length());
2626

27-
if (interfaceNumber == 2) {
28-
// composite device
29-
assertEquals(0xef, testDevice.classCode());
30-
assertEquals(0x02, testDevice.subclassCode());
31-
assertEquals(0x01, testDevice.protocolCode());
32-
} else {
27+
if (isLoopbackDevice()) {
3328
// simple device
3429
assertEquals(0xff, testDevice.classCode());
3530
assertEquals(0x00, testDevice.subclassCode());
3631
assertEquals(0x00, testDevice.protocolCode());
32+
} else {
33+
// composite device
34+
assertEquals(0xef, testDevice.classCode());
35+
assertEquals(0x02, testDevice.subclassCode());
36+
assertEquals(0x01, testDevice.protocolCode());
3737
}
3838

3939
var isComposite = isCompositeDevce();
@@ -132,7 +132,7 @@ void endpointDescriptors_areCorrect() {
132132

133133
@Test
134134
void configurationDescription_isAvailable() {
135-
var expectedLength = isLoopbackDevice() ? 69 : 98;
135+
var expectedLength = isLoopbackDevice() ? 69 : 115;
136136

137137
var configDesc = testDevice.configurationDescriptor();
138138
assertNotNull(configDesc);

java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class TestDeviceBase {
4141
/**
4242
* Composite test device loopback interface number
4343
*/
44-
static final int LOOPBACK_INTF_COMPOSITE = 2;
44+
static final int LOOPBACK_INTF_COMPOSITE = 3;
4545
/**
4646
* Interface number of connected test device
4747
*/
92 Bytes
Binary file not shown.
92 Bytes
Binary file not shown.
92 Bytes
Binary file not shown.

test-devices/composite-stm32/platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
tinyusb_flags =
33
-D CFG_TUD_CDC=1
44
-D CFG_VENDOR_ADVANCED=1
5+
-D CFG_VENDOR_ADVANCED_NUM_INTF=2
56
-D CFG_TUSB_RHPORT1_MODE=OPT_MODE_NONE
7+
-D CFG_WINUSB=OPT_WINUSB_MSOS20
68
platform = ststm32
79
framework = cmsis
810
debug_tool = stlink

test-devices/composite-stm32/src/main.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ void cust_vendor_halt_cleared_cb(uint8_t ep_addr) {
157157

158158
// --- Control messages (see README)
159159

160+
#define REQUEST_SAVE_VALUE 0x01
161+
#define REQUEST_SAVE_DATA 0x02
162+
#define REQUEST_SEND_DATA 0x03
163+
#define REQUEST_RESET_BUFFERS 0x04
164+
#define REQUEST_GET_INTF_NUM 0x05
165+
160166
static uint32_t saved_value = 0;
161167

162168
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) {
@@ -168,35 +174,46 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
168174

169175
switch (request->bRequest) {
170176

171-
case 0x01:
177+
case REQUEST_SAVE_VALUE:
172178
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 0) {
173179
// save value from wValue
174180
saved_value = request->wValue;
175181
return tud_control_status(rhport, request);
176182
}
177183
break;
178184

179-
case 0x02:
185+
case REQUEST_SAVE_DATA:
180186
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 4) {
181187
// receive into `saved_value`
182188
return tud_control_xfer(rhport, request, &saved_value, 4);
183189
}
184190
break;
185191

186-
case 0x03:
192+
case REQUEST_SEND_DATA:
187193
if (request->bmRequestType_bit.direction == TUSB_DIR_IN && request->wLength == 4) {
188194
// transmit from `saved_value`
189195
return tud_control_xfer(rhport, request, &saved_value, 4);
190196
}
191197
break;
192198

193-
case 0x04:
199+
case REQUEST_RESET_BUFFERS:
194200
if (request->bmRequestType_bit.direction == TUSB_DIR_OUT && request->wLength == 0) {
195201
reset_buffers();
196202
return tud_control_status(rhport, request);
197203
}
198204
break;
199205

206+
case REQUEST_GET_INTF_NUM:
207+
if (request->bmRequestType_bit.direction == TUSB_DIR_IN && request->wLength == 1) {
208+
uint8_t intf_num = request->wIndex & 0xff;
209+
if (intf_num < 4) {
210+
// return inteface number
211+
return tud_control_xfer(rhport, request, &intf_num, 1);
212+
}
213+
}
214+
break;
215+
216+
#if CFG_WINUSB == OPT_WINUSB_MSOS20
200217
case MSOS_VENDOR_CODE:
201218
if (request->wIndex == 7) {
202219
// Get Microsoft OS 2.0 compatible descriptor
@@ -205,6 +222,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
205222
return tud_control_xfer(rhport, request, (uint8_t*) desc_ms_os_20, total_len);
206223
}
207224
break;
225+
#endif
208226

209227
default:
210228
break;

test-devices/composite-stm32/src/usb_descriptors.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,17 @@ uint8_t const* tud_descriptor_device_cb(void) {
4949

5050
// --- Configuration Descriptor ---
5151

52-
enum {
53-
INTF_CDC_COMM = 0,
54-
INTF_CDC_DATA,
55-
INTF_LOOPBACK,
56-
INTF_NUM_TOTAL
57-
};
58-
59-
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + 9 + 7 + 7)
52+
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + 8 + 9 + 9 + 7 + 7)
6053

6154
uint8_t const desc_configuration[] = {
6255
// Config number, interface count, string index, total length, attribute, power in mA
6356
TUD_CONFIG_DESCRIPTOR(1, INTF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 500),
6457
// CDC interfaces
6558
TUD_CDC_DESCRIPTOR(INTF_CDC_COMM, 0, EP_CDC_COMM, 8, EP_CDC_DATA_RX, EP_CDC_DATA_TX, BULK_MAX_PACKET_SIZE),
59+
// Interface association descriptor (IAD)
60+
CUSTOM_VENDOR_INTERFACE_ASSOCIATION(INTF_LOOPBACK_CTRL, 2, 0x04),
61+
// Echo interface (no endpoint, just control messages)
62+
CUSTOM_VENDOR_INTERFACE(INTF_LOOPBACK_CTRL, 0),
6663
// Loopback interface
6764
CUSTOM_VENDOR_INTERFACE(INTF_LOOPBACK, 2),
6865
// Loopback endpoint OUT
@@ -82,6 +79,8 @@ uint8_t const* tud_descriptor_configuration_cb(uint8_t configuration_index) {
8279

8380
// --- BOS Descriptor ---
8481

82+
#if CFG_WINUSB == OPT_WINUSB_MSOS20
83+
8584
#define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)
8685

8786
#define MS_OS_20_DESC_LEN 0xB2
@@ -108,7 +107,7 @@ uint8_t const desc_ms_os_20[] = {
108107
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_CONFIGURATION), 0, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A),
109108

110109
// Function Subset header: length, type, first interface, reserved, subset length
111-
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), INTF_LOOPBACK, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08),
110+
U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), INTF_LOOPBACK_CTRL, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08),
112111

113112
// MS OS 2.0 Compatible ID descriptor: length, type, compatible ID, sub compatible ID
114113
U16_TO_U8S_LE(0x0014), U16_TO_U8S_LE(MS_OS_20_FEATURE_COMPATBLE_ID), 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
@@ -129,6 +128,7 @@ uint8_t const desc_ms_os_20[] = {
129128

130129
TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size");
131130

131+
#endif
132132

133133

134134
// --- String Descriptors ---
@@ -138,7 +138,8 @@ const char* const string_table[] = {
138138
0, // 0 - supported languages (see below)
139139
"JavaDoesUSB", // 1 - manufacturer
140140
"Composite", // 2 - product
141-
board_serial_num // 3 - serial number
141+
board_serial_num, // 3 - serial number
142+
"Loopback IAD" // 4 - interface association descriptor
142143
};
143144

144145

test-devices/composite-stm32/src/usb_descriptors.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@
1313

1414
#include <stdint.h>
1515

16+
#define OPT_WINUSB_NONE 0
17+
#define OPT_WINUSB_MSOS20 2
18+
19+
#ifndef CFG_WINUSB
20+
#define CFG_WINUSB OPT_WINUSB_MSOS20
21+
#endif
22+
23+
24+
// interfaces
25+
enum {
26+
INTF_CDC_COMM = 0,
27+
INTF_CDC_DATA,
28+
INTF_LOOPBACK_CTRL,
29+
INTF_LOOPBACK,
30+
INTF_NUM_TOTAL
31+
};
32+
33+
1634
#define INTR_MAX_PACKET_SIZE 16
1735
#define BULK_MAX_PACKET_SIZE 64
1836

@@ -24,8 +42,11 @@
2442
#define EP_LOOPBACK_RX 0x01
2543
#define EP_LOOPBACK_TX 0x82
2644

27-
#define MSOS_VENDOR_CODE 0x44
45+
#if CFG_WINUSB == OPT_WINUSB_MSOS20
2846

47+
#define MSOS_VENDOR_CODE 0x44
2948
extern uint8_t const desc_ms_os_20[];
3049

50+
#endif
51+
3152
void usb_init_serial_num();

0 commit comments

Comments
 (0)