Skip to content

Commit

Permalink
Convert mbed-usb target, enable mbed-usb tests (ARMmbed#49)
Browse files Browse the repository at this point in the history
* Convert mbed-usb target, enable mbed-usb tests

* Fix ByteBuffer compile error

* Add missing requirements, fix some pyserial issues

* Move CDC_ECM to its own target since it needs RTOS
  • Loading branch information
multiplemonomials authored Sep 22, 2022
1 parent f317dbc commit 7cd7e60
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 50 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ add_subdirectory(connectivity)

# The directories below contain optional target libraries
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
add_subdirectory(drivers/usb EXCLUDE_FROM_ALL)
add_subdirectory(features EXCLUDE_FROM_ALL)
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)
Expand Down
1 change: 1 addition & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ target_sources(mbed-core-sources
source/Watchdog.cpp
)

add_subdirectory(usb)
55 changes: 40 additions & 15 deletions drivers/usb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(mbed-usb INTERFACE)
if(MBED_ENABLE_OS_INTERNAL_TESTS)
if(MBED_BUILD_GREENTEA_TESTS)
add_subdirectory(tests/TESTS)
endif()
endif()

target_include_directories(mbed-usb
INTERFACE
include
include/usb
include/usb/internal
)

target_sources(mbed-usb
INTERFACE
if("DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
add_library(mbed-usb STATIC EXCLUDE_FROM_ALL
source/AsyncOp.cpp
source/ByteBuffer.cpp
source/EndpointResolver.cpp
Expand All @@ -21,15 +18,43 @@ target_sources(mbed-usb
source/TaskBase.cpp
source/USBAudio.cpp
source/USBCDC.cpp
source/USBCDC_ECM.cpp
source/USBDevice.cpp
source/USBHID.cpp
source/USBKeyboard.cpp
source/USBMIDI.cpp
source/USBMSD.cpp
source/USBMouse.cpp
source/USBMouseKeyboard.cpp
source/USBSerial.cpp
)
source/USBSerial.cpp)

target_include_directories(mbed-usb
PUBLIC
include
include/usb
include/usb/internal
)

target_link_libraries(mbed-usb PUBLIC mbed-core-flags)

# USB Mass Storage Device library is separate because it pulls in a dependency on mbed-storage-blockdevice
add_library(mbed-usb-msd STATIC EXCLUDE_FROM_ALL
source/msd/USBMSD.cpp)

target_include_directories(mbed-usb-msd
PUBLIC
include/usb/msd
)

target_link_libraries(mbed-usb-msd PUBLIC mbed-usb mbed-storage-blockdevice)

# USB CDC ECM library is separate because it pulls in a dependency on mbed-rtos-flags
add_library(mbed-usb-cdc-ecm STATIC EXCLUDE_FROM_ALL
source/cdc_ecm/USBCDC_ECM.cpp)


target_include_directories(mbed-usb-cdc-ecm
PUBLIC
include/usb/cdc_ecm
)

target_link_libraries(mbed-usb INTERFACE mbed-storage)
target_link_libraries(mbed-usb-cdc-ecm PUBLIC mbed-usb mbed-rtos-flags)
endif()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions drivers/usb/tests/TESTS/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(usb_device)
20 changes: 15 additions & 5 deletions drivers/usb/tests/TESTS/host_tests/usb_device_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
import six
import mbed_host_tests

# Pyserial 3.5 has a compatibility breaking capitalization change :((
try:
from serial import portNotOpenError as PortNotOpenError
except ImportError:
from serial import PortNotOpenError as PortNotOpenError


MSG_KEY_DEVICE_READY = 'ready'
MSG_KEY_SERIAL_NUMBER = 'usb_dev_sn'
Expand Down Expand Up @@ -130,7 +136,11 @@ def __init__(self):

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

# Note: Need to set dsrdtr on open to true to avoid exception on Linux
# https://github.com/pyserial/pyserial/issues/67
mbed_serial = serial.Serial(dsrdtr=True)

mbed_serial.dtr = False
try:
mbed_serial.port = retry_fun_call(
Expand All @@ -148,12 +158,12 @@ def port_open_wait(self):
mbed_serial.dtr = True
try:
mbed_serial.read() # wait until closed
except (serial.portNotOpenError, serial.SerialException):
except (PortNotOpenError, serial.SerialException):
pass

def port_open_close(self):
"""Open the serial and close it with a delay."""
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=False)
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=True)
mbed_serial.dtr = False
try:
mbed_serial.port = retry_fun_call(
Expand All @@ -179,7 +189,7 @@ def send_data_sequence(self, chunk_size=1):
chunk_size defines the size of data sent in each write operation.
The input buffer content is discarded.
"""
mbed_serial = serial.Serial(write_timeout=0.1, dsrdtr=False)
mbed_serial = serial.Serial(write_timeout=0.1, dsrdtr=True)
try:
mbed_serial.port = retry_fun_call(
fun=functools.partial(self.get_usb_serial_name, self.dut_usb_dev_sn), # pylint: disable=not-callable
Expand Down Expand Up @@ -214,7 +224,7 @@ def send_data_sequence(self, chunk_size=1):

def loopback(self):
"""Open the serial and send back every byte received."""
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=False)
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=True)
mbed_serial.dtr = False
try:
mbed_serial.port = retry_fun_call(
Expand Down
4 changes: 4 additions & 0 deletions drivers/usb/tests/TESTS/usb_device/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_subdirectory(basic)
add_subdirectory(hid)
add_subdirectory(msd)
add_subdirectory(serial)
21 changes: 21 additions & 0 deletions drivers/usb/tests/TESTS/usb_device/basic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2022 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
set(TEST_SKIPPED "USB Device is not supported for this target")
endif()

mbed_greentea_add_test(
TEST_NAME
mbed-usb-device-basic
TEST_SOURCES
main.cpp
USBEndpointTester.cpp
USBTester.cpp
HOST_TESTS_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
TEST_SKIPPED
${TEST_SKIPPED}
TEST_REQUIRED_LIBS
mbed-usb
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

#if USB_DEVICE_TESTS
#if DEVICE_USBDEVICE

#include "stdint.h"
#include "stdlib.h"
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/tests/TESTS/usb_device/basic/USBTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

#if USB_DEVICE_TESTS
#if DEVICE_USBDEVICE

#include "stdint.h"
#include "USBTester.h"
Expand Down
7 changes: 1 addition & 6 deletions drivers/usb/tests/TESTS/usb_device/basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
* limitations under the License.
*/

#if !USB_DEVICE_TESTS
#error [NOT_SUPPORTED] usb device tests not enabled
#else

#include <stdio.h>
#include <string.h>
#include "mbed.h"
Expand Down Expand Up @@ -664,5 +660,4 @@ int main()
Harness::run(specification);
}

#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
#endif // !defined(USB_DEVICE_TESTS)
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
19 changes: 19 additions & 0 deletions drivers/usb/tests/TESTS/usb_device/hid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2022 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
set(TEST_SKIPPED "USB Device is not supported for this target")
endif()

mbed_greentea_add_test(
TEST_NAME
mbed-usb-device-hid
TEST_SOURCES
main.cpp
HOST_TESTS_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
TEST_SKIPPED
${TEST_SKIPPED}
TEST_REQUIRED_LIBS
mbed-usb
)
9 changes: 2 additions & 7 deletions drivers/usb/tests/TESTS/usb_device/hid/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
* limitations under the License.
*/

#if !USB_DEVICE_TESTS
#error [NOT_SUPPORTED] usb device tests not enabled
#else

#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
#if !DEVICE_USBDEVICE
#error [NOT_SUPPORTED] USB Device not supported for this target
#else

Expand Down Expand Up @@ -388,5 +384,4 @@ int main()
return !Harness::run(specification);
}

#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
#endif // !defined(USB_DEVICE_TESTS)
#endif // !DEVICE_USBDEVICE
24 changes: 24 additions & 0 deletions drivers/usb/tests/TESTS/usb_device/msd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2022 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
set(TEST_SKIPPED "USB Device is not supported for this target")
endif()

if(MBED_GREENTEA_TEST_BAREMETAL)
set(TEST_SKIPPED "USB MSD test is not compatible with mbed-baremetal")
endif()

mbed_greentea_add_test(
TEST_NAME
mbed-usb-device-msd
TEST_SOURCES
main.cpp
HOST_TESTS_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
TEST_SKIPPED
${TEST_SKIPPED}
TEST_REQUIRED_LIBS
mbed-usb-msd
mbed-storage-fat
)
7 changes: 1 addition & 6 deletions drivers/usb/tests/TESTS/usb_device/msd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
* limitations under the License.
*/

#if !USB_DEVICE_TESTS
#error [NOT_SUPPORTED] usb device tests not enabled
#else

#if !defined(MBED_CONF_RTOS_PRESENT)
#error [NOT_SUPPORTED] USB stack and test cases require RTOS to run.
#else
Expand Down Expand Up @@ -492,5 +488,4 @@ int main()
}

#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
#endif // !defined(MBED_CONF_RTOS_PRESENT)
#endif // !defined(USB_DEVICE_TESTS)
#endif // !defined(MBED_CONF_RTOS_PRESENT)
19 changes: 19 additions & 0 deletions drivers/usb/tests/TESTS/usb_device/serial/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2022 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
set(TEST_SKIPPED "USB Device is not supported for this target")
endif()

mbed_greentea_add_test(
TEST_NAME
mbed-usb-device-serial
TEST_SOURCES
main.cpp
HOST_TESTS_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
TEST_SKIPPED
${TEST_SKIPPED}
TEST_REQUIRED_LIBS
mbed-usb
)
9 changes: 2 additions & 7 deletions drivers/usb/tests/TESTS/usb_device/serial/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
* limitations under the License.
*/

#if !USB_DEVICE_TESTS
#error [NOT_SUPPORTED] usb device tests not enabled
#else

#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
#if !DEVICE_USBDEVICE
#error [NOT_SUPPORTED] USB Device not supported for this target
#else

Expand Down Expand Up @@ -879,5 +875,4 @@ int main()
return !Harness::run(specification);
}

#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
#endif // !defined(USB_DEVICE_TESTS)
#endif // !DEVICE_USBDEVICE
6 changes: 5 additions & 1 deletion tools/requirements-ci-build.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
# It installs flashing support through the mbed tools packages and also the mbedhtrun test runner.
mbed-host-tests
mbed-greentea
mbed-ls
mbed-ls

# For USB Device host tests
hidapi>=0.7.99
pyusb>=1.2.0

0 comments on commit 7cd7e60

Please sign in to comment.