使用vivado 的sdk 建立自己的ps端程序-lwip-udp

1、说在前面的话

     本人在建立lwip工程项目的时候,跟着网上的各种各样的教程都是直接选择相关的模板直接进行修改然后修改程序的,这样的具体的操作过程如下:

FIle->New->Application Project , 然后通过选择下面的工程实例进行构建工程,其中会生成一个bsp 和一个你的工程的文件夹

 

本人希望能够在以又的lwip的协议栈的基础上,建立自己的工程项目。,那么建立的过程就有一定的不同:

1、FIle->New-> board support package , 然后选择standalone 点击完成。这个时候就可以添加自己的想要依赖地库了。

2、之后再选FIle->New->Application Project,然后模板中选择helloworld

然后就建立了一个自己的工程,内部的main 函数在hello world 中,你可以围绕main 建立自己想要实现的功能。

 

/*
 * main.c
 *
 *  Created on: 2020年1月16日
 *      Author: Scottar
 */


//--------------------------------------------------
//          blog.csdn.net/FPGADesigner
//          copyright by CUIT Qi Liu
//      Zynq Lwip UDP Communication Test Program
//--------------------------------------------------

#include "sleep.h"
#include "user_udp.h"
#include "sys_intr.h"
#include "lwip/inet.h"

extern unsigned udp_connected_flag;
static  XScuGic Intc;   //GIC
#define DEFAULT_IP_ADDRESS	"192.168.1.10"
#define DEFAULT_IP_MASK		"255.255.255.0"
#define DEFAULT_GW_ADDRESS	"192.168.1.1"
//
//#define DEFAULT_IP_ADDRESS	"115.156.163.175"
//#define DEFAULT_IP_MASK		"255.255.254.0"
//#define DEFAULT_GW_ADDRESS	"115.156.163.254"


static void assign_default_ip(ip_addr_t *ip, ip_addr_t *mask, ip_addr_t *gw)
{
	int err;

	xil_printf("Configuring default IP %s \r\n", DEFAULT_IP_ADDRESS);

	err = inet_aton(DEFAULT_IP_ADDRESS, ip);
	if (!err)
		xil_printf("Invalid default IP address: %d\r\n", err);

	err = inet_aton(DEFAULT_IP_MASK, mask);
	if (!err)
		xil_printf("Invalid default IP MASK: %d\r\n", err);

	err = inet_aton(DEFAULT_GW_ADDRESS, gw);
	if (!err)
		xil_printf("Invalid default gateway address: %d\r\n", err);
}

int main(void)
{
	struct netif *netif, server_netif;
	struct ip4_addr ipaddr, netmask, gw;

	/*  开发板MAC地址  */
	unsigned char mac_ethernet_address [] =
		{0x00, 0x0a, 0x35, 0x00, 0x01, 0x02};
	/*  开启中断系统  */
	Init_Intr_System(&Intc);
	Setup_Intr_Exception(&Intc);
	//定义一个网卡
	netif = &server_netif;
	/*两种方案实现对网卡ip、子网掩码、以及网关*/
	//方案1
	IP4_ADDR(&ipaddr,  192, 168,   1, 10);
	IP4_ADDR(&netmask, 255, 255, 255, 0);
	IP4_ADDR(&gw,      192, 168,   1, 1);
	//方案2
//	assign_default_ip(&(netif->ip_addr), &(netif->netmask), &(netif->gw));
	//初始化协议栈相关核心
	lwip_init();   //初始化lwIP库
	/* 添加网络接口并将其设置为默认接口 */
	if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, XPAR_XEMACPS_0_BASEADDR)) {
			xil_printf("Error adding N/W interface\r\n");
			return -1;
	}
	netif_set_default(netif);
	netif_set_up(netif);        //启动网络
	user_udp_init();            //初始化UDP

	while(1)
	{
		/*  将MAC队列中的包传输的LwIP/IP栈中   */
		xemacif_input(netif);

			sleep(0.1);
			udp_printf();

	}
	return 0;
}


 

/*
 * sys_intr.c
 *
 *  Created on: 2020年1月16日
 *      Author: Scottar
 */


#include "sys_intr.h"

