如何使printf在STM32F103上工作? [英] How to make printf work on STM32F103?
问题描述
我对STM32F103还是陌生的.我有一个用于STM32F103的演示代码,我正在使用arm-none-eabi进行编译.
I am new to the world of STM32F103. I have a demo code for STM32F103 and I am using arm-none-eabi to compile it.
我尝试了在Google上可以找到的东西,但到目前为止没有任何效果.我已经花了三天时间解决这个问题.
I tried what I could find on Google, but nothing worked so far. I have already spent three days on the problem.
任何人都可以给我一个有关printf的演示代码,该代码可以正常工作吗?
Anyone can give me a demo code for printf which works well?
我的makefile的一部分:
Part of my makefile:
CFLAG = -mcpu=$(CPU) -mthumb -Wall -fdump-rtl-expand -specs=nano.specs --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lm -lrdimon -Wl,--end-group
LDFLAG = -mcpu=$(CPU) -T ./stm32_flash.ld -specs=nano.specs --specs=rdimon.specs -Wl,--start-group -lgcc -lc -lm -lrdimon -Wl,--end-group
推荐答案
编写自己的printf
实现是一种选择,在我看来,这可能是最推荐的选择.从标准库实现中获得一些启发,并编写自己的版本,仅能满足您的要求.通常,您需要做的是,首先将putc
函数重新定位为通过串行接口发送char.然后使用putc
自定义实现覆盖printf
方法.也许,一种非常简单的方法是通过递归调用putc
函数来按字符发送字符串.
Writing an own printf
implementation is an option, and probably the most recommended option according to me. Get some inspiration from the standard library implementation and write your own version, only to cater your requirements. In general, what you have to do is, first retarget a putc
function to send char s through your serial interface. Then override the printf
method by using the putc
custom implementation. Perhaps, a very simple approach is sending the string character-wise by recursive calls for putc
function.
最后但并非最不重要的是,您可以找到一些轻量级的printf
实现.这些轻量级实现提供的代码大小和功能集位于自定义编写的printf
函数和常规标准printf
函数(即野兽)之间.我最近已经尝试过 Tiny Printf ,并对其在ARM内核上的性能感到非常满意内存占用量和所需的执行周期数.
Last but not least, you can find some lightweight printf
implementations. The code size and the set of features offered by these lightweight implementations lie in between the custom written printf
function and the stock standard printf
function (aka the beast). I have recently tried this Tiny Printf and very pleased with its performance on an ARM core in terms of memory footprint and the number of execution cycles required.
-PS
从我自己的著作有时复制而来
Copied from my own writings sometime back.
这篇关于如何使printf在STM32F103上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!