Skip to content

Commit

Permalink
Test device: fix ZLP handling
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Aug 18, 2024
1 parent 78aa0dd commit cc2ac04
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
29 changes: 13 additions & 16 deletions java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,11 @@ private static void resetDevice() {
resetBuffers();

// drain loopback data
while (true) {
try {
testDevice.transferIn(config.endpointLoopbackIn(), 1);
} catch (UsbTimeoutException e) {
break;
}
}
drainData(config.endpointLoopbackIn());

// drain interrupt data
if (isLoopbackDevice()) {
while (true) {
try {
testDevice.transferIn(config.endpointEchoIn(), 1);
} catch (UsbTimeoutException e) {
break;
}
}
}
if (isLoopbackDevice())
drainData(config.endpointEchoIn());

// reset buffers again
resetBuffers();
Expand All @@ -96,6 +83,16 @@ static void resetBuffers() {
(byte) 0x04, (short) 0, (short) config.interfaceNumber()), null);
}

static void drainData(int endpointNumber) {
while (true) {
try {
testDevice.transferIn(endpointNumber, 1);
} catch (UsbTimeoutException e) {
break;
}
}
}

static byte[] generateRandomBytes(int numBytes, long seed) {
var random = new Random(seed);
var bytes = new byte[numBytes];
Expand Down
13 changes: 2 additions & 11 deletions java-does-usb/src/test/java/net/codecrete/usb/TimeoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ void bulkTransfer_doesNotTimeOut() {
var data = generateRandomBytes(20, 7280277392L);
testDevice.transferOut(config.endpointLoopbackOut(), data);

var received = testDevice.transferIn(config.endpointLoopbackIn(), 200);
var received = testDevice.transferIn(config.endpointLoopbackIn(), 200);
assertArrayEquals(data, received);

}

@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
void bulkTransferOut_timesOut() {
var endpointOut = config.endpointLoopbackOut();
var endpointIn = config.endpointLoopbackIn();

// The test device has an internal buffer of about 2KB for full-speed
// and 16KB for high-speed. The first transfer should not time out.
Expand All @@ -59,14 +57,7 @@ void bulkTransferOut_timesOut() {
}
});

// drain data in loopback loop
while (true) {
try {
testDevice.transferIn(endpointIn, 200);
} catch (UsbTimeoutException e) {
break;
}
}
drainData(config.endpointLoopbackIn());
}

@Test
Expand Down
20 changes: 13 additions & 7 deletions test-devices/composite-stm32/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// FIFO buffer for loopback data
tu_fifo_t loopback_fifo;
uint8_t loopback_buffer[512];
bool delay_loopback_reset = false;

// RX buffer for loopback
uint8_t loopback_rx_buffer[64];
Expand Down Expand Up @@ -62,7 +63,11 @@ int main(void) {

// reset device in predictable state
void reset_buffers(void) {
tu_fifo_clear(&loopback_fifo);
if (cust_vendor_is_transmitting(EP_LOOPBACK_TX)) {
delay_loopback_reset = true;
} else {
tu_fifo_clear(&loopback_fifo);
}
}

// --- Loopback
Expand All @@ -74,6 +79,11 @@ void loopback_init(void) {
// Check if the next transmission should be started
void loopback_check_tx(void) {

if (delay_loopback_reset) {
tu_fifo_clear(&loopback_fifo);
delay_loopback_reset = false;
}

tu_fifo_buffer_info_t info;
tu_fifo_get_read_info(&loopback_fifo, &info);

Expand Down Expand Up @@ -123,19 +133,15 @@ void cust_vendor_rx_cb(uint8_t ep_addr, uint32_t recv_bytes) {

// Invoked when last tx transfer finished
void cust_vendor_tx_cb(uint8_t ep_addr, uint32_t sent_bytes) {
// If buffer has been reset in the mean time,
// we might not be able to advance it fully or at all.
int max_advance = tu_fifo_count(&loopback_fifo);
if (sent_bytes > max_advance)
sent_bytes = max_advance;
if (sent_bytes > 0)
tu_fifo_advance_read_pointer(&loopback_fifo, sent_bytes);

loopback_check_tx();
loopback_check_rx();

// check ZLP
if ((sent_bytes & (BULK_MAX_PACKET_SIZE - 1)) == 0
if (sent_bytes > 0
&& (sent_bytes & (BULK_MAX_PACKET_SIZE - 1)) == 0
&& !cust_vendor_is_transmitting(ep_addr))
cust_vendor_start_transmit(EP_LOOPBACK_TX, NULL, 0);

Expand Down
16 changes: 14 additions & 2 deletions test-devices/loopback-stm32/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// FIFO buffer for loopback data
tu_fifo_t loopback_fifo;
uint8_t loopback_buffer[BUFFER_SIZE] __attribute__ ((aligned(4)));
bool delay_loopback_reset = false;

uint16_t bulk_packet_size = 64;
const int num_rx_packets = 2;
Expand Down Expand Up @@ -74,7 +75,12 @@ int main(void) {

// reset device in predictable state
void reset_buffers(void) {
tu_fifo_clear(&loopback_fifo);
if (cust_vendor_is_transmitting(EP_LOOPBACK_TX)) {
delay_loopback_reset = true;
} else {
tu_fifo_clear(&loopback_fifo);
}

num_echos = 0;
}

Expand All @@ -87,6 +93,11 @@ void loopback_init(void) {
// Check if the next transmission should be started
void loopback_check_tx(void) {

if (delay_loopback_reset) {
tu_fifo_clear(&loopback_fifo);
delay_loopback_reset = false;
}

uint16_t n = tu_fifo_count(&loopback_fifo);

if (n > 0 && !cust_vendor_is_transmitting(EP_LOOPBACK_TX)) {
Expand Down Expand Up @@ -143,7 +154,8 @@ void cust_vendor_tx_cb(uint8_t ep_addr, uint32_t sent_bytes) {
loopback_check_rx();

// check ZLP
if ((sent_bytes & (bulk_packet_size - 1)) == 0
if (sent_bytes > 0
&& (sent_bytes & (bulk_packet_size - 1)) == 0
&& !cust_vendor_is_transmitting(ep_addr)) {
cust_vendor_start_transmit(EP_LOOPBACK_TX, NULL, 0);
led_busy();
Expand Down

0 comments on commit cc2ac04

Please sign in to comment.