Forum: ARM programming with GCC/GNU tools Printf, putc, and getc serial issues

Author: Rick Mc (waterhazard)
Posted on:

Rate this post
0 useful
not useful
I am using Eclipse with the ARM GNU Toolset. The code I am using was 
ported from Keil, which used a retarget.c file to output to UART1 just 
fine. My problem is that printf, putc, and getc do not take/output any 
data to/from the serial port, but if I use my created fputc and fgetc 
functions directly they do. More oddly, when debugging and watching the 
variables, it appears that getc is taking its input from a printf 
statement earlier in the code! Another strange thing is that if I try 
the command fprintf(stdout,"A"); it outputs just fine to the serial 
terminal yet printf never shows up.

To try and fix this I have copied my fputc and fgetc code into _write 
and _read in the syscalls.c, and roughly configured the rest of the file 
per http://www.embecosm.com/appnotes/ean9/ean9-howto-n... . I 
have also tried including the lines setbuf (stdout, NULL); and setvbuf( 
stdout, 0, _IONBF, 0 ); but that had no effect either. I have searched 
extensively but nobody else seems to be having a problem quite like 
mine, any help would be appreciated.

Author: William Cooke (bdk6)
Posted on:

Rate this post
1 useful
not useful
This sounds very much like the problem I had (have).  In my case, and it 
sounds like yours as well, the _write and _read calls that printf and 
family uses to talk with the "files" are defined within the newlib or 
newlib-nano libraries.  They are defined as weak symbols, which means 
that if you write another function with the same name (and not weak) it 
will override the weak version.  But that depends on it being linked in 
BEFORE the one in the library.
I assume you have have working _read and _write functions that you are 
using to talk with the serial port.  If you compile those functions and 
link them at the same time you compile the main program, it should work. 
If you put them into a library, it is very difficult to convince ld to 
link them instead of the built-in versions.  ld has to find the strong 
versions before the weak ones.
The simple answer is to put those functions into a separate file that is 
always compiled with your main program.  Say you put them in file 
my_io.c then compile your program something like this:
gcc -o myprog.elf main.c my_io.c [other files]
In that case, gcc and ld are forced to see the strong versions before 
trying to link in the weak versions in the library.  It isn't elegant 
and I am still trying to find a better solution.
Hope this helps


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.