//---------------------------------------------------------
//                    设置中断异常
//---------------------------------------------------------
void Setup_Intr_Exception(XScuGic * IntcInstancePtr)
{
	Xil_ExceptionInit();
	Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
			(Xil_ExceptionHandler)XScuGic_InterruptHandler,
			(void *)IntcInstancePtr);
	Xil_ExceptionEnable();
}

//---------------------------------------------------------
//                    初始化中断系统
//---------------------------------------------------------
int Init_Intr_System(XScuGic * IntcInstancePtr)
{
	int Status;

	XScuGic_Config *IntcConfig;
	IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
	if (NULL == IntcConfig) {
		return XST_FAILURE;
	}

	Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
					IntcConfig->CpuBaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	return XST_SUCCESS;
}

 

/*
 * sys_intr.h
 *
 *  Created on: 2020年1月16日
 *      Author: Scottar
 */


#ifndef SYS_INTR_H_
#define SYS_INTR_H_

#include "xparameters.h"
#include "xil_exception.h"
#include "xdebug.h"
#include "xscugic.h"

#define INTC_DEVICE_ID          XPAR_SCUGIC_SINGLE_DEVICE_ID

int Init_Intr_System(XScuGic * IntcInstancePtr);
void Setup_Intr_Exception(XScuGic * IntcInstancePtr);

#endif /* SYS_INTR_H_ */

 

#include "user_udp.h"

//---------------------------------------------------------
//                    变量定义
//---------------------------------------------------------
struct udp_pcb *connected_pcb = NULL;
static struct pbuf *pbuf_to_be_sent = NULL;
char send_buff[10] = "HelloWorld";  //待发送字符
struct ip4_addr ipaddr;

static unsigned local_port = 5002;      //本地端口
static unsigned remote_port = 8080;  //远程端口

//---------------------------------------------------------
//                  UDP连接初始化函数
//---------------------------------------------------------
int user_udp_init(void)
{
	struct udp_pcb *pcb;
	err_t err;

	/*  创建UDP控制块   */
	pcb = udp_new();
	if (!pcb) {
		xil_printf("Error Creating PCB.\r\n");
		return -1;
	}
	/*  绑定本地端口   */
	err = udp_bind(pcb, IP_ADDR_ANY, local_port);
	if (err != ERR_OK) {
		xil_printf("Unable to bind to port %d\r\n", local_port);
		return -2;
	}
	/*  设置远程地址   */
	IP4_ADDR(&ipaddr, 192, 168, 1, 100);
//	IP4_ADDR(&ipaddr, 192, 168, 1, 101);
//	IP4_ADDR(&ipaddr, 115, 156, 162, 123);
//	IP4_ADDR(&ipaddr, 115, 156, 162, 76);
	connected_pcb = pcb;

	/*  申请pbuf资源  */
	pbuf_to_be_sent = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_ROM);
	memset(pbuf_to_be_sent->payload, 0, 10);
	memcpy(pbuf_to_be_sent->payload, (u8 *)send_buff, 10);

	return 0;
}

//---------------------------------------------------------
//                   UDP发送数据函数
//---------------------------------------------------------
void udp_printf(void)
{
	err_t err;
	struct udp_pcb *tpcb = connected_pcb;
	if (!tpcb) {
		xil_printf("error connect.\r\n");
	}

	/*  发送字符串  */
	err = udp_sendto(tpcb, pbuf_to_be_sent, &ipaddr, remote_port);
	if (err != ERR_OK) {
//		xil_printf("Error on udp send : %d\r\n", err);
		return;
	}
}
/*
 * usr_udp.h
 *
 *  Created on: 2020年1月16日
 *      Author: Scottar
 */


#ifndef SRC_USER_UDP_H_
#define SRC_USER_UDP_H_

#include "lwip/err.h"
#include "lwip/udp.h"
#include "lwip/init.h"
#include "lwipopts.h"
#include "lwip/err.h"
#include "lwipopts.h"
#include "netif/xadapter.h"
#include "xil_printf.h"

int user_udp_init(void);
void udp_printf(void);

#endif /* SRC_USER_UDP_H_ */

 

 

参考:

https://www.xilinx.com/support/documentation/application_notes/xapp1026.pdf

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有水杯和雨伞的工科男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值