STM32CubeMX学习笔记(42)——ETH接口+LwIP协议栈使用(静态IP)

资讯 2024-06-24 阅读:28 评论:0
STM32F4xx 系列控制器内部集成了一个以太网外设...
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

STM32F4xx 系列控制器内部集成了一个以太网外设,它实际是一个通过 DMA 控制器进行介质访问控制(MAC),它的功能就是实现 MAC 层的任务。借助以太网外设,STM32F4xx 控制器可以通过 ETH 外设按照 IEEE 802.3-2002 标准发送和接收 MAC 数据包。ETH 内部自带专用的 DMA 控制器用于 MAC,ETH 支持两个工业标准接口介质独立接口(MII)和简化介质独立接口(RMII)用于与外部 PHY 芯片连接。MII 和 RMII 接口用于 MAC 数据包传输,ETH 还集成了站管理接口(SMI)接口专门用于与外部 PHY 通信,用于访问 PHY 芯片寄存器。

The STM32F4x series controller has an internal collection of & #xff0c with an Ethernet external setting; it is actually a medium access control (MAC) & #xff0c via a DMA controller; its function is to achieve the MAC layer. The STM32F4x controller is capable of sending and receiving MAC data packages by ETH external setting in accordance with IEEE 802.3-2002 standards. The ETH internal self-contained DMA controller is used for MAC& #xff0c; ETH supports two industry standard interface interfaces independent interfaces (MII) and simplified media independent interfaces (RMII) for external PHY chip connections. MII and RMII interfaces are used for MAC data package transmission & #xff0c; ETH is also integrated into station management interfaces (SMI) dedicated to external PHY interfaces & #ff0c; access to PHY chips.

物理层定义了以太网使用的传输介质、传输速度、数据编码方式和冲突检测机制,PHY 芯片是物理层功能实现的实体,生活中常用水晶头网线+水晶头插座+PHY 组合构成了物理层。

The physical layer defines the transmission medium, transmission speed, data coding and conflict detection mechanisms used in Tainet & #xff0c; the PHYs is the entity that performs the functions of the physical layer & #xff0c; the life crystalline & #43; crystal plug #43; and the PHYs combination forms the physical layer.

ETH 有专用的 DMA 控制器,它通过 AHB 主从接口与内核和存储器相连,AHB 主接口用于控制数据传输,而 AHB 从接口用于访问“控制与状态寄存器”(CSR)空间。在进行数据发送是,先将数据有存储器以 DMA 传输到发送 TX FIFO 进行缓冲,然后由 MAC 内核发送;接收数据时,RX FIFO 先接收以太网数据帧,再由 DMA 传输至存储器。ETH 系统功能框图见下图。

ETH has a dedicated DMA controller & #xff0c; it connects the interface from the AHB main to the kernel and memory xff0c; the AHB main interface is used to control data transmission xff0c; while the AHB is used from the interface to access the " Control and Status Register " (CSR) space. The data are sent xff0c; the data are transferred to DMA first to send TX FIFO for a buffer xff0c; then xff1b by MAC internal nucles; xff0c when receiving data; RXFIFO first receives data from the Toonet Data Frame xff0c; and then the DMA transmits to the Storageer.

LwIP 是 Light Weight Internet Protocol 的缩写,是由瑞士计算机科学院 Adam Dunkels等开发的适用于嵌入式领域的开源轻量级 TCP/IP 协议栈。它可以移植到含有操作系统的平台中,也可以在无操作系统的平台下运行。由于它开源、占用的 RAM 和 ROM 比较少、支持较为完整的 TCP/IP 协议、且十分便于裁剪、调试,被广泛应用在中低端的 32 位控制器平台。

LwIP is a Light Weight Internet Protocol acronym & #xff0c; open source light TCP/IP depositories for embedded areas developed by the Swiss Academy of Computer Sciences, among others, Adam Dunkels. It can be transplanted to platforms containing operating systems & #xff0c; it can also operate under platforms without operating systems. Because it has fewer open sources, RAMs and ROMs, supports more complete TCP/IP protocols, and is very easy to tailor, calibrate #xff0c; it is widely applied to 32-bit control platforms at lower and middle ends.

