阅读DMA Controller Core 官方手册

阅读DMA Controller Core 官方手册

 

DMA控制器框架图

怎样去设定一个DMA控制器

实例化DMA控制器

参数配置界面如下图所示:

 对于width of the DMA length register的配置尝试如下: 

 

 

正如官方文档中描述的那样:

DMA Length register的位宽决定了DMA Length的最大值,决定了可以用这个DMA控制器传递数据的个数。

DMA的工作模式:

搭建一个系统:

数据从ROM里面,经过DMA控制器传送到RAM里面,软件程序单独在RAM_Pro内存空间里面运行。

注意:片上ROM与片上RAM可以不与Nios II 处理器相连,这样会导致处理器监控不到ROMRAM里面的数据,在Eclipse里面的debug界面中没法查找到相应的内存空间,但是连上处理器之后,可以查询得到,建议连上

顶层例化:

 DMA控制器软件程序的编写:

 需要根据用户手册来指导编写:

在Eclipse中编写代码如下:

/*
 * "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>
#include <stdlib.h>
#include "sys/alt_dma.h"
#include "system.h"
static volatile int rx_done = 0;
static void done(void* handle, void* data)
{
    rx_done++;
}

int main(int argc, char* argv[], char* envp[])
{
    int rc;
    alt_dma_txchan txchan;
    alt_dma_rxchan rxchan;
    void* tx_data = (void*) 0x2000;
    void* rx_data = (void*) 0x1000;
    if((txchan = alt_dma_txchan_open("/dev/dma")) == NULL)
    {
        printf ("Failed to open transmit channel
");
        exit(1);
    }

    if((rxchan = alt_dma_rxchan_open("/dev/dma")) == NULL)
    {
        printf ("Failed to open receive channel
");
        exit(1);
    }

    if((rc = alt_dma_txchan_send(txchan,tx_data,256,NULL,NULL)) < 0)
    {
        printf ("Failed to post transmit request,reason = %i
",rc);
        exit(1);
    }

    if ((rc = alt_dma_rxchan_prepare(rxchan,rx_data,256,NULL,NULL)) < 0)
    {
        printf ("Failed to post read request,reason = %i
",rc);
        exit(1);
    }

    while(!rx_done)
    {
        printf ("Transfer successful!
");
    }
    printf ("Hello from Nios II!
");
    return 0;
}

由于数据存储在片上的ROMRAM中,因此可以用In-System Memory Content Editor来进行查看:

首先给ROM初始化数据文件和Instance ID:

 

 然后,同样需要给RAM分配Instance ID

 

最后在In-System Memory Content Editor里面进行查看:

 经过核对:数据传输无误。

原文地址:https://www.cnblogs.com/chensimin1990/p/6490486.html