mouse_ 发表于 2018-1-19 16:44:09

Eclipse 构建 STM32 工程中 如何实现printf函数

自己搭建了 stm32 Eclipse 的工程 但是无法实现prinf函数 想请教一下各路神仙怎么实现啊

pythonworld 发表于 2018-1-19 19:27:10

                                                                           STM32中printf重定向到串口
      学习STM32过程中,经常打交道的莫过于串口,你可以将任何信息,当然重要的是调试信息打印到串口中输出,总是用一个字节发送函数或者字符串发送函数,总是有些不放便,之前编程中熟悉的莫过于printf了,下面就给出了用printf打印到串口的方案,当然方案不止一个,仅供参考。

1、 添加printf的头文件   #include <stdio.h>
2、重写int fputc(int ch, FILE *f)函数
int fputc(int ch, FILE *f);
函数

int fputc(int ch, FILE *f)
{

USART_SendData(USART1, (uint8_t) ch);

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);

return ch;
}

3、修改一下选中Use MicroLIB   Target——Code Generation——选中Use MicroLIB



这样就可以像以前那样使用printf了。http://blog.csdn.net/wdscq1234/article/details/7944036

Inc_brza 发表于 2018-1-19 20:11:00

pythonworld 发表于 2018-1-19 19:27
STM32中printf重定向到串 ...

你这个是错的,因为环境是gcc而不是MDK。
楼主在问问题钱不会搜论坛就直接发帖子,资源太浪费了。
参考下面的帖子吧
https://www.stmcu.org.cn/module/forum/thread-612930-1-1.html
https://www.stmcu.org.cn/module/forum/forum.php?mod=viewthread&tid=603927&highlight=printf

maxtch 发表于 2018-1-19 20:27:19

其实 GCC 环境下 newlib 已经有 printf 了,反倒是缺 _write。

我这里有参考代码(我因为有不止一个设备会用 write 所以加了一层抽象,但是概念是类似的):http://github.com/SushiBits/LCDClock-Firmware/blob/master/system/src/usart.c

mouse_ 发表于 2018-1-23 09:30:58

Inc_brza 发表于 2018-1-19 20:11
你这个是错的,因为环境是gcc而不是MDK。
楼主在问问题钱不会搜论坛就直接发帖子,资源太浪费了。
参考下 ...

试过了还是不行

chrome777 发表于 2018-1-24 16:11:03

Eclipse对C库支持的不好,默认的是newlib库,你得添加对C库的函数支持,这里有点文章你参考下,应该能帮到你。
http://blog.csdn.net/qiuzhiqian1990/article/details/55190018?locationNum=10&fps=1

pythonworld 发表于 2018-2-26 19:06:56

一下代码可用,请参考!!:victory:
#include <stdio.h>
#include <unistd.h>
#include "stm32l4xx_hal.h"

extern UART_HandleTypeDef hlpuart1;
int _write(int32_t fd, char* ptr, int32_t len);

int _write(int32_t fd, char* ptr, int32_t len) {
        if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
                int32_t i = 0;
                while (i < len)
                        HAL_UART_Transmit(&hlpuart1, (uint8_t *) &ptr, 1, 0xFFFF);

        }
        return len;
}

Han0097 发表于 2020-8-17 11:12:05

pythonworld 发表于 2018-1-19 19:27
STM32中printf重定向到串 ...

人家说的是Eclipse不是KEIL:L
页: [1]
查看完整版本: Eclipse 构建 STM32 工程中 如何实现printf函数