-
Notifications
You must be signed in to change notification settings - Fork 3k
Add USB CDC and Serial tests #7755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add USB CDC and Serial tests #7755
Conversation
@c1728p9 @jamesbeyond Mind taking a look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fkjagodzinski were these tests passing for you? When I run tests-usb_device-serial
the test case CDC loopback
consistently fails.
"""Exception raised by retry_fun_call().""" | ||
|
||
|
||
def retry_fun_call(fun, num_retries=3, retry_delay=0.0): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are retries needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Operations like finding the serial port and opening it have to be retried due to variable delays on the host side. You can check how this works on your machine after setting verbose
in line https://github.com/fkjagodzinski/mbed-os/blob/082c63e16a11b14a25b9b327843d2e8b1d7db408/TESTS/host_tests/usb_device_serial.py#L55
Search is based on the id received from the device itself. | ||
Raises RuntimeError if the device is not found. | ||
""" | ||
vid, pid = (int(i, base=16) for i in str(usb_id_str).split(',')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the device serial number be used instead? The serial number can be made unique per device. These tests will likely be running in an environment with many boards connected. VID/PID may not be sufficient to determine the device you are connected to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll look into this. This should be rather straight forward to implement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed as requested -- using USB SN to identify the correct device on the host system.
Case("Serial USB reconnect", test_serial_usb_reconnect), | ||
Case("Serial terminal reopen", test_serial_term_reopen), | ||
Case("Serial getc", test_serial_getc), | ||
Case("Serial printf/scanf", test_serial_printf_scanf), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may also be good to test line_coding_changed
if you are testing USBCDC in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New test case added -- Serial line coding change
.
@fkjagodzinski Please respond to the latest review |
@0xc0170, Filip is on holiday, he will continue this PR when back to the office next week |
Is @fkjagodzinski back? |
@cmonr, yes I'm back and will update this PR soon. |
@c1728p9, thanks for your feedback. 👍 These 2 new commits update the code according to your comments. I'm now moving to |
The |
TESTS/usb_device/serial/main.cpp
Outdated
while (!usb_serial.connected()) { | ||
wait_ms(1); | ||
} | ||
wait_ms(TX_DELAY_MS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a delay here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first usb_serial.printf()
call from the for loop has to be delayed. More details can be found in a comment for TX_DELAY_MS
macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a problem with the host machine or the mbed board? If this is an mbed problem then it needs to be root caused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This behaviour is Linux specific, when tests run on Win host, I do not see characters echoed with ^
prefix.
TESTS/usb_device/serial/main.cpp
Outdated
{ | ||
USBCDC usb_cdc(false, USB_CDC_VID, USB_CDC_PID); | ||
char usb_cdc_sn[USB_SN_MAX_LEN] = { }; | ||
usb_desc2str(usb_cdc.string_iserial_desc(), usb_cdc_sn, USB_SN_MAX_LEN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will return the same serial number for all connected devices so the host won't be able to determine the right one to connect to. If testing is running concurrently on CI it may get these boards mixed up.
Instead, could the host test create and send a serial number to use in the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll look into this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. The USB device now uses a SN (UUID) received from host on test suite setup.
TESTS/usb_device/serial/main.cpp
Outdated
TEST_ASSERT_TRUE(usb_cdc.configured()); | ||
TEST_ASSERT_TRUE(usb_cdc.ready()); | ||
|
||
wait_ms(USB_DISCONNECT_DELAY_MS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is USB_DISCONNECT_DELAY_MS needed? usb_cdc.wait_ready()
should be enough to ensure that the host opened the serial port.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, no issue on the device side. Additional delay USB_DISCONNECT_DELAY_MS
helps Windows hosts handle this test case correctly. My Linux machine does not need it. I'll look into this and add comments for clarification.
TEST_ASSERT_FALSE(usb_cdc.configured()); | ||
TEST_ASSERT_FALSE(usb_cdc.ready()); | ||
|
||
wait_ms(USB_RECONNECT_DELAY_MS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this need to be 200ms? Only a 1ms delay should be needed between disconnect and reconned as explained in the basic test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! 1 ms delay works as expected.
TESTS/usb_device/serial/main.cpp
Outdated
{ | ||
USBCDC usb_cdc(false, USB_CDC_VID, USB_CDC_PID); | ||
char usb_cdc_sn[USB_SN_MAX_LEN] = { }; | ||
usb_desc2str(usb_cdc.string_iserial_desc(), usb_cdc_sn, USB_SN_MAX_LEN); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to send lots of data to the device as part of this test. Event if its more data then the device can hold at once, the built in flow control should ensure that nothing is dropped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
9e06f07
to
05d77ab
Compare
9d5082c
to
8533c0d
Compare
@c1728p9 @jamesbeyond All good? |
@c1728p9 @jamesbeyond Ping |
@c1728p9 thanks for your feedback. Although the |
Hi @fkjagodzinski I looked into the As a fix for this DTR (data terminal ready) can be set to False before opening the serial port. This prevents the device from sending any data. Once the port is opened and ready DTR can be re-enabled to allow testing like normal. With this change I don't need any of the calls to |
|
|
||
def port_open_wait(self): | ||
"""Open the serial and wait until it's closed by the device.""" | ||
mbed_serial = serial.Serial(dsrdtr=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is dsrdtr
set to false here and below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just setting the default dsrdtr
value explicitly. It has to stay False
to allow setting the DTR
line manually, i.e. setting DTR
True
after the call to open()
.
05d77ab
to
aa0cdea
Compare
/morph build |
Build : SUCCESSBuild number : 3588 Triggering tests/morph test |
Exporter Build : SUCCESSBuild number : 3198 |
Test : FAILUREBuild number : 3364 |
The USB device tests require an additional USB cable connected to the host. These tests should not run on the CI until additional cables are provided. @cmonr, @0xc0170, @studavekar, could you update the CI config and replace |
Please send PR, and notify @studavekar |
@c1728p9, @fkjagodzinski HW test CI config is updated to filter out new USB tests. |
Great! Thanks @studavekar. |
/morph test |
Will restart in a bit. Figured we had a spurious network issue, when this is only waiting on a test. |
Test : FAILUREBuild number : 3371 |
/morph test |
Test : FAILUREBuild number : 3372 |
/morph test |
Test : FAILUREBuild number : 3373 |
/morph test |
Test : FAILUREBuild number : 3376 |
/morph test |
Test : FAILUREBuild number : 3397 |
/morph test |
Test : SUCCESSBuild number : 3404 |
@fkjagodzinski the test case |
This should be fixed with #9033. |
@fkjagodzinski @c1728p9 I'm preparing to submit a PR for Renesas' s USB Device feature, so I'll be glad if you could rebase |
Description
Add
tests-usb_device-serial
with a few test cases:CDC RX multiple bytes
,CDC RX multiple bytes concurrent
,CDC RX single bytes
,CDC RX single bytes concurrent
,CDC USB reconnect
,CDC loopback
,Serial USB reconnect
,Serial getc
,Serial printf/scanf
,Serial line coding change
,Serial terminal reopen
.Pull request type
CC @c1728p9 @jamesbeyond