ZYNQ裸机PS端UDP

c 复制代码
#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
#define DMA_IRQ_ID  			61U



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

#endif /* SYS_INTR_H_ */
c 复制代码
#include "sys_intr.h"
#include "user_udp.h"

volatile unsigned dma_ddr_addr_flag = 0;  //dma读区域标志
//---------------------------------------------------------
//                    设置中断异常
//---------------------------------------------------------
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 *	GIC_ConfigPtr	;
	GIC_ConfigPtr = XScuGic_LookupConfig(INTC_DEVICE_ID);
	if (NULL == GIC_ConfigPtr) {
		print("XScuGic_LookupConfig fail\n");
		return XST_FAILURE;
	}

	Status = XScuGic_CfgInitialize(IntcInstancePtr, GIC_ConfigPtr,
			GIC_ConfigPtr->CpuBaseAddress);
	if (Status != XST_SUCCESS) {
		print("XScuGic_CfgInitialize fail\n");
		return XST_FAILURE;
	}

	Status = XScuGic_Connect(IntcInstancePtr, DMA_IRQ_ID,
			   (Xil_ExceptionHandler)DMAirqHandler,
			   (void *)IntcInstancePtr);
	if(Status != XST_SUCCESS){
		print("XScuGic_Connect fail\n");
		return Status;
	}
	Setup_Intr_Exception(IntcInstancePtr);
	XScuGic_SetPriorityTriggerType(IntcInstancePtr, DMA_IRQ_ID,
						0xA0, 0x03);
	XScuGic_Enable(IntcInstancePtr, DMA_IRQ_ID);
	return Status;
}

void DMAirqHandler(void *CallbackRef){
	XTime tEnd,tCur;
	u32 tUsed = 0;
    XTime_GetTime(&tCur);
	if(!dma_ddr_addr_flag){
//		Xil_DCacheInvalidateRange((u32 *)ADC_DMA_ADDR,SEND_LEN*SEND_CNT);
//		adc_udp_send((u8 *)ADC_DMA_ADDR,SEND_CNT,SEND_LEN);
	}
	else{
//		Xil_DCacheInvalidateRange((u32 *)(ADC_DMA_ADDR + SEND_CNT*SEND_LEN),SEND_LEN*SEND_CNT);
//		adc_udp_send((u8 *)(ADC_DMA_ADDR + SEND_CNT*SEND_LEN),SEND_CNT,SEND_LEN);
	}
	dma_ddr_addr_flag ^= 1;
    XTime_GetTime(&tEnd);
    tUsed = ((tEnd-tCur)*1000000)/(COUNTS_PER_SECOND);
    printf("UDP TX SPEED = %f Mbit/s\n send all time = %f ms \n send all data = %f MB\n",
    		((SEND_LEN * SEND_CNT * 8)/(tUsed/1000000.0))/(1024*1024),(tUsed/1000.0),(SEND_LEN * SEND_CNT)/(1024*1024.0));
}
c 复制代码
#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"
#include "xtime_l.h"
#include "sleep.h"

#define SEND_CNT         8192
#define SEND_LEN         256*4
#define ADC_DMA_ADDR     0x10000000

#define LOCAL_PORT   	7  //本地端口
#define REMOTE_PORT     8080 //远程端口

int user_udp_init(void);
void adc_udp_send(u8 * dma_addr,u32 send_cnt,u32 send_len);
void udp_recv_callback(void *arg, struct udp_pcb *tpcb,
           struct pbuf *p, struct ip_addr *addr, u16_t port);


#endif /* SRC_USER_UDP_H_ */
c 复制代码
#include "user_udp.h"

//---------------------------------------------------------
//                    变量定义
//---------------------------------------------------------
struct udp_pcb *connected_pcb = NULL;
static struct pbuf *pbuf_to_be_sent = NULL;
u32  udp_serial_num = 0;


volatile unsigned udp_connected_flag = 0;  //连接标志

//---------------------------------------------------------
//                  UDP连接初始化函数
//---------------------------------------------------------
int user_udp_init(void)
{
	struct udp_pcb *adc_pcb;
	ip_addr_t ipaddr;
	err_t err;
	udp_connected_flag = 0;

	/*  创建UDP控制块   */
	adc_pcb = udp_new();
	if (!pcb) {
		xil_printf("Error Creating PCB.\r\n");
		return -1;
	}
	/*  绑定本地端口   */
	err = udp_bind(adc_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, 183);
	err = udp_connect(adc_pcb, &ipaddr, REMOTE_PORT);
	if (err != ERR_OK) {
		xil_printf("Unable to connect remote port.\r\n");
		return -3;
	}
	else {
		xil_printf("Connected Success.\r\n");
		connected_pcb = adc_pcb;
		udp_connected_flag = 1;
	}
	udp_recv(adc_pcb,udp_recv_callback,NULL);
	return 0;
}

