Forum Discussion
Altera_Forum
Honored Contributor
15 years ago....continue from previous post ...
this is an example of a larger chunk transfer using dma from ssram_0 to buffer (allocated in sdram_0)
/*
* NIOSimg.cpp
*
* Created on: Apr 26, 2010
* Author: suki
*/
# include <stdio.h># include <stdlib.h># include <stddef.h># include <string.h>
# include <system.h># include <io.h>
# include <alt_types.h># include "sys/alt_dma.h"# include "sys/alt_cache.h"# include "sys/alt_alarm.h"# include "alt_types.h"
# define DEPTH 0x4b000
static volatile int txrx_done = 0;
//callback function when DMA transfer done
static void txrxDone(void * handle, void * data)
{
txrx_done = 1;
}
void initMEM(int base_addr,int len)
{
for (int i=0;i<len;i++)
{
IOWR_8DIRECT(base_addr,i,i);
}
}
int main()
{
printf("testing memory to memory DMA transfer operation of 0x%x bytes\n",DEPTH);
alt_8 buffer;
//memset((void *)SSRAM_0_BASE,0x7a,0x10);//this write base on byte
initMEM(SDRAM_1_BASE,DEPTH);
//memset((void *)(SDRAM_1_BASE+0x10),0x33,0x10);
memset(buffer,0x33,DEPTH);
int rc; //request
alt_dma_txchan txchan;
alt_dma_rxchan rxchan;
void* tx_data = (void*)SDRAM_1_BASE; /* pointer to data to send */
void* rx_buffer = (void*)&buffer; /* pointer to rx buffer */
/* Create the transmit channel */
if ((txchan = alt_dma_txchan_open("/dev/dma_0")) == NULL)
{
printf ("Failed to open transmit channel\n");
exit (1);
}
/* Create the receive channel */
if ((rxchan = alt_dma_rxchan_open("/dev/dma_0")) == NULL)
{
printf ("Failed to open receive channel\n");
exit (1);
}
/* Post the transmit request */
if ((rc = alt_dma_txchan_send (txchan,
tx_data,
DEPTH,
NULL,
NULL)) < 0)
{
printf ("Failed to post transmit request, reason = %i\n", rc);
//exit (1);
}
/* Post the receive request */
if ((rc = alt_dma_rxchan_prepare (rxchan,
rx_buffer,
DEPTH,
txrxDone,
NULL)) < 0)
{
printf ("Failed to post read request, reason = %i\n", rc);
//exit (1);
}
printf("DMA transfer running. . .");
/* wait for transfer to complete */
while (!txrx_done);
printf ("Transfer finish!\n");
printf("Verifying transfered data using memcmp. . .");
if (!(memcmp((void*)SDRAM_1_BASE,(void*)&buffer,DEPTH)))
printf("Verification succesful!\n");
else
printf("Verification failed\n");
// printf("content of buffer\n");
// for (int i=0;i<sizeof(buffer)/2;i++)
// printf("%d: %x\n",i,buffer); // this output width ,base on what we declare
/*printf("content of ssram_0\n");
for (int i=0;i<sizeof(buffer);i=i+2)
{
printf("%d: %x\n",i/2,IORD_16DIRECT(SSRAM_0_BASE,i));
}
printf("content of sdram_1\n");
for (int i=0;i<sizeof(buffer);i=i+2)
{
printf("%d: %x\n",i/2,IORD_16DIRECT(SDRAM_1_BASE,i));
}
memcpy((void*)SDRAM_1_BASE,(void *)SSRAM_0_BASE,20);
printf("content of sdram_1: after memcpy operation\n");
for (int i=0;i<sizeof(buffer);i=i+2)
{
printf("%d: %x\n",i/2,IORD_16DIRECT(SDRAM_1_BASE,i));
}*/
return 0;
}
The output:
testing memory to memory DMA transfer operation of 0x4b000 bytes
DMA transfer running. . .Transfer finish!
Verifying transfered data using memcmp. . .Verification succesful!
cheers, sukiminna.