• HPS FIFO Communication on Cyclone V Altera SoC

    09/02/2019 at 10:49 0 comments

    Hi, 

    I am trying to communicate HPS and FIFO on altera cyclone V SoC. I am able to write data but I am not able to read back the data. Anyone please help where I am wrong

    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <fcntl.h> 
    #include <sys/mman.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include "C:\altera\15.1\embedded\ip\altera\hps\altera_hps\hwlib\include\hwlib.h"
    #include "C:\altera\15.1\embedded\ip\altera\hps\altera_hps\hwlib\include\soc_cv_av\socal\socal.h"
    #include "C:\altera\15.1\embedded\ip\altera\hps\altera_hps\hwlib\include\soc_cv_av\socal\hps.h" 
    #include "C:\altera\15.1\embedded\ip\altera\hps\altera_hps\hwlib\include\soc_cv_av\socal\alt_gpio.h" 
    #include "D:\TestDesigns\customfifoloopback1\hps_0.h"
    //fifo status pointers
    //#define FIFO_FULL		  ((*(FIFO_write_status_ptr+1))& 1 ) 
    //#define FIFO_EMPTY	  ((*(FIFO_read_ptr+1))& 2 )
    
    //Main bus addresses
    #define FIFO_BASE            0xC0000000
    #define FIFO_SPAN            0x00001000
    
    //LW Addresses
    #define REG_BASE 0xFF200000
    #define REG_SPAN 0x00200000
    
    //initializing the pointers
    void* virtual_base;
    void* h2f_virtual_base;
    volatile unsigned int * FIFO_write_status_ptr= NULL;
    volatile unsigned int * FIFO_read_status_ptr = NULL ;
    volatile unsigned int * FIFO_write_ptr = NULL ;
    volatile unsigned int * FIFO_read_ptr = NULL ;
    unsigned int value;
    int data[7];
    int i;
    //int FIFO_FULL;
    //int FIFO_EMPTY;
    //main functions
    int main ()
    {
    	
    int fd = EXIT_FAILURE;	
    fd=open("/dev/mem",(O_RDWR|O_SYNC));
    if (fd < 0) {
    		perror("open");
    		exit(EXIT_FAILURE);
    			}			
    printf("fd is ok\n");
    
    //accessing the virtual addresses of the fifo pointers
    virtual_base=mmap(NULL,REG_SPAN,(PROT_READ|PROT_WRITE),MAP_SHARED,fd,REG_BASE);
    h2f_virtual_base=mmap(NULL,REG_SPAN,(PROT_READ|PROT_WRITE),MAP_SHARED,fd,FIFO_BASE);
    printf("virtual base pointer to open device memory file is ok\n");
    
    FIFO_write_status_ptr = (unsigned int *)(virtual_base+FIFO_0_IN_CSR_BASE);
    FIFO_read_status_ptr = (unsigned int *)(virtual_base+FIFO_0_OUT_CSR_BASE);
    FIFO_write_ptr=(unsigned int *)(h2f_virtual_base+FIFO_0_IN_BASE);
    FIFO_read_ptr= (unsigned int *)(h2f_virtual_base+FIFO_0_OUT_BASE);
    printf("fifo pointers are ok\n");
    	
    //entering data into the fifo
    for(i=0;i<8;i++)
    {
    	data[i]=i+1;
    	*FIFO_write_ptr=data[i];
    	printf("////////////////////////////\n");
    	printf("you entered the value %d\n",*FIFO_write_ptr);
    	printf("fifo write status pointer is %d\n",*FIFO_write_status_ptr);
    	printf("//////////////////////////////\n");	
    }
    
    
    printf("reading back the data\n");
    
    for(i=0;i<8;i++)
    {
    	//reading data from the fifo
    	printf("////////////////////////////\n");
    	printf("the output value from the fifo is%d\n",*FIFO_read_ptr);
    	printf("fifo read status pointer is %d\n",*FIFO_read_status_ptr);
    	printf("/////////////////////////////\n");
    }
    return 0;
    }