IAR 9.30.xx 无法使用 printf 函数串口打印的问题解决
问题描述
使用 IAR 9.22.xx 打开工程并调试程序时,可以正常使用 printf 函数进行串口打印,但用新发布的版本 IAR 9.30.xx 打开工程后,出现了无法使用 printf 函数进行串口打印的问题(除了无法进行串口打印外,程序正常运行且其它功能正常)。
问题分析
在 IAR 9.30.xx 的 IAR 对 printf 重定向有了新的要求,导致 IAR 9.22.xx 版本无法运行含有 printf 功能的例程。
问题解决
IAR 9.30.xx 的 printf 重定向
重定向的详细内容请查阅文档,这里给出参考操作。
- 在源码目录下,新建
write.c
文件用于存放我们重定向的代码; - 打开需要重定向的工程,在工程中添加我们刚刚新建的
write.c
; - 编辑“write.c”文件,添加重定向代码。代码内容如下,需要根据 UART 设置修改相关函数。
#include <LowLevelIOInterface.h>
#include <stdint.h>
#include "bzero_bsp_uart.h"
#pragma module_name = "?__write"
extern UART_HandleTypeDef huart1;
//uint8_t USART_Transmit(UART_HandleTypeDef* usart, uint8_t *pdata, uint16_t Size);
/*
* If the __write implementation uses internal buffering, uncomment
* the following line to ensure that we are called with "buffer" as 0
* (i.e. flush) when the application terminates.
*/
size_t __write(int handle, const unsigned char * buffer, size_t size)
{
if (buffer == 0)
{
/*
* This means that we should flush internal buffers. Since we
* don't we just return. (Remember, "handle" == -1 means that all
* handles should be flushed.)
*/
return 0;
}
/* This template only writes to "standard out" and "standard err",
* for all other file handles it returns failure. */
*
if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
{
return _LLIO_ERROR;
}
/* Sending in normal mode */
if(HAL_UART_Transmit(&huart1, (uint8_t *)buffer, size, 0xFFFF) == HAL_OK)
{
return size;
}
else
{
return _LLIO_ERROR;
}
}
参考
https://blog.csdn.net/ic2121/article/details/125529481
https://bbs.21ic.com/icview-3234134-1-1.html