Altera_Forum
Honored Contributor
16 years agoDMA memtest.c confusion
Hi,
I am using DMA device in my Nios program for DE1 board. The code memtest.c is taken from the example given in Nios IDE. The part of the code is shown below between green arterisk: ******************************************************# 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 < 0x1000; 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); } ******************************************************* My questions are: In the red for loop: 1) Is it intended that the "offset=+4" ? From what I understand, offset is int and data_written is int pointer. So for this instruction(assume data_written =0x0000), IOWR_32DIRECT((int)data_written, offset, pattern); would write 'pattern' value into 0x0000 to 0x0003 since is writing 32bit. In the next loop, this instruction will write into 0x000F to 0x0013. This is because data_written+offset=data_written+4=0x0000+0x000F since int 4 translate into 16 byte. So logically thinking,"offset=+1" would be more suitable since it will fill up each memory right? In the blue for loop: 2) Assuming the memory start from 0x0000 and end at 0x00FF. Does the instruction if ((rc = alt_dma_txchan_send (txchan, data_written, 0x1000, NULL, NULL)) < 0) cause an error since it is reading 0x1000 byte data but the memory is only 0x100 byte ? Please correct me if I am wrong. Thanks. :)