使用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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章