Skip to content

Commit

Permalink
change(teardown): Corrected timeouts with Ubuntu 22.04
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-jam committed Dec 19, 2024
1 parent 66a5a42 commit 7b722b5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static uint8_t tx_buf[CONFIG_TINYUSB_CDC_TX_BUFSIZE + 1] = { 0 };

#define TEARDOWN_CMD_KEY 0xAA
#define TEARDOWN_RPL_KEY 0x55
#define TEARDOWN_CMD_RPL_SIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64) - 1)
#define TEARDOWN_ATTACH_TIMEOUT_MS 2000
#define TEARDOWN_CMD_RPL_SIZE ((TUD_OPT_HIGH_SPEED ? 512 : 64))
#define TEARDOWN_ATTACH_TIMEOUT_MS 5000
#define TEARDOWN_COMMAND_TIMEOUT_MS 5000
#define TEARDOWN_AMOUNT 10

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@pytest.mark.esp32p4
@pytest.mark.usb_device

def find_tusb_cdc(vid, pid):
def get_tusb_cdc_device(vid, pid):
ports = comports()
for cdc in ports:
if cdc.vid == vid and cdc.pid == pid:
Expand All @@ -23,10 +23,10 @@ def find_tusb_cdc(vid, pid):
def wait_for_tusb_cdc(vid, pid, timeout=30):
start_time = time()
while time() - start_time < timeout:
tusb_cdc = find_tusb_cdc(vid, pid)
sleep(0.5) # Check every 0.5 seconds
tusb_cdc = get_tusb_cdc_device(vid, pid)
if tusb_cdc:
return tusb_cdc
sleep(0.5) # Check every 0.5 seconds
return None

def teardown_device(key_len, amount):
Expand Down Expand Up @@ -57,16 +57,17 @@ def teardown_device(key_len, amount):
# Send the key command
res = cdc.write(COMMAND)
assert res == key_len
# Wait for the response
# Get the response
res = cdc.readline()
assert len(res) == key_len
print(f"Sent {len(COMMAND)}: {COMMAND.hex().upper()}")
print(f"Received {len(res)}: {res.hex().upper()}")

# Explicitly close the cdc
cdc.close()
# Check if the response matches the expected response
if res == EXPECTED_RESPONSE:
print("Response matches expected value.")
else:
print(f"Sent {len(COMMAND)}: {COMMAND.hex().upper()}")
print(f"Received {len(res)}: {res.hex().upper()}")
raise Exception("Error: Response does not match expected value.")

except serial.SerialException as e:
Expand All @@ -75,8 +76,8 @@ def teardown_device(key_len, amount):

# Wait for the device to disconnect
print("Waiting for the device to disconnect...")
while find_tusb_cdc(TUSB_VID, TUSB_PID):
sleep(0.5) # Check every 0.5 seconds
while get_tusb_cdc_device(TUSB_VID, TUSB_PID):
sleep(0.1) # Poll every 0.1 second while tinyusb cdc dev still in list

print("Finished all iterations.")

Expand All @@ -89,6 +90,7 @@ def test_usb_teardown_device(dut) -> None:
MPS = 512
else:
MPS = 64
MPS -= 1 # On Linux, the serial port kept opened, while len==MPS, https://github.com/pyserial/pyserial/issues/753
# On Linux, the serial port kept opened, while len==MPS, https://github.com/pyserial/pyserial/issues/753
# that can be seen via Beagle: CDC OUT transaction appears only when terminal is closed.
teardown_device(MPS, 10) # Teardown tusb device 10 times

0 comments on commit 7b722b5

Please sign in to comment.