针对 LwIP 应用开发了测试平台,其中有一个是在 STM32F4x7 系列控制器运行的
(文件编号为:STSW-STM32070)。

A test platform has been developed for LwIP applications & #xff0c; one of these is run at
(file number & #xff1a; STSW-STM32070).

静态IP地址(又称固定IP地址)是长期分配给一台计算机或网络设备使用的 IP 地址。一般来说,一般是特殊的服务器或者采用专线上网的计算机才拥有固定的 IP 地址而且需要比较昂贵的费用。静态IP是二级路由必须用到的。

static IP address( also known as fixed IP address & #xff09; is a long-term IP address assigned to a computer or network equipment.

静态IP是可以直接上网的IP段,该IP在ISP装机时会划分一个IP地址给你,让计算机在连接网络时不再自动获取网络地址,避免了网络连接上的困扰,宽带运营商会提供一根一个IP地址、子网掩码、网关和DNS服务器地址给用户。在未使用路由器的情况下,只需要把这根入户网线连接到电脑上,并且手动设置电脑上的IP地址,这样电脑才能上网。静态IP地址不会改变,并且主要用于互联网上的网站应用或服务。一些游戏者和使用VOIP的人往往也倾向于选择静态IP地址,因为沟通更容易。

Static IPs provide a direct access IP section & #xff0c; when ISPs are installed, the IP will divide an IP address to you & #xff0c; the computer will no longer automatically access the web address xff0c when connected to the network; it avoids trouble with network connections xff0c; the Broadband Chamber of Commerce provides a single IP address, a subnet mask, a gateway and a DNS server address to users. In the absence of a router, the IP will divide an IP address to the computer xff0c; and the IP address on a manual computer xff0c; so the computer will be able to access it. The static IP address will not change xff0c; and it will be used primarily for web applications or services on the Internet. Some gamers and VOIP users also tend to choose a static IP address xff0c; it is easier to communicate.

动态IP地址和静态IP地址相比。

The dynamic IP address is compared to the static IP address.

  • 其一:为了节省lP资源,通过电话拨号、ADSL虚拟拨号等方式上网的机器是不分配固定IP地址的。而是由ISP动态临时分配,提高lP地址利用率;
  • 其二:在局域网中为了客户机设置简便,也常采用动态分配IP地址,这意味着您每次连接互联网时得到的lP地址是不同的。尽管这不影响您访问互联网,但是您的朋友、用户却不能访问到您。因为,他们不知道您的计算机在哪里。这就像每个人都有一部电话,但您的电话号码天天都在改变。

LAN8720A 是 SMSC 公司(已被 Microchip 公司收购)设计的一个体积小、功耗低、全能型 10/100Mbps 的以太网物理层(PHY)收发器。LAN8720A 总共只有 24Pin,。

LAN8720A is a small, low-capacity, full-power 10/100Mbps transceiver of the Ethernet Physics layer designed by SMSC (which has been acquired by Microchip). LAN8720A totals only 24 Pin #xff0c;

由它组成的网络结构见下图

LAN8720A 通过 RMII 与 MAC 连接。RJ45 是网络插座,在与 LAN8720A 连接之间还需要一个变压器,所以一般使用带电压转换和 LED 指示灯的 HY911105A 型号的插座。一般来说,,不过 。

The network structure consisting of it is shown in figure
 >br/>LAN8720A is connected to MAC through RMII. RJ45 is a network plugin xff0c; a transformer xff0c is also needed to connect to LAN8720A; therefore, the HY91105A plugin with voltage conversion and LED light is generally used.</p> 
</blockquote> 
<p><img src=

  • PHY 芯片地址设置
    LAN8720A 可以通过 PHYAD0 引脚来配置,该引脚与 RXER 引脚复用,芯片内部自带下拉电阻,当硬复位结束后, LAN8720A 会读取该引脚电平,作为器件的 SMI 地址,接下拉电阻时(浮空也可以,因为芯片内部自带了下拉电阻),设置 SMI 地址为 0,当外接上拉电阻后,可以设置为 1。

    PHY chip settings
    LAN8720A can be configured xff0c by PHYAD0 induction; xff0c can be reused by xff0c with RXER induction; xff0c within the chip itself; xff0c xff0c xff0c xff0c xff0c; LAN8720A will read xff0c; SMI addressxff0c as device; xff08 xff08 when pulled; xff0c; since xff0c; xffff09 1

  • nINT/REFCLKO 引脚功能配置
    nINT/REFCLKO 引脚用于 RMII 接口中 REF_CLK 信号线

    nINT/RFCLKO footing configuration
    nINT/RFCLKO footing for REF_CLK signals in the RRMII interface

    • ,它也可以被设置成 50MHz 时钟输出,这样可以直接与 STM32F4xx 的 REF_CLK 引脚连接为其提供 50MHz 时钟源,这种模式要求,由 LAN8720A 内部 PLL 电路陪频得到 50MHz 时钟,此时 nIN/REFCLKO 引脚的中断功能不可用,用于 50MHz 时钟输出。
    • ,LAN8720A 被设置为时钟输入,即接入 STM32F4xx 的 REF_CLK 引脚和 LAN8720A 的 XTAL1/CLKIN 引脚,。

    nINTSEL 与 LED2 引脚共用,一般使用下拉,LAN8720A 外接 25MHz 石英晶振,通过内部陪频到 50MHz,然后通过 REFCLKO 引脚,输出 50MHz 参考时钟给 MAC 控制器。这种方式,可以降低 BOM 成本。

    nINTSEL shares & #xff0c with LED2; general drop & #xff0c; LAN8720A outside 25MHz stone crystalline & #xff0c; attends 50MHz, through in-house filiation; then leads xff0c; output 50MHz reference clock to MAC controller. This way xff0c; can reduce BOM costs.

    • 如果不外接晶振,需要通过板子的MCO1或者MCO2通过分频倍频操作来输出50Mhz来驱动网口。

ETH 相关硬件在 STM32F4xx 控制器分布情况如下:

The distribution of ETH-related hardware in STM32F4x controllers is as follows: xff1a;

接口ETHGPIO
MIIMII_TX_CLKPC3
MII_TXD0PB12/PG13
MII_TXD1PB13/PG14
MII_TXD2PC2
MII_TXD3PB8/PE2
MII_TX_ENPB11/PG11
MII_RX_CLKPA1
MII_RXD0PC4
MII_RXD1PC5
MII_RXD2PB0
MII_RXD3PB1
MII_RX_ERPB10
MII_RX_DVPA7
MII_CRSPA0
MII_COLPA3
RMIIRMII_TXD0PB12/PG13
RMII_TXD1PB13/PG14
RMII_TX_ENPG11
RMII_RXD0PC4
RMII_RXD1PC5
RMII_CRS_DVPA7
RMII_REF_CLKPA1
SMIMDIOPA1
MDCPC1
其他PPS_OUTPB5/PG8

1. 打开 STM32CubeMX 软件,点击“新建工程”

Open STM32CubeMX software & #xff0c; click "New Project"
/></p> 
<p><strong>2. 选择 MCU 和封装</strong><br /> <img src=

2. Select MCU and seal
/></p> 
<p><strong>3. 配置时钟</strong><br /> RCC 设置,选择 HSE(外部高速时钟) 为 Crystal/Ceramic Resonator(晶振/陶瓷谐振器)<br /> <img src=
选择 Clock Configuration,配置系统时钟 SYSCLK 为 168MHz
修改 HCLK 的值为 168 后,输入回车,软件会自动修改所有配置

3. Configure clocks
RCC settings & #xff0c; select HSE (external high-speed clock) for Crystal/Ceramic Resonator (Crystal/Turnification)
br/> for Crystal Navigation & #xff0c; configure system clocks for 168 MHz to modify HCLK values after 168 #xff0c; enter #xf0c; automatically modify all configurations

4. 配置调试模式
非常重要的一步,否则会造成第一次烧录程序后续无法识别调试器
SYS 设置,选择 Debug 为 Serial Wire

4. Configure debugging mode
xff0c> is a very important step; otherwise, the first burner will not be followed by the debugger
SYS setting #xff0c; select Debug as Serial Wire
 ></p> 
 
 
<p>在  中选择  设置,模式选择  使用简化版MII(介质独立接口)。</p><p>Select settings & #xff0c; mode selection Use simplified version MII (media independent interface).</p> 
<p><img src=

  • MII: Medium Independent Interface(介质独立接口),用于连接介质访问控制层(MAC)子层和物理层(PHY)之间的标准以太网接口,提供数据传输路径。由于 MII ,由此产生的 I/O 口需求及功耗较大。对于 MII 接口,一般是,再由 PHY 提供 TX_CLK 和 RX_CLK 时钟,不需要与 MAC 层时钟一致。

  • RMII: Reduced Medium Independent Interface,RMII 接口是 MII 接口的简化版本,MII 需要 16 根通信线,RMII ,在功能上是相同的。对于 RMII 接口,一般需要,同时接入 MAC 和 PHY。需与MAC层时钟一致,通常从 MAC 层获取该时钟源。现在一般都用RMII模式。

在 进行具体参数配置。

Makes a specific parameter configuration.

Advanced : Ethernet Media Configuration(以太网媒体配置):

Advanced: Ethernet Media Consultation( Ethernet Media Configuration & #xff09; & #xff1a;

  • Auto Negotiation(自适应功能): 选择 ,一般选择使能自适应功能,系统会自动寻找最优工作方式,包括选择 10MBit/s 或者 100MBit/s 的以太网速度以及全双工模式或半双工模式。
  • Speed(以太网速度): 可选 10MBit/s 或 100MBit/s,它设定 ETH_MACCR 寄存器的 FES 位的值,。
  • Duplex Mode(以太网工作模式): 可选全双工模式或半双工模式,它设定 ETH_MACCR 寄存器 DM 位的值。。

General : Ethernet Configuration(以太网配置):

General: Ethernet Consultation( Ethernet configuration & #xff09; & #xff1a;

  • Ethernet MAC Address(以太网MAC地址): 默认即可
  • PHY Address(PHY芯片地址):

注意:LAN8720A 可以通过 PHYAD0 引脚(如PHY芯片引脚10)来配置,该引脚与 RXER 引脚复用,芯片内部自带下拉电阻,当硬复位结束后, LAN8720A 会读取该引脚电平,作为器件的 SMI 地址,接下拉电阻时(浮空也可以,因为芯片内部自带了下拉电阻),设置 SMI 地址为 0,当外接上拉电阻后,可以设置为 1。。

Note that xff1a; LAN8720A can be used to configure xff0c; xff09; xff0c; xER ff0c; xff0c; xff0c; xff0c; xff0c; xff0c; xff0c; xff0c; xff0c; xff0c; xff0c; xfff0c; xy; #xfff0c; xy; xrng; xrong ; xffp; xffp; xffffffk; g g; gffffffffffd ; xxffffffd; d d v; xxxffffffffk; n str; xxxfffffffffffffft ; n n xr; xtxtxr xxxxfffffffffffffffffffft r r r r r r r r r r r r r r r r r r r r r r r r r r

Ethernet Basic Configuration(以太网基本配置):

Ethernet Basic Consultation( Ethernet Basic Configuration xff09; xff1a;

  • Rx Mode(接收模式): 选择 轮询方法。ST 官方例程文件包含了中断引脚的相关配置,主要用于指示接收到以太网帧,我们这里不需要使用。
  • TX IP Header Checksum Computation(发送数据校验和): 选择 使能发送数据硬件校验和。这个需要硬件支持,。

在 进行高级参数配置。

Makes an advanced parameter configuration.

  • PHY: 选择 ,因为没有我的 PHY 芯片型号 LAN8720A
  • PHY name: 可改为 PHY 芯片型号 LAN8720A

  • PHY special control/status register Offset(特殊控制/状态寄存器): 按照芯片手册填写,
  • PHY Speed mask(以太网速度状态位): 按照芯片手册填写,
  • PHY Speed mask(以太网工作模式状态位): 按照芯片手册填写,
  • 其他保持默认

GPIO 设置,在右边图中找到 ETH 对应引脚,将引脚配置成跟原理图上的一致


GPIO settings & #xff0c; ETH matching kicks & #xff0c found in right graphs
 > <br/> <imgsrc=

在 中选择 设置,勾选 使能协议栈。

Select the setup & #xff0c; ticking the enabling bar.

在 进行通用参数配置。

Making a generic parameter configuration.

IPv4 - DHCP Options:

  • LWIP_DHCP(DHCP Module): 选择 。使用固定IP地址。

IP Address Settings:

  • IP_ADDRESS(IP Address): 填写IP地址。
  • NETMASK_ADDRESS(Netmask Address): 填写掩码地址。
  • GATEWAY_ADDRESS(Gateway Address): 填写网关地址。

Protocols Options:

  • LWIP_ICMP(ICMP Module Activation)控制报文协议: 选择 。主要用于网络的调试与维护,ping 的时候用。
  • LWIP_IGMP(IGMP Module)互联网组管理协议: 选择 。可以实现多播数据的接收。
  • LWIP_DNS(DNS Module)域名解析: 选择 。通过域名解析用户就可以在知道服务器域名的情况下,获得该服务器的 IP 地址。
  • LWIP_UDP(UDP Module)用户数据报协议: 选择 。看需求,一般选择用 TCP 协议。
  • MEMP_NUM_UDP_PCB(Number of UDP Connections): UDP协议控制块数量,决定 UDP 协议控制块需要的 POOL 资源。
  • LWIP_TCP(TCP Module)传输控制协议: 选择 。
  • MEMP_NUM_TCP_PCB(Number of TDP Connections): 同时活动的TCP连接数。

在 进行关键选项配置。
Infrastructure - OS Awarness Option:

Making key options configuration.
Infrastructure - OSAwarness Option:

  • NO_SYS(OS Awarness): 表示无操作系统模拟层,这个宏非常重要,因为无操作系统与有操作系统的移植和编写是完全不一样的,我们现在是无操作系统移植。

Infrastructure - Timers Options:

  • LWIP_TIMERS(Use Support For sys_timeout): 默认 。使用 LwIP 提供的定时器,用于超时机制。

Infrastructure - Core Locking and MPU Options:

  • SYS_LIGHTWEIGHT_PROT(Memory Functions Protection): 默认 。平台锁,保护关键区域内缓存的分配与释放。

Infrastructure - Heap and Memory Pools Options:

  • MEM_SIZE(Heap Memory Size): 默认 。堆内存的大小。如果应用程序将发送很多需要复制的数据应该设置得大一点。

Infrastructure - Internal Memory Pool Sizes:

  • MEMP_NUM_PBUF(Number of Memory Pool struct Pbufs): 默认 。memp 结构的 pbuf 数量,如果应用从 ROM 或者静态存储区发送大量数据时,这个值应该设置大一点。
  • MEMP_NUM_RAW_PCB(Number of Raw Protocol Control Blocks): 默认 。 原始连接(就是应用程不经过传输层直接到IP层获取数据)PCB 的数目,该项依赖 LWIP_RAW 项的开启。
  • MEMP_NUM_TCP_PCB(Number of Listening TCP Connections): 默认 。 同时建立激活的 TCP 连接的数目(要求参数 LWIP_TCP 使能)。
  • MEMP_NUM_TCP_SEG(Number of TCP Segments simultaneously queued): 默认 。 最多同时在队列的 TCP_SEG 的数目。

Pbuf Options:

  • PBUF_POOL_SIZE(Number of Buffers in the Pbuf Pool): 默认 。 内存池大小。
  • PBUF_POOL_BUFSIZE(Size of each pbuf in the pbuf pool): 默认 。 每个 pbuf 内存池大小。

IPv4 - ARP Options:

  • LWIP_ARP(ARP Functionality): 选择 。 地址解析协议,通过目标设备的 IP 地址,查询目标设备的 MAC 地址,以保证通信的
    顺利进行。

Callback - TCP Options:

  • TCP_TTL(Number of Time-To-Live Used by TCP Packets): 默认 。TCP TTL时间。
  • TCP_WND(TCP Receive Window Maximum Size): 默认 。TCP 窗口长度。
  • TCP_QUEUE_OOSEQ(Allow Out-Of-Order Incoming Packets): 默认 。TCP队列到达顺序。如果设备内存不足,则定义为0。
  • TCP_MSS(Maximum Segment Size): 默认 。最大 TCP 报文段,TCP_MSS = MTU - IP 报头大小 - TCP 报头大小。
  • TCP_SND_BUF(TCP Sender Buffer Space): 默认 。TCP 发送缓冲区大小(字节)。
  • TCP_SND_QUEUELEN(TCP Sender Buffer Space): 默认 。TCP 发送缓冲区队列的最大长度。

Network Interfaces Options:

  • LWIP_NETIF_STATUS_CALLBACK(Callback Function on Interface Status Changes): 默认 。当 netif 状态设置为 up 或 down 时调用此函数。
  • LWIP_NETIF_LINK_CALLBACK(Callback Function on Interface Link Changes): 默认 。当 netif 链接设置为 up 或 down 时,将调用此函数。

NETIF - Loopback Interface Options:

  • LWIP_NETIF_LOOPBACK(NETIF Loopback): 默认 。支持发送数据包的目的地 IP。

Thread Safe APIs - Socket Options:

  • LWIP_SOCKET(Socket API): 默认 。Socket API。

输入项目名和项目路径

选择应用的 IDE 开发环境 MDK-ARM V5

每个外设生成独立的 文件
不勾:所有初始化代码都生成在 main.c
勾选:初始化代码生成在对应的外设文件。 如 GPIO 初始化代码生成在 gpio.c 中。

点击 GENERATE CODE 生成代码

Enter the IDE development environment
 / >br/> <img <br/> <imgsrc=https://alg-blog.csdnimg.n/img_convert/a408d1dc06c6d429474f3bng/ each externally generated independent document
xd1a; all initial codes are generated in .

在 的死循环中添加 函数。

Adds a function to the life cycle of %1.

然后加入以下代码不断打印 IP 地址。

Add the following code to print IP addresses on a continuous basis.

 

查看效果:

使用电脑ping上面的ip:

View effectsxff1a;
 alt=br/> use computer ping ip:

链接:https://pan.baidu.com/s/1ds3cqlt1JpjeaSF5-Tpurg?pwd=e4gv 提取码:e4gv

Links & #xff1a; extraction codexff1a;e4gv

初始化LwIP的内存管理和各个协议层。
按顺序执行了:

Initializes LwIP memory management and protocol layers.
executed in sequence xff1a;

  1. 网络接口的添加
  2. 初始化底层

然后LwIP就可以用了。

Then LwIP can use it.

收包用的是调用 把数据包接回来,给 处理。
发包则是由 交由 制作数据包,调用 发出去。

The package is called to bring the data package back to xff0c; process.
The package is sent to make the data package xff0c; the call is sent.

 
 
 

不断地接收来自接口的信息,并检查是否延时

Receive information from interfaces on an ongoing basis & #xff0c; check for delay

 
 

用户代码要加在 和 之间,否则下次使用 STM32CubeMX 重新生成代码后,会被删除。


/></p> 
<hr /> 
<p>? 由 <a href=Leung
写于 2022 年 8 月 25 日

♪ by Leung in August 25, 2022

? 参考:从零开始Cubemx配置STM32搭载freeRTOS以及lwip实现tcp网络通信(二)
    STM32cubeMX配置LWIP+FREERTOS
    LwIP的配置

& xff1a; 美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
随机文章
热门文章
最近发表
标签列表