C Application Not giving Output

Hi,
I just made the following qsys system where I just want to read and write data to my register.


Here is my verilog code for the 8 bit register (userdefined8bitreg) which I just simply want to write and read from

module userreg8bit(
input write,
input [7:0] writedata,
input read,
output [7:0] readdata,
input clk,
input reset
);

reg [7:0] Regreaddata;

assign readdata=Regreaddata;

always @ (posedge clk)
	begin
		if(reset)
			begin
				Regreaddata<=0;
			end
		else if(write)
			begin
				if(read)
					begin
						Regreaddata<=writedata;
					end
			end
	end

endmodule

Now, I wrote simple C program for my HPS as

 #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:\masterarbeit3\HPSFPGAFIFO\hps_0.h"
 #define REG_BASE 0xFF200000 /*LWFPGA SLAVE Address*/
 #define REG_SPAN 0x00200000 /*LWFPGA SLAVE SPAN*/
* volatile unsigned char *reg_addr; /*this is the pointer that writes to the register. This is our write input*/
 void* virtual_base; /*pointer to open device memory file*/
 int main ()
 {
* int fd = EXIT_FAILURE;
 int value; /*this is input value to the register*/
 unsigned char regvalue;
* printf("Please enter a number from 1 to 15: ");
 scanf("%d",&value);
 printf("you entered the value :%d",value);
 fd=open("/dev/mem",(O_RDWR|O_SYNC));
 if (fd < 0) {
 perror("open");
 exit(EXIT_FAILURE);
 }
* virtual_base=mmap(NULL,REG_SPAN,(PROT_READ|PROT_WRITE),MAP_SHARED,fd,REG_BASE);
* reg_addr = (unsigned char *) (virtual_base+USERREG8BIT_0_BASE);
* /*writing the value*/
 *reg_addr=value;
 /*reading the value back*/
regvalue=*reg_addr;
printf("%c\n",regvalue);
* return 0;
}

Here is my make file which i am using to compile my c code

#
TARGET = HPSFPGAFIFO

#
CROSS_COMPILE = arm-linux-gnueabihf-
ALT_DEVICE_FAMILY ?= soc_cv_av 
CFLAGS := -g $(OFLAG) -Wall -Werror -std=c99 $(MULTILIBFLAGS) -I$(HWLIBS_ROOT)/include -I$(HWLIBS_ROOT)/include -D$(ALT_DEVICE_FAMILY) 

ALL_HWLIBS_SRC = $(wildcard $(HWLIBS_ROOT)/src/hwmgr/*.c) $(wildcard $(HWLIBS_ROOT)/src/hwmgr/$(ALT_DEVICE_FAMILY)/*.c $(wildcard $(HWLIBS_ROOT)/src/utils/*.c)) 
LDFLAGS =  -g -Wall  
CC = $(CROSS_COMPILE)gcc
ARCH= arm


build: $(TARGET)
$(TARGET): HPSFPGAFIFO.o 
	$(CC) $(LDFLAGS)   $^ -o $@  
%.o : %.c
	$(CC) $(CFLAGS) -c $< -o $@

.PHONY: clean
clean:
	rm -f $(TARGET) *.a *.o *~ 

Now when I run this c program on my Arrow Socket board, it doesn’t give me back on written data. as you can also see in the figure below?


Any ideas? why i am not able to read back my written data?

You are printing the value from your register as a character. ASCII value 12 corresponds to a line feed. Are you sure it’s not working? Can you change the %c in your last printf to a %d and see if it works?

1 Like

@SusananhM
Hi, thank you so much. yea that worked for me. It is showing me correct values. Thanks a lot for your suggestion. That helped me a lot. Just one question, if you see the output in the figure the line where i try to print *reg_addr as %c, the program doesn’t show me the output. I don’t understand what is the deal with %c. I mean you also told me to replace %c with %d. What is the problem with %c because I have already instantiated *reg_addr as unsigned character then where is the problem? Second question? Is it always necessary to declare pointers as unsigned characters? I mean whatever examples I see on internet they declare pointers like *reg_addr as unsigned character?

It’s not necessary to declare pointers as unsigned characters. You can declare them as any legal type. What type you use when you declare them determines how much space in memory you are allocating. Since you declared it as a char, you got a single byte of memory space. Your program would work just fine if you declared it as another type.

You also want to have certain types because functions treat different types different ways when performing their calculations (and because they can only accept certain types).

Printf accepts whatever type you give to it, relying on the format string to cast that type to print it out. So, the format string tells printf how to interpret your arguments. For %c, it takes your variable from memory and interprets it as encoded ASCII. For %d, it takes your variable value from memory and interprets it as a decimal number. (There’s also some automatic promotion done since it’s a char to an int before printf consumes it - I’ll link to a couple of pages at the bottom with more info - printf is actually a complicated function & subject).

When you tested your program with the value 12, you made an unfortunate choice - that’s ASCII for line feed so it made it harder to figure out what was going on.

Here’s some helpful links:


1 Like