cancel
Showing results for 
Search instead for 
Did you mean: 

printf(), retarget and syscalls.c

Konami
Senior II

I'm bringing up an STM32F103 part using a Makefile project generated by CubeMX. Previously I've always used the Keil IDE and during this switch I encountered some difficulty with retargetting printf. I implemented my fputc prototype as I usually would be nothing was working. I then realized that I should have been using the __io_putchar prototype since I'm using GCC. However that still did not work until I finally realized I needed to include syscalls.c. Everything is working now, but I'm still unclear on a few things I was hoping could be cleared up.

  1. What is syscalls.c and why have I not encountered it at all while using Keil? I can't even find it when trying to look through a previous project.
  2. Before adding syscalls.c my code was still compiling but it seemed like printf had no effect. What was I calling when I called printf and why was the compiler not throwing an error?
  3. What's the proper way to debug this kind of issue in the future? Ie. I'm calling printf but nothing is happening and I can't step into it in the debugger.
  4. Why does Keil's toolchain use fputc instead of __io_putchar, and what's the difference?

1 REPLY 1

Keil != GNU/GCC, they have been doing embedded systems with semi-hosting for decades, and the libraries and methods are different. The Keil and IAR tool chains are not derived from GNU/GCC, and consequential have solved problems differently.

In GNU/GCC the File IO, abort handlers, and heap allocator code are handled in a syscalls or newlib file. The startup.s file also has to manage symbols and statics set up in the linker script, whereas the Keil linker builds a table structure describing the load regions and unpacks this with the __main library function before calling your main() function.

The libraries can link due to "weak" functions that provide default behaviour, but can be replaced by user and board specific code when/if you choose to supply it.

I guess by understanding how your selected tools function, rather than stabbing around in the dark.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..