/
main.c
206 lines (171 loc) · 5.52 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <ztest.h>
#include <tc_util.h>
#include <misc/byteorder.h>
#include <usb/usb_device.h>
#include <usb/usb_common.h>
/* Max packet size for endpoints */
#define BULK_EP_MPS 64
#define ENDP_BULK_IN 0x81
#define VALID_EP ENDP_BULK_IN
#define INVALID_EP 0x20
struct usb_device_desc {
struct usb_if_descriptor if0;
struct usb_ep_descriptor if0_in_ep;
} __packed;
#define INITIALIZER_IF \
{ \
.bLength = sizeof(struct usb_if_descriptor), \
.bDescriptorType = USB_INTERFACE_DESC, \
.bInterfaceNumber = 0, \
.bAlternateSetting = 0, \
.bNumEndpoints = 1, \
.bInterfaceClass = CUSTOM_CLASS, \
.bInterfaceSubClass = 0, \
.bInterfaceProtocol = 0, \
.iInterface = 0, \
}
#define INITIALIZER_IF_EP(addr, attr, mps, interval) \
{ \
.bLength = sizeof(struct usb_ep_descriptor), \
.bDescriptorType = USB_ENDPOINT_DESC, \
.bEndpointAddress = addr, \
.bmAttributes = attr, \
.wMaxPacketSize = sys_cpu_to_le16(mps), \
.bInterval = interval, \
}
USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_device_desc dev_desc = {
.if0 = INITIALIZER_IF,
.if0_in_ep = INITIALIZER_IF_EP(ENDP_BULK_IN, USB_DC_EP_BULK,
BULK_EP_MPS, 0),
};
static void status_cb(struct usb_cfg_data *cfg,
enum usb_dc_status_code status,
const u8_t *param)
{
ARG_UNUSED(cfg);
ARG_UNUSED(status);
ARG_UNUSED(param);
}
/* EP Bulk IN handler, used to send data to the Host */
static void bulk_in(u8_t ep, enum usb_dc_ep_cb_status_code ep_status)
{
}
/* Describe EndPoints configuration */
static struct usb_ep_cfg_data device_ep[] = {
{
.ep_cb = bulk_in,
.ep_addr = ENDP_BULK_IN
},
};
USBD_CFG_DATA_DEFINE(primary, device) struct usb_cfg_data device_config = {
.usb_device_description = NULL,
.interface_descriptor = &dev_desc.if0,
.cb_usb_status = status_cb,
.interface = {
.vendor_handler = NULL,
.class_handler = NULL,
.custom_handler = NULL,
},
.num_endpoints = ARRAY_SIZE(device_ep),
.endpoint = device_ep,
};
static void test_usb_disable(void)
{
zassert_equal(usb_disable(), TC_PASS, "usb_disable() failed");
}
static void test_usb_deconfig(void)
{
zassert_equal(usb_deconfig(), TC_PASS, "usb_deconfig() failed");
}
/* Test USB Device Cotnroller API */
static void test_usb_dc_api(void)
{
/* Control endpoins are configured */
zassert_equal(usb_dc_ep_mps(0x0), 64,
"usb_dc_ep_mps(0x00) failed");
zassert_equal(usb_dc_ep_mps(0x80), 64,
"usb_dc_ep_mps(0x80) failed");
/* Bulk EP is not configured yet */
zassert_equal(usb_dc_ep_mps(ENDP_BULK_IN), 0,
"usb_dc_ep_mps(ENDP_BULK_IN) not configured");
zassert_equal(usb_dc_set_address(0x01), TC_PASS,
"usb_dc_set_address(0x01)");
}
/* Test USB Device Cotnroller API for invalid parameters */
static void test_usb_dc_api_invalid(void)
{
size_t size;
u8_t byte;
/* Set stall to invalid EP */
zassert_not_equal(usb_dc_ep_set_stall(INVALID_EP), TC_PASS,
"usb_dc_ep_set_stall(INVALID_EP)");
/* Clear stall to invalid EP */
zassert_not_equal(usb_dc_ep_clear_stall(INVALID_EP), TC_PASS,
"usb_dc_ep_clear_stall(INVALID_EP)");
/* Check if the selected endpoint is stalled */
zassert_not_equal(usb_dc_ep_is_stalled(INVALID_EP, &byte), TC_PASS,
"usb_dc_ep_is_stalled(INVALID_EP, stalled)");
zassert_not_equal(usb_dc_ep_is_stalled(VALID_EP, NULL), TC_PASS,
"usb_dc_ep_is_stalled(VALID_EP, NULL)");
/* Halt invalid EP */
zassert_not_equal(usb_dc_ep_halt(INVALID_EP), TC_PASS,
"usb_dc_ep_halt(INVALID_EP)");
/* Enable invalid EP */
zassert_not_equal(usb_dc_ep_enable(INVALID_EP), TC_PASS,
"usb_dc_ep_enable(INVALID_EP)");
/* Disable invalid EP */
zassert_not_equal(usb_dc_ep_disable(INVALID_EP), TC_PASS,
"usb_dc_ep_disable(INVALID_EP)");
/* Flush invalid EP */
zassert_not_equal(usb_dc_ep_flush(INVALID_EP), TC_PASS,
"usb_dc_ep_flush(INVALID_EP)");
/* Set callback to invalid EP */
zassert_not_equal(usb_dc_ep_set_callback(INVALID_EP, NULL), TC_PASS,
"usb_dc_ep_set_callback(INVALID_EP, NULL)");
/* Write to invalid EP */
zassert_not_equal(usb_dc_ep_write(INVALID_EP, &byte, sizeof(byte),
&size),
TC_PASS, "usb_dc_ep_write(INVALID_EP)");
/* Read invalid EP */
zassert_not_equal(usb_dc_ep_read(INVALID_EP, &byte, sizeof(byte),
&size),
TC_PASS, "usb_dc_ep_read(INVALID_EP)");
zassert_not_equal(usb_dc_ep_read_wait(INVALID_EP, &byte, sizeof(byte),
&size),
TC_PASS, "usb_dc_ep_read_wait(INVALID_EP)");
zassert_not_equal(usb_dc_ep_read_continue(INVALID_EP), TC_PASS,
"usb_dc_ep_read_continue(INVALID_EP)");
/* Get endpoint max packet size for invalid EP */
zassert_not_equal(usb_dc_ep_mps(INVALID_EP), TC_PASS,
"usb_dc_ep_mps(INVALID_EP)");
}
static void test_usb_dc_api_read_write(void)
{
size_t size;
u8_t byte;
/* Read invalid EP */
zassert_not_equal(usb_read(INVALID_EP, &byte, sizeof(byte), &size),
TC_PASS, "usb_read(INVALID_EP)");
/* Write to invalid EP */
zassert_not_equal(usb_write(INVALID_EP, &byte, sizeof(byte), &size),
TC_PASS, "usb_write(INVALID_EP)");
}
/*test case main entry*/
void test_main(void)
{
ztest_test_suite(test_device,
/* Test API for not USB attached state */
ztest_unit_test(test_usb_dc_api_invalid),
ztest_unit_test(test_usb_dc_api),
ztest_unit_test(test_usb_dc_api_read_write),
ztest_unit_test(test_usb_dc_api_invalid),
ztest_unit_test(test_usb_deconfig),
ztest_unit_test(test_usb_disable));
ztest_run_test_suite(test_device);
}