Skip to content

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

Merged

Conversation

fkjagodzinski
Copy link
Member

@fkjagodzinski fkjagodzinski commented Aug 10, 2018

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

[ ] Fix
[ ] Refactor
[ ] Target update
[x] Feature
[ ] Breaking change

CC @c1728p9 @jamesbeyond

@cmonr
Copy link
Contributor

cmonr commented Aug 14, 2018

@c1728p9 @jamesbeyond Mind taking a look?

Copy link
Contributor

@c1728p9 c1728p9 left a 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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are retries needed?

Copy link
Member Author

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(','))
Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

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),
Copy link
Contributor

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.

Copy link
Member Author

@fkjagodzinski fkjagodzinski Sep 6, 2018

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.

@0xc0170
Copy link
Contributor

0xc0170 commented Aug 22, 2018

@fkjagodzinski Please respond to the latest review

@jamesbeyond
Copy link
Contributor

@0xc0170, Filip is on holiday, he will continue this PR when back to the office next week

@cmonr
Copy link
Contributor

cmonr commented Aug 27, 2018

Is @fkjagodzinski back?

@fkjagodzinski
Copy link
Member Author

@cmonr, yes I'm back and will update this PR soon.

@fkjagodzinski
Copy link
Member Author

@c1728p9, thanks for your feedback. 👍 These 2 new commits update the code according to your comments.

I'm now moving to CDC loopback failures.

@fkjagodzinski
Copy link
Member Author

The CDC loopback test case is fixed now. @c1728p9 could you take a look?

@0xc0170 0xc0170 dismissed c1728p9’s stale review September 25, 2018 11:39

Needs new review

while (!usb_serial.connected()) {
wait_ms(1);
}
wait_ms(TX_DELAY_MS);
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

{
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);
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

TEST_ASSERT_TRUE(usb_cdc.configured());
TEST_ASSERT_TRUE(usb_cdc.ready());

wait_ms(USB_DISCONNECT_DELAY_MS);
Copy link
Contributor

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.

Copy link
Member Author

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);
Copy link
Contributor

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.

Copy link
Member Author

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.

{
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);
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@c1728p9 c1728p9 force-pushed the feature-hal-spec-usb-device branch from 9e06f07 to 05d77ab Compare October 3, 2018 20:15
@cmonr
Copy link
Contributor

cmonr commented Oct 18, 2018

@c1728p9 @jamesbeyond All good?

@cmonr
Copy link
Contributor

cmonr commented Oct 25, 2018

@c1728p9 @jamesbeyond Ping

@fkjagodzinski
Copy link
Member Author

fkjagodzinski commented Oct 25, 2018

@c1728p9 thanks for your feedback.

Although the TX_DELAY_MS value should matter only for Linux hosts, when I removed this delay, I found another issue present on Windows hosts. I suspect this to be a host side issue and think this PR should proceed. I'll update this test suite in another PR if it is needed.

@c1728p9
Copy link
Contributor

c1728p9 commented Oct 26, 2018

Hi @fkjagodzinski I looked into the TX_DELAY_MS failures on Windows and found them to be a race-condition in pyserial. As part of pyserial's serial port open call it first opens the physical serial port and then does a flush of TX and RX buffers. Any data that is received between the open and flush is lost, causing tests like the loopback test to hang and timeout.

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 wait_ms(TX_DELAY_MS); and I don't see any failures even across multiple runs. I pushed my changes https://github.com/c1728p9/mbed-os/tree/increase_stability. Feel free to make use of this in this or a subsequent PR.

@fkjagodzinski
Copy link
Member Author

  • cherry-picked a few fixes from @c1728p9,
  • added a fix to handle the Linux hosts correctly (regarding the DTR line)


def port_open_wait(self):
"""Open the serial and wait until it's closed by the device."""
mbed_serial = serial.Serial(dsrdtr=False)
Copy link
Contributor

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?

Copy link
Member Author

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().

@c1728p9 c1728p9 force-pushed the feature-hal-spec-usb-device branch from 05d77ab to aa0cdea Compare November 2, 2018 23:30
@cmonr
Copy link
Contributor

cmonr commented Nov 9, 2018

/morph build

@mbed-ci
Copy link

mbed-ci commented Nov 9, 2018

Build : SUCCESS

Build number : 3588
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/7755/

Triggering tests

/morph test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Nov 9, 2018

@mbed-ci
Copy link

mbed-ci commented Nov 9, 2018

@fkjagodzinski
Copy link
Member Author

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

https://github.com/ARMmbed/austin-mbed-ci/blob/9d0b962742d574cacb1b7c2cc1a4364d8819a3c0/jobs/test_matrix_raas/config.xml#L344-L345

and replace tests-usb_device-basic with tests-usb_device-*, or should I create a PR to that repo?

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 9, 2018

Please send PR, and notify @studavekar

@studavekar
Copy link
Contributor

studavekar commented Nov 9, 2018

@c1728p9, @fkjagodzinski HW test CI config is updated to filter out new USB tests.

@fkjagodzinski
Copy link
Member Author

Great! Thanks @studavekar.

@cmonr
Copy link
Contributor

cmonr commented Nov 9, 2018

/morph test

@cmonr
Copy link
Contributor

cmonr commented Nov 9, 2018

Will restart in a bit. Figured we had a spurious network issue, when this is only waiting on a test.

@mbed-ci
Copy link

mbed-ci commented Nov 10, 2018

@cmonr
Copy link
Contributor

cmonr commented Nov 10, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Nov 10, 2018

@cmonr
Copy link
Contributor

cmonr commented Nov 10, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Nov 11, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 12, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Nov 12, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Nov 14, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Nov 14, 2018

@adbridge
Copy link
Contributor

/morph test

@mbed-ci
Copy link

mbed-ci commented Nov 14, 2018

@cmonr cmonr merged commit 7e866c4 into ARMmbed:feature-hal-spec-usb-device Nov 15, 2018
@cmonr cmonr removed the needs: CI label Nov 15, 2018
@fkjagodzinski fkjagodzinski deleted the test-usb-serial branch November 15, 2018 09:41
@c1728p9
Copy link
Contributor

c1728p9 commented Nov 27, 2018

@fkjagodzinski the test case Serial printf/scanf fails when IAR is used. Can you investigate and correct this failure?

@fkjagodzinski
Copy link
Member Author

@fkjagodzinski the test case Serial printf/scanf fails when IAR is used. Can you investigate and correct this failure?

This should be fixed with #9033.

@TomoYamanaka
Copy link
Contributor

@fkjagodzinski @c1728p9
Thank you so much. I confirmed that the above test case is success with IAR by adopting #9033 .

I'm preparing to submit a PR for Renesas' s USB Device feature, so I'll be glad if you could rebase feature-hal-spec-usb-device repository to merge #9033 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants