**********实现sdram到flash的数据传输******************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/alt_dma.h>
#include "system.h"
static volatile int rx_done = 0;
alt_u8 i;
int rc;
alt_dma_txchan txchan;
alt_dma_rxchan rxchan;
void* tx_data = (void*) 0x02002000; /* 源地址,0x02002000是sdram的某一起始地址 */
void* rx_buffer = (void*) 0x04402000; /* 目标地址,0x04010000为flash的某一起始地址*/
//DMA传输结束回调函数
static void done_t(void* handle, void* data)//done_t()是DMA完成后被调用的回调函数
{
rx_done++;
}
int main (int argc, char* argv[], char* envp[])
{
alt_u8 *p=tx_data;
alt_u8 *q=rx_buffer;
printf("Welcome!\n");
for(i=0;i<128;i++)
{
*p++=i;
}
printf ("1\n");
/* 打开发送通道 */
if (!(txchan = alt_dma_txchan_open("/dev/DMA")))
{
printf ("Failed to open transmit channel\n");
exit (1);
}
/* 打开接收通道 */
if (!(rxchan = alt_dma_rxchan_open("/dev/DMA")))
{
printf ("Failed to open receive channel\n");
exit (1);
}
/* 开始发送数据 */
if ((rc = alt_dma_txchan_send (txchan, //选用transport通道
tx_data, //a pointer to the start of the data to send.
128, //the length of the data to send in bytes.
NULL,
NULL)) < 0)
{
printf ("Failed to post transmit request, reason = %i\n", rc);
exit (1);
}
/* 开始接收数据*/
if ((rc = alt_dma_rxchan_prepare (rxchan, //选中recieve通道
rx_buffer,//目标地址
128, //the maximum length of the data to receive
done_t, //done_t()是DMA完成后被调用的回调函数
NULL)) < 0)
{
printf ("Failed to post read request, reason = %i\n", rc);
exit (1);
}
/* 等待传输结束 ,即等待rx_done的值变为1,等待回调函数的执行*/
printf ("2\n");
while (!rx_done);
printf ("Transfer successful!\n");
printf ("3\n");
p=tx_data;
q=rx_buffer;
for(i=0;i<128;i++)
{
printf(" flash%x=%x\n", *q++);
printf(" sdram%x=%x\n", *p++);
}
return 0;
}
一个为理想不懈前进的人,一个永不言败人!
http://shop57496282.taobao.com/
欢迎光临网上店铺!