利用ZYNQ SOC快速開啟演算法驗證通路(6)——LWIP實現千兆TCP/IP網路傳輸
阿新 • • 發佈:2018-12-18
1 /******************************************************************************
2 *
3 * Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
30 *
31 ******************************************************************************/
32
33 #include <stdio.h>
34
35 #include "xparameters.h"
36
37 #include "netif/xadapter.h"
38
39 #include "platform.h"
40 #include "platform_config.h"
41 #if defined (__arm__) || defined(__aarch64__)
42 #include "xil_printf.h"
43 #endif
44
45 #include "lwip/tcp.h"
46 #include "xil_cache.h"
47
48 #if LWIP_DHCP==1
49 #include "lwip/dhcp.h"
50 #endif
51
52 /* defined by each RAW mode application */
53 void print_app_header();
54 int start_application();
55 int transfer_data();
56 void tcp_fasttmr(void);
57 void tcp_slowtmr(void);
58
59 /* missing declaration in lwIP */
60 void lwip_init();
61
62 #if LWIP_DHCP==1
63 extern volatile int dhcp_timoutcntr;
64 err_t dhcp_start(struct netif *netif);
65 #endif
66
67 extern volatile int TcpFastTmrFlag;
68 extern volatile int TcpSlowTmrFlag;
69 static struct netif server_netif;
70 struct netif *echo_netif;
71
72 void
73 print_ip(char *msg, struct ip_addr *ip)
74 {
75 print(msg);
76 xil_printf("%d.%d.%d.%d\n\r", ip4_addr1(ip), ip4_addr2(ip),
77 ip4_addr3(ip), ip4_addr4(ip));
78 }
79
80 void
81 print_ip_settings(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr *gw)
82 {
83
84 print_ip("Board IP: ", ip);
85 print_ip("Netmask : ", mask);
86 print_ip("Gateway : ", gw);
87 }
88
89 #if defined (__arm__) && !defined (ARMR5)
90 #if XPAR_GIGE_PCS_PMA_SGMII_CORE_PRESENT == 1 || XPAR_GIGE_PCS_PMA_1000BASEX_CORE_PRESENT == 1
91 int ProgramSi5324(void);
92 int ProgramSfpPhy(void);
93 #endif
94 #endif
95
96 #ifdef XPS_BOARD_ZCU102
97 #ifdef XPAR_XIICPS_0_DEVICE_ID
98 int IicPhyReset(void);
99 #endif
100 #endif
101
102 int main()
103 {
104 struct ip_addr ipaddr, netmask, gw;
105
106 /* the mac address of the board. this should be unique per board */
107 unsigned char mac_ethernet_address[] =
108 { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };
109
110 echo_netif = &server_netif;
111 #if defined (__arm__) && !defined (ARMR5)
112 #if XPAR_GIGE_PCS_PMA_SGMII_CORE_PRESENT == 1 || XPAR_GIGE_PCS_PMA_1000BASEX_CORE_PRESENT == 1
113 ProgramSi5324();
114 ProgramSfpPhy();
115 #endif
116 #endif
117
118 /* Define this board specific macro in order perform PHY reset on ZCU102 */
119 #ifdef XPS_BOARD_ZCU102
120 IicPhyReset();
121 #endif
122
123 init_platform();
124
125 #if LWIP_DHCP==1
126 ipaddr.addr = 0;
127 gw.addr = 0;
128 netmask.addr = 0;
129 #else
130 /* initliaze IP addresses to be used */
131 IP4_ADDR(&ipaddr, 192, 168, 1, 10);
132 IP4_ADDR(&netmask, 255, 255, 255, 0);
133 IP4_ADDR(&gw, 192, 168, 1, 1);
134 #endif
135 print_app_header();
136
137 lwip_init();//網路引數初始化
138
139 /* Add network interface to the netif_list, and set it as default */
140 if (!xemac_add(echo_netif, &ipaddr, &netmask,
141 &gw, mac_ethernet_address,
142 PLATFORM_EMAC_BASEADDR)) {
143 xil_printf("Error adding N/W interface\n\r");
144 return -1;
145 }
146 netif_set_default(echo_netif);
147
148 /* now enable interrupts */
149 platform_enable_interrupts();
150
151 /* specify that the network if is up */
152 netif_set_up(echo_netif);
153
154 #if (LWIP_DHCP==1)
155 /* Create a new DHCP client for this interface.
156 * Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
157 * the predefined regular intervals after starting the client.
158 */
159 dhcp_start(echo_netif);
160 dhcp_timoutcntr = 24;
161
162 while(((echo_netif->ip_addr.addr) == 0) && (dhcp_timoutcntr > 0))
163 xemacif_input(echo_netif);
164
165 if (dhcp_timoutcntr <= 0) {
166 if ((echo_netif->ip_addr.addr) == 0) {
167 xil_printf("DHCP Timeout\r\n");
168 xil_printf("Configuring default IP of 192.168.1.10\r\n");
169 IP4_ADDR(&(echo_netif->ip_addr), 192, 168, 1, 10);
170 IP4_ADDR(&(echo_netif->netmask), 255, 255, 255, 0);
171 IP4_ADDR(&(echo_netif->gw), 192, 168, 1, 1);
172 }
173 }
174
175 ipaddr.addr = echo_netif->ip_addr.addr;
176 gw.addr = echo_netif->gw.addr;
177 netmask.addr = echo_netif->netmask.addr;
178 #endif
179
180 print_ip_settings(&ipaddr, &netmask, &gw);//列印關鍵網路引數
181
182 /* start the application (web server, rxtest, txtest, etc..) */
183 start_application();//設定回撥函式,這些函式在特定事件發生時以函式指標的方式被呼叫
184
185 /* receive and process packets */
186 while (1) {
187 if (TcpFastTmrFlag) {//傳送處理,如差錯重傳,通過定時器置位標誌位
188 tcp_fasttmr();
189 TcpFastTmrFlag = 0;
190 }
191 if (TcpSlowTmrFlag) {
192 tcp_slowtmr();
193 TcpSlowTmrFlag = 0;
194 }
195 xemacif_input(echo_netif);//連續接收資料包,並將資料包存入LWIP
196 transfer_data();//空函式
197 }
198
199 /* never reached */
200 cleanup_platform();
201
202 return 0;
203 }