AnsweredAssumed Answered

return value for getc(), getchar() and scanf() in semihosting

Question asked by Markus Krug on Sep 30, 2017
Latest reply on Oct 11, 2017 by Alice_Yang



I tried to use semihosting on the K60F120 TWR board with CW10.7 and the gcc toolchain. The first attempt with printf() worked as expected.

Unfortunately reading from the terminal window did not work for me so far. I can read whatever I typed in the terminal window with the above mentioned functions. However, it seems to me that the input buffer of the terminal window has a lot of 0x00 values that are read by the above mentioned functions. For example a meaningless code like:

char input_ch1 =getchar();

char input_ch2 =getchar();

char input_ch3 =getchar();

char input_ch4 =getchar();

char input_ch5 =getchar();

char input_ch6 =getchar();

char input_ch7 =getchar(); 

waits until I type a character in the terminal window and presents this correctly to input_ch1. The two following getchar() will copy '\r' and '\n' to the variables input_ch2 and input_ch3. As far as I can see this is as the function is specified in ANSI-C. From now on every getchar() will copy 0x00 to the corresponding variable. All this happens without hitting the keyboard again. So it seems to me that the input buffer is filled with a lot of 0x00 after the first time 'return' was hit. From my understanding the function should return EOF at this point. Even worse, if I type the next character it gets completely ignored - 

I get the same results with getc(stdin). If I replace getchar() with scanf("%c",&input_chx) I get the same results for the values of input_chx and a return value of 1 for scanf() - showing he is still able to copy data from the input buffer that fit to the format string.


Finally I checked how many 0x00 are in the input buffer. I tested this several times and surprisingly the number seems to be constant. It looks to me that the input buffer has a size of 128 bytes. However, even if I discard all these 0x00 till the end the above mentioned functions continue to read something from the input buffer. After all the 0x00 a lot of memory garbage is read. So I guess the problem is somewhere in the semihosting implementation of gcc and CW10.7 that is not providing the EOF value and resetting the input buffer correctly. It might also linked with the libraries one has to use. So far I did not try to link different libraries in case I want to use semihosting.


Anyone get a successful read for character per keyboard input using semihosting with CW10.7 and gcc?


Best Regards