转载:
#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);
{
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++)
{
}
while(1);
return 0;
}