407自帶乙太網mac模塊,一般外掛一個PHY晶元就可以實現乙太網物理層;以下是stm32f407VE+enc28j60+lwip2.0.2實現最基本的乙太網通信功能。 1. 新建工程,此處省略1000字。。。2. 在工程目錄下新建一個LWIP文件夾,將lwip-2.0.2.zip解壓,將src文件 ...
407自帶乙太網mac模塊,一般外掛一個PHY晶元就可以實現乙太網物理層;以下是stm32f407VE+enc28j60+lwip2.0.2實現最基本的乙太網通信功能。
1. 新建工程,此處省略1000字。。。
2. 在工程目錄下新建一個LWIP文件夾,將lwip-2.0.2.zip解壓,將src文件夾裡面的core、include、netif文件夾放到LWIP文件夾下。
在LWIP目錄下新建一個arch文件夾待用。
3. 在\LWIP\arch根目錄下新建cc.h文件,內容如下:
1 #include "stdio.h" 2 3 typedef unsigned char u8_t; 4 typedef signed char s8_t; 5 typedef unsigned short u16_t; 6 typedef signed short s16_t; 7 typedef unsigned long u32_t; 8 typedef signed long s32_t; 9 10 typedef u32_t mem_ptr_t; 11 12 /*************CPU存放數據定義為小段模式******************/ 13 #ifndef BYTE_ORDER 14 #define BYTE_ORDER LITTLE_ENDIAN 15 #endif 16 17 /**********使用KEIL/RVMDK開發工具時的巨集定義**************/ 18 //#if defined(__arm__) && defined(__ARMCC_VERSION) 19 #define PACK_STRUCT_BEGIN __packed 20 #define PACK_STRUCT_STRUCT 21 #define PACK_STRUCT_END 22 #define PACK_STRUCT_FIELD(x) x 23 /*---define (sn)printf formatters for these lwip types, for lwip DEBUG/STATS--*/ 24 25 #define U16_F "4d" 26 #define S16_F "4d" 27 #define X16_F "4x" 28 #define U32_F "8ld" 29 #define S32_F "8ld" 30 #define X32_F "8lx" 31 32 #ifndef LWIP_PLATFORM_ASSERT 33 #define LWIP_PLATFORM_ASSERT(x) \ 34 do \ 35 { printf("Assertion \"%s\" failed at line %d in %s\n", x, __LINE__, __FILE__); \ 36 } while(0) 37 #endif 38 39 #ifndef LWIP_PLATFORM_DIAG 40 #define LWIP_PLATFORM_DIAG(x) do {printf x;} while(0) 41 #endif 42 4.在LWIP\include根目錄下新建一個lwipopts.h文件,內容如下: 43 #ifndef __LWIPOPTS_H__ 44 #define __LWIPOPTS_H__ 45 46 /** 47 * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain 48 * critical regions during buffer allocation, deallocation and memory 49 * allocation and deallocation. 50 */ 51 #define SYS_LIGHTWEIGHT_PROT 0 52 53 /** 54 * NO_SYS==1: Provides VERY minimal functionality. Otherwise, 55 * use lwIP facilities. 56 */ 57 #define NO_SYS 1 // 無操作系統 58 /** 59 * NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1 60 * Mainly for compatibility to old versions. 61 */ 62 #define NO_SYS_NO_TIMERS 1 // 這樣就不需要定義sys_now函數 63 64 /* ---------- Memory options ---------- */ 65 /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which 66 lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2 67 byte alignment -> define MEM_ALIGNMENT to 2. */ 68 #define MEM_ALIGNMENT 4 69 70 /* MEM_SIZE: the size of the heap memory. If the application will send 71 a lot of data that needs to be copied, this should be set high. */ 72 #define MEM_SIZE (5 * 1024) 73 74 /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application 75 sends a lot of data out of ROM (or other static memory), this 76 should be set high. */ 77 #define MEMP_NUM_PBUF 10 78 /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One 79 per active UDP "connection". */ 80 #define MEMP_NUM_UDP_PCB 6 81 /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP 82 connections. */ 83 #define MEMP_NUM_TCP_PCB 10 84 /* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP 85 connections. */ 86 #define MEMP_NUM_TCP_PCB_LISTEN 6 87 /* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP 88 segments. */ 89 #define MEMP_NUM_TCP_SEG 12 90 /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active 91 timeouts. */ 92 #define MEMP_NUM_SYS_TIMEOUT 3 93 94 /* ---------- Pbuf options ---------- */ 95 /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ 96 #define PBUF_POOL_SIZE 10 97 98 /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */ 99 #define PBUF_POOL_BUFSIZE 1500 100 101 /* ---------- TCP options ---------- */ 102 #define LWIP_TCP 1 103 #define TCP_TTL 255 104 105 /* Controls if TCP should queue segments that arrive out of 106 order. Define to 0 if your device is low on memory. */ 107 #define TCP_QUEUE_OOSEQ 0 108 109 /* TCP Maximum segment size. */ 110 #define TCP_MSS (1500 - 40) /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) */ 111 112 /* TCP sender buffer space (bytes). */ 113 #define TCP_SND_BUF (2 * TCP_MSS) 114 115 /* TCP sender buffer space (pbufs). This must be at least = 2 * 116 TCP_SND_BUF/TCP_MSS for things to work. */ 117 #define TCP_SND_QUEUELEN (6 * TCP_SND_BUF) / TCP_MSS 118 119 /* TCP receive window. */ 120 #define TCP_WND (2 * TCP_MSS) 121 122 /* ---------- ICMP options ---------- */ 123 #define LWIP_ICMP 1 124 125 /* ---------- DHCP options ---------- */ 126 /* Define LWIP_DHCP to 1 if you want DHCP configuration of 127 interfaces. DHCP is not implemented in lwIP 0.5.1, however, so 128 turning this on does currently not work. */ 129 #define LWIP_DHCP 0 130 131 /* ---------- UDP options ---------- */ 132 #define LWIP_UDP 1 133 #define UDP_TTL 255 134 135 /* ---------- Statistics options ---------- */ 136 #define LWIP_STATS 0 137 #define LWIP_PROVIDE_ERRNO 1 138 139 // LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c) 140 #define LWIP_NETCONN 0 141 142 // LWIP_SOCKET==1: Enable Socket API (require to use sockets.c) 143 #define LWIP_SOCKET 0 144 145 // 這個必須要定義, 否則在執行lwip_init函數時會在串口中提示: 146 // Assertion "Struct packing not implemented correctly. Check your lwIP port." failed at line 345 in Library\lwip\core\init.c 147 #define PACK_STRUCT_BEGIN __packed 148 /*******************************************************************************************************************/ 149 #define LWIP_DEBUG 1 150 #define LWIP_DBG_TYPES_ON LWIP_DBG_OFF 151 152 /** 153 * ETHARP_DEBUG: Enable debugging in etharp.c. 154 */ 155 #define ETHARP_DEBUG LWIP_DBG_OFF 156 157 /** 158 * NETIF_DEBUG: Enable debugging in netif.c. 159 */ 160 #define NETIF_DEBUG LWIP_DBG_OFF 161 162 /** 163 * PBUF_DEBUG: Enable debugging in pbuf.c. 164 */ 165 #define PBUF_DEBUG LWIP_DBG_OFF 166 167 /** 168 * API_LIB_DEBUG: Enable debugging in api_lib.c. 169 */ 170 #define API_LIB_DEBUG LWIP_DBG_OFF 171 172 /** 173 * API_MSG_DEBUG: Enable debugging in api_msg.c. 174 */ 175 #define API_MSG_DEBUG LWIP_DBG_OFF 176 177 /** 178 * SOCKETS_DEBUG: Enable debugging in sockets.c. 179 */ 180 #define SOCKETS_DEBUG LWIP_DBG_OFF 181 182 /** 183 * ICMP_DEBUG: Enable debugging in icmp.c. 184 */ 185 #define ICMP_DEBUG LWIP_DBG_OFF 186 187 /** 188 * IGMP_DEBUG: Enable debugging in igmp.c. 189 */ 190 #define IGMP_DEBUG LWIP_DBG_OFF 191 192 /** 193 * INET_DEBUG: Enable debugging in inet.c. 194 */ 195 #define INET_DEBUG LWIP_DBG_OFF 196 197 /** 198 * IP_DEBUG: Enable debugging for IP. 199 */ 200 #define IP_DEBUG LWIP_DBG_OFF 201 202 /** 203 * IP_REASS_DEBUG: Enable debugging in ip_frag.c for both frag & reass. 204 */ 205 #define IP_REASS_DEBUG LWIP_DBG_OFF 206 /** 207 * RAW_DEBUG: Enable debugging in raw.c. 208 */ 209 #define RAW_DEBUG LWIP_DBG_OFF 210 211 /** 212 * MEM_DEBUG: Enable debugging in mem.c. 213 */ 214 #define MEM_DEBUG LWIP_DBG_OFF 215 216 /** 217 * MEMP_DEBUG: Enable debugging in memp.c. 218 */ 219 #define MEMP_DEBUG LWIP_DBG_OFF 220 221 /** 222 * SYS_DEBUG: Enable debugging in sys.c. 223 */ 224 #define SYS_DEBUG LWIP_DBG_OFF 225 226 /** 227 * TCP_DEBUG: Enable debugging for TCP. 228 */ 229 #define TCP_DEBUG LWIP_DBG_OFF 230 231 /** 232 * TCP_INPUT_DEBUG: Enable debugging in tcp_in.c for incoming debug. 233 */ 234 #define TCP_INPUT_DEBUG LWIP_DBG_OFF 235 /** 236 * TCP_FR_DEBUG: Enable debugging in tcp_in.c for fast retransmit. 237 */ 238 #define TCP_FR_DEBUG LWIP_DBG_OFF 239 240 /** 241 * TCP_RTO_DEBUG: Enable debugging in TCP for retransmit 242 * timeout. 243 */ 244 #define TCP_RTO_DEBUG LWIP_DBG_OFF 245 246 /** 247 * TCP_CWND_DEBUG: Enable debugging for TCP congestion window. 248 */ 249 #define TCP_CWND_DEBUG LWIP_DBG_OFF 250 251 /** 252 * TCP_WND_DEBUG: Enable debugging in tcp_in.c for window updating. 253 */ 254 #define TCP_WND_DEBUG LWIP_DBG_OFF 255 256 /** 257 * TCP_OUTPUT_DEBUG: Enable debugging in tcp_out.c output functions. 258 */ 259 #define TCP_OUTPUT_DEBUG LWIP_DBG_OFF 260 /** 261 * TCP_RST_DEBUG: Enable debugging for TCP with the RST message. 262 */ 263 #define TCP_RST_DEBUG LWIP_DBG_OFF 264 265 /** 266 * TCP_QLEN_DEBUG: Enable debugging for TCP queue lengths. 267 */ 268 #define TCP_QLEN_DEBUG LWIP_DBG_OFF 269 270 /** 271 * UDP_DEBUG: Enable debugging in UDP. 272 */ 273 #define UDP_DEBUG LWIP_DBG_OFF 274 275 /** 276 * TCPIP_DEBUG: Enable debugging in tcpip.c. 277 */ 278 #define TCPIP_DEBUG LWIP_DBG_OFF 279 280 /** 281 * PPP_DEBUG: Enable debugging for PPP. 282 */ 283 #define PPP_DEBUG LWIP_DBG_OFF 284 285 /** 286 * SLIP_DEBUG: Enable debugging in slipif.c. 287 */ 288 #define SLIP_DEBUG LWIP_DBG_OFF 289 290 /** 291 * DHCP_DEBUG: Enable debugging in dhcp.c. 292 */ 293 #define DHCP_DEBUG LWIP_DBG_OFF 294 295 /** 296 * AUTOIP_DEBUG: Enable debugging in autoip.c. 297 */ 298 #define AUTOIP_DEBUG LWIP_DBG_OFF 299 300 /** 301 * SNMP_MSG_DEBUG: Enable debugging for SNMP messages. 302 */ 303 #define SNMP_MSG_DEBUG LWIP_DBG_OFF 304 305 /** 306 * SNMP_MIB_DEBUG: Enable debugging for SNMP MIBs. 307 */ 308 #define SNMP_MIB_DEBUG LWIP_DBG_OFF 309 310 /** 311 * DNS_DEBUG: Enable debugging for DNS. 312 */ 313 #define DNS_DEBUG LWIP_DBG_OFF 314 #endif /* __LWIPOPTS_H__ */cc.h
4.在LWIP\include根目錄下新建一個lwipopts.h文件,內容如下:
1 #ifndef __LWIPOPTS_H__ 2 #define __LWIPOPTS_H__ 3 4 /** 5 * SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain 6 * critical regions during buffer allocation, deallocation and memory 7 * allocation and deallocation. 8 */ 9 #define SYS_LIGHTWEIGHT_PROT 0 10 11 /** 12 * NO_SYS==1: Provides VERY minimal functionality. Otherwise, 13 * use lwIP facilities. 14 */ 15 #define NO_SYS 1 // 無操作系統 16 /** 17 * NO_SYS_NO_TIMERS==1: Drop support for sys_timeout when NO_SYS==1 18 * Mainly for compatibility to old versions. 19 */ 20 #define NO_SYS_NO_TIMERS 1 // 這樣就不需要定義sys_now函數 21 22 /* ---------- Memory options ---------- */ 23 /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which 24 lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2 25 byte alignment -> define MEM_ALIGNMENT to 2. */ 26 #define MEM_ALIGNMENT 4 27 28 /* MEM_SIZE: the size of the heap memory. If the application will send 29 a lot of data that needs to be copied, this should be set high. */ 30 #define MEM_SIZE (5 * 1024) 31 32 /* MEMP_NUM_PBUF: the number of memp struct pbufs. If the application 33 sends a lot of data out of ROM (or other static memory), this 34 should be set high. */ 35 #define MEMP_NUM_PBUF 10 36 /* MEMP_NUM_UDP_PCB: the number of UDP protocol control blocks. One 37 per active UDP "connection". */ 38 #define MEMP_NUM_UDP_PCB 6 39 /* MEMP_NUM_TCP_PCB: the number of simulatenously active TCP 40 connections. */ 41 #define MEMP_NUM_TCP_PCB 10 42 /* MEMP_NUM_TCP_PCB_LISTEN: the number of listening TCP 43 connections. */ 44 #define MEMP_NUM_TCP_PCB_LISTEN 6 45 /* MEMP_NUM_TCP_SEG: the number of simultaneously queued TCP 46 segments. */ 47 #define MEMP_NUM_TCP_SEG 12 48 /* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active 49 timeouts. */ 50 #define MEMP_NUM_SYS_TIMEOUT 3 51 52 /* ---------- Pbuf options ---------- */ 53 /* PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */ 54 #define PBUF_POOL_SIZE 10 55 56 /* PBUF_POOL_BUFSIZE: the size of each pbuf in the pbuf pool. */ 57 #define PBUF_POOL_BUFSIZE 1500 58 59 /* ---------- TCP options ---------- */ 60 #define LWIP_TCP 1 61 #define TCP_TTL 255 62 63 /* Controls if TCP should queue segments that arrive out of 64 order. Define to 0 if your device is low on memory. */ 65 #define TCP_QUEUE_OOSEQ 0 66 67 /* TCP Maximum segment size. */ 68 #define TCP_MSS (1500 - 40) /* TCP_MSS = (Ethernet MTU - IP header size - TCP header size) */ 69 70 /* TCP sender buffer space (bytes). */ 71 #define TCP_SND_BUF (2 * TCP_MSS) 72 73 /* TCP sender buffer space (pbufs). This must be at least = 2 * 74 TCP_SND_BUF/TCP_MSS for things to work. */ 75 #define TCP_SND_QUEUELEN (6 * TCP_SND_BUF) / TCP_MSS 76 77 /* TCP receive window. */ 78 #define TCP_WND (2 * TCP_MSS) 79 80 /* ---------- ICMP options ---------- */ 81 #define LWIP_ICMP 1 82 83 /* ---------- DHCP options ---------- */ 84 /* Define LWIP_DHCP to 1 if you want DHCP configuration of 85 interfaces. DHCP is not implemented in lwIP 0.5.1, however, so 86 turning this on does currently not work. */ 87 #define LWIP_DHCP 0 88 89 /* ---------- UDP options ---------- */ 90 #define LWIP_UDP 1 91 #define UDP_TTL 255 92 93 /* ---------- Statistics options ---------- */ 94 #define LWIP_STATS 0 95 #define LWIP_PROVIDE_ERRNO 1 96 97 // LWIP_NETCONN==1: Enable Netconn API (require to use api_lib.c) 98 #define LWIP_NETCONN 0 99 100 // LWIP_SOCKET==1: Enable Socket API (require to use sockets.c) 101 #define LWIP_SOCKET 0 102 103 // 這個必須要定義, 否則在執行lwip_init函數時會在串口中提示: 104 // Assertion "Struct packing not implemented correctly. Check your lwIP port." failed at line 345 in Library\lwip\core\init.c 105 #define PACK_STRUCT_BEGIN __packed 106 /*******************************************************************************************************************/ 107 #define LWIP_DEBUG 1 108 #define LWIP_DBG_TYPES_ON LWIP_DBG_OFF 109 110 /** 111 * ETHARP_DEBUG: Enable debugging in etharp.c. 112 */ 113 #define ETHARP_DEBUG LWIP_DBG_OFF 114 115 /** 116 * NETIF_DEBUG: Enable debugging in netif.c. 117 */ 118 #define NETIF_DEBUG LWIP_DBG_OFF 119 120 /** 121 * PBUF_DEBUG: Enable debugging in pbuf.c. 122 */ 123 #define PBUF_DEBUG LWIP_DBG_OFF 124 125 /** 126 * API_LIB_DEBUG: Enable debugging in api_lib.c. 127 */ 128 #define API_LIB_DEBUG LWIP_DBG_OFF 129 130 /** 131 * API_MSG_DEBUG: Enable debugging in api_msg.c. 132 */ 133 #define API_MSG_DEBUG LWIP_DBG_OFF 134 135 /** 136 * SOCKETS_DEBUG: Enable debugging in sockets.c. 137 */ 138 #define SOCKETS_DEBUG LWIP_DBG_OFF 139 140 /** 141 * ICMP_DEBUG: Enable debugging in icmp.c. 142 */ 143 #define ICMP_DEBUG LWIP_DBG_OFF 144 145 /** 146 * IGMP_DEBUG: Enable debugging in igmp.c. 147 */ 148 #define IGMP_DEBUG LWIP_DBG_OFF 149 150 /** 151 * INET_DEBUG: Enable debugging in inet.c. 152 */ 153 #define INET_DEBUG LWIP_DBG_OFF 154 155 /** 156 * IP_DEBUG: Enable debugging for IP. 157 */ 158 #define IP_DEBUG LWIP_DBG_OFF 159 160 /** 161 * IP_REASS_DEBUG: Enable debugging in ip_frag.c for both frag & reass. 162 */ 163 #define IP_REASS_DEBUG LWIP_DBG_OFF 164 /** 165 * RAW_DEBUG: Enable debugging in raw.c. 166 */ 167 #define RAW_DEBUG LWIP_DBG_OFF 168 169 /** 170 * MEM_DEBUG: Enable debugging in mem.c. 171 */ 172 #define MEM_DEBUG LWIP_DBG_OFF 173 174 /** 175 * MEMP_DEBUG: Enable debugging in memp.c. 176 */ 177 #define MEMP_DEBUG LWIP_DBG_OFF 178 179 /** 180 * SYS_DEBUG: Enable debugging in sys.c. 181 */ 182 #define SYS_DEBUG LWIP_DBG_OFF 183 184 /** 185 * TCP_DEBUG: Enable debugging for TCP. 186 */ 187 #define TCP_DEBUG LWIP_DBG_OFF 188 189 /** 190 * TCP_INPUT_DEBUG: Enable debugging in tcp_in.c for incoming debug. 191 */ 192 #define TCP_INPUT_DEBUG LWIP_DBG_OFF 193 /** 194 * TCP_FR_DEBUG: Enable debugging in tcp_in.c for fast retransmit. 195 */ 196 #define TCP_FR_DEBUG LWIP_DBG_OFF 197 198 /** 199 * TCP_RTO_DEBUG: Enable debugging in TCP for retransmit 200 * timeout. 201 */ 202 #define TCP_RTO_DEBUG LWIP_DBG_OFF 203 204 /** 205 * TCP_CWND_DEBUG: Enable debugging for TCP congestion window. 206 */ 207 #define TCP_CWND_DEBUG LWIP_DBG_OFF 208 209 /** 210 * TCP_WND_DEBUG: Enable debugging in tcp_in.c for window updating. 211 */ 212 #define TCP_WND_DEBUG LWIP_DBG_OFF 213 214 /** 215 * TCP_OUTPUT_DEBUG: Enable debugging in tcp_out.c output functions. 216 */ 217 #define TCP_OUTPUT_DEBUG LWIP_DBG_OFF 218 /** 219 * TCP_RST_DEBUG: Enable debugging for TCP with the RST message. 220 */ 221 #define TCP_RST_DEBUG LWIP_DBG_OFF 222 223 /** 224 * TCP_QLEN_DEBUG: Enable debugging for TCP queue lengths. 225 */ 226 #define TCP_QLEN_DEBUG LWIP_DBG_OFF 227 228 /** 229 * UDP_DEBUG: Enable debugging in UDP. 230 */ 231 #define UDP_DEBUG LWIP_DBG_OFF 232 233 /** 234 * TCPIP_DEBUG: Enable debugging in tcpip.c. 235 */ 236 #define TCPIP_DEBUG LWIP_DBG_OFF 237 238 /** 239 * PPP_DEBUG: Enable debugging for PPP. 240 */ 241 #define PPP_DEBUG LWIP_DBG_OFF 242 243 /** 244 * SLIP_DEBUG: Enable debugging in slipif.c. 245 */ 246 #define SLIP_DEBUG LWIP_DBG_OFF 247 248 /** 249 * DHCP_DEBUG: Enable debugging in dhcp.c. 250 */ 251 #define DHCP_DEBUG LWIP_DBG_OFF 252 253 /** 254 * AUTOIP_DEBUG: Enable debugging in autoip.c. 255 */ 256 #define AUTOIP_DEBUG LWIP_DBG_OFF 257 258 /** 259 * SNMP_MSG_DEBUG: Enable debugging for SNMP messages. 260 */ 261 #define SNMP_MSG_DEBUG LWIP_DBG_OFF 262 263 /** 264 * SNMP_MIB_DEBUG: Enable debugging for SNMP MIBs. 265 */ 266 #define SNMP_MIB_DEBUG LWIP_DBG_OFF 267 268 /** 269 * DNS_DEBUG: Enable debugging for DNS. 270 */ 271 #define DNS_DEBUG LWIP_DBG_OFF 272 #endif /* __LWIPOPTS_H__ */lwipopt.h
5.修改\LWIP\netif目錄下的ethernetif.c文件,按中文提示修改:
1 /** 2 * @file 3 * Ethernet Interface Skeleton 4 * 5 */ 6 7 /* 8 * Copyright (c) 2001-2004