vscode+stm32重定向printf函数

/*IAR编译器使用printf重定向*/
int fputc(int ch,FILE *file)
{
	while(__HAL_UART_GET_FLAG(bsp_log.port, UART_FLAG_TC) == 0);
	HAL_UART_Transmit(bsp_log.port, (uint8_t*)&ch,1,0xff);
	return ch;
}

/*vscode使用printf重定向*/
int _write(int fd, char *pBuffer, int size)
{
    // for (int i = 0; i < size; i++)
    // {
    //     while ((USART1->SR & 0X40) == 0)
    //         ;                     //等待上一次串口数据发送完成
    //     USART1->DR = (u8)pBuffer; //写DR,串口1将发送数据
    // }
    while (__HAL_UART_GET_FLAG(bsp_log.port, UART_FLAG_TC) == 0)
        ;
    HAL_UART_Transmit(bsp_log.port, (uint8_t *)pBuffer, size, 0xff);
    return size;
}

#Makefile中添加
    -u _printf_float

例:# libraries
    LIBS = -lc -lm -lnosys 
    LIBDIR = 
    LDFLAGS = $(MCU) -specs=nano.specs -u _printf_float -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

 

你可能感兴趣的:(STM32)