cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
amrs
Visitor
Visitor
14,515 Views
Registered: ‎10-08-2008

Getchar problem with Microblaze, getting NULs after each character?

Jump to solution

Hello, I'm not really a programmer, but I need to write simple test sw for an FPGA, so I'm using Microblaze. Much to my chagrin, I've run into a problem I can' t seem to solve. I input data from UART with getchar, but every character seems to be followed by a NUL. I don't really understand why? I guess I can work around this by throwing all NULs away, but I'd really like to know why this happens?

 

I have some test software from an older project (Virtex 4 FX, ISE 8.1 era, PowerPC instead of Microblaze) where getchar is used, but it doesn't seem to do anything special to get rid of extra NULs.

 

Example code, a simple input loop like this:

 

#include <stdio.h>

main()
{
  unsigned char mychar;

  while(1)
    {
      xil_printf("Press any key...\r\n");

      mychar=getchar();

      xil_printf("You pressed %c (%x)\r\n", mychar, mychar);
    }
}
 

If I run this and press 'a' on the keyboard, it prints this:

 

Press any key...
You pressed a (61)
Press any key...
You pressed  (0)
Press any key...

 

So it gets the 'a' and then a NUL, for some reason. I've tried different terminals, HyperTerminal and Putty, different boards, Spartan Starter and ML505. Seems to make no difference. Using EDK 10.1, SP2.

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
johnmcd
Xilinx Employee
Xilinx Employee
7,868 Views
Registered: ‎02-01-2008

This is fixed in EDK 13.3

View solution in original post

0 Kudos
11 Replies
shuns
Explorer
Explorer
14,500 Views
Registered: ‎10-01-2008

HI,

 

Do you hit return when testing this? Maybe something like this one.

http://www.soe.ucsc.edu/classes/cmps012a/Fall98/faq/scanfQ.html

 

0 Kudos
amrs
Visitor
Visitor
14,499 Views
Registered: ‎10-08-2008

shuns wrote:

HI,

 

Do you hit return when testing this? Maybe something like this one.

http://www.soe.ucsc.edu/classes/cmps012a/Fall98/faq/scanfQ.html

 


Not hitting return, no, so shouldn't be any white space. Just pressing a random key produces that key and then the NULL. Pressing return seems to do the same, in fact, although it's hard to see from that printout since it actually prints at least a carriage return then.

 

I don't really understand how I could use that scanf example to fix my problem. 

 

0 Kudos
savadi
Visitor
Visitor
13,955 Views
Registered: ‎12-05-2008

Good morning

 

i am working on EDk 9.2. i know in Xilinx  C, we use xil_printf for printf. But do use for scanf? How can I find this solution 

 

Thanks 

0 Kudos
hoosha
Adventurer
Adventurer
10,897 Views
Registered: ‎02-29-2008

I have the exact same problem in EDK 12.3. Did you find a solution to your problm by any chance?

 

Thank you,

Hooman

0 Kudos
robn
Moderator
Moderator
10,879 Views
Registered: ‎11-10-2010

Try clearing the buffer with this command:

 

setvbuf(stdin, NULL, _IONBF, 0);

0 Kudos
hoosha
Adventurer
Adventurer
10,875 Views
Registered: ‎02-29-2008
I do that right before entering the loop for calling getchar() but it doesn't help. or should I do it everytime?
0 Kudos
rickys
Xilinx Employee
Xilinx Employee
10,869 Views
Registered: ‎08-01-2007

A simple get string function. Stolen from Terry.

 

int mygets(char *string){
	int x=0;
	int ret = -1;

	while(x != '\r' && x != '\n'){
	   ret++;
	   x=inbyte();
	   *string++ = x;
	}
	*string = '\0'; // optional to add end of string symbol

	return ret;
}

 

0 Kudos
johnli
Observer
Observer
10,752 Views
Registered: ‎08-10-2011

Having exactly the same problem in 13.2 today. If you use RealTerm the NUL will be displayed.

A get string loop terminate at '/n' or '/r' will return an char array double the length of input with /0 between every char.

In you print the char array with xil_printf you will get ... the 1st char! (The string terminates with NUL)

 

Steps:

Try to use XUartLite_RecvByte instead of getchar, if not working try read HW's RX fifo and stat reg directly, to see if HW is indeed giving 2 bytes for each byte entered.

 

If the HW says 2 bytes, then it's likely a HW bug. Tape the uart rx signal to a 3rd party(or your own) uart softcore and verify only 1 byte is sent from the wire. This is obviously going to waste lots of time, so consider filter out all NUL returned from getchar as a SW workaround. (unless you are obsessed in exposing a Xilinx bug)

 

ASIC & FPGA/SoC designer
NYC HPC
0 Kudos
johnli
Observer
Observer
10,744 Views
Registered: ‎08-10-2011

XUartLite_RecvByte works, indicating a possible bug in getchar.

ASIC & FPGA/SoC designer
NYC HPC
johnmcd
Xilinx Employee
Xilinx Employee
7,869 Views
Registered: ‎02-01-2008

This is fixed in EDK 13.3

View solution in original post

0 Kudos
Visitor
Visitor
3,833 Views
Registered: ‎11-14-2008

I'm still seeing this problem in 14.1.

0 Kudos