summaryrefslogtreecommitdiff
path: root/bsp/hc32/tests/test_cherryusb.c
blob: 568b0697b537d56aa9915ff6f9a25d93d6e7d3ee (plain)
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
/*
 * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-08-08     CDT          first version
 * 2026-05-27     CDT          Support HC32F4A2
 * 2026-06-03     CDT          Support HC32F467
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* 请关注并阅读bsp/hc32/ev_hc32xxxx/README.md中,关于USB使用的相关注意事项
*/

#if defined(RT_CHERRYUSB_HOST) && defined(RT_CHERRYUSB_DEVICE)
#if defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F4A8) || defined(HC32F467)
#define TEST_USBH_CORE_BASE (CM_USBFS_BASE)
#define TEST_USBD_CORE_BASE (CM_USBHS_BASE)
#else
#error "Only one USB role can be selected"
#endif
#else
#if defined(RT_CHERRYUSB_HOST)
#define TEST_USBH_CORE_BASE (CM_USBFS_BASE)
#elif defined(RT_CHERRYUSB_DEVICE)
#define TEST_USBD_CORE_BASE (CM_USBFS_BASE)
#endif
#endif

#if defined(RT_CHERRYUSB_HOST)
#include "usbh_core.h"
#if defined(RT_CHERRYUSB_HOST_CDC_ECM) || defined(RT_CHERRYUSB_HOST_CDC_RNDIS) || defined(RT_CHERRYUSB_HOST_MSC)
/*  使用USB Host 时,应确保主机对设备供电充足

    ************************* menuconfig: ECM 关键配置 *************************
    RT-Thread Kernel --->[*] Enable soft timer with a timer thread
                            (4096) The stack size of timer thread

    RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB
                                                  [*] Enable usb host mode
                                                      Selectot usb host ip.... --->
                                                          [*]dwc2_hc
                                                      [*] Enable usb cdc ecm driver

    RT-Thread Components--->Network---> lwIP--->lwIP version ---> v2.1.2
                                                ...
                                                [*] Enable alloc ip address through DHCP
                                                ...
                                                (4096) the stack size of lwIP thread
                                                [*] Not use Rx thread
                                                [*] Not use Tx thread
                                                ...
                                                [*] Enable ping features

    备注:CherryUSB Host枚举设备时,默认选择Configuration 1,若指定设备(如CH397A模组CDC-ECM模式)需要选择Configuration 2,重新定义
         uint8_t usbh_get_hport_active_config_index(struct usbh_hubport *hport)。


    ************************* menuconfig: MSC 关键配置 *************************
    RT-Thread Kernel --->[*] Enable soft timer with a timer thread
                            (4096) The stack size of timer thread

    RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB
                                                  [*] Enable usb host mode
                                                      Selectot usb host ip.... --->
                                                          [*]dwc2_hc
                                                      [*] Enable usb msc driver
                                                      ...
                                                      (/)usb host dfs mount point
*/


/*  ECM 测试
msh />ipconfig
    network interface: u0 (Default)
    MTU: 1500
    MAC: 4e 61 e9 06 9b ff
    FLAGS: UP LINK_UP ETHARP BROADCAST IGMP
    ip address: 192.168.225.29
    gw address: 192.168.225.1
    net mask  : 255.255.255.0
    dns server #0: 108.7.254.31
    dns server #1: 112.7.254.31
msh />ping www.baidu.com
    60 bytes from 183.2.172.177 icmp_seq=0 ttl=51 time=149 ms
    60 bytes from 183.2.172.177 icmp_seq=1 ttl=51 time=69 ms
    60 bytes from 183.2.172.177 icmp_seq=2 ttl=51 time=51 ms
    60 bytes from 183.2.172.177 icmp_seq=3 ttl=51 time=52 ms
*/
static int cherryusb_host_init(void)
{
    usbh_initialize(0, TEST_USBH_CORE_BASE, RT_NULL);
    return 0;
}
INIT_APP_EXPORT(cherryusb_host_init);

#if defined(RT_CHERRYUSB_HOST_CDC_ECM) || defined(RT_CHERRYUSB_HOST_CDC_RNDIS)
void ipconfig(void)
{
    extern void list_if(void);
    list_if();
}
MSH_CMD_EXPORT(ipconfig, list network interface information);

uint8_t usbh_get_hport_active_config_index(struct usbh_hubport *hport)
{
    uint8_t config_index = 0U;   /* Default to configuration index 0 */

    if ((0x1A86U == hport->device_desc.idVendor) && (0x5397U == hport->device_desc.idProduct))
    {
        config_index = 1U;      /* For CH397, we need to select configuration 2 */
    }

    return config_index;
}
#endif

#endif
#endif

#if defined(RT_CHERRYUSB_DEVICE)
#if defined(RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM)
/*
    ************************* menuconfig: ACM 关键配置 *************************
    RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB
                                                  [*] Enable usb device mode
                                                      Selectot usb device ip.... --->
                                                         [*]dwc2_hc
                                                      [*] Enable usb cdc acm device
                                                          Select usb device template...--->
                                                             [*] cdc acm
*/

static int cherryusb_device_cdc_acm_init(void)
{
    extern void cdc_acm_init(uint8_t busid, uint32_t reg_base);
    cdc_acm_init(0, TEST_USBD_CORE_BASE);
    return 0;
}
INIT_APP_EXPORT(cherryusb_device_cdc_acm_init);

static void cherryusb_cdc_send(int argc, char **argv)
{
    extern void cdc_acm_data_send_with_dtr_test(uint8_t busid);
    cdc_acm_data_send_with_dtr_test(0);
}
MSH_CMD_EXPORT(cherryusb_cdc_send, cdc acm data send with dtr test);

#endif
#endif