#include "stdio.h"
#include "stm32f4xx_hal.h" // 根据你的型号修改,如 stm32h7xx_hal.h
extern UART_HandleTypeDef huart1; // 声明你使用的串口句柄
/* 重定向 fputc 到串口 */
#if defined(__GNUC__)
/* GCC / AC6 (STM32CubeIDE) 编译器通用的 _write 重写 */
int _write(int file, char *ptr, int len)
{
HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, HAL_MAX_DELAY);
return len;
}
#elif defined(__CC_ARM) || defined(__CLANG_ARM)
/* Keil MDK (ARMCC / AC6) 编译器 */
#ifdef __MICROLIB
/* 如果开启了 MicroLIB,重写 __io_putchar */
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#else
/* 如果没有开启 MicroLIB(使用标准库),必须重写 fputc */
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
#endif /* __MICROLIB */
#endif /* __GNUC__ */
|