Skip to content

Commit 08a47ef

Browse files
committed
Fine-tune tests
1 parent 9a6bc21 commit 08a47ef

File tree

8 files changed

+61
-25
lines changed

8 files changed

+61
-25
lines changed

java-does-usb/src/main/java/net/codecrete/usb/USBDevice.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ public interface USBDevice {
216216
* <p>
217217
* This method can send data to bulk and interrupt endpoints.
218218
* </p>
219+
* <p>
220+
* If the sent data length is a multiple of the packet size, it is often
221+
* required to send an additional zero-length packet (ZLP) for the device
222+
* to actually process the data. This method will not do it automatically.
223+
* </p>
219224
*
220225
* @param endpointNumber endpoint number (in the range between 1 and 127)
221226
* @param data data to send
@@ -231,6 +236,11 @@ public interface USBDevice {
231236
* <p>
232237
* This method can send data to bulk and interrupt endpoints.
233238
* </p>
239+
* <p>
240+
* If the sent data length is a multiple of the packet size, it is often
241+
* required to send an additional zero-length packet (ZLP) for the device
242+
* to actually process the data. This method will not do it automatically.
243+
* </p>
234244
*
235245
* @param endpointNumber the endpoint number (in the range between 1 and 127)
236246
* @param timeout the timeout period, in milliseconds (0 for no timeout)

java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ protected EndpointInputStream(USBDeviceImpl device, int endpointNumber, int buff
6464
arena = Arena.openShared();
6565

6666
int packetSize = device.getEndpoint(USBDirection.IN, endpointNumber).packetSize();
67-
int n = (int) Math.round(Math.sqrt((double) bufferSize / packetSize));
68-
n = Math.min(Math.max(n, 4), 32); // 32 limits packet size to 16KB (for USB HS)
69-
transferSize = n * packetSize;
70-
int maxOutstandingTransfers = Math.max((bufferSize + transferSize / 2) / transferSize, 2);
67+
68+
// use between 4 and 32 packets per transfer (256B to 2KB for FS, 2KB to 16KB for HS)
69+
int numPacketsPerTransfer = (int) Math.round(Math.sqrt((double) bufferSize / packetSize));
70+
numPacketsPerTransfer = Math.min(Math.max(numPacketsPerTransfer, 4), 32);
71+
transferSize = numPacketsPerTransfer * packetSize;
72+
73+
// use at least 2 outstanding transfers (3 in total)
74+
int maxOutstandingTransfers = Math.max((bufferSize + transferSize / 2) / transferSize, 3);
7175

7276
configureEndpoint();
7377

java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ protected EndpointOutputStream(USBDeviceImpl device, int endpointNumber, int buf
6767
arena = Arena.openShared();
6868

6969
packetSize = device.getEndpoint(USBDirection.OUT, endpointNumber).packetSize();
70-
int n = (int) Math.round(Math.sqrt((double) bufferSize / packetSize));
71-
n = Math.min(Math.max(n, 4), 32); // 32 limits packet size to 16KB (for USB HS)
72-
transferSize = n * packetSize;
73-
int maxOutstandingTransfers = Math.max((bufferSize + transferSize / 2) / transferSize, 2);
70+
71+
// use between 4 and 32 packets per transfer (256B to 2KB for FS, 2KB to 16KB for HS)
72+
int numPacketsPerTransfer = (int) Math.round(Math.sqrt((double) bufferSize / packetSize));
73+
numPacketsPerTransfer = Math.min(Math.max(numPacketsPerTransfer, 4), 32);
74+
transferSize = numPacketsPerTransfer * packetSize;
75+
76+
// use at least 2 outstanding transfers (3 in total)
77+
int maxOutstandingTransfers = Math.max((bufferSize + transferSize / 2) / transferSize, 3);
7478

7579
configureEndpoint();
7680

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void transferWithZLP_succeeds() {
4242
var inEndpoint = testDevice.getEndpoint(USBDirection.IN, LOOPBACK_EP_IN);
4343
byte[] sampleData = generateRandomBytes(inEndpoint.packetSize(), 97333894);
4444
testDevice.transferOut(LOOPBACK_EP_OUT, sampleData);
45+
testDevice.transferOut(LOOPBACK_EP_OUT, new byte[0]);
4546
byte[] data = testDevice.transferIn(LOOPBACK_EP_IN);
4647
assertArrayEquals(sampleData, data);
4748
data = testDevice.transferIn(LOOPBACK_EP_IN);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ void bulkTransfer_doesNotTimeOut() {
4040
@Test
4141
@Timeout(value = 1, unit = TimeUnit.SECONDS)
4242
void bulkTransferOut_timesOut() {
43-
// The test device has an internal buffer of about 500 bytes.
44-
// So the first transfer should not time out.
43+
// The test device has an internal buffer of about 2KB for full-speed
44+
// and 16KB for high-speed. The first transfer should not time-out.
45+
final int bufferSize = 32 * testDevice
46+
.getEndpoint(USBDirection.OUT, LOOPBACK_EP_OUT).packetSize();
4547

4648
byte[] data = generateRandomBytes(100, 9383073929L);
4749
testDevice.transferOut(LOOPBACK_EP_OUT, data, 200);
4850

4951
assertThrows(USBTimeoutException.class, () -> {
50-
for (int i = 0; i < 50; i++) {
52+
for (int i = 0; i < bufferSize / data.length; i++) {
5153
testDevice.transferOut(LOOPBACK_EP_OUT, data, 200);
5254
}
5355
});

test-devices/loopback-stm32/lib/tinyusb/dwc2/dcd_dwc2.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ static void edpt_schedule_packets(uint8_t rhport, uint8_t const epnum, uint8_t c
222222
{
223223
(void) rhport;
224224

225+
TU_ASSERT(epnum < DWC2_EP_MAX, );
226+
225227
dwc2_regs_t * dwc2 = DWC2_REG(rhport);
226228

227229
// EP0 is limited to one packet each xfer
@@ -743,6 +745,9 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
743745
uint8_t const epnum = tu_edpt_number(ep_addr);
744746
uint8_t const dir = tu_edpt_dir(ep_addr);
745747

748+
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
749+
TU_ASSERT(epnum < ep_count);
750+
746751
xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
747752
xfer->buffer = buffer;
748753
xfer->ff = NULL;
@@ -783,6 +788,9 @@ bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16
783788
uint8_t const epnum = tu_edpt_number(ep_addr);
784789
uint8_t const dir = tu_edpt_dir(ep_addr);
785790

791+
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
792+
TU_ASSERT(epnum < ep_count);
793+
786794
xfer_ctl_t * xfer = XFER_CTL_BASE(epnum, dir);
787795
xfer->buffer = NULL;
788796
xfer->ff = ff;
@@ -1024,6 +1032,9 @@ static void handle_rxflvl_irq(uint8_t rhport)
10241032

10251033
case GRXSTS_PKTSTS_OUTRX:
10261034
{
1035+
uint8_t const ep_count = _dwc2_controller[rhport].ep_count;
1036+
TU_ASSERT(epnum < ep_count, );
1037+
10271038
// Out packet received
10281039
xfer_ctl_t *xfer = XFER_CTL_BASE(epnum, TUSB_DIR_OUT);
10291040

@@ -1288,7 +1299,7 @@ void dcd_int_handler(uint8_t rhport)
12881299

12891300
if(int_status & GINTSTS_SOF)
12901301
{
1291-
dwc2->gotgint = GINTSTS_SOF;
1302+
dwc2->gintsts = GINTSTS_SOF;
12921303

12931304
if (_sof_en)
12941305
{

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@
1818
#include "usb_descriptors.h"
1919
#include "vendor_custom.h"
2020

21+
#if BOARD_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED
22+
#define BUFFER_SIZE 16384
23+
#else
24+
#define BUFFER_SIZE 2048
25+
#endif
26+
2127
// FIFO buffer for loopback data
2228
tu_fifo_t loopback_fifo;
23-
uint8_t loopback_buffer[2048] __attribute__ ((aligned(4)));
29+
uint8_t loopback_buffer[BUFFER_SIZE] __attribute__ ((aligned(4)));
2430

2531
uint16_t bulk_packet_size = 64;
32+
const int num_rx_packets = 2;
33+
const int num_tx_packets = 4;
2634

2735
// buffer for echoed packet
2836
uint8_t echo_buffer[16];
@@ -81,7 +89,7 @@ void loopback_check_tx(void) {
8189
uint16_t n = tu_fifo_count(&loopback_fifo);
8290

8391
if (n > 0 && !cust_vendor_is_transmitting(EP_LOOPBACK_TX)) {
84-
uint16_t max_size = 2 * bulk_packet_size;
92+
uint16_t max_size = num_tx_packets * bulk_packet_size;
8593
if (n > max_size)
8694
n = max_size;
8795

@@ -93,8 +101,8 @@ void loopback_check_tx(void) {
93101
void loopback_check_rx(void) {
94102

95103
uint16_t n = tu_fifo_remaining(&loopback_fifo);
96-
if (n >= bulk_packet_size && !cust_vendor_is_receiving(EP_LOOPBACK_RX))
97-
cust_vendor_prepare_recv_fifo(EP_LOOPBACK_RX, &loopback_fifo, bulk_packet_size);
104+
if (n >= num_rx_packets * bulk_packet_size && !cust_vendor_is_receiving(EP_LOOPBACK_RX))
105+
cust_vendor_prepare_recv_fifo(EP_LOOPBACK_RX, &loopback_fifo, num_rx_packets * bulk_packet_size);
98106
}
99107

100108

test-devices/loopback-stm32/src/vendor_custom.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void cv_reset(uint8_t rhport) {
5858
// nothing to do
5959
}
6060

61-
// Open interface is the descriptor matches this class
61+
// Open interface if the descriptor matches this class
6262
uint16_t cv_open(uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len) {
6363

6464
cv_num_eps_open = 0;
@@ -185,17 +185,15 @@ void cust_vendor_prepare_recv(uint8_t ep_addr, void* buf, uint32_t buf_len) {
185185

186186
uint8_t const rhport = BOARD_TUD_RHPORT;
187187

188-
if (usbd_edpt_busy(rhport, ep_addr))
189-
return;
188+
TU_ASSERT(!usbd_edpt_busy(rhport, ep_addr), );
190189

191190
usbd_edpt_xfer(rhport, ep_addr, buf, buf_len);
192191
}
193192

194193
void cust_vendor_prepare_recv_fifo(uint8_t ep_addr, tu_fifo_t * fifo, uint32_t buf_len) {
195194
uint8_t const rhport = BOARD_TUD_RHPORT;
196195

197-
if (usbd_edpt_busy(rhport, ep_addr))
198-
return;
196+
TU_ASSERT(!usbd_edpt_busy(rhport, ep_addr), );
199197

200198
usbd_edpt_xfer_fifo(rhport, ep_addr, fifo, buf_len);
201199
}
@@ -205,8 +203,7 @@ void cust_vendor_start_transmit(uint8_t ep_addr, void const * data, uint32_t dat
205203

206204
uint8_t const rhport = BOARD_TUD_RHPORT;
207205

208-
if (usbd_edpt_busy(rhport, ep_addr))
209-
return;
206+
TU_ASSERT(!usbd_edpt_busy(rhport, ep_addr), );
210207

211208
usbd_edpt_xfer(rhport, ep_addr, (void*) data, data_len);
212209
}
@@ -215,8 +212,7 @@ void cust_vendor_start_transmit_fifo(uint8_t ep_addr, tu_fifo_t * fifo, uint32_t
215212

216213
uint8_t const rhport = BOARD_TUD_RHPORT;
217214

218-
if (usbd_edpt_busy(rhport, ep_addr))
219-
return;
215+
TU_ASSERT(!usbd_edpt_busy(rhport, ep_addr), );
220216

221217
usbd_edpt_xfer_fifo(rhport, ep_addr, (void*) fifo, data_len);
222218
}

0 commit comments

Comments
 (0)