有人可能會問了,PHY的初始化在哪呢?其實,調用HAL\_ETH\_Init()函數的時候, HAL庫就已經對我們的PHY進行初始化了,當然,每個不一樣的PHY肯定是不一樣的配置,所以,這就需要我們自己對PHY參數進行配置,我們開發板使用的是LAN8720A芯片,LAN8720A 復位時需要一段延時時間,這里需要定義延時時間長度,大約 5~50ms即可,驅動代碼中需要獲取 PHY 的速度和工作模式, LAN8720A 的R31 是特殊控制/狀態寄存器,包括指示以太網速度和工作模式的狀態位,所以,我們需要在stm32f4xx\_hal\_conf.h中添加我們自己的PHY配置,具體見代碼清單 3?3。
```
1 /* ############ Ethernet peripheral configuration ################# */
2
3 /* Section 1 : Ethernet peripheral configuration */
4
5 /* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
6 #define MAC_ADDR0 2U
7 #define MAC_ADDR1 0U
8 #define MAC_ADDR2 0U
9 #define MAC_ADDR3 0U
10 #define MAC_ADDR4 0U
11 #define MAC_ADDR5 0U
12
13 /* Definition of the Ethernet driver buffers size and count */
14 #define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE
15 /* buffer size for receive */
16 #define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE
17 /* buffer size for transmit */
18 #define ETH_RXBUFNB ((uint32_t)8U)
19 /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
20 #define ETH_TXBUFNB ((uint32_t)8U)
21 /* 4 Tx buffers of size ETH_TX_BUF_SIZE */
22
23 /* Section 2: PHY configuration section */
24
25 /* LAN8720_PHY_ADDRESS Address*/
26 #define LAN8720_PHY_ADDRESS 0U
27 /* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
28 #define PHY_RESET_DELAY ((uint32_t)0x00000005U)
29 /* PHY Configuration delay */
30 #define PHY_CONFIG_DELAY ((uint32_t)0x00000005U)
31
32 #define PHY_READ_TO ((uint32_t)0x0000FFFFU)
33 #define PHY_WRITE_TO ((uint32_t)0x0000FFFFU)
34
35 /* Section 3: Common PHY Registers */
36
37 #define PHY_BCR ((uint16_t)0x00U)
38 /*!< Transceiver Basic Control Register */
39 #define PHY_BSR ((uint16_t)0x01U)
40 /*!< Transceiver Basic Status Register */
41
42 #define PHY_RESET ((uint16_t)0x8000U) /*!< PHY Reset */
43 #define PHY_LOOPBACK ((uint16_t)0x4000U)
44 /*!< Select loop-back mode */
45 #define PHY_FULLDUPLEX_100M ((uint16_t)0x2100U)
46 /*!< Set the full-duplex mode at 100 Mb/s */
47 #define PHY_HALFDUPLEX_100M ((uint16_t)0x2000U)
48 /*!< Set the half-duplex mode at 100 Mb/s */
49 #define PHY_FULLDUPLEX_10M ((uint16_t)0x0100U)
50 /*!< Set the full-duplex mode at 10 Mb/s */
51 #define PHY_HALFDUPLEX_10M ((uint16_t)0x0000U)
52 /*!< Set the half-duplex mode at 10 Mb/s */
53 #define PHY_AUTONEGOTIATION ((uint16_t)0x1000U)
54 /*!< Enable auto-negotiation function */
55 #define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200U)
56 /*!< Restart auto-negotiation function */
57 #define PHY_POWERDOWN ((uint16_t)0x0800U)
58 /*!< Select the power down mode */
59 #define PHY_ISOLATE ((uint16_t)0x0400U)
60 /*!< Isolate PHY from MII */
61
62 #define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020U)
63 /*!< Auto-Negotiation process completed */
64 #define PHY_LINKED_STATUS ((uint16_t)0x0004U)
65 /*!< Valid link established */
66 #define PHY_JABBER_DETECTION ((uint16_t)0x0002U)
67 /*!< Jabber condition detected*/
68
69 /* Section 4: Extended PHY Registers */
70 #define PHY_SR ((uint16_t)0x1FU)
71 /*!< PHY status register Offset*/
72
73 #define PHY_SPEED_STATUS ((uint16_t)0x0004U)
74 /*!< PHY Speed mask */
75 #define PHY_DUPLEX_STATUS ((uint16_t)0x0010U)
76 /*!< PHY Duplex mask*/
```
- 說明
- 第1章:網絡協議簡介
- 1.1:常用網絡協議
- 1.2:網絡協議的分層模型
- 1.3:協議層報文間的封裝與拆封
- 第2章:LwIP簡介
- 2.1:LwIP的優缺點
- 2.2:LwIP的文件說明
- 2.2.1:如何獲取LwIP源碼文件
- 2.2.2:LwIP文件說明
- 2.3:查看LwIP的說明文檔
- 2.4:使用vscode查看源碼
- 2.4.1:查看文件中的符號列表(函數列表)
- 2.4.2:函數定義跳轉
- 2.5:LwIP源碼里的example
- 2.6:LwIP的三種編程接口
- 2.6.1:RAW/Callback API
- 2.6.2:NETCONN API
- 2.6.3:SOCKET API
- 第3章:開發平臺介紹
- 3.1:以太網簡介
- 3.1.1:PHY層
- 3.1.2:MAC子層
- 3.2:STM32的ETH外設
- 3.3:MII 和 RMII 接口
- 3.4:PHY:LAN8720A
- 3.5:硬件設計
- 3.6:軟件設計
- 3.6.1:獲取STM32的裸機工程模板
- 3.6.2:添加bsp_eth.c與bsp_eth.h
- 3.6.3:修改stm32f4xx_hal_conf.h文件
- 第4章:LwIP的網絡接口管理
- 4.1:netif結構體
- 4.2:netif使用
- 4.3:與netif相關的底層函數
- 4.4:ethernetif.c文件內容
- 4.4.1:ethernetif數據結構
- 4.4.2:ethernetif_init()
- 4.4.3:low_level_init()
- 第5章:LwIP的內存管理
- 5.1:幾種內存分配策略
- 5.1.1:固定大小的內存塊
- 5.1.2:可變長度分配
- 5.2:動態內存池(POOL)
- 5.2.1:內存池的預處理
- 5.2.2:內存池的初始化
- 5.2.3:內存分配
- 5.2.4:內存釋放
- 5.3:動態內存堆
- 5.3.1:內存堆的組織結構
- 5.3.2:內存堆初始化
- 5.3.3:內存分配
- 5.3.4:內存釋放
- 5.4:使用C庫的malloc和free來管理內存
- 5.5:LwIP中的配置
- 第6章:網絡數據包
- 6.1:TCP/IP協議的分層思想
- 6.2:LwIP的線程模型
- 6.3:pbuf結構體說明
- 6.4:pbuf的類型
- 6.4.1:PBUF_RAM類型的pbuf
- 6.4.2:PBUF_POOL類型的pbuf
- 6.4.3:PBUF_ROM和PBUF_REF類型pbuf
- 6.5:pbuf_alloc()
- 6.6:pbuf_free()
- 6.7:其它pbuf操作函數
- 6.7.1:pbuf_realloc()
- 6.7.2:pbuf_header()
- 6.7.3:pbuf_take()
- 6.8:網卡中使用的pbuf
- 6.8.1:low_level_output()
- 6.8.2:low_level_input()
- 6.8.3:ethernetif_input()
- 第7章:無操作系統移植LwIP
- 7.1:將LwIP添加到裸機工程
- 7.2:移植頭文件
- 7.3:移植網卡驅動
- 7.4:LwIP時基
- 7.5:協議棧初始化
- 7.6:獲取數據包
- 7.6.1:查詢方式
- 7.6.2:ping命令詳解
- 7.6.3:中斷方式
- 第8章:有操作系統移植LwIP
- 8.1:LwIP中添加操作系統
- 8.1.1:拷貝FreeRTOS源碼到工程文件夾
- 8.1.2:添加FreeRTOS源碼到工程組文件夾
- 8.1.3:指定FreeRTOS頭文件的路徑
- 8.1.4:修改stm32f10x_it.c
- 8.2:lwipopts.h文件需要加入的配置
- 8.3:sys_arch.c/h文件的編寫
- 8.4:網卡底層的編寫
- 8.5:協議棧初始化
- 8.6:移植后使用ping測試基本響應
- 第9章:LwIP一探究竟
- 9.1:網卡接收數據的流程
- 9.2:內核超時處理
- 9.2.1:sys_timeo結構體與超時鏈表
- 9.2.2:注冊超時事件
- 9.2.3:超時檢查
- 9.3:tcpip_thread線程
- 9.4:LwIP中的消息
- 9.4.1:消息結構
- 9.4.2:數據包消息
- 9.4.3:API消息
- 9.5:揭開LwIP神秘的面紗
- 第10章:ARP協議
- 10.1:鏈路層概述
- 10.2:MAC地址的基本概念
- 10.3:初識ARP
- 10.4:以太網幀結構
- 10.5:IP地址映射為物理地址
- 10.6:ARP緩存表
- 10.7:ARP緩存表的超時處理
- 10.8:ARP報文
- 10.9:發送ARP請求包
- 10.10:數據包接收流程
- 10.10.1:以太網之數據包接收
- 10.10.2:ARP數據包處理
- 10.10.3:更新ARP緩存表
- 10.11:數據包發送流程
- 10.11.1:etharp_output()函數
- 10.11.2:etharp_output_to_arp_index()函數
- 10.11.3:etharp_query()函數
- 第11章:IP協議
- 11.1:IP地址.md
- 11.1.1:概述
- 11.1.2:IP地址編址
- 11.1.3:特殊IP地址