TI中文支持网
TI专业的中文技术问题搜集分享网站

tm4c1294ncpdt uip例程初始化问题

大家帮忙看看我的初始化有什么问题没,为什么设备断电重启后偶尔会出现无法ping通的情况,还有我这样修改MAC地址是对的吗?

修改MAC地址:

ROM_EMACAddrSet(EMAC0_BASE, 0, (uint8_t *)&sTempAddr);

uip_ethaddr.addr[0] = u8HostMACAddr[0];
uip_ethaddr.addr[1] = u8HostMACAddr[1];
uip_ethaddr.addr[2] = u8HostMACAddr[2]; 
uip_ethaddr.addr[3] = u8HostMACAddr[3];
uip_ethaddr.addr[4] = u8HostMACAddr[4];
uip_ethaddr.addr[5] = u8HostMACAddr[5];

初始化:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
{
    uip_ipaddr_t sIPAddr;
    staticstructuip_eth_addr sTempAddr;
    staticstructuip_eth_addr sTempAddr1;
    int32_t i32PeriodicTimer, i32ARPTimer;
    uint32_t i;//ui32User0, ui32User1;
    uint32_t ui32Temp, ui32PHYConfig;
    staticu8_t u8Test[3] = {0xff,0xff,0xff};                  // 建立新连接发送的测试数据(无效数据)
    // Run from the PLL at 120 MHz.
    ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                           SYSCTL_OSC_MAIN |
                                           SYSCTL_USE_PLL |
                                           SYSCTL_CFG_VCO_480), 120000000);
     
    // Initialize the UART, clear the terminal, and print banner.
    ui32PHYConfig = (EMAC_PHY_TYPE_INTERNAL | EMAC_PHY_INT_MDIX_EN |
                     EMAC_PHY_AN_100B_T_FULL_DUPLEX);
    PinoutSet(true,false);                            // 系统管脚初始化
    EEPROMInit();                                      // eeprom初始化
    Spi_Init();                                        // SSI管脚初始化
    /*
    MAP_FlashUserGet(&ui32User0, &ui32User1);
    if((ui32User0 == 0xffffffff) || (ui32User1 == 0xffffffff))
    {  
        ui32User0 = 0x00002058;
        ui32User1 = 0x00002058;
        MAP_FlashUserSet(ui32User0, ui32User1);    
    }
    
    sTempAddr.addr[0] = ((ui32User0 >> 0) & 0xff);
    sTempAddr.addr[1] = ((ui32User0 >> 8) & 0xff);
    sTempAddr.addr[2] = ((ui32User0 >> 16) & 0xff);
    sTempAddr.addr[3] = ((ui32User1 >> 0) & 0xff);
    sTempAddr.addr[4] = ((ui32User1 >> 8) & 0xff);
    sTempAddr.addr[5] = ((ui32User1 >> 16) & 0xff);
    */
    // MAC地址
    sTempAddr.addr[0] = u8HostMACAddr[0];
    sTempAddr.addr[1] = u8HostMACAddr[1];
    sTempAddr.addr[2] = u8HostMACAddr[2];
    sTempAddr.addr[3] = u8HostMACAddr[3];
    sTempAddr.addr[4] = u8HostMACAddr[4];
    sTempAddr.addr[5] = u8HostMACAddr[5];
    
    // 读取系统设置参数
    EEPROMRead((uint32_t *)ipSeting,0x0400,sizeof(ipSeting));
    // 若当前地址无有效数据,则写入初始数据
    //if ((ipSeting[0] != 0xff) && (ipSeting[1] != 0xff) && (ipSeting[2] != 0xff) && (ipSeting[3] != 0xff))
    if((ipSeting[0] == 0xff) && (ipSeting[1] == 0xff) && (ipSeting[2] == 0xff) && (ipSeting[3] == 0xff))
    {
        InitNetPara();                                                 // 网络参数初始化
        InitSysPara();                                                 // 初始化默认数据
        uip_setethaddr(sTempAddr);
        Sc16is752ipw_Uart_Init();                                      // Sc16is752ipw spi转串口初始化
        EEPROMProgram((uint32_t *)ipSeting,0x0400,sizeof(ipSeting));   // 写入EEPROM
    }
    else
    {     
        // 更新参数值
        /***********************IP,子网掩码,网关修改后本次应答不更新,下次操作时更新*****************/
        // 主机IP
        u8HostIpAddr[0] = ipSeting[39] | (((uint16_t)ipSeting[38]) << 8);     
        u8HostIpAddr[1] = ipSeting[37] | (((uint16_t)ipSeting[36]) << 8);
        
        // 子网掩码
        u8HostNetMask[0] = ipSeting[43] | (((uint16_t)ipSeting[42]) << 8);
        u8HostNetMask[1] = ipSeting[41] | (((uint16_t)ipSeting[40]) << 8);
        
        // 网关
        u8HostGetWay[0] = ipSeting[47] | (((uint16_t)ipSeting[46]) << 8);
        u8HostGetWay[1] = ipSeting[45] | (((uint16_t)ipSeting[44]) << 8);
        /*******************************************************************************************/
        
        /************************************下列参数实时更新***************************************/
        // MAC地址
        u8HostMACAddr[0] = ipSeting[28];
        u8HostMACAddr[1] = ipSeting[29];
        u8HostMACAddr[2] = ipSeting[30];
        u8HostMACAddr[3] = ipSeting[31];
        u8HostMACAddr[4] = ipSeting[32];
        u8HostMACAddr[5] = ipSeting[33];
        
        // 控制端主机IP
        u8CtrlIpAddr[0] = ipSeting[51] | (((uint16_t)ipSeting[50]) << 8);
        u8CtrlIpAddr[1] = ipSeting[49] | (((uint16_t)ipSeting[48]) << 8);
        
        // 串口1相关
        u32UartBaud[0] = ipSeting[56] | (((uint32_t)(ipSeting[57])) << 8)
            | (((uint32_t)(ipSeting[58])) << 16)| (((uint32_t)(ipSeting[59])) << 24);
        u8UartDataBit[0] = ipSeting[60];       
        u8UartStopBit[0] = ipSeting[62];
        u8UartParityBit[0] = ipSeting[61];
        
        // 串口2相关
        u32UartBaud[1] = ipSeting[64] | (((uint32_t)(ipSeting[65])) << 8)
            | (((uint32_t)(ipSeting[66])) << 16)| (((uint32_t)(ipSeting[67])) << 24);
        u8UartDataBit[1] = ipSeting[68];   
        u8UartStopBit[1] = ipSeting[70];   
        u8UartParityBit[1] = ipSeting[69];
        
        // 串口3相关
        u32UartBaud[2] = ipSeting[80] | (((uint32_t)(ipSeting[81])) << 8)
            | (((uint32_t)(ipSeting[82])) << 16)| (((uint32_t)(ipSeting[83])) << 24);
        u8UartDataBit[2] = ipSeting[84];   
        u8UartStopBit[2] = ipSeting[86];       
        u8UartParityBit[2] = ipSeting[85];
        
        // 485相关
        u32UartBaud[3] = ipSeting[72] | (((uint32_t)(ipSeting[73])) << 8)
            | (((uint32_t)(ipSeting[74])) << 16)| (((uint32_t)(ipSeting[75])) << 24);
        u8UartDataBit[3] = ipSeting[76];   
        u8UartStopBit[3] = ipSeting[78];   
        u8UartParityBit[3] = ipSeting[77]; 
        // MAC地址
        sTempAddr.addr[0] = u8HostMACAddr[0];
        sTempAddr.addr[1] = u8HostMACAddr[1];
        sTempAddr.addr[2] = u8HostMACAddr[2];
        sTempAddr.addr[3] = u8HostMACAddr[3];
        sTempAddr.addr[4] = u8HostMACAddr[4];
        sTempAddr.addr[5] = u8HostMACAddr[5];
        /*******************************************************************************************/
    }
    Delay_us(500);
    // Configure SysTick for a periodic interrupt.
    ROM_SysTickPeriodSet(ui32SysClock / SYSTICKHZ);
    ROM_SysTickEnable();
    ROM_SysTickIntEnable();
    // Enable and reset the Ethernet modules.
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_EMAC0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_EPHY0);
    ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_EMAC0);
    ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_EPHY0);
    // Wait for the MAC to be ready.
    while(!ROM_SysCtlPeripheralReady(SYSCTL_PERIPH_EMAC0))
    {
    }
    
    // Configure for use with the internal PHY.
    EMACPHYConfigSet(EMAC0_BASE, ui32PHYConfig);
    
    // Reset the MAC.
    ROM_EMACReset(EMAC0_BASE);
    // Initialize the MAC and set the DMA mode.
    ROM_EMACInit(EMAC0_BASE, ui32SysClock,
                 EMAC_BCONFIG_MIXED_BURST | EMAC_BCONFIG_PRIORITY_FIXED, 4, 4,
                 0);
    // Set MAC configuration options.
    ROM_EMACConfigSet(EMAC0_BASE,
                      (EMAC_CONFIG_FULL_DUPLEX | EMAC_CONFIG_CHECKSUM_OFFLOAD |
                       EMAC_CONFIG_7BYTE_PREAMBLE | EMAC_CONFIG_IF_GAP_96BITS |
                       EMAC_CONFIG_USE_MACADDR0 |
                       EMAC_CONFIG_SA_FROM_DESCRIPTOR |
                       EMAC_CONFIG_BO_LIMIT_1024),
                      (EMAC_MODE_RX_STORE_FORWARD |
                       EMAC_MODE_TX_STORE_FORWARD |
                       EMAC_MODE_TX_THRESHOLD_64_BYTES |
                       EMAC_MODE_RX_THRESHOLD_64_BYTES), 0);
    // Initialize the Ethernet DMA descriptors.
    InitDescriptors(EMAC0_BASE);
    EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
    EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
    EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
    EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
    EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
    EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
    Delay_us(5000);
    // Program the hardware with its MAC address (for filtering).
    MAP_EMACAddrSet(EMAC0_BASE, 0, (uint8_t *)&sTempAddr);
    Delay_us(5000);
    // Wait for the link to become active.
    while((EMACPHYRead(EMAC0_BASE, 0, EPHY_BMSR) & EPHY_BMSR_LINKSTAT) == 0)
    {
    }
    
    // Set MAC filtering options.  We receive all broadcast and multicast
    // packets along with those addressed specifically for us.
    ROM_EMACFrameFilterSet(EMAC0_BASE, (EMAC_FRMFILTER_SADDR |
                                        EMAC_FRMFILTER_PASS_MULTICAST |
                                        EMAC_FRMFILTER_PASS_NO_CTRL));
    // Clear any pending interrupts.
    ROM_EMACIntClear(EMAC0_BASE, EMACIntStatus(EMAC0_BASE,false));
    // Initialize the uIP TCP/IP stack.
    uip_init();
    // Set the local MAC address (for uIP).
    // EMACHashFilterSet(EMAC0_BASE,pui32HashHi1,pui32HashLo1);
    // EMACHashFilterGet(EMAC0_BASE,pui32HashLo,pui32HashHi);
       
    uip_ethaddr.addr[0] = u8HostMACAddr[0];
    uip_ethaddr.addr[1] = u8HostMACAddr[1];
    uip_ethaddr.addr[2] = u8HostMACAddr[2];    
    uip_ethaddr.addr[3] = u8HostMACAddr[3];
    uip_ethaddr.addr[4] = u8HostMACAddr[4];
    uip_ethaddr.addr[5] = u8HostMACAddr[5];
    
    uip_ipaddr(sIPAddr, (u8HostIpAddr[0] & 0x00ff),(u8HostIpAddr[0] >> 8), (u8HostIpAddr[1] & 0x00ff), (u8HostIpAddr[1] >> 8));
    uip_sethostaddr(sIPAddr);
    uip_ipaddr(sIPAddr, (u8HostGetWay[0] & 0x00ff),(u8HostGetWay[0] >> 8), (u8HostGetWay[1] & 0x00ff), (u8HostGetWay[1] >> 8));
    uip_setdraddr(sIPAddr);
    uip_ipaddr(sIPAddr, (u8HostNetMask[0] & 0x00ff),(u8HostNetMask[0] >> 8), (u8HostNetMask[1] & 0x00ff), (u8HostNetMask[1] >> 8));
    uip_setnetmask(sIPAddr);
    Rs485_Init();                          // 485初始化(必须放在读取系统参数后)
    Sc16is752ipw_Uart_Init();              // Sc16is752ipw spi转串口初始化
    // Uart_SetPara(ChannelB,u16UartBaud[2],GetUartDataBit(u8UartDataBit[2]),GetUartStopBit(u8UartStopBit[2]),GetUartParBit(u8UartParityBit[2]));
    // Sc16is752ipw_Uart_Enable();
    
    // Enable the Ethernet MAC transmitter and receiver.
    ROM_EMACTxEnable(EMAC0_BASE);
    ROM_EMACRxEnable(EMAC0_BASE);
    // Enable the Ethernet interrupt.
    ROM_IntEnable(INT_EMAC0);
    // Enable the Ethernet RX Packet interrupt source.
    ROM_EMACIntEnable(EMAC0_BASE, EMAC_INT_RECEIVE);
    // Mark the first receive descriptor as available to the DMA to start
    // the receive processing.
    g_psRxDescriptor[g_ui32RxDescIndex].ui32CtrlStatus |= DES0_RX_CTRL_OWN;
    myudp_init();
    udp_server_connected(UDP_SERVER_PORT,0);           // 用于UDP广播(未指定客户端端口)
    // Main Application Loop.
    i32PeriodicTimer = 0;
    i32ARPTimer = 0;
    
    uip_len = 1;
   
    while(1)
    {
        // Wait for an event to occur.  This can be either a System Tick event,
        // or an RX Packet event.
        while(!g_ui32Flags)
        {
        }
        // If SysTick, Clear the SysTick interrupt flag and increment the
        // timers.
        if(HWREGBITW(&g_ui32Flags, FLAG_SYSTICK) == 1)
        {
            HWREGBITW(&g_ui32Flags, FLAG_SYSTICK) = 0;
            i32PeriodicTimer += SYSTICKMS;
            i32ARPTimer += SYSTICKMS;
        }
        
        if(u8NetFlag)
        {
            HWREG(GPIO_PORTE_BASE + (GPIO_O_DATA + (GPIO_PIN_3 << 2))) = 0;
            u8NetFlag++;
            if(u8NetFlag >= 25)
            {
                u8NetFlag = 0;
                HWREG(GPIO_PORTE_BASE + (GPIO_O_DATA + (GPIO_PIN_3 << 2))) = GPIO_PIN_3;
            }
        }
        // 串口接收转发处理
        SmartUartPoll();
        SmartSpiUartPoll();
        if(1 == u8NetSendFlag)
        {  
            u8NetSendFlag = 0;
            PacketTransmit(EMAC0_BASE, uip_buf, uip_len);
            uip_len = 0;
        }
        elseif(2 == u8NetSendFlag)
        {
            Udp_SendData(u8Test,sizeof(u8Test));
            uip_arp_out();
            u8NetSendFlag = 0;
            PacketTransmit(EMAC0_BASE, uip_buf, uip_len);
            uip_len = 0;      
        }
        
        // Check for an RX Packet and read it.
        if(HWREGBITW(&g_ui32Flags, FLAG_RXPKT))
        {
            // Clear the RX Packet event flag.
            HWREGBITW(&g_ui32Flags, FLAG_RXPKT) = 0;
            // Get the packet and set uip_len for uIP stack usage.
            uip_len = (unsignedshort)PacketReceive(EMAC0_BASE, uip_buf,sizeof(g_pui8UIPBuffer));
            // Process incoming IP packets here.
            if(BUF->type == htons(UIP_ETHTYPE_IP))
            {
                uip_arp_ipin();    // 进行IP包Arp部分的处理(更新ARP缓冲表)
                uip_input();       // 处理缓存uip_buf中的IP数据
                
                // If the above function invocation resulted in data that
                // should be sent out on the network, the global variable
                // uip_len is set to a value > 0.
                if(uip_len > 0)
                {
                    uip_arp_out();
                    PacketTransmit(EMAC0_BASE, uip_buf, uip_len);
                    uip_len = 0;
                                        
                    /*************本次应答后更新网络相关参数**********************/
                    if(1 == u8UpdateParaFlag)
                    {                   
                        sTempAddr.addr[0] = u8HostMACAddr[0];
                        sTempAddr.addr[1] = u8HostMACAddr[1];
                        sTempAddr.addr[2] = u8HostMACAddr[2];
                        sTempAddr.addr[3] = u8HostMACAddr[3];
                        sTempAddr.addr[4] = u8HostMACAddr[4];
                        sTempAddr.addr[5] = u8HostMACAddr[5];
                        /*
                        ui32User0 = (uint32_t)(0x000000ff & u8HostMACAddr[0]) | (uint32_t)((0x000000ff & u8HostMACAddr[1]) << 8) |
                            (uint32_t)((0x000000ff & u8HostMACAddr[2]) << 16);
                        
                        ui32User1 = (uint32_t)(0x000000ff & u8HostMACAddr[3]) | (uint32_t)((0x000000ff & u8HostMACAddr[4]) << 8) |
                            (uint32_t)((0x000000ff & u8HostMACAddr[5]) << 16);
                        
                        FlashUserSet(ui32User0,ui32User1);
                        */
                                    /************************************下列参数实时更新***************************************/
            
                        if((uip_ethaddr.addr[0] != sTempAddr.addr[0]) || (uip_ethaddr.addr[1] != sTempAddr.addr[1]) ||
                            (uip_ethaddr.addr[0] != sTempAddr.addr[2]) || (uip_ethaddr.addr[1] != sTempAddr.addr[3]) ||
                            (uip_ethaddr.addr[0] != sTempAddr.addr[4]) || (uip_ethaddr.addr[1] != sTempAddr.addr[5]))
                        {
                            for(i = 0; i < 4; ++i)
                            {
                                arp_table[i].ipaddr[0] = 0;
                                arp_table[i].ipaddr[1] = 0;
                            }
                        }
                        
                        EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);  
                        EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
                        EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
                        EMACAddrGet(EMAC0_BASE,0,(uint8_t *)&sTempAddr1);
                        ROM_EMACAddrSet(EMAC0_BASE, 0, (uint8_t *)&sTempAddr);
                        Delay_us(500);
                        
                        uip_ethaddr.addr[0] = u8HostMACAddr[0];
                        uip_ethaddr.addr[1] = u8HostMACAddr[1];
                        uip_ethaddr.addr[2] = u8HostMACAddr[2];    
                        uip_ethaddr.addr[3] = u8HostMACAddr[3];
                        uip_ethaddr.addr[4] = u8HostMACAddr[4];
                        uip_ethaddr.addr[5] = u8HostMACAddr[5];
                        
                        uip_hostaddr[0] = u8HostIpAddr[0];
                        uip_hostaddr[1] = u8HostIpAddr[1];
                        
                        uip_netmask[0] = u8HostNetMask[0];
                        uip_netmask[1] = u8HostNetMask[1];
                        uip_draddr[0] = u8HostGetWay[0];
                        uip_draddr[1] = u8HostGetWay[1];
                        
                        // 将新数据写入EEPROM
                        EEPROMProgram((uint32_t *)ipSeting,0x0400,sizeof(ipSeting));
                        u8UpdateParaFlag = 0;                  // 清零
                    }
                    /*************本次应答后更新网络相关参数**********************/
                }
            }
            // Process incoming ARP packets here.
            elseif(BUF->type == htons(UIP_ETHTYPE_ARP))
            {
                // uip_arp_arpin判断ARP包的类型是响应包还是请求包,如果是响应包,那么更新ARP表。
                // 如果是请求包,那么发送ARP回复帧包括本机的IP地址和MAC地址
                uip_arp_arpin();
                // If the above function invocation resulted in data that
                // should be sent out on the network, the global variable
                // uip_len is set to a value > 0.
                if(uip_len > 0)
                {
                    PacketTransmit(EMAC0_BASE, uip_buf, uip_len);
                    uip_len = 0;
                }
            }
        }
        // Process TCP/IP Periodic Timer here.
        if(i32PeriodicTimer > UIP_PERIODIC_TIMER_MS)
        {
            i32PeriodicTimer = 0;
            for(ui32Temp = 0; ui32Temp < UIP_CONNS; ui32Temp++)
            {
                uip_periodic(ui32Temp);
                // If the above function invocation resulted in data that
                // should be sent out on the network, the global variable
                // uip_len is set to a value > 0.
                if(uip_len > 0)
                {
                    uip_arp_out();
                    PacketTransmit(EMAC0_BASE, uip_buf, uip_len);
                    uip_len = 0;
                }
            }
            #if UIP_UDP
            for(ui32Temp = 0; ui32Temp < UIP_UDP_CONNS; ui32Temp++)
            {
                uip_udp_periodic(ui32Temp);
                // If the above function invocation resulted in data that
                // should be sent out on the network, the global variable
                // uip_len is set to a value > 0.
                if(uip_len > 0)
                {
                    uip_arp_out();
                    PacketTransmit(EMAC0_BASE, uip_buf, uip_len);
                    uip_len = 0;
                }
            }
            #endif
        }
        // Process ARP Timer here.
        if(i32ARPTimer > UIP_ARP_TIMER_MS)
        {
            i32ARPTimer = 0;
            uip_arp_timer();
        }
    }
}

xyz549040622:

在某些初始化后面加延时试试,因为你这是偶尔出现的情况,所以不好判断。

赞(0)
未经允许不得转载:TI中文支持网 » tm4c1294ncpdt uip例程初始化问题
分享到: 更多 (0)