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 小时前
串口接收不到数据之电阻虚焊bug分析思路
单片机·bug
Z.Virgil3 小时前
【案例72】Apache检测到目标 URL 存在 http host 头攻击漏洞的解决方案
linux·服务器·网络协议·http·https·apache
wh_xia_jun4 小时前
ardunio超声波测距实验
单片机
清澈的爱524 小时前
STM32CubeIDE关于printf()串口输出重定向的问题
stm32·单片机·嵌入式硬件
活跃家族4 小时前
初识HTTP
网络·网络协议·http
大山很山4 小时前
关于单片机的【汇编指令系统】
汇编·单片机
蓝黑墨水9 小时前
F1C100S/F1C200S的资料来源说明
单片机
一只邪恶大泡11 小时前
HTTP与HTTPS的区别
网络协议·http·https
蜘蛛news11 小时前
快手怎么关闭ip地址 抖音ip属地如何隐藏
网络·网络协议·tcp/ip
千千道11 小时前
STM32 的 CAN 通讯全攻略
stm32·单片机·物联网