Retarget Printf To USART In STM32 Develop Using GCC Toolchain

Compiler:Yagarto or CodeSourcery Lite(Not tested but should work)

Linker&Startup File: Using the default linker & startup file provided in the STM32 StdPeriph Library for TrueStudio.Any version should do but I used V3.5.

Syscalls.c: implement the basic needed system calls.Define the usart port you want to retarget the printf function by modify below section or setup a compiler define

#ifndef STDOUT_USART
#define STDOUT_USART 2
#endif

#ifndef STDERR_USART
#define STDERR_USART 2
#endif

#ifndef STDIN_USART
#define STDIN_USART 2
#endif

After adding this file to your project you will be able to call printf which is implemented by new lib by #include  "stdio.h"in your code.

As we all know the printf implemented in new lib will take a huge amount of RAM.This even lead to a stack overflow in STM32F103RB with only 20K RAM.

That's why we need:

printf.c:this file provide a private implement of printf beside the built in printf of new lib.Which require much less RAM in the while.

Is that all?

Surely not!

/***************************************************************/

 

Issue Description

You will find a very weird phenomenon:

When you call printf with conversion specifiers like this:

printf("Test number is:%d\r\n",TestNumber);

the printf provided above will be called.

while using printf directly with a string like:

printf("hello world\r\n");

the built in printf will be called again.

/***************************************************************/

A simple workaround is rename printf function in printf.c  to any other name you like such as uprintf. Then just use uprintf instead of printf.

What a coward!You can't even face the problem.

After reading the GCC compiler guide, I found a simple parameter which fix this problem.

-fno-builtin

By adding this parameter to your IDE's compile command, the built in function will be disabled and only printf in printf.c will be called.

For SlickEdit user,below is the full command for compile:

arm-none-eabi-gcc -c -fdata-sections -ffunction-sections -fno-builtin -O0 -mcpu=cortex-m3 -mthumb -g -gdwarf-2 -Wall %i %defd -o "%bd%n.o" "%f"

Surely you can fine tune the optimize level or the debug infomation format by yourself.

暂时没有引用通告

TrackBack URL: