IAR 9.30.xx 无法使用 printf 函数串口打印的问题解决

在升级到IAR9.30.xx版本后,printf函数无法进行串口打印。问题源于新版本对printf重定向的要求变化。为解决此问题,需要在源码目录下创建write.c文件,并添加相应的重定向代码,实现串口打印功能。代码示例中展示了如何根据UART设置重定向printf到串口。参考链接提供了详细步骤和代码实现。

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值