【转载】Nios II DMA: memory to memory
2011-09-23 09:20
316 查看
转载: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;
}
本实例在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;
}
相关文章推荐
- Timer-triggered memory-to-memory DMA transfer demonstrator
- DMA : Timer Trigger Memory-to-memory mode,
- NIOS2 DMA Memory to Peripheral Trnasfer
- 解决 - java.lang.OutOfMemoryError: unable to create new native thread (转载)
- 解决 - java.lang.OutOfMemoryError: unable to create new native thread(转载)
- [转载].基于Nios II的DMA传输
- zedboard平台下DMA实现memory to memory传输
- 转载:.NET Memory Leak: XmlSerializing your way to a Memory Leak
- 【转载】基于Nios II的DMA传输总结(附源码)
- 【转载】Nios II DMA:多次发起
- NIOS2 DMA Memory to Memory Transfer
- 【stm32f103】DMA应用memory to memory(寄存器版)
- 15.0坂本Nios ii 编译on_chip_memory过小的解决方案
- [转载] iPhone 5 forensics – prepare to be assimilate
- Failed to allocate memory: 8 (andriod模拟器无法启动)
- 由"java.lang.OutOfMemoryError: unable to create new native thread"说起
- Android Memory Leaks OR Different Ways to Leak
- Find a new software to memory the vocabulary
- java.lang.OutOfMemoryError: unable to create new native thread
- Android:Failed to allocate memory: 8;This application has requested the Runtime to terminate it ...