热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

利用ZYNQSOC快速打开算法验证通路(3)——PS端DMA缓存数据到PS端DDR

上篇该系列博文中讲述W5500接收到上位机传输的数据,此后需要将数据缓存起来。当数据量较大或者其他数据带宽较高的情况下,片上缓存(OCM)已无法满足需求,这时需要将大量数据保存在外

  上篇该系列博文中讲述W5500接收到上位机传输的数据,此后需要将数据缓存起来。当数据量较大或者其他数据带宽较高的情况下,片上缓存(OCM)已无法满足需求,这时需要将大量数据保存在外挂的DDR SDRAM中。

  最简单的方式是使用Xilinx的读写地址库函数Xil_In32()和Xil_Out32(),当然不仅支持32bit位宽,还包括8 16和64bit。但这种方式每次读写都要占用CPU,无法在读写的同时接收后续数据或者对之前的数据进一步处理,也就无法形成类似FPGA逻辑设计中的“流水线结构”,此时前段数据缓存过程中,后段数据会被丢弃。所以,需要利用PS端CPU子系统内的专用硬件DMA完成高速的批量数据搬移工作。

利用ZYNQ SOC快速打开算法验证通路(3)——PS端DMA缓存数据到PS端DDR

  在Xilinx SDK的system.mss页面下直接导入ps_dma示例工程。

  1 #include 
  2 #include 
  3 #include "sleep.h"
  4 #include "xparameters.h"
  5 #include "xil_types.h"
  6 #include "xil_assert.h"
  7 #include "xil_io.h"
  8 #include "xil_exception.h"
  9 #include "xil_cache.h"
 10 #include "xil_printf.h"
 11 #include "xscugic.h"
 12 #include "xdmaps.h"
 13 
 14 /************************** Constant Definitions *****************************/
 15 /*
 16  * The following constants map to the XPAR parameters created in the
 17  * xparameters.h file. They are defined here such that a user can easily
 18  * change all the needed parameters in one place.
 19  */
 20 #define DMA_DEVICE_ID             XPAR_XDMAPS_1_DEVICE_ID
 21 #define INTC_DEVICE_ID            XPAR_SCUGIC_SINGLE_DEVICE_ID
 22 
 23 #define DMA_DONE_INTR_0            XPAR_XDMAPS_0_DONE_INTR_0
 24 #define DMA_DONE_INTR_1            XPAR_XDMAPS_0_DONE_INTR_1
 25 #define DMA_DONE_INTR_2            XPAR_XDMAPS_0_DONE_INTR_2
 26 #define DMA_DONE_INTR_3            XPAR_XDMAPS_0_DONE_INTR_3
 27 #define DMA_DONE_INTR_4            XPAR_XDMAPS_0_DONE_INTR_4
 28 #define DMA_DONE_INTR_5            XPAR_XDMAPS_0_DONE_INTR_5
 29 #define DMA_DONE_INTR_6            XPAR_XDMAPS_0_DONE_INTR_6
 30 #define DMA_DONE_INTR_7            XPAR_XDMAPS_0_DONE_INTR_7
 31 #define DMA_FAULT_INTR            XPAR_XDMAPS_0_FAULT_INTR
 32 
 33 
 34 
 35 #define TEST_ROUNDS    1    /* Number of loops that the Dma transfers run.*/
 36 #define DMA_LENGTH    1024    /* Length of the Dma Transfers */
 37 #define TIMEOUT_LIMIT     0x2000    /* Loop count for timeout */
 38 
 39 /**************************** Type Definitions *******************************/
 40 
 41 
 42 /***************** Macros (Inline Functions) Definitions *********************/
 43 
 44 
 45 /************************** Function Prototypes ******************************/
 46 
 47 int XDmaPs_Example_W_Intr(XScuGic *GicPtr, u16 DeviceId);
 48 int SetupInterruptSystem(XScuGic *GicPtr, XDmaPs *DmaPtr);
 49 void DmaDoneHandler(unsigned int Channel, XDmaPs_Cmd *DmaCmd,
 50             void *CallbackRef);
 51 
 52 /************************** Macro Definitions *****************************/
 53 
 54 
 55 /************************** Variable Definitions *****************************/
 56 #ifdef __ICCARM__
 57 #pragma data_alignment=32
 58 static int Src[DMA_LENGTH];
 59 static int Dst[DMA_LENGTH];
 60 #pragma data_alignment=4
 61 #else
 62 static int Src[DMA_LENGTH] __attribute__ ((aligned (32)));
 63 static int Dst[DMA_LENGTH] __attribute__ ((aligned (32)));
 64 #endif
 65 
 66 XDmaPs DmaInstance;
 67 #ifndef TESTAPP_GEN
 68 XScuGic GicInstance;
 69 #endif
 70 
 71 /****************************************************************************/
 72 /**
 73 *
 74 * This is the main function for the DmaPs interrupt example.
 75 *
 76 * @param    None.
 77 *
 78 * @return    XST_SUCCESS to indicate success, otherwise XST_FAILURE.
 79 *
 80 * @note        None.
 81 *
 82 ****************************************************************************/
 83 #ifndef TESTAPP_GEN
 84 int main(void)
 85 {
 86     int Status;
 87 
 88     Status = XDmaPs_Example_W_Intr(&GicInstance,DMA_DEVICE_ID);
 89     if (Status != XST_SUCCESS) {
 90         xil_printf("Error: XDMaPs_Example_W_Intr failed\r\n");
 91         return XST_FAILURE;
 92     }
 93 
 94     xil_printf("XDMaPs_Example_W_Intr passed\r\n");
 95     return XST_SUCCESS;
 96 
 97 }
 98 #endif
 99 
