Software APIs
usbdev_config_host_test.c
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 //
5 // USB CONFIG HOST test
6 //
7 // Test basic configuration of the USB device by the host/DPI.
8 //
9 // It requires interaction with the USB DPI model or a physical host in order
10 // to receive and respond to the Control Transfers that are involved in the
11 // identification and configuration of the device.
12 //
13 // The test configures USB Endpoint 1 as a simpleserial endpoint which should
14 // appear as /dev/ttyUSBx on a Linux-based host.
15 
20 #include "sw/device/lib/testing/pinmux_testutils.h"
21 #include "sw/device/lib/testing/test_framework/check.h"
23 #include "sw/device/lib/testing/usb_testutils.h"
24 #include "sw/device/lib/testing/usb_testutils_controlep.h"
25 
26 #include "hw/top_earlgrey/sw/autogen/top_earlgrey.h" // Generated.
27 
28 /**
29  * Configuration values for USB.
30  */
31 static const uint8_t config_descriptors[] = {
32  USB_CFG_DSCR_HEAD(
33  USB_CFG_DSCR_LEN + USB_INTERFACE_DSCR_LEN + 2 * USB_EP_DSCR_LEN, 1),
34  // Single interface...
35  VEND_INTERFACE_DSCR(0, 1, 0x50, 1),
36  // ... describing two Endpoints; endpoint 1 OUT and endpoint 1 IN
37  USB_BULK_EP_DSCR(0, 1, 32, 0),
38  USB_BULK_EP_DSCR(1, 1, 32, 4),
39 };
40 
41 /**
42  * USB device context types.
43  */
44 static usb_testutils_ctx_t usbdev;
45 static usb_testutils_controlep_ctx_t usbdev_control;
46 
47 /**
48  * Pinmux handle
49  */
50 static dif_pinmux_t pinmux;
51 
52 OTTF_DEFINE_TEST_CONFIG();
53 
54 bool test_main(void) {
55  // In simulation the DPI model connects VBUS shortly after reset and
56  // prolonged delays when asserting or deasserting pull ups are wasteful.
57  uint32_t timeout_micros = 6000u;
58  bool prompt = false;
59 
61  // FPGA platforms where user intervention may be required.
62  timeout_micros = 30 * 1000 * 1000u;
63  // Report instructions/progress to user, when driven manually.
64  prompt = true;
65  LOG_INFO("Running USBDEV_CONFIG_HOST test");
66  LOG_INFO("Awaiting configuration from the host");
67  }
68 
69  CHECK_DIF_OK(dif_pinmux_init(
71  pinmux_testutils_init(&pinmux);
72  CHECK_DIF_OK(dif_pinmux_input_select(
75 
76  // Call `usbdev_init` here so that DPI will not start until the
77  // simulation has finished all of the printing, which takes a while
78  // if `--trace` was passed in.
79  CHECK_STATUS_OK(usb_testutils_init(&usbdev, /*pinflip=*/false,
80  /*en_diff_rcvr=*/true,
81  /*tx_use_d_se0=*/false));
82  CHECK_STATUS_OK(usb_testutils_controlep_init(
83  &usbdev_control, &usbdev, 0, config_descriptors,
84  sizeof(config_descriptors), NULL, 0));
85 
86  // Proceed only when the device has been configured; this allows host-side
87  // software to establish communication.
88  ibex_timeout_t timeout = ibex_timeout_init(timeout_micros);
89  while (usbdev_control.device_state != kUsbTestutilsDeviceConfigured &&
90  !ibex_timeout_check(&timeout)) {
91  CHECK_STATUS_OK(usb_testutils_poll(&usbdev));
92  }
93 
94  bool success = (usbdev_control.device_state == kUsbTestutilsDeviceConfigured);
95  if (success && prompt) {
96  LOG_INFO("Configuration received");
97  }
98  CHECK(success);
99 
100  CHECK_STATUS_OK(usb_testutils_fin(&usbdev));
101 
102  return true;
103 }