STM32F1 串口输出Printf 输出重定向

注意下列代码huart1串号对齐。

增加以下代码:(增加输出重定向会消耗大概7K左右内存。)

#include "stdio.h"
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//使用串口发送出去

    return ch;
}

如果输出中文乱码

把代码UTF-8转换为GBK格式

如果需要支持浮点数输出

把 #add_compile_options(-mfloat-abi=soft)  前面的#取消掉,增加后会消耗11.3K内存。

 

 

 如果是老版本CubeMX 在CMakeLists.txt 增加 -u _printf_float 让该函数例外不要被前面的-specs 裁剪缩小内存。

 

posted @ 2021-01-27 15:10  河岸瓶风  阅读(362)  评论(0编辑  收藏  举报