

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "Header/hwlib.h"
#include "Header/socal/hps.h"
#include "Header/socal/socal.h"
#include "Header/socal/alt_gpio.h"

#define HW_ADDR (0xFF600000)
#define HW_REGS_SPAN ( 0x00080000)
#define HW_REGS_MASK ( HW_REGS_SPAN-1)


int main(int argc, char** argv) {
	void *virtual_base;
	volatile unsigned long *f2h_addr= NULL;
	int fd;
	// variables to detect changes in the register
	uint32_t leser=0; 
	uint32_t leser_alt=0;

	// open /dev/mem
	if( (fd=open("/dev/mem", (O_RDWR | O_SYNC ))) ==-1) {
		printf("ERROR: could not open /dev/mem \n");
		close(fd);
		return(1);
	}
	// map memory
	printf("map memory at address: %X \n",HW_ADDR);
	virtual_base = mmap(NULL, HW_REGS_SPAN, (PROT_READ | PROT_WRITE), MAP_SHARED, fd,(int) HW_ADDR);
	if( virtual_base == MAP_FAILED) {
		printf("ERROR: mmap() failed...\n");
		close(fd);
		return(1);
	}
	// calculate the address of the
	f2h_addr = virtual_base + ((unsigned long)(ALT_F2H_SLV_SLV_B32_OFST) & (unsigned long)(HW_REGS_MASK));
	printf("Preparations ready, begin reading: \n");
	int i=0;
	for (i=0; i<60;i++){
		// read memory
		leser = alt_read_hword(f2h_addr);
		// if old value != current value, do:
		if(leser!= leser_alt){
			printf("Gelesener Wert: %u \n",leser);
			leser_alt = leser;
		}
	}
	// unmap the memory
    	if(munmap(virtual_base,HW_REGS_SPAN) !=0){
    		printf("ERROR: memory unmap failed! \n");
    		close(fd);
    		return(1);
    	}
    	close(fd);

    	return 0;
}
