Skip to content

Commit c2b7ec9

Browse files
committed
docs: update net rst
Signed-off-by: sakumisu <[email protected]>
1 parent 7a2089f commit c2b7ec9

File tree

1 file changed

+66
-29
lines changed

1 file changed

+66
-29
lines changed

docs/source/demo/usbh_net.rst

Lines changed: 66 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ usbh_net
33

44
本节主要介绍 USB 网卡的使用,USB 网卡推荐采用 AIR780(RNDIS),EC20(ECM/RNDIS), 手机(RNDIS),RTL8152 USB 网卡,AX88772 USB 网卡。
55

6-
USB 网卡传输层面已经对接好了 LWIP 的收发接口,因此,用户只需要包含 **platform/XXX/usbh_lwip.c** 并根据需要开启对应的网卡类的宏即可
6+
.. note:: 看完下面内容,你将感受到什么是全自动化的 USB 网卡驱动对接,无需手动调用任何函数
77

8-
- 当前支持以下网卡类:
8+
USB 网卡相关的宏和文件
9+
------------------------
10+
11+
网卡相关的宏如下,主要用于根据不同的网络组件注册网卡驱动:
912

1013
.. code-block:: C
1114
@@ -14,34 +17,24 @@ USB 网卡传输层面已经对接好了 LWIP 的收发接口,因此,用户
1417
// #define CONFIG_USBHOST_PLATFORM_CDC_NCM
1518
// #define CONFIG_USBHOST_PLATFORM_ASIX
1619
// #define CONFIG_USBHOST_PLATFORM_RTL8152
17-
// #define CONFIG_USBHOST_PLATFORM_BL616
1820
19-
- 包含了对接 LWIP 的输入输出接口,举例如下
21+
.. note:: 如果使用 Kconfig 系统,上述宏自定生成,其他平台请手动定义。
2022

21-
.. code-block:: C
23+
USB 网卡传输层面已经对接好了相关网络组件,列举如下:
2224

23-
static err_t usbh_cdc_ecm_linkoutput(struct netif *netif, struct pbuf *p)
24-
{
25-
int ret;
26-
(void)netif;
25+
- 自定义 OS + LWIP 请使用 **platform/lwip/usbh_lwip.c**,需要自行包含该文件,并使能上述相关的宏
26+
- RT-THREAD + LWIP 请使用 **platform/rtthread/usbh_lwip.c**,在 Kconfig 中使能对应的网卡驱动后自动勾选该文件
27+
- ESP-IDF + LWIP 请使用 **platform/freertos/usbh_net.c**,在 Kconfig 中使能对应的网卡驱动后自动勾选该文件
28+
- NUTTX + NUTTX 网络组件 请使用 **platform/nuttx/usbh_net.c**,在 Kconfig 中使能对应的网卡驱动后自动勾选该文件
2729

28-
usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
29-
ret = usbh_cdc_ecm_eth_output(p->tot_len);
30-
if (ret < 0) {
31-
return ERR_BUF;
32-
} else {
33-
return ERR_OK;
34-
}
35-
}
30+
.. note:: 如果是自行添加代码,别忘了添加 USB 网卡驱动相关的源文件,例如 **class/usbh_cdc_ecm.c**。所以我们推荐搭配对应平台使用哦,省去自己添加文件的麻烦
3631

37-
void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen)
38-
{
39-
usbh_lwip_eth_input_common(&g_cdc_ecm_netif, buf, buflen);
40-
}
32+
USB 网卡对接过程
33+
-------------------
4134

35+
下面举例对接 LWIP 的对接过程。
4236

43-
- 网卡类枚举完成后,注册 netif,并且创建网卡接收线程(因此使用 RTTHREAD 时不需要使用 RTT 的接收线程模块)。
44-
- 必须开启 DHCP client 服务,用于从 USB 网卡获取 IP 地址。
37+
- 在 USB 网卡枚举完成以后,会 **自动** 调用 `usbh_xxx_run` 函数,此时注册 netif 驱动,并且开启 DHCP 客户端和获取 IP 的定时器。
4538

4639
.. code-block:: C
4740
@@ -75,17 +68,61 @@ USB 网卡传输层面已经对接好了 LWIP 的收发接口,因此,用户
7568
#endif
7669
}
7770
78-
- 获取到 IP 以后,就与 USB 没有关系了,直接使用 LWIP 的接口即可。
71+
- `usbh_lwip_eth_output_common` 用于将发送 pbuf 组装成 USB 网卡数据包
72+
- `usbh_lwip_eth_input_common` 用于将 USB 网卡数据组装成 pbuf
73+
- 实际网卡发送和接收处理
74+
75+
.. code-block:: C
76+
77+
static err_t usbh_cdc_ecm_linkoutput(struct netif *netif, struct pbuf *p)
78+
{
79+
int ret;
80+
(void)netif;
81+
82+
usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
83+
ret = usbh_cdc_ecm_eth_output(p->tot_len);
84+
if (ret < 0) {
85+
return ERR_BUF;
86+
} else {
87+
return ERR_OK;
88+
}
89+
}
90+
91+
void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen)
92+
{
93+
usbh_lwip_eth_input_common(&g_cdc_ecm_netif, buf, buflen);
94+
}
95+
96+
- USB 网卡 拔出以后会 **自动** 调用 `usbh_xxx_stop` 函数,此时需要停止 DHCP 客户端,删除定时器,并且移除 netif。
97+
98+
.. code-block:: C
99+
100+
void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class)
101+
{
102+
struct netif *netif = &g_cdc_ecm_netif;
103+
(void)cdc_ecm_class;
104+
105+
#if LWIP_DHCP
106+
dhcp_stop(netif);
107+
dhcp_cleanup(netif);
108+
usb_osal_timer_delete(dhcp_handle);
109+
#endif
110+
netif_set_down(netif);
111+
netif_remove(netif);
112+
}
113+
114+
- 因为 USB 网卡内部已经对接了LWIP,因此用户可以直接使用 LWIP 的 API,无需关心 USB 的实现。
79115

80-
- 需要注意以下参数
116+
USB 网卡 LWIP 配置宏相关注意事项
117+
------------------------------------
81118

82-
LWIP_TCPIP_CORE_LOCKING_INPUT 用于不使用 lwip 内置的 tcpip 线程,而使用 USB 自己的处理线程
119+
**LWIP_TCPIP_CORE_LOCKING_INPUT** 用于不使用 lwip 内置的 tcpip 线程,而使用 USB 自己的接收处理线程
83120

84-
LWIP_TCPIP_CORE_LOCKING 在现在 lwip 版本中默认是打开的,也推荐必须打开。
121+
**LWIP_TCPIP_CORE_LOCKING** 在现在 lwip 版本中默认是打开的,也推荐必须打开。
85122

86-
PBUF_POOL_BUFSIZE 推荐大于1600,搭配 LWIP_TCPIP_CORE_LOCKING_INPUT 使用,因为我们提供了使用 zero mempy 的方式,使用静态 pbuf,而不是把数据 copy 到 pbuf 中。
123+
**PBUF_POOL_BUFSIZE** 推荐大于1600,搭配 LWIP_TCPIP_CORE_LOCKING_INPUT 使用,因为我们提供了使用 zero mempy 的方式,使用静态 pbuf,而不是把数据 copy 到 pbuf 中。
87124

88-
TCPIP_THREAD_STACKSIZE 推荐大于 1K,防止栈溢出。
125+
**TCPIP_THREAD_STACKSIZE** 推荐大于 1K,防止栈溢出。
89126

90127
.. code-block:: C
91128

0 commit comments

Comments
 (0)