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;
}
相关推荐
IT B业生8 小时前
51单片机教程(六)- LED流水灯
单片机·嵌入式硬件·51单片机
一枝小雨8 小时前
51单片机学习心得2(基于STC89C52):串口通信(UART)
单片机·嵌入式硬件·51单片机
点点滴滴的记录8 小时前
RPC核心实现原理
网络·网络协议·rpc
IT B业生9 小时前
51单片机教程(一)- 开发环境搭建
单片机·嵌入式硬件·51单片机
程思扬10 小时前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
海绵波波10711 小时前
Webserver(4.8)UDP、广播、组播
单片机·网络协议·udp
好想有猫猫11 小时前
【51单片机】串口通信原理 + 使用
c语言·单片机·嵌入式硬件·51单片机·1024程序员节
stm 学习ing12 小时前
C语言 循环高级
c语言·开发语言·单片机·嵌入式硬件·算法·嵌入式实时数据库
橘色的喵13 小时前
Linux编程:DMA增加UDP 数据传输吞吐量并降低延迟
linux·udp·dma·网络驱动·低延迟·吞吐量·nic
w微信1501350781213 小时前
小华一级 代理商 HC32F005C6PA-TSSOP20 HC32F005系列
c语言·arm开发·单片机·嵌入式硬件