//---------------------------------------------------------
//                   UDP发送数据函数
//---------------------------------------------------------
void adc_udp_send(u8 * dma_addr,u32 send_cnt,u32 send_len){
	err_t err;
	for(int i=0;i<send_cnt;i++){
		usleep(4);
		struct udp_pcb *tpcb = connected_pcb;
		if (!tpcb) {
			xil_printf("error connect.\r\n");
		}
		/*  申请pbuf资源  */
		pbuf_to_be_sent = pbuf_alloc(PBUF_TRANSPORT, send_len + 4, PBUF_POOL);
		*(u32 *)(dma_addr + send_len * i - 4) = udp_serial_num;
		udp_serial_num = udp_serial_num + 1;
		memset(pbuf_to_be_sent->payload, 0, send_len + 4);  //初始化内存区域
		memcpy(pbuf_to_be_sent->payload, (u8 *)(dma_addr + send_len * i - 4), send_len + 4);  //将内存区域的值copy过来
		//pbuf_to_be_sent->payload = (u32 *)(dma_addr+send_len*i);
		/*  发送字符串  */
		err = udp_send(tpcb, pbuf_to_be_sent);
		if (err != ERR_OK) {
			xil_printf("Error on udp send : %d \r\n", err);
			pbuf_free(pbuf_to_be_sent);
			return;
		}
		pbuf_free(pbuf_to_be_sent);  //释放pbuf
	}
}

//---------------------------------------------------------
//                   UDP接收数据并根据数据来发送或者接收并读写文件
//---------------------------------------------------------

void udp_recv_callback(void *arg, struct udp_pcb *tpcb,
           struct pbuf *p_rx, struct ip_addr *addr, u16_t port){
	err_t err;
	char *pData;
	char buff[28];
	if(p_rx != NULL){
		pData = (char *)p_rx-payload;
		if(p_rx->len >= 7){
			if ((pData[0] == 'r') && (pData[1] == 'e')
			&&  (pData[2] == 'a') && (pData[2] == 'd')
			&&  (pData[3] == 'l') && (pData[4] == 'o')
		 	&&  (pData[3] == 'g')){

		 	}
		}
	}
}
c 复制代码
//--------------------------------------------------
//          blog.csdn.net/FPGADesigner
//          copyright by CUIT Qi Liu
//      Zynq Lwip UDP Communication Test Program
//--------------------------------------------------


#include "user_udp.h"
#include "sys_intr.h"


extern unsigned udp_connected_flag;
extern unsigned dma_ddr_addr_flag;

static  XScuGic Intc;   //GIC

int main(void)
{
	struct netif *netif, server_netif;
	ip_addr_t ipaddr, netmask, gw;
	/*  开发板MAC地址  */
	unsigned char mac_ethernet_address [] =
		{0x00, 0x0a, 0x35, 0x00, 0x01, 0x02};
	/*  开启中断系统  */
	Init_Intr_System(&Intc);
	netif = &server_netif;
	IP4_ADDR(&ipaddr,  192, 168,   1, 10);
	IP4_ADDR(&netmask, 255, 255, 255, 0);
	IP4_ADDR(&gw,      192, 168,   1, 1);

	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();            //初始化adcUDP
	user_tftp_init();

	while(1)
	{
		/*  将MAC队列中的包传输的LwIP/IP栈中   */
		xemacif_input(netif);
	}
	return 0;
}
相关推荐
森旺电子1 小时前
51单片机仿真摇号抽奖机源程序 12864液晶显示
单片机·嵌入式硬件·51单片机
_oP_i1 小时前
HTTP 请求Media typetext/plain application/json text/json区别
网络协议·http·json
OkeyProxy2 小时前
HTTP、HTTPS和SOCKS5代理協議
网络协议·https·云计算·代理服务器·海外ip代理
zquwei3 小时前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring
Rorsion3 小时前
各种电机原理介绍
单片机·嵌入式硬件
群联云防护小杜3 小时前
如何给负载均衡平台做好安全防御
运维·服务器·网络·网络协议·安全·负载均衡
ihengshuai3 小时前
HTTP协议及安全防范
网络协议·安全·http
善 .6 小时前
单片机的内存是指RAM还是ROM
单片机·嵌入式硬件
超级码农ProMax6 小时前
STM32——“SPI Flash”
stm32·单片机·嵌入式硬件
Asa3197 小时前
stm32点灯Hal库
stm32·单片机·嵌入式硬件