100 
101 /*****************************************************************************/
102 /**
103  *
104  * Interrupt Example to test the DMA.
105  *
106  * @param    DeviceId is the Device ID of the DMA controller.
107  *
108  * @return    XST_SUCCESS to indicate success, otherwise XST_FAILURE.
109  *
110  * @note    None.
111  *
112  ****************************************************************************/
113 int XDmaPs_Example_W_Intr(XScuGic *GicPtr, u16 DeviceId)
114 {
115     int Index;
116     unsigned int Channel = 0;
117     int Status;
118     int TestStatus;
119     int TestRound;
120     int TimeOutCnt;
121     volatile int Checked[XDMAPS_CHANNELS_PER_DEV];
122     XDmaPs_Config *DmaCfg;
123     XDmaPs *DmaInst = &DmaInstance;
124     XDmaPs_Cmd DmaCmd;
125 
126     memset(&DmaCmd, 0, sizeof(XDmaPs_Cmd));
127 
128     DmaCmd.ChanCtrl.SrcBurstSize = 4;
129     DmaCmd.ChanCtrl.SrcBurstLen = 4;
130     DmaCmd.ChanCtrl.SrcInc = 1;
131     DmaCmd.ChanCtrl.DstBurstSize = 4;
132     DmaCmd.ChanCtrl.DstBurstLen = 4;
133     DmaCmd.ChanCtrl.DstInc = 1;
134     DmaCmd.BD.SrcAddr = (u32) Src;
135     DmaCmd.BD.DstAddr = (u32) Dst;
136     DmaCmd.BD.Length = DMA_LENGTH * sizeof(int);
137 
138 
139     /*
140      * Initialize the DMA Driver
141      */
142     DmaCfg = XDmaPs_LookupConfig(DeviceId);
143     if (DmaCfg == NULL) {
144         return XST_FAILURE;
145     }
146 
147     Status = XDmaPs_CfgInitialize(DmaInst,
148                    DmaCfg,
149                    DmaCfg->BaseAddress);
150     if (Status != XST_SUCCESS) {
151         return XST_FAILURE;
152     }
153 
154 
155     /*
156      * Setup the interrupt system.
157      */
158     Status = SetupInterruptSystem(GicPtr, DmaInst);
159     if (Status != XST_SUCCESS) {
160         return XST_FAILURE;
161     }
162 
163 
164     TestStatus = XST_SUCCESS;
165 
166     for (TestRound = 0; TestRound ) {
167         xil_printf("Test round %d\r\n", TestRound);
168         for (Channel = 0;
169              Channel < XDMAPS_CHANNELS_PER_DEV;
170              Channel++) {
171 
172 
173             /* Initialize source */
174             for (Index = 0; Index )
175                 Src[Index] = DMA_LENGTH - Index;
176 
177             /* Clear destination */
178             for (Index = 0; Index )
179                 Dst[Index] = 0;
180 
181             Checked[Channel] = 0;
182 
183             /* Set the Done interrupt handler */
184             XDmaPs_SetDoneHandler(DmaInst,
185                            Channel,
186                            DmaDoneHandler,
187                            (void *)Checked);
188 
189 
190             Status = XDmaPs_Start(DmaInst, Channel, &DmaCmd, 0);
191             if (Status != XST_SUCCESS) {
192                 return XST_FAILURE;
193             }
194 
195             TimeOutCnt = 0;
196 
197             /* Now the DMA is done */
198             while (!Checked[Channel]
199                    && TimeOutCnt < TIMEOUT_LIMIT) {
200                 TimeOutCnt++;
201             }
202 
203             if (TimeOutCnt >= TIMEOUT_LIMIT) {
204                 TestStatus = XST_FAILURE;
205             }
206 
207             if (Checked[Channel] <0) {
208                 /* DMA controller failed */
209                 TestStatus = XST_FAILURE;
210             }
211         }
212     }
213 
214     return TestStatus;
215 
216 }
217 
218 
219 /******************************************************************************/
220 /**
221  *
222  * This function connects the interrupt handler of the interrupt controller to
223  * the processor.  This function is seperate to allow it to be customized for
224  * each application. Each processor or RTOS may require unique processing to
225  * connect the interrupt handler.
226  *
227  * @param    GicPtr is the GIC instance pointer.
228  * @param    DmaPtr is the DMA instance pointer.
229  *
230  * @return    None.
231  *
232  * @note    None.
233  *
234  ****************************************************************************/
235 int SetupInterruptSystem(XScuGic *GicPtr, XDmaPs *DmaPtr)
236 {
237     int Status;
238 #ifndef TESTAPP_GEN    
239     XScuGic_Config *GicConfig;
240 
241 
242     Xil_ExceptionInit();
243 
244     /*
245      * Initialize the interrupt controller driver so that it is ready to
246      * use.
247      */
248     GicCOnfig= XScuGic_LookupConfig(INTC_DEVICE_ID);
249     if (NULL == GicConfig) {
250         return XST_FAILURE;
251     }
252 
253     Status = XScuGic_CfgInitialize(GicPtr, GicConfig,
254                        GicConfig->CpuBaseAddress);
255     if (Status != XST_SUCCESS) {
256         return XST_FAILURE;
257     }
258 
259     /*
260      * Connect the interrupt controller interrupt handler to the hardware
261      * interrupt handling logic in the processor.
262      */
263     Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
264                  (Xil_ExceptionHandler)XScuGic_InterruptHandler,
265                  GicPtr);
266 #endif
267     /*
268      * Connect the device driver handlers that will be called when an interrupt
269      * for the device occurs, the device driver handler performs the specific
270      * interrupt processing for the device
271      */
272 
273     /*
274      * Connect the Fault ISR
275      */
276     Status = XScuGic_Connect(GicPtr,
277                  DMA_FAULT_INTR,
278                  (Xil_InterruptHandler)XDmaPs_FaultISR,
279                  (void *)DmaPtr);
280     if (Status != XST_SUCCESS) {
281         return XST_FAILURE;
282     }
283 
284     /*
285      * Connect the Done ISR for all 8 channels of DMA 0
286      */
287     Status = XScuGic_Connect(GicPtr,
288                  DMA_DONE_INTR_0,
289                  (Xil_InterruptHandler)XDmaPs_DoneISR_0,
290                  (void *)DmaPtr);
291     Status |= XScuGic_Connect(GicPtr,
292                  DMA_DONE_INTR_1,
293                  (Xil_InterruptHandler)XDmaPs_DoneISR_1,
294                  (void *)DmaPtr);
295     Status |= XScuGic_Connect(GicPtr,
296                  DMA_DONE_INTR_2,
297                  (Xil_InterruptHandler)XDmaPs_DoneISR_2,
298                  (void *)DmaPtr);
299     Status |= XScuGic_Connect(GicPtr,
300                  DMA_DONE_INTR_3,
301                  (Xil_InterruptHandler)XDmaPs_DoneISR_3,
302                  (void *)DmaPtr);
303     Status |= XScuGic_Connect(GicPtr,
304                  DMA_DONE_INTR_4,
305                  (Xil_InterruptHandler)XDmaPs_DoneISR_4,
306                  (void *)DmaPtr);
307     Status |= XScuGic_Connect(GicPtr,
308                  DMA_DONE_INTR_5,
309                  (Xil_InterruptHandler)XDmaPs_DoneISR_5,
310                  (void *)DmaPtr);
311     Status |= XScuGic_Connect(GicPtr,
312                  DMA_DONE_INTR_6,
313                  (Xil_InterruptHandler)XDmaPs_DoneISR_6,
314                  (void *)DmaPtr);
315     Status |= XScuGic_Connect(GicPtr,
316                  DMA_DONE_INTR_7,
317                  (Xil_InterruptHandler)XDmaPs_DoneISR_7,
318                  (void *)DmaPtr);
319 
320     if (Status != XST_SUCCESS)
321         return XST_FAILURE;
322 
323     /*
324      * Enable the interrupts for the device
325      */
326     XScuGic_Enable(GicPtr, DMA_DONE_INTR_0);
327     XScuGic_Enable(GicPtr, DMA_DONE_INTR_1);
328     XScuGic_Enable(GicPtr, DMA_DONE_INTR_2);
329     XScuGic_Enable(GicPtr, DMA_DONE_INTR_3);
330     XScuGic_Enable(GicPtr, DMA_DONE_INTR_4);
331     XScuGic_Enable(GicPtr, DMA_DONE_INTR_5);
332     XScuGic_Enable(GicPtr, DMA_DONE_INTR_6);
333     XScuGic_Enable(GicPtr, DMA_DONE_INTR_7);
334     XScuGic_Enable(GicPtr, DMA_FAULT_INTR);
335 
336     Xil_ExceptionEnable();
337 
338     return XST_SUCCESS;
339 
340 }
341 
342 
343 /*****************************************************************************/
344 /**
345 *
346 * DmaDoneHandler.
347 *
348 * @param    Channel is the Channel number.
349 * @param    DmaCmd is the Dma Command.
350 * @param    CallbackRef is the callback reference data.
351 *
352 * @return    None.
353 *
354 * @note        None.
355 *
356 ******************************************************************************/
357 void DmaDoneHandler(unsigned int Channel, XDmaPs_Cmd *DmaCmd, void *CallbackRef)
358 {
359 
360     /* done handler */
361     volatile int *Checked = (volatile int *)CallbackRef;
362     int Index;
363     int Status = 1;
364     int *Src;
365     int *Dst;
366 
367     Src = (int *)DmaCmd->BD.SrcAddr;
368     Dst = (int *)DmaCmd->BD.DstAddr;
369 
370     /* DMA successful */
371     /* compare the src and dst buffer */
372     for (Index = 0; Index ) {
373         if ((Src[Index] != Dst[Index]) ||
374                 (Dst[Index] != DMA_LENGTH - Index)) {
375             Status = -XST_FAILURE;
376         }
377     }
378 
379 
380     Checked[Channel] = Status;
381 }
ps_dma_demo
推荐阅读
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • C++中的三角函数计算及其应用
    本文介绍了C++中的三角函数的计算方法和应用,包括计算余弦、正弦、正切值以及反三角函数求对应的弧度制角度的示例代码。代码中使用了C++的数学库和命名空间,通过赋值和输出语句实现了三角函数的计算和结果显示。通过学习本文,读者可以了解到C++中三角函数的基本用法和应用场景。 ... [详细]
  • 【爬虫】关于企业信用信息公示系统加速乐最新反爬虫机制
    ( ̄▽ ̄)~又得半夜修仙了,作为一个爬虫小白,花了3天时间写好的程序,才跑了一个月目标网站就更新了,是有点悲催,还是要只有一天的时间重构。升级后网站的层次结构并没有太多变化,表面上 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了操作系统的定义和功能,包括操作系统的本质、用户界面以及系统调用的分类。同时还介绍了进程和线程的区别,包括进程和线程的定义和作用。 ... [详细]
  • 本文介绍了OkHttp3的基本使用和特性,包括支持HTTP/2、连接池、GZIP压缩、缓存等功能。同时还提到了OkHttp3的适用平台和源码阅读计划。文章还介绍了OkHttp3的请求/响应API的设计和使用方式,包括阻塞式的同步请求和带回调的异步请求。 ... [详细]
  • Todayatworksomeonetriedtoconvincemethat:今天在工作中有人试图说服我:{$obj->getTableInfo()}isfine ... [详细]
  • MySQL数据库锁机制及其应用(数据库锁的概念)
    本文介绍了MySQL数据库锁机制及其应用。数据库锁是计算机协调多个进程或线程并发访问某一资源的机制,在数据库中,数据是一种供许多用户共享的资源,如何保证数据并发访问的一致性和有效性是数据库必须解决的问题。MySQL的锁机制相对简单,不同的存储引擎支持不同的锁机制,主要包括表级锁、行级锁和页面锁。本文详细介绍了MySQL表级锁的锁模式和特点,以及行级锁和页面锁的特点和应用场景。同时还讨论了锁冲突对数据库并发访问性能的影响。 ... [详细]
  • 深入解析Linux下的I/O多路转接epoll技术
    本文深入解析了Linux下的I/O多路转接epoll技术,介绍了select和poll函数的问题,以及epoll函数的设计和优点。同时讲解了epoll函数的使用方法,包括epoll_create和epoll_ctl两个系统调用。 ... [详细]
  • Python中的PyInputPlus模块原文:https ... [详细]
author-avatar
飞儿gxj_505
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有