DMA
/* * "Hello World" example. * * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT * device in your system's hardware. * The memory footprint of this hosted application is ~69 kbytes by default * using the standard reference design. * * For a reduced footprint version of this template, and an explanation of how * to reduce the memory footprint for a given application, see the * "small_hello_world" template. * */ /* #include <stdio.h>
int main() { printf("Hello from Nios II!\n");
return 0; }*/
#include "inc/sopc.h"
#include "string.h" #include <stdio.h> #include <sys/unistd.h> #include <string.h> #include <sys/alt_irq.h> #include "system.h" #include "alt_types.h" #include "inc/altera_avalon_dma_regs.h"
/*----------------------------------------------------------------------------- * Define *-----------------------------------------------------------------------------*/ #define DAT_LEN 100 #define SRC_ADDR (SDRAM_BASE + 0x20000) #define DST_ADDR (SDRAM_BASE + 0x30000)
/*----------------------------------------------------------------------------- * Function prototypes *-----------------------------------------------------------------------------*/ static void DMA_Init(void); //初始化DMA
/*----------------------------------------------------------------------------- * Variable *-----------------------------------------------------------------------------*/ unsigned int dma_end_flag = 0;
/* * === FUNCTION ====================================================================== * Name: main * Description: 通过DMA,将SDRAM中一个地址中的数据传输到另一个地址 * ==== /*----------------------------------------------------------------------------- * Variable *-----------------------------------------------------------------------------*/ unsigned short * ram = (unsigned short *)(SDRAM_BASE+0x10000); //SDRAM地址 unsigned short * ram_src = (unsigned short *)SRC_ADDR; unsigned short * ram_dst = (unsigned short *)DST_ADDR; /* * === FUNCTION ====================================================================== * Name: main * Description: 函数主程序 * ===================================================================================== */ int ll(void) { int i; printf("Hello from Nios II!\n"); memset(ram,0,100);
//向ram中写数据,当ram写完以后,ram的地址已经变为(SDRAM_BASE+0x10100) for(i=0;i<100;i++){ *(ram++) = i; }
//逆向读取ram中的数据 for(i=0;i<100;i++){ printf("%d\n",*(--ram)); }
for(i=0; i<100; i++){ *(ram_src++) = i*i; }
//初始化DMA DMA_Init(); //等待中断结束,说明传输完成 while(dma_end_flag == 0); //打印接收地址的数据 for(i=0;i<100;i++){ printf("%d\n",*(ram_dst++)); } return 0; }
/************************************/ //DMA中断 static void Handle_DMA_Interrupts(void* context, alt_u32 id) { //清除中断标志 IOWR_ALTERA_AVALON_DMA_STATUS(DMA_BASE, 0); //停止DMA传输 IOWR_ALTERA_AVALON_DMA_CONTROL(DMA_BASE, 0x092); dma_end_flag = 1; }
/************************************/ static void DMA_Init(void) //DMA初始化 { //注册DMA中断 alt_irq_register(DMA_IRQ, NULL, Handle_DMA_Interrupts); //设置传输模式为半字传输 IOWR_ALTERA_AVALON_DMA_CONTROL(DMA_BASE, 0x092); //清除中断标志 IOWR_ALTERA_AVALON_DMA_STATUS(DMA_BASE, 0); //初始化源地址和目的地址 IOWR_ALTERA_AVALON_DMA_RADDRESS(DMA_BASE, SRC_ADDR); IOWR_ALTERA_AVALON_DMA_WADDRESS(DMA_BASE, DST_ADDR); //初始化长度寄存器 IOWR_ALTERA_AVALON_DMA_LENGTH(DMA_BASE, 2*DAT_LEN); //启动DMA IOWR_ALTERA_AVALON_DMA_CONTROL(DMA_BASE, 0x09a); }