【转载】Nios II DMA: memory to memory

转载:http://blog.ednchina.com/conan85420/428608/message.aspx

本实例在SDRAM中开辟了一个存储空间,无符号字符数组,通过DMA,将SDRAM中数据发送到on_chip ram中。其中可以通过串口监控DMA开始与结束目标地址空间的数据,此外也可以通过LED的状态,知晓DMA是否执行完毕。

此外还可以通过Dubeg模式下,memory观察目标地址空间的数据变化。

点击看大图

上图是发起DMA前,地址空间0x801000往后的地址全为零。

点击看大图

上图是DMA传输完成后,地址空间0x801000往后的地址的数据变成红色,发生改变,数值与chr数组中元素的值一致,说明DMA传输成功。

#include <stdio.h>

#include <stdlib.h>

#include "unistd.h"

#include "string.h"

#include "io.h"

#include "system.h"

#include "sys/alt_dma.h"

#include "sys/alt_cache.h"

#include "sys/alt_irq.h"

#include "alt_types.h"

#include "sys/alt_irq.h"

 

#include "altera_avalon_dma_regs.h"

#include "altera_avalon_uart_regs.h"

 

 

volatile alt_u8 chr[20] = {1,2,3,4,6,5,7,8,9,10,

                        11,12,13,14,15,16,17,18,19,20} ;//数据源

static volatile int rx_done = 0;

 

static void done (void* handle, void* data)

{

rx_done++;

IOWR(LED_BASE,0, rx_done);//LED显示rx_done的变化

}

 

int main (int argc, char* argv[], char* envp[])

{

alt_u8 i="0";

int rc;

alt_dma_txchan txchan;

alt_dma_rxchan rxchan;

void * tx_data = (void*) chr;

alt_u8 *rx = (alt_u8 *)ONCHIP_RAM_BASE;

void* rx_buffer = (void*) ONCHIP_RAM_BASE;//0x00801000; /* pointer to rx buffer */

IOWR(LED_BASE,0, 0);

for(i =0;i<20;i++)  //串口输出目标地址空间的数据

{

   printf ("%c\n",*(rx+i));

}

printf ("Let's begin!\n");

 

if ((txchan = alt_dma_txchan_open("/dev/DMA0")) == NULL)

{

printf ("Failed to open transmit channel\n");

}

 

if ((rxchan = alt_dma_rxchan_open("/dev/DMA0")) == NULL)

{

printf ("Failed to open receive channel\n");

}

 

if ((rc = alt_dma_txchan_send (txchan,

tx_data,

20,

NULL,

NULL)) < 0)

{

printf ("Failed to post transmit request, reason = %i\n", rc);

}

 

if ((rc = alt_dma_rxchan_prepare (rxchan,

rx_buffer,

20,

done,

NULL)) < 0)

{

printf ("Failed to post read request, reason = %i\n", rc);

 

}

while (!rx_done);

printf ("Transfer successful!\n");

for(i =0;i<20;i++)

{

   printf ("%c",*(rx+i)); //再次输出dma传输后目标地址的数据

}

while(1);

return 0;

}

原文地址:https://www.cnblogs.com/kongtiao/p/2185793.html