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时,如果需要将printf函数的输出重定向到特定的设备或接口,需要通过重定向fputc函数来实现。首先,需要包含头文件stdio.h,并在代码中实现fputc函数重定向。通过重定向,将printf函数的输出重定向到USART1,即将调试信息打印到USART1中。具体的代码实现如下: ```c #include <stdio.h> // 重定向fputc函数 int fputc(int ch, FILE *f) { // 将字符发送到USART1 USART_SendChar(USART1, (uint8_t)ch); // 返回字符 return ch; } int main(void) { USART_Configuration(); // 使用printf函数输出调试信息 printf("\r\nstm32f103rct6\r\n"); printf("\r\nCortex-M3\r\n"); while (1); return 0; } ``` 通过重定向fputc函数,可以将printf函数的输出重定向到USART1,并将调试信息打印到该设备中。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [STM32使用printf重定向](https://blog.csdn.net/weixin_48120109/article/details/126474339)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [STM32printf函数重定向](https://blog.csdn.net/qq_29344757/article/details/75363639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值