1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "freertos/FreeRTOS.h"
8 #include "freertos/semphr.h"
9 #include "hcd.h"
10 #include "usb_private.h"
11 #include "usb/usb_types_ch9.h"
12 
13 #define URB_CONTEXT_VAL ((void *)0xDEADBEEF)
14 
15 // ------------------------------------------------- HCD Event Test ----------------------------------------------------
16 
17 /**
18  * @brief Expect (wait) for an HCD port event
19  *
20  * @param port_hdl Port handle to expect event from
21  * @param expected_event Port event to expect
22  */
23 void test_hcd_expect_port_event(hcd_port_handle_t port_hdl, hcd_port_event_t expected_event);
24 
25 /**
26  * @brief Expect (wait) for an HCD pipe event
27  *
28  * @param pipe_hdl Pipe handle to expect event from
29  * @param expected_event Pipe event to expect
30  */
31 void test_hcd_expect_pipe_event(hcd_pipe_handle_t pipe_hdl, hcd_pipe_event_t expected_event);
32 
33 /**
34  * @brief Get the current number of queued port events (dequeued using test_hcd_expect_port_event())
35  *
36  * @param port_hdl Port handle
37  * @return int Number of port events currently queued
38  */
39 int test_hcd_get_num_port_events(hcd_port_handle_t port_hdl);
40 
41 /**
42  * @brief Get the current number of queued pipe events (dequeued using test_hcd_expect_pipe_event())
43  *
44  * @param pipe_hdl Pipe handle
45  * @return int Number of pipe events currently queued
46  */
47 int test_hcd_get_num_pipe_events(hcd_pipe_handle_t pipe_hdl);
48 
49 // ----------------------------------------------- Driver/Port Related -------------------------------------------------
50 
51 /**
52  * @brief Sets up the HCD and initializes an HCD port.
53  *
54  * @return hcd_port_handle_t Port handle
55  */
56 hcd_port_handle_t test_hcd_setup(void);
57 
58 /**
59  * @brief Frees and HCD port and uninstalls the HCD
60  *
61  * @param port_hdl Port handle
62  */
63 void test_hcd_teardown(hcd_port_handle_t port_hdl);
64 
65 /**
66  * @brief Wait for a connection on an HCD port
67  *
68  * @note This function will internally call test_usb_set_phy_state() to allow for a connection
69  *
70  * @param port_hdl Port handle
71  * @return usb_speed_t Speed of the connected device
72  */
73 usb_speed_t test_hcd_wait_for_conn(hcd_port_handle_t port_hdl);
74 
75 /**
76  * @brief Wait for a disconnection on an HCD port
77  *
78  * @note This fucntion will internally call test_usb_set_phy_state() to force a disconnection
79  *
80  * @param port_hdl Port handle
81  * @param already_disabled Whether the HCD port is already in the disabled state
82  */
83 void test_hcd_wait_for_disconn(hcd_port_handle_t port_hdl, bool already_disabled);
84 
85 // ------------------------------------------------- Pipe alloc/free ---------------------------------------------------
86 
87 /**
88  * @brief Test the allocation of a pipe
89  *
90  * @param port_hdl Port handle
91  * @param ep_desc Endpoint descriptor
92  * @param dev_addr Device address of the pipe
93  * @param dev_speed Device speed of the pipe
94  * @return hcd_pipe_handle_t Pipe handle
95  */
96 hcd_pipe_handle_t test_hcd_pipe_alloc(hcd_port_handle_t port_hdl, const usb_ep_desc_t *ep_desc, uint8_t dev_addr, usb_speed_t dev_speed);
97 
98 /**
99  * @brief Test the freeing of a pipe
100  *
101  * @param pipe_hdl Pipe handle
102  */
103 void test_hcd_pipe_free(hcd_pipe_handle_t pipe_hdl);
104 
105 /**
106  * @brief Allocate a URB
107  *
108  * @param num_isoc_packets Number of isochronous packets
109  * @param data_buffer_size Size of the data buffer of the URB
110  * @return urb_t* URB
111  */
112 urb_t *test_hcd_alloc_urb(int num_isoc_packets, size_t data_buffer_size);
113 
114 /**
115  * @brief Free a URB
116  *
117  * @param urb URB
118  */
119 void test_hcd_free_urb(urb_t *urb);
120 
121 // --------------------------------------------------- Enumeration -----------------------------------------------------
122 
123 /**
124  * @brief Do some basic enumeration of the device
125  *
126  * For tests that need a device to have been enumerated (such as bulk tests). This function will enumerate that device
127  * using the device's default pipe. The minimal enumeration will include
128  *
129  * - Getting the device's descriptor and updating the default pipe's MPS
130  * - Setting the device's address and updating the default pipe to use that address
131  * - Setting the device to configuration 1 (i.e., the first configuration found
132  *
133  * @param default_pipe The connected device's default pipe
134  * @return uint8_t The address of the device after enumeration
135  */
136 uint8_t test_hcd_enum_device(hcd_pipe_handle_t default_pipe);
137