zynqmp zcu102 arm 裸机UDP

sdk2017没有udp例程只有tcp例程。

以下是调试通的udp例程:

1.创建lwip测试工程或者在现有工程添加lwip库。

2.添加udp代码,也可以从tcp例程修改:


int udpinit()
{
	struct ip_addr ipaddr, netmask, gw;

	/* the mac address of the board. this should be unique per board */
	unsigned char mac_ethernet_address[] =
	{ 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };

	echo_netif = &server_netif;
#if defined (__arm__) && !defined (ARMR5)
#if XPAR_GIGE_PCS_PMA_SGMII_CORE_PRESENT == 1 || XPAR_GIGE_PCS_PMA_1000BASEX_CORE_PRESENT == 1
	ProgramSi5324();
	ProgramSfpPhy();
#endif
#endif

/* Define this board specific macro in order perform PHY reset on ZCU102 */
#ifdef XPS_BOARD_ZCU102
	IicPhyReset();
#endif

	//init_platform();

#if LWIP_DHCP==1
    ipaddr.addr = 0;
	gw.addr = 0;
	netmask.addr = 0;
#else
	/* initliaze IP addresses to be used */
	IP4_ADDR(&ipaddr,  192, 168,   1, 111);
	IP4_ADDR(&netmask, 255, 255, 255,  0);
	IP4_ADDR(&gw,      192, 168,   1,  1);
#endif
	xil_printf("lwip init\r\n");

	lwip_init();

  	/* Add network interface to the netif_list, and set it as default */
	xil_printf("net interface 1:\r\n");
	if (!xemac_add(echo_netif, &ipaddr, &netmask,
						&gw, mac_ethernet_address,
						PLATFORM_EMAC_BASEADDR)) {
		xil_printf("Error!\n\r");
		return -1;
	}

//	xil_printf("net interface 2:\r\n");
//	if (!xemac_add(echo_netif, &ipaddr, &netmask,
//							&gw, mac_ethernet_address,
//							XPAR_XEMACPS_1_BASEADDR)) {
//		xil_printf("Error!\n\r");
//			//return -1;
//	}

	netif_set_default(echo_netif);

	/* now enable interrupts */
	platform_enable_interrupts();

	/* specify that the network if is up */
	netif_set_up(echo_netif);

#if (LWIP_DHCP==1)
	/* Create a new DHCP client for this interface.
	 * Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
	 * the predefined regular intervals after starting the client.
	 */
	dhcp_start(echo_netif);
	dhcp_timoutcntr = 24;

	while(((echo_netif->ip_addr.addr) == 0) && (dhcp_timoutcntr > 0))
		xemacif_input(echo_netif);

	if (dhcp_timoutcntr <= 0) {
		if ((echo_netif->ip_addr.addr) == 0) {
			xil_printf("DHCP Timeout\r\n");
			xil_printf("Configuring default IP of 192.168.1.111\r\n");
			IP4_ADDR(&(echo_netif->ip_addr),  192, 168,   1, 111);
			IP4_ADDR(&(echo_netif->netmask), 255, 255, 255,  0);
			IP4_ADDR(&(echo_netif->gw),      192, 168,   1,  1);
		}
	}

	ipaddr.addr = echo_netif->ip_addr.addr;
	gw.addr = echo_netif->gw.addr;
	netmask.addr = echo_netif->netmask.addr;
#endif

	print_ip_settings(&ipaddr, &netmask, &gw);

	/* start the application (web server, rxtest, txtest, etc..) */
	start_application();
	g_init = 1;
	/* receive and process packets */
	int i = 0;
	char buf [50];
	//while (1) {

	sprintf(buf, "%d", i++);

	//	transfer_data(buf);
	//	sleep(1);
	//}
  

	return 0;
}

/*必须调用*/
void rcvLoop(void)
{
	if(g_init > 0)
	{
		xemacif_input(echo_netif);
	}
}



struct udp_pcb *pUdp = NULL;
struct pbuf *pbuf = NULL;
static int g_init = 0;

int transfer_data(char *p)
{
	err_t ret;

	if(0 == g_init)
	{
		return -1;
	}

	pbuf = pbuf_alloc(PBUF_TRANSPORT, strlen(p), PBUF_POOL);//PBUF_REF
	strcpy(pbuf->payload, p);

	ret = udp_send(pUdp, pbuf);
	if (ret != ERR_OK) {
		//xil_printf("udp_send error\r\n");
	}

	pbuf_free(pbuf);

	return 0;
}

int start_application()
{
	err_t err;
	unsigned port = 8080;

	//start_application
	struct ip_addr remote_addr;
	pUdp = udp_new();

#if 0
	pUdp->local_port = port;
	err = inet_aton("192.168.1.111", &pUdp->local_ip);
	if (!err) {
		xil_printf("Invalid Server IP address: %d\r\n", err);
		return;
	}

	pUdp->remote_port = port;
	err = inet_aton("192.168.1.100", &pUdp->remote_ip);
	if (!err) {
		xil_printf("Invalid Server IP address: %d\r\n", err);
		return;
	}

	err = udp_bind(pUdp, IP_ADDR_ANY, pUdp->local_port);
	if (err != ERR_OK) {
		xil_printf("Unable to bind to port %d: err = %d\n\r", pUdp->local_port, err);
		return -2;
	}
#endif

	IP4_ADDR(&remote_addr,  192, 168, 1, 100);
	xil_printf("remote_addr : 192, 168, 1, 100\r\n");
	xil_printf("remote port : %d\r\n", port);

	err = udp_connect(pUdp, &remote_addr, port);
	if (err != ERR_OK) {
		xil_printf("udp_client: Error on udp_connect: %d\r\n", err);
		udp_remove(pUdp);
		return;
	}

	g_init = 1;

#if 0
	
	struct tcp_pcb *pcb;
	/* create new TCP PCB structure */
	pcb = tcp_new();
	if (!pcb) {
		xil_printf("Error creating PCB. Out of Memory\n\r");
		return -1;
	}

	/* bind to specified @port */
	err = tcp_bind(pcb, IP_ADDR_ANY, port);
	if (err != ERR_OK) {
		xil_printf("Unable to bind to port %d: err = %d\n\r", port, err);
		return -2;
	}

	/* we do not need any arguments to callback functions */
	tcp_arg(pcb, NULL);

	/* listen for connections */
	pcb = tcp_listen(pcb);
	if (!pcb) {
		xil_printf("Out of memory while tcp_listen\n\r");
		return -3;
	}

	/* specify callback to use for incoming connections */
	tcp_accept(pcb, accept_callback);

	xil_printf("TCP echo server started @ port %d\n\r", port);
#endif
	return 0;
}

3.只测试了发送接口,接收没有测试。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值