Skip to content

Commit e21df72

Browse files
fkjagodzinski0xc0170
authored andcommitted
Tests: USB: Add data toggle test
1 parent 69ac3f5 commit e21df72

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

TESTS/host_tests/pyusb_basic.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,19 @@ def _callback_ep_test_abort(self, key, value, timestamp):
290290
except (RuntimeError) as exc:
291291
self.report_error(exc)
292292

293+
def _callback_ep_test_data_toggle(self, key, value, timestamp):
294+
self.log("Received serial %s" % (value))
295+
296+
dev = self.find_device(value)
297+
if(dev == None):
298+
return
299+
300+
try:
301+
ep_test_data_toggle(dev, log=print)
302+
self.report_success()
303+
except (RuntimeError) as exc:
304+
self.report_error(exc)
305+
293306
def _callback_reset_support(self, key, value, timestamp):
294307
status = "false" if sys.platform == "darwin" else "true"
295308
self.log("Reset supported: %s" % status)
@@ -345,6 +358,7 @@ def setup(self):
345358
self.register_callback('ep_test_parallel_transfers', self._callback_ep_test_parallel_transfers)
346359
self.register_callback('ep_test_parallel_transfers_ctrl', self._callback_ep_test_parallel_transfers_ctrl)
347360
self.register_callback('ep_test_abort', self._callback_ep_test_abort)
361+
self.register_callback('ep_test_data_toggle', self._callback_ep_test_data_toggle)
348362

349363
self.register_callback('reset_support', self._callback_reset_support)
350364

@@ -1354,6 +1368,106 @@ def ep_test_abort(dev, log, verbose=False):
13541368
NUM_PACKETS_UNTIL_ABORT * ep_out.wMaxPacketSize, payload_size))
13551369

13561370

1371+
def ep_test_data_toggle(dev, log, verbose=False):
1372+
"""Test data toggle reset for bulk OUT/IN endpoint pairs.
1373+
1374+
Given a USB device
1375+
When an interface is set
1376+
Then the data toggle bits for all endpoints are reset to DATA0
1377+
When clear feature is called for an endpoint that *IS NOT* stalled
1378+
Then the data toggle is reset to DATA0 for that endpoint
1379+
When clear halt is called for an endpoint that *IS* stalled
1380+
Then the data toggle is reset to DATA0 for that endpoint
1381+
"""
1382+
cfg = dev.get_active_configuration()
1383+
for intf in cfg:
1384+
log('interface {}, alt {} -- '.format(intf.bInterfaceNumber, intf.bAlternateSetting), end='')
1385+
if intf.bAlternateSetting == 0:
1386+
log('skipping the default AlternateSetting')
1387+
continue
1388+
log('running tests')
1389+
1390+
if verbose:
1391+
log('Testing data toggle reset for bulk endpoint pair.')
1392+
1393+
# 1.1 reset OUT and IN data toggle to DATA0
1394+
intf.set_altsetting()
1395+
bulk_out, bulk_in = find_ep_pair(intf, usb.ENDPOINT_TYPE_BULK)
1396+
1397+
# 1.2 send and receive a single data packet,
1398+
# so both OUT and IN endpoints switch to DATA1
1399+
loopback_ep_test(bulk_out, bulk_in, bulk_out.wMaxPacketSize)
1400+
1401+
# 1.3 reset OUT and IN data toggle to DATA0
1402+
# USB spec, section 9.1.1.5
1403+
# "
1404+
# Configuring a device or changing an alternate setting causes all of the status and
1405+
# configuration values associated with endpoints in the affected interfaces to be set to their default values.
1406+
# This includes setting the data toggle of any endpoint using data toggles to the value DATA0.
1407+
# "
1408+
intf.set_altsetting()
1409+
bulk_out, bulk_in = find_ep_pair(intf, usb.ENDPOINT_TYPE_BULK)
1410+
1411+
# 1.4 verify that host and USB device are still in sync with respect to data toggle
1412+
try:
1413+
loopback_ep_test(bulk_out, bulk_in, bulk_out.wMaxPacketSize)
1414+
except usb.USBError as err:
1415+
if verbose:
1416+
log(USB_ERROR_FMT.format(err, bulk_out, bulk_in, bulk_out.wMaxPacketSize))
1417+
raise_unconditionally(lineno(), 'Data toggle not reset when setting interface.')
1418+
1419+
# 2.1 reset OUT and IN data toggle to DATA0
1420+
intf.set_altsetting()
1421+
bulk_out, bulk_in = find_ep_pair(intf, usb.ENDPOINT_TYPE_BULK)
1422+
1423+
# 2.2 send and receive a single data packet,
1424+
# so both OUT and IN endpoints switch to DATA1
1425+
loopback_ep_test(bulk_out, bulk_in, bulk_out.wMaxPacketSize)
1426+
1427+
# 2.3 reset OUT data toggle to DATA0
1428+
# USB spec, section 9.4.5
1429+
# "
1430+
# For endpoints using data toggle, regardless of whether an endpoint has the Halt feature set, a
1431+
# ClearFeature(ENDPOINT_HALT) request always results in the data toggle being reinitialized to DATA0.
1432+
# "
1433+
bulk_out.clear_halt()
1434+
# request_endpoint_read_start(dev, bulk_out)
1435+
1436+
# 2.4 verify that host and USB device are still in sync with respect to data toggle
1437+
try:
1438+
loopback_ep_test(bulk_out, bulk_in, bulk_out.wMaxPacketSize)
1439+
except usb.USBError as err:
1440+
if verbose:
1441+
log(USB_ERROR_FMT.format(err, bulk_out, bulk_in, bulk_out.wMaxPacketSize))
1442+
raise_unconditionally(lineno(), 'Data toggle not reset when calling ClearFeature(ENDPOINT_HALT) '
1443+
'on an endpoint that has not been halted.')
1444+
1445+
# 3.1 reset OUT and IN data toggle to DATA0
1446+
intf.set_altsetting()
1447+
bulk_out, bulk_in = find_ep_pair(intf, usb.ENDPOINT_TYPE_BULK)
1448+
1449+
# 3.2 send and receive a single data packet,
1450+
# so both OUT and IN endpoints switch to DATA1
1451+
loopback_ep_test(bulk_out, bulk_in, bulk_out.wMaxPacketSize)
1452+
1453+
# 3.3 reset IN data toggle to DATA0
1454+
# USB spec, section 9.4.5
1455+
# "
1456+
# For endpoints using data toggle, regardless of whether an endpoint has the Halt feature set, a
1457+
# ClearFeature(ENDPOINT_HALT) request always results in the data toggle being reinitialized to DATA0.
1458+
# "
1459+
usb.control.set_feature(dev, FEATURE_ENDPOINT_HALT, bulk_in)
1460+
bulk_in.clear_halt()
1461+
1462+
# 3.4 verify that host and USB device are still in sync with respect to data toggle
1463+
try:
1464+
loopback_ep_test(bulk_out, bulk_in, bulk_out.wMaxPacketSize)
1465+
except usb.USBError as err:
1466+
if verbose:
1467+
log(USB_ERROR_FMT.format(err, bulk_out, bulk_in, bulk_out.wMaxPacketSize))
1468+
raise_unconditionally(lineno(), 'Data toggle not reset when clearing endpoint halt.')
1469+
1470+
13571471
def device_reset_test(log):
13581472
"""
13591473
Test USB implementation against repeated reset

TESTS/usb_device/basic/main.cpp

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,45 @@ void ep_test_abort()
385385
}
386386
}
387387

388+
/** Test data toggle reset for bulk OUT/IN endpoint pairs
389+
*
390+
* Given a USB device
391+
* When an interface is set
392+
* Then the data toggle bits for all endpoints are reset to DATA0
393+
* When clear feature is called for an endpoint that *IS NOT* stalled
394+
* Then the data toggle is reset to DATA0 for that endpoint
395+
* When clear halt is called for an endpoint that *IS* stalled
396+
* Then the data toggle is reset to DATA0 for that endpoint
397+
*/
398+
void ep_test_data_toggle()
399+
{
400+
uint16_t vendor_id = 0x0d28;
401+
// Use a product ID different than that used in other tests,
402+
// to help Windows hosts use the correct configuration descriptor.
403+
uint16_t product_id = 0x0206;
404+
uint16_t product_release = 0x0001;
405+
char _key[11] = { };
406+
char _value[128] = { };
407+
408+
{
409+
USBEndpointTester serial(get_phy(), vendor_id, product_id, product_release, false);
410+
greentea_send_kv("ep_test_data_toggle", serial.get_serial_desc_string());
411+
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
412+
#if EP_DBG
413+
wait_ms(100);
414+
printf("cnt_cb_set_conf = %lu\r\n", serial.get_cnt_cb_set_conf());
415+
printf("cnt_cb_set_intf = %lu\r\n", serial.get_cnt_cb_set_intf());
416+
printf("cnt_cb_bulk_out = %lu\r\n", serial.get_cnt_cb_bulk_out());
417+
printf("cnt_cb_bulk_in = %lu\r\n", serial.get_cnt_cb_bulk_in());
418+
printf("cnt_cb_int_out = %lu\r\n", serial.get_cnt_cb_int_out());
419+
printf("cnt_cb_int_in = %lu\r\n", serial.get_cnt_cb_int_in());
420+
printf("cnt_cb_iso_out = %lu\r\n", serial.get_cnt_cb_iso_out());
421+
printf("cnt_cb_iso_in = %lu\r\n", serial.get_cnt_cb_iso_in());
422+
#endif
423+
TEST_ASSERT_EQUAL_STRING("pass", _key);
424+
}
425+
}
426+
388427
/** Test USB implementation against repeated reset
389428
390429
Given an initialized USB (HOST <---> DUT connection established)
@@ -602,7 +641,8 @@ Case cases[] = {
602641
Case("endpoint test halt", ep_test_halt),
603642
Case("endpoint test parallel transfers", ep_test_parallel_transfers),
604643
Case("endpoint test parallel transfers ctrl", ep_test_parallel_transfers_ctrl),
605-
Case("endpoint test abort", ep_test_abort)
644+
Case("endpoint test abort", ep_test_abort),
645+
Case("endpoint test data toggle reset", ep_test_data_toggle)
606646
};
607647

608648
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)

0 commit comments

Comments
 (0)