Altera_Forum
Honored Contributor
15 years agoNiosII DMA Tutorial
Hello, may I know where can I get NiosII DMA Tutorial? Currently, I am doing a project to implement DMA on FPGA board. So I would to refer to some tutorials that can help me. Thanks.
Hi aprado,
I have this piece of code for a simple dma core. I think you need to make a few changes in order to use with your sgdma core.# include "sys/alt_dma.h"# include "altera_avalon_dma.h"
# define DMA_LENGTH 512
volatile int dma_complete;
short rx_buffer; // pointer to rx buffer
short tx_buffer; // pointer to tx buffer
void dma_done (void* handle, void* data)
{
int rc;
// turn the LED off (for DEBUG)
rc = IORD_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE);
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, rc & ~0x10);
dma_complete = 1;
}
int test_dma(void)
{
int rc;
alt_dma_txchan txchan;
alt_dma_rxchan rxchan;
void* tx_data = (void*) tx_buffer; // pointer to data to send
dma_complete = 0;
// Create the transmit channel
if ((txchan = alt_dma_txchan_open("/dev/ts_dma")) == NULL) {
printf ("Failed to open transmit channel\n");
return -1;
}
// Create the receive channel
if ((rxchan = alt_dma_rxchan_open("/dev/ts_dma")) == NULL) {
printf ("Failed to open receive channel\n");
return -2;
}
if(alt_dma_txchan_ioctl(txchan, ALT_DMA_SET_MODE_16, NULL)<0) {
return -3;
}
if(alt_dma_rxchan_ioctl(rxchan,ALT_DMA_SET_MODE_16,NULL)<0) {
return -4;
}
if(alt_dma_txchan_ioctl(txchan,ALT_DMA_TX_ONLY_OFF,NULL)<0) {
return -5;
}
if(alt_dma_rxchan_ioctl(rxchan,ALT_DMA_RX_ONLY_OFF,NULL)<0) {
return -6;
}
printf ("Test DMA transfer...\n");
// turn a LED on (for DEBUG)
rc = IORD_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE);
IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, rc | 0x10);
// Post the transmit request
if ((rc = alt_dma_txchan_send (txchan,tx_data,DMA_LENGTH-16,NULL,NULL)) < 0) {
printf ("Failed to post transmit request, reason = %i\n", rc);
return -7;
}
// Post the receive request
if ((rc = alt_dma_rxchan_prepare (rxchan,rx_buffer,DMA_LENGTH-16, dma_done,NULL)) < 0) {
printf ("Failed to post read request, reason = %i\n", rc);
return -8;
}
// wait for transfer to complete
while (!dma_complete)
TK_SLEEP(10);
printf ("Transfer successful!\n");
return 0;
}