1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include "usbh_core.h"
7 #include "usbh_cdc_acm.h"
8 #include "usbh_hid.h"
9 #include "usbh_msc.h"
10 #include "usbh_video.h"
11 #include "usbh_audio.h"
12 
13 #ifndef CONFIG_TEST_USBH_CDC_ACM
14 #define CONFIG_TEST_USBH_CDC_ACM 1
15 #endif
16 #ifndef TEST_USBH_CDC_SPEED
17 #define TEST_USBH_CDC_SPEED 0
18 #endif
19 #ifndef CONFIG_TEST_USBH_HID
20 #define CONFIG_TEST_USBH_HID 1
21 #endif
22 #ifndef CONFIG_TEST_USBH_MSC
23 #define CONFIG_TEST_USBH_MSC 1
24 #endif
25 #ifndef TEST_USBH_MSC_FATFS
26 #define TEST_USBH_MSC_FATFS 0
27 #endif
28 #ifndef TEST_USBH_MSC_FATFS_SPEED
29 #define TEST_USBH_MSC_FATFS_SPEED 0
30 #endif
31 #ifndef CONFIG_TEST_USBH_AUDIO
32 #define CONFIG_TEST_USBH_AUDIO 0
33 #endif
34 #ifndef CONFIG_TEST_USBH_VIDEO
35 #define CONFIG_TEST_USBH_VIDEO 0
36 #endif
37 
38 #if defined(TEST_USBH_CDC_ECM) || defined(TEST_USBH_CDC_RNDIS) || defined(TEST_USBH_ASIX) || defined(TEST_USBH_RTL8152)
39 #error we have move those class implements into platform/none/usbh_lwip.c, and you should call tcpip_init(NULL, NULL) in your app
40 #endif
41 
42 #if CONFIG_TEST_USBH_CDC_ACM
43 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_buffer[4096];
44 
45 #if TEST_USBH_CDC_SPEED
46 #define TEST_LEN   (16 * 1024)
47 #define TEST_COUNT (10240)
48 
49 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_speed_buffer[TEST_LEN];
50 #endif
51 
usbh_cdc_acm_callback(void * arg,int nbytes)52 void usbh_cdc_acm_callback(void *arg, int nbytes)
53 {
54     //struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)arg;
55 
56     if (nbytes > 0) {
57         for (size_t i = 0; i < nbytes; i++) {
58             USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
59         }
60         USB_LOG_RAW("nbytes:%d\r\n", (unsigned int)nbytes);
61     }
62 }
63 
usbh_cdc_acm_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)64 static void usbh_cdc_acm_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
65 {
66     int ret;
67     struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
68 
69     /* test with only one buffer, if you have more cdc acm class, modify by yourself */
70 #if TEST_USBH_CDC_SPEED
71     const uint32_t test_len[] = { 512, 1 * 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024 };
72 
73     memset(cdc_speed_buffer, 0xAA, TEST_LEN);
74 
75     for (uint8_t j = 0; j < 6; j++) {
76         uint32_t start_time = (uint32_t)xTaskGetTickCount();
77         for (uint32_t i = 0; i < TEST_COUNT; i++) {
78             usbh_bulk_urb_fill(&cdc_acm_class->bulkout_urb, cdc_acm_class->hport, cdc_acm_class->bulkout, cdc_speed_buffer, test_len[j], 0XFFFFFFF, NULL, NULL);
79             ret = usbh_submit_urb(&cdc_acm_class->bulkout_urb);
80             if (ret < 0) {
81                 USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
82                 while (1) {
83                 }
84             } else {
85             }
86         }
87         uint32_t time_ms = xTaskGetTickCount() - start_time;
88         USB_LOG_RAW("per packet len:%d, out speed:%f MB/S\r\n", (unsigned int)test_len[j], (test_len[j] * TEST_COUNT / 1024 / 1024) * 1000 / ((float)time_ms));
89     }
90 #endif
91     memset(cdc_buffer, 0x55, 4096);
92 
93     /* for common, we use timeout with 0xffffffff, this is just a test */
94     usbh_bulk_urb_fill(&cdc_acm_class->bulkout_urb, cdc_acm_class->hport, cdc_acm_class->bulkout, cdc_buffer, sizeof(cdc_buffer), 3000, NULL, NULL);
95     ret = usbh_submit_urb(&cdc_acm_class->bulkout_urb);
96     if (ret < 0) {
97         USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
98         goto delete;
99     } else {
100         USB_LOG_RAW("send over:%d\r\n", (unsigned int)cdc_acm_class->bulkout_urb.actual_length);
101     }
102 
103     /* we can change cdc_acm_class->bulkin->wMaxPacketSize with 4096 for testing zlp, default is ep mps  */
104     usbh_bulk_urb_fill(&cdc_acm_class->bulkin_urb, cdc_acm_class->hport, cdc_acm_class->bulkin, cdc_buffer, cdc_acm_class->bulkin->wMaxPacketSize, 0xffffffff, usbh_cdc_acm_callback, cdc_acm_class);
105     ret = usbh_submit_urb(&cdc_acm_class->bulkin_urb);
106     if (ret < 0) {
107         USB_LOG_RAW("bulk in error,ret:%d\r\n", ret);
108         goto delete;
109     } else {
110     }
111     // clang-format off
112 delete:
113     usb_osal_thread_delete(NULL);
114     // clang-format on
115 }
116 #endif
117 
118 #if CONFIG_TEST_USBH_HID
119 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t hid_buffer[128];
120 
usbh_hid_callback(void * arg,int nbytes)121 void usbh_hid_callback(void *arg, int nbytes)
122 {
123     struct usbh_hid *hid_class = (struct usbh_hid *)arg;
124 
125     if (nbytes > 0) {
126         for (int i = 0; i < nbytes; i++) {
127             USB_LOG_RAW("0x%02x ", hid_buffer[i]);
128         }
129         USB_LOG_RAW("nbytes:%d\r\n", (unsigned int)nbytes);
130         usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
131         usbh_submit_urb(&hid_class->intin_urb);
132     } else if (nbytes == -USB_ERR_NAK) { /* only dwc2 should do this */
133         usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
134         usbh_submit_urb(&hid_class->intin_urb);
135     } else {
136     }
137 }
138 
usbh_hid_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)139 static void usbh_hid_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
140 {
141     int ret;
142     struct usbh_hid *hid_class = (struct usbh_hid *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
143     ;
144 
145     /* test with only one buffer, if you have more hid class, modify by yourself */
146 
147     /* Suggest you to use timer for int transfer and use ep interval */
148     usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
149     ret = usbh_submit_urb(&hid_class->intin_urb);
150     if (ret < 0) {
151         goto delete;
152     }
153     // clang-format off
154 delete:
155     usb_osal_thread_delete(NULL);
156     // clang-format on
157 }
158 #endif
159 
160 #if CONFIG_TEST_USBH_MSC
161 
162 #if TEST_USBH_MSC_FATFS
163 #include "ff.h"
164 
165 #if TEST_USBH_MSC_FATFS_SPEED
166 #define WRITE_SIZE_MB (128UL)
167 #define WRITE_SIZE (1024UL * 1024UL * WRITE_SIZE_MB)
168 #define BUF_SIZE (1024UL * 128UL)
169 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[BUF_SIZE];
170 #else
171 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[25 * 100];
172 #endif
173 
174 USB_NOCACHE_RAM_SECTION FATFS fs;
175 USB_NOCACHE_RAM_SECTION FIL fnew;
176 UINT fnum;
177 FRESULT res_sd = 0;
178 
usb_msc_fatfs_test()179 int usb_msc_fatfs_test()
180 {
181     const char *tmp_data = "cherryusb fatfs demo...\r\n";
182 
183     USB_LOG_RAW("data len:%d\r\n", (unsigned int)strlen(tmp_data));
184     for (uint32_t i = 0; i < 100; i++) {
185         memcpy(&read_write_buffer[i * 25], tmp_data, strlen(tmp_data));
186     }
187 
188     res_sd = f_mount(&fs, "2:", 1);
189     if (res_sd != FR_OK) {
190         USB_LOG_RAW("mount fail,res:%d\r\n", (unsigned int)res_sd);
191         return -1;
192     }
193 
194     USB_LOG_RAW("test fatfs write\r\n");
195     res_sd = f_open(&fnew, "2:test.txt", FA_CREATE_ALWAYS | FA_WRITE);
196     if (res_sd == FR_OK) {
197         res_sd = f_write(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum);
198         if (res_sd == FR_OK) {
199             USB_LOG_RAW("write success, write len:%d\n", fnum);
200         } else {
201             USB_LOG_RAW("write fail\r\n");
202             goto unmount;
203         }
204         f_close(&fnew);
205     } else {
206         USB_LOG_RAW("open fail\r\n");
207         goto unmount;
208     }
209     USB_LOG_RAW("test fatfs read\r\n");
210 
211     res_sd = f_open(&fnew, "2:test.txt", FA_OPEN_EXISTING | FA_READ);
212     if (res_sd == FR_OK) {
213         res_sd = f_read(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum);
214         if (res_sd == FR_OK) {
215             USB_LOG_RAW("read success, read len:%d\n", fnum);
216         } else {
217             USB_LOG_RAW("read fail\r\n");
218             goto unmount;
219         }
220         f_close(&fnew);
221     } else {
222         USB_LOG_RAW("open fail\r\n");
223         goto unmount;
224     }
225 
226 #if TEST_USBH_MSC_FATFS_SPEED
227     for (uint32_t i = 0; i < BUF_SIZE; i++) {
228         read_write_buffer[i] = i % 256;
229     }
230 
231     USB_LOG_RAW("test fatfs write speed\r\n");
232     res_sd = f_open(&fnew, "2:cherryusb_msc_test.bin", FA_OPEN_ALWAYS | FA_WRITE);
233     if (res_sd == FR_OK) {
234         uint32_t write_size = WRITE_SIZE;
235         uint32_t start_time = (uint32_t)xTaskGetTickCount();
236         while (write_size > 0) {
237             res_sd = f_write(&fnew, read_write_buffer, BUF_SIZE, (UINT*)&fnum);
238             if (res_sd != FR_OK) {
239                 printf("Write file failed, cause: %s\n", res_sd);
240                 goto unmount;
241             }
242             write_size -= BUF_SIZE;
243         }
244         if (res_sd == FR_OK) {
245             uint32_t time_ms = xTaskGetTickCount() - start_time;
246             USB_LOG_RAW("Fatfs write speed:%f MB/S\r\n", (WRITE_SIZE_MB * 1000 / (float)time_ms));
247         } else {
248             USB_LOG_RAW("write fail\r\n");
249             goto unmount;
250         }
251         f_close(&fnew);
252     } else {
253         USB_LOG_RAW("open fail\r\n");
254         goto unmount;
255     }
256     USB_LOG_RAW("test fatfs read speed\r\n");
257 
258     res_sd = f_open(&fnew, "2:cherryusb_msc_test.bin", FA_OPEN_EXISTING | FA_READ);
259     if (res_sd == FR_OK) {
260         uint32_t write_size = WRITE_SIZE;
261         uint32_t start_time = (uint32_t)xTaskGetTickCount();
262         while (write_size > 0) {
263             res_sd = f_read(&fnew, read_write_buffer, BUF_SIZE, (UINT*)&fnum);
264             if (res_sd != FR_OK) {
265                 printf("Read file failed, cause: %s\n", res_sd);
266                 goto unmount;
267             }
268             write_size -= BUF_SIZE;
269         }
270         if (res_sd == FR_OK) {
271             uint32_t time_ms = xTaskGetTickCount() - start_time;
272             USB_LOG_RAW("Fatfs read speed:%f MB/S\r\n", (WRITE_SIZE_MB * 1000 / (float)time_ms));
273         } else {
274             USB_LOG_RAW("read fail\r\n");
275             goto unmount;
276         }
277         f_close(&fnew);
278     } else {
279         USB_LOG_RAW("open fail\r\n");
280         goto unmount;
281     }
282 #endif
283     f_mount(NULL, "2:", 1);
284     return 0;
285 unmount:
286     f_mount(NULL, "2:", 1);
287     return -1;
288 }
289 #endif
290 
291 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t partition_table[512];
292 
usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)293 static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
294 {
295     int ret;
296     struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
297 
298     /* test with only one buffer, if you have more msc class, modify by yourself */
299 #if TEST_USBH_MSC_FATFS == 0
300     ret = usbh_msc_scsi_init(msc_class);
301     if (ret < 0) {
302         USB_LOG_RAW("scsi_init error,ret:%d\r\n", ret);
303         goto delete;
304     }
305     /* get the partition table */
306     ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
307     if (ret < 0) {
308         USB_LOG_RAW("scsi_read10 error,ret:%d\r\n", ret);
309         goto delete;
310     }
311     for (uint32_t i = 0; i < 512; i++) {
312         if (i % 16 == 0) {
313             USB_LOG_RAW("\r\n");
314         }
315         USB_LOG_RAW("%02x ", partition_table[i]);
316     }
317     USB_LOG_RAW("\r\n");
318 #else
319     usb_msc_fatfs_test();
320 #endif
321 
322     // clang-format off
323 delete:
324     usb_osal_thread_delete(NULL);
325     // clang-format on
326 }
327 #endif
328 
329 #if CONFIG_TEST_USBH_CDC_ACM
usbh_cdc_acm_run(struct usbh_cdc_acm * cdc_acm_class)330 void usbh_cdc_acm_run(struct usbh_cdc_acm *cdc_acm_class)
331 {
332     usb_osal_thread_create("usbh_cdc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_acm_thread, cdc_acm_class);
333 }
334 
usbh_cdc_acm_stop(struct usbh_cdc_acm * cdc_acm_class)335 void usbh_cdc_acm_stop(struct usbh_cdc_acm *cdc_acm_class)
336 {
337 }
338 #endif
339 
340 #if CONFIG_TEST_USBH_HID
usbh_hid_run(struct usbh_hid * hid_class)341 void usbh_hid_run(struct usbh_hid *hid_class)
342 {
343     usb_osal_thread_create("usbh_hid", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_hid_thread, hid_class);
344 }
345 
usbh_hid_stop(struct usbh_hid * hid_class)346 void usbh_hid_stop(struct usbh_hid *hid_class)
347 {
348 }
349 #endif
350 
351 #if CONFIG_TEST_USBH_MSC
usbh_msc_run(struct usbh_msc * msc_class)352 void usbh_msc_run(struct usbh_msc *msc_class)
353 {
354     usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, msc_class);
355 }
356 
usbh_msc_stop(struct usbh_msc * msc_class)357 void usbh_msc_stop(struct usbh_msc *msc_class)
358 {
359 }
360 #endif
361 
362 #if CONFIG_TEST_USBH_AUDIO
363 #error "commercial charge"
364 #endif
365 
366 #if CONFIG_TEST_USBH_VIDEO
367 #error "commercial charge"
368 #endif
369 
370 #if 0
371 #include "usbh_aoa.h"
372 
373 static struct aoa_string_info deviceinfo = {
374     .acc_manufacturer = "CherryUSB",
375     .acc_model = "CherryUSB",
376     .acc_description = "Android Open Accessory CherryUSB",
377     .acc_version = "1.0",
378     .acc_uri = "http://developer.android.com/tools/adk/index.html",
379     .acc_serial = "CherryUSB"
380 };
381 
382 int aoa_switch(int argc, char **argv)
383 {
384     struct usbh_hubport *hport = usbh_find_hubport(0, 1, 1);
385 
386     usbh_aoa_switch(hport, &deviceinfo);
387     return 0;
388 }
389 
390 SHELL_CMD_EXPORT_ALIAS(aoa_switch, aoa_switch, aoa_switch);
391 #endif