Skip to content

Commit 7cd7e60

Browse files
author
Jamie Smith
authored
Convert mbed-usb target, enable mbed-usb tests (ARMmbed#49)
* 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
1 parent f317dbc commit 7cd7e60

File tree

21 files changed

+157
-50
lines changed

21 files changed

+157
-50
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ add_subdirectory(connectivity)
257257

258258
# The directories below contain optional target libraries
259259
add_subdirectory(drivers/device_key EXCLUDE_FROM_ALL)
260-
add_subdirectory(drivers/usb EXCLUDE_FROM_ALL)
261260
add_subdirectory(features EXCLUDE_FROM_ALL)
262261
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
263262
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)

drivers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ target_sources(mbed-core-sources
5353
source/Watchdog.cpp
5454
)
5555

56+
add_subdirectory(usb)

drivers/usb/CMakeLists.txt

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
# Copyright (c) 2020 ARM Limited. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
add_library(mbed-usb INTERFACE)
4+
if(MBED_ENABLE_OS_INTERNAL_TESTS)
5+
if(MBED_BUILD_GREENTEA_TESTS)
6+
add_subdirectory(tests/TESTS)
7+
endif()
8+
endif()
59

6-
target_include_directories(mbed-usb
7-
INTERFACE
8-
include
9-
include/usb
10-
include/usb/internal
11-
)
12-
13-
target_sources(mbed-usb
14-
INTERFACE
10+
if("DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
11+
add_library(mbed-usb STATIC EXCLUDE_FROM_ALL
1512
source/AsyncOp.cpp
1613
source/ByteBuffer.cpp
1714
source/EndpointResolver.cpp
@@ -21,15 +18,43 @@ target_sources(mbed-usb
2118
source/TaskBase.cpp
2219
source/USBAudio.cpp
2320
source/USBCDC.cpp
24-
source/USBCDC_ECM.cpp
2521
source/USBDevice.cpp
2622
source/USBHID.cpp
2723
source/USBKeyboard.cpp
2824
source/USBMIDI.cpp
29-
source/USBMSD.cpp
3025
source/USBMouse.cpp
3126
source/USBMouseKeyboard.cpp
32-
source/USBSerial.cpp
33-
)
27+
source/USBSerial.cpp)
28+
29+
target_include_directories(mbed-usb
30+
PUBLIC
31+
include
32+
include/usb
33+
include/usb/internal
34+
)
35+
36+
target_link_libraries(mbed-usb PUBLIC mbed-core-flags)
37+
38+
# USB Mass Storage Device library is separate because it pulls in a dependency on mbed-storage-blockdevice
39+
add_library(mbed-usb-msd STATIC EXCLUDE_FROM_ALL
40+
source/msd/USBMSD.cpp)
41+
42+
target_include_directories(mbed-usb-msd
43+
PUBLIC
44+
include/usb/msd
45+
)
46+
47+
target_link_libraries(mbed-usb-msd PUBLIC mbed-usb mbed-storage-blockdevice)
48+
49+
# USB CDC ECM library is separate because it pulls in a dependency on mbed-rtos-flags
50+
add_library(mbed-usb-cdc-ecm STATIC EXCLUDE_FROM_ALL
51+
source/cdc_ecm/USBCDC_ECM.cpp)
52+
53+
54+
target_include_directories(mbed-usb-cdc-ecm
55+
PUBLIC
56+
include/usb/cdc_ecm
57+
)
3458

35-
target_link_libraries(mbed-usb INTERFACE mbed-storage)
59+
target_link_libraries(mbed-usb-cdc-ecm PUBLIC mbed-usb mbed-rtos-flags)
60+
endif()
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(usb_device)

drivers/usb/tests/TESTS/host_tests/usb_device_serial.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
import six
2828
import mbed_host_tests
2929

30+
# Pyserial 3.5 has a compatibility breaking capitalization change :((
31+
try:
32+
from serial import portNotOpenError as PortNotOpenError
33+
except ImportError:
34+
from serial import PortNotOpenError as PortNotOpenError
35+
3036

3137
MSG_KEY_DEVICE_READY = 'ready'
3238
MSG_KEY_SERIAL_NUMBER = 'usb_dev_sn'
@@ -130,7 +136,11 @@ def __init__(self):
130136

131137
def port_open_wait(self):
132138
"""Open the serial and wait until it's closed by the device."""
133-
mbed_serial = serial.Serial(dsrdtr=False)
139+
140+
# Note: Need to set dsrdtr on open to true to avoid exception on Linux
141+
# https://github.com/pyserial/pyserial/issues/67
142+
mbed_serial = serial.Serial(dsrdtr=True)
143+
134144
mbed_serial.dtr = False
135145
try:
136146
mbed_serial.port = retry_fun_call(
@@ -148,12 +158,12 @@ def port_open_wait(self):
148158
mbed_serial.dtr = True
149159
try:
150160
mbed_serial.read() # wait until closed
151-
except (serial.portNotOpenError, serial.SerialException):
161+
except (PortNotOpenError, serial.SerialException):
152162
pass
153163

154164
def port_open_close(self):
155165
"""Open the serial and close it with a delay."""
156-
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=False)
166+
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=True)
157167
mbed_serial.dtr = False
158168
try:
159169
mbed_serial.port = retry_fun_call(
@@ -179,7 +189,7 @@ def send_data_sequence(self, chunk_size=1):
179189
chunk_size defines the size of data sent in each write operation.
180190
The input buffer content is discarded.
181191
"""
182-
mbed_serial = serial.Serial(write_timeout=0.1, dsrdtr=False)
192+
mbed_serial = serial.Serial(write_timeout=0.1, dsrdtr=True)
183193
try:
184194
mbed_serial.port = retry_fun_call(
185195
fun=functools.partial(self.get_usb_serial_name, self.dut_usb_dev_sn), # pylint: disable=not-callable
@@ -214,7 +224,7 @@ def send_data_sequence(self, chunk_size=1):
214224

215225
def loopback(self):
216226
"""Open the serial and send back every byte received."""
217-
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=False)
227+
mbed_serial = serial.Serial(timeout=0.5, write_timeout=0.1, dsrdtr=True)
218228
mbed_serial.dtr = False
219229
try:
220230
mbed_serial.port = retry_fun_call(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_subdirectory(basic)
2+
add_subdirectory(hid)
3+
add_subdirectory(msd)
4+
add_subdirectory(serial)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2022 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
5+
set(TEST_SKIPPED "USB Device is not supported for this target")
6+
endif()
7+
8+
mbed_greentea_add_test(
9+
TEST_NAME
10+
mbed-usb-device-basic
11+
TEST_SOURCES
12+
main.cpp
13+
USBEndpointTester.cpp
14+
USBTester.cpp
15+
HOST_TESTS_DIR
16+
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
17+
TEST_SKIPPED
18+
${TEST_SKIPPED}
19+
TEST_REQUIRED_LIBS
20+
mbed-usb
21+
)

drivers/usb/tests/TESTS/usb_device/basic/USBEndpointTester.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if USB_DEVICE_TESTS
18+
#if DEVICE_USBDEVICE
1919

2020
#include "stdint.h"
2121
#include "stdlib.h"

drivers/usb/tests/TESTS/usb_device/basic/USBTester.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if USB_DEVICE_TESTS
18+
#if DEVICE_USBDEVICE
1919

2020
#include "stdint.h"
2121
#include "USBTester.h"

drivers/usb/tests/TESTS/usb_device/basic/main.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if !USB_DEVICE_TESTS
19-
#error [NOT_SUPPORTED] usb device tests not enabled
20-
#else
21-
2218
#include <stdio.h>
2319
#include <string.h>
2420
#include "mbed.h"
@@ -664,5 +660,4 @@ int main()
664660
Harness::run(specification);
665661
}
666662

667-
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
668-
#endif // !defined(USB_DEVICE_TESTS)
663+
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2022 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
5+
set(TEST_SKIPPED "USB Device is not supported for this target")
6+
endif()
7+
8+
mbed_greentea_add_test(
9+
TEST_NAME
10+
mbed-usb-device-hid
11+
TEST_SOURCES
12+
main.cpp
13+
HOST_TESTS_DIR
14+
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
15+
TEST_SKIPPED
16+
${TEST_SKIPPED}
17+
TEST_REQUIRED_LIBS
18+
mbed-usb
19+
)

drivers/usb/tests/TESTS/usb_device/hid/main.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if !USB_DEVICE_TESTS
19-
#error [NOT_SUPPORTED] usb device tests not enabled
20-
#else
21-
22-
#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
18+
#if !DEVICE_USBDEVICE
2319
#error [NOT_SUPPORTED] USB Device not supported for this target
2420
#else
2521

@@ -388,5 +384,4 @@ int main()
388384
return !Harness::run(specification);
389385
}
390386

391-
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
392-
#endif // !defined(USB_DEVICE_TESTS)
387+
#endif // !DEVICE_USBDEVICE
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2022 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
5+
set(TEST_SKIPPED "USB Device is not supported for this target")
6+
endif()
7+
8+
if(MBED_GREENTEA_TEST_BAREMETAL)
9+
set(TEST_SKIPPED "USB MSD test is not compatible with mbed-baremetal")
10+
endif()
11+
12+
mbed_greentea_add_test(
13+
TEST_NAME
14+
mbed-usb-device-msd
15+
TEST_SOURCES
16+
main.cpp
17+
HOST_TESTS_DIR
18+
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
19+
TEST_SKIPPED
20+
${TEST_SKIPPED}
21+
TEST_REQUIRED_LIBS
22+
mbed-usb-msd
23+
mbed-storage-fat
24+
)

drivers/usb/tests/TESTS/usb_device/msd/main.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if !USB_DEVICE_TESTS
19-
#error [NOT_SUPPORTED] usb device tests not enabled
20-
#else
21-
2218
#if !defined(MBED_CONF_RTOS_PRESENT)
2319
#error [NOT_SUPPORTED] USB stack and test cases require RTOS to run.
2420
#else
@@ -492,5 +488,4 @@ int main()
492488
}
493489

494490
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
495-
#endif // !defined(MBED_CONF_RTOS_PRESENT)
496-
#endif // !defined(USB_DEVICE_TESTS)
491+
#endif // !defined(MBED_CONF_RTOS_PRESENT)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2022 ARM Limited. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
if(NOT "DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS)
5+
set(TEST_SKIPPED "USB Device is not supported for this target")
6+
endif()
7+
8+
mbed_greentea_add_test(
9+
TEST_NAME
10+
mbed-usb-device-serial
11+
TEST_SOURCES
12+
main.cpp
13+
HOST_TESTS_DIR
14+
"${CMAKE_CURRENT_LIST_DIR}/../../host_tests"
15+
TEST_SKIPPED
16+
${TEST_SKIPPED}
17+
TEST_REQUIRED_LIBS
18+
mbed-usb
19+
)

drivers/usb/tests/TESTS/usb_device/serial/main.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if !USB_DEVICE_TESTS
19-
#error [NOT_SUPPORTED] usb device tests not enabled
20-
#else
21-
22-
#if !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
18+
#if !DEVICE_USBDEVICE
2319
#error [NOT_SUPPORTED] USB Device not supported for this target
2420
#else
2521

@@ -879,5 +875,4 @@ int main()
879875
return !Harness::run(specification);
880876
}
881877

882-
#endif // !defined(DEVICE_USBDEVICE) || !DEVICE_USBDEVICE
883-
#endif // !defined(USB_DEVICE_TESTS)
878+
#endif // !DEVICE_USBDEVICE

tools/requirements-ci-build.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
# It installs flashing support through the mbed tools packages and also the mbedhtrun test runner.
33
mbed-host-tests
44
mbed-greentea
5-
mbed-ls
5+
mbed-ls
6+
7+
# For USB Device host tests
8+
hidapi>=0.7.99
9+
pyusb>=1.2.0

0 commit comments

Comments
 (0)