【FRDM-MCXN947】移植lwip及ping测试
<p><span style="font-size:16px;">测试</span>开发板<span style="font-size:16px;">以太网,移植lwip并ping测试,参考SDK包里的例程进行移植。</span></p><p> </p>
<p><span style="font-size:16px;"><strong>一、硬件部分</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">开发板以太网接口部分电路图</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><strong><span style="font-size:16px;">二、配置接口</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">配置以太网对应的引脚</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;"><strong>三、下载源码</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">下载lwip2.1.3版本</span></p>
<p><span style="font-size:16px;">下载地址:<a href="http://download.savannah.nongnu.org/releases/lwip/" target="_blank">http://download.savannah.nongnu.org/releases/lwip/</a></span></p>
<p></p>
<p> </p>
<p><span style="font-size:16px;"><strong>四、添加源码到工程</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">复制SDK工程中的部分源码和LWIP源码到工程目录下middleware文件中,并添加文件到工程。</span></p>
<p></p>
<p> </p>
<p><strong><span style="font-size:16px;">五、程序部分</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">5.1、net_ping.c</span></p>
<pre>
<code>#include "main.h"
/* IP address configuration. */
#ifndef configIP_ADDR0
#define configIP_ADDR0 192
#endif
#ifndef configIP_ADDR1
#define configIP_ADDR1 168
#endif
#ifndef configIP_ADDR2
#define configIP_ADDR2 1
#endif
#ifndef configIP_ADDR3
#define configIP_ADDR3 102
#endif
/* Netmask configuration. */
#ifndef configNET_MASK0
#define configNET_MASK0 255
#endif
#ifndef configNET_MASK1
#define configNET_MASK1 255
#endif
#ifndef configNET_MASK2
#define configNET_MASK2 255
#endif
#ifndef configNET_MASK3
#define configNET_MASK3 0
#endif
/* Gateway address configuration. */
#ifndef configGW_ADDR0
#define configGW_ADDR0 192
#endif
#ifndef configGW_ADDR1
#define configGW_ADDR1 168
#endif
#ifndef configGW_ADDR2
#define configGW_ADDR2 1
#endif
#ifndef configGW_ADDR3
#define configGW_ADDR3 1
#endif
/* Ethernet configuration. */
extern phy_lan8741_resource_t g_phy_resource;
#define EXAMPLE_ENET_BASE ENET0
#define EXAMPLE_PHY_ADDRESSBOARD_ENET0_PHY_ADDRESS
#define EXAMPLE_PHY_OPS &phylan8741_ops
#define EXAMPLE_PHY_RESOURCE &g_phy_resource
#define EXAMPLE_CLOCK_FREQ (50000000U)
/* Must be after include of app.h */
#ifndef configMAC_ADDR
#include "fsl_silicon_id.h"
#endif
#ifndef EXAMPLE_NETIF_INIT_FN
/*! @brief Network interface initialization function. */
#define EXAMPLE_NETIF_INIT_FN ethernetif0_init
#endif /* EXAMPLE_NETIF_INIT_FN */
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
phy_lan8741_resource_t g_phy_resource;
static phy_handle_t phyHandle;
/*******************************************************************************
* Code
******************************************************************************/
static void MDIO_Init(void)
{
(void)CLOCK_EnableClock(s_enetClock);
ENET_SetSMI(EXAMPLE_ENET_BASE, CLOCK_GetCoreSysClkFreq());
}
static status_t MDIO_Write(uint8_t phyAddr, uint8_t regAddr, uint16_t data)
{
return ENET_MDIOWrite(EXAMPLE_ENET_BASE, phyAddr, regAddr, data);
}
static status_t MDIO_Read(uint8_t phyAddr, uint8_t regAddr, uint16_t *pData)
{
return ENET_MDIORead(EXAMPLE_ENET_BASE, phyAddr, regAddr, pData);
}
void init_net(void)
{
struct netif netif;
ip4_addr_t netif_ipaddr, netif_netmask, netif_gw;
ethernetif_config_t enet_config =
{
.phyHandle = &phyHandle,
.phyAddr = EXAMPLE_PHY_ADDRESS,
.phyOps = EXAMPLE_PHY_OPS,
.phyResource = EXAMPLE_PHY_RESOURCE,
#ifdef configMAC_ADDR
.macAddress = configMAC_ADDR
#endif
};
CLOCK_AttachClk(MUX_A(CM_ENETRMIICLKSEL, 0));
CLOCK_EnableClock(kCLOCK_Enet);
SYSCON0->PRESETCTRL2 = SYSCON_PRESETCTRL2_ENET_RST_MASK;
SYSCON0->PRESETCTRL2 &= ~SYSCON_PRESETCTRL2_ENET_RST_MASK;
MDIO_Init();
g_phy_resource.read= MDIO_Read;
g_phy_resource.write = MDIO_Write;
time_init();
/* Set MAC address. */
#ifndef configMAC_ADDR
(void)SILICONID_ConvertToMacAddr(&enet_config.macAddress);
#endif
/* Get clock after hardware init. */
enet_config.srcClockHz = EXAMPLE_CLOCK_FREQ;
IP4_ADDR(&netif_ipaddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3);
IP4_ADDR(&netif_netmask, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3);
IP4_ADDR(&netif_gw, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3);
lwip_init();
netif_add(&netif, &netif_ipaddr, &netif_netmask, &netif_gw, &enet_config, EXAMPLE_NETIF_INIT_FN, ethernet_input);
netif_set_default(&netif);
netif_set_up(&netif);
while (ethernetif_wait_linkup(&netif, 5000) != ERR_OK)
{
PRINTF("PHY Auto-negotiation failed. Please check the cable connection and link partner setting.\r\n");
}
ping_init(&netif_gw);
PRINTF("\r\n************************************************\r\n");
PRINTF(" PING example\r\n");
PRINTF("************************************************\r\n");
PRINTF(" IPv4 Address : %u.%u.%u.%u\r\n", ((u8_t *)&netif_ipaddr), ((u8_t *)&netif_ipaddr),
((u8_t *)&netif_ipaddr), ((u8_t *)&netif_ipaddr));
PRINTF(" IPv4 Subnet mask : %u.%u.%u.%u\r\n", ((u8_t *)&netif_netmask), ((u8_t *)&netif_netmask),
((u8_t *)&netif_netmask), ((u8_t *)&netif_netmask));
PRINTF(" IPv4 Gateway : %u.%u.%u.%u\r\n", ((u8_t *)&netif_gw), ((u8_t *)&netif_gw),
((u8_t *)&netif_gw), ((u8_t *)&netif_gw));
PRINTF("************************************************\r\n");
while (1)
{
/* Poll the driver, get any outstanding frames */
ethernetif_input(&netif);
sys_check_timeouts(); /* Handle all system timeouts for all core protocols */
}
}
</code></pre>
<p> </p>
<p><span style="font-size:16px;">5.2、net_ping.h</span></p>
<pre>
<code>#ifndef __NET_PING_H
#define __NET_PING_H
void init_net(void);
#endif
</code></pre>
<p><span style="font-size:16px;">5.3、main.c</span></p>
<pre>
<code>#include "main.h"
int main(void)
{
BOARD_InitDEBUG_UARTPins();
BOARD_PowerMode_OD();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
SysTick_Init();
init_led();
init_key();
init_net();
while (1)
{
}
}
</code></pre>
<p> </p>
<p><strong><span style="font-size:16px;">六、运行结果</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">6.1、下载程序后,复位开发板,连接网线,运行后串口输出</span></p>
<p> </p>
<p></p>
<p> </p>
<p><span style="font-size:16px;">6.2、ping网络</span></p>
<p></p>
<p> </p>
<p><span style="font-size:16px;"><strong>七、附件</strong></span></p>
<p> </p>
<div><span style="font-size:16px;">程序源码:</span></div>
<p>测试开发板以太网,移植lwip并ping测试,参考SDK包里的例程进行移植,不错,过程和结果交代的都很清楚</p>
页:
[1]