Nios II version 1.0 SP1, just released, contains a new software example design called "memtest". This example design checks using #ifdef to see if the system contains a DMA. If it does, it performs a test of memory using HAL DMA code in addition to the normal tests it runs. Pasted below is a copy of the DMA test function which includes calls to the DMA HAL routines.
-Nathan Knight Altera Applications
/******************************************************************
*
- Purpose: Tests every bit in the memory device within the
- specified address range using DMA. The DMA controller provides
- a more rigourous test of the memory since it performs back-to-
- back memory accesses at full system speed.
*
******************************************************************/ #ifdef DMA_NAME static int MemDMATest(unsigned int memory_base, unsigned int nBytes) { int rc; int ret_code = 0; int pattern, offset; alt_dma_txchan txchan; alt_dma_rxchan rxchan; void
* data_written; void* data_read; /
* Get a couple buffers for the test */ /
* Using the "uncached" version of malloc to avoid cache coherency issues */ data_written = (void
*)alt_uncached_malloc(0x1000); data_read = (void*)alt_uncached_malloc(0x1000); /
* Fill write buffer with known values */ for (pattern = 1, offset = 0; offset < sizeof(data_written); pattern++, offset+=4) { IOWR_32DIRECT((int)data_written, offset, pattern); }
/* Create the transmit channel */ if ((txchan = alt_dma_txchan_open("/dev/dma")) == NULL) { printf ("Failed to open transmit channel\n"); exit (1); } /* Create the receive channel */ if ((rxchan = alt_dma_rxchan_open("/dev/dma")) == NULL) { printf ("Failed to open receive channel\n"); exit (1); } for(offset = memory_base; offset < (memory_base + nBytes); offset +=
0x1000) { /
* Use DMA to transfer from write buffer to memory under test */ /
* Post the transmit request */ if ((rc = alt_dma_txchan_send (txchan, data_written, 0x1000, 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, (void*)offset, 0x1000, dma_done, NULL)) < 0) { printf ("Failed to post read request, reason = %i\n", rc); exit (1); } /* Wait for transfer to complete */ while (!rx_done); rx_done = 0; /* Clear the read buffer before we fill it */ memset(data_read, 0, 0x1000); /* Use DMA to read data back into read buffer from memory under test */ /* Post the transmit request */ if ((rc = alt_dma_txchan_send (txchan, (void*)offset, 0x1000, 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, data_read, 0x1000, dma_done, NULL)) < 0) { printf ("Failed to post read request, reason = %i\n", rc); exit (1); }
/* Wait for transfer to complete */ while (!rx_done); rx_done = 0; if (memcmp(data_written, data_read, 0x1000)) { ret_code = offset; break; } } alt_uncached_free(data_written); alt_uncached_free(data_read); return ret_code; } #endif /* DMA_NAME */