Altera_Forum
Honored Contributor
14 years agoDMA problem
why when i use my dma, the program stucks here: --- Quote Start --- while (!rx_done); printf ("Transfer successful!\n"); --- Quote End --- pls help! thanks!
Hi, this is my code:
--- Quote Start --- # include <stdio.h># include <stdlib.h># include <unistd.h># include "altera_avalon_pio_regs.h"# include "system.h"# include "alt_types.h"# include "sys/alt_dma.h"# include "sys/alt_flash.h" # include "sys/alt_flash_dev.h"# include "altera_avalon_dma_regs.h"# include "altera_avalon_dma.h" # define loop 16384# define size 65536 static volatile int rx_done = 0; static void done (void* handle, void* data) { rx_done = 1; } int main (int argc, char* argv[], char* envp[]) { alt_flash_fd* fd; int ret_code, i; int *src_ptr; int *dest_ptr; int addr; int rc; alt_dcache_flush_all(); alt_u32 start,end; alt_dma_txchan txchan; alt_dma_rxchan rxchan; addr = 0; src_ptr = SDRAM_BASE + 0x200000; //Location of data written in SDRAM dest_ptr = EXT_FLASH_BASE; //Location of data written in flash void* tx_data = (void*) SDRAM_BASE + 0x200000; /* pointer to data to send */ void* rx_buffer = (void*) EXT_FLASH_BASE + 0x100000; /* pointer to rx buffer */ //Open flash fd = alt_flash_open_dev(EXT_FLASH_NAME); if(fd!=NULL) { //Generate data into SDRAM for(i=0;i<loop;i++) { IOWR(src_ptr, i, i); } //Write data into flash while(addr < size) { ret_code = alt_write_flash(fd, addr, src_ptr+addr, 0x10000); if(ret_code != 0) { printf("Error writing into flash: %d\n", ret_code); exit(1); } addr += 0x10000; // next 64kb block } //Read from flash for(i=0;i<loop;i++) { printf("Value at %X is %d, written is %d\n", dest_ptr, *dest_ptr, IORD(src_ptr, i)); dest_ptr++; } }else { printf("Unable to open flash\n"); exit(1); } /* Create the transmit channel */ if ((txchan = alt_dma_txchan_open(DMA_NAME)) == NULL) { printf ("Failed to open transmit channel\n"); exit (1); } /* Create the receive channel */ if ((rxchan = alt_dma_rxchan_open(DMA_NAME)) == NULL) { printf ("Failed to open receive channel\n"); exit (1); } /* Post the transmit request */ if ((rc = alt_dma_txchan_send (txchan, tx_data, size, 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, size, done, NULL)) < 0) { printf ("Failed to post read request, reason = %i\n", rc); exit (1); } /* wait for transfer to complete */ while (!rx_done); printf ("Transfer successful!\n"); alt_flash_close_dev(fd); return 0; } --- Quote End --- I am not sure where it stuck because it does not return any error messages. so i assume dma is working. pls help. thanks!