IAR9.30.X版本printf重定向串口打印(GD32)

在 IAR 9.30.x版本的 IAR 对 printf 重定向有了新的要求,不能再使用fputc了。解决方法如下:

1、打开Options for node "Project"  ----->General Options

2、可以选择新增文件,或者在工程已有的文件中添加如下代码

/*******************
 *
 * Copyright 1998-2017 IAR Systems AB.
 *
 * This is a template implementation of the "__write" function used by
 * the standard library.  Replace it with a system-specific
 * implementation.
 *
 * The "__write" function should output "size" number of bytes from
 * "buffer" in some application-specific way.  It should return the
 * number of characters written, or _LLIO_ERROR on failure.
 *
 * If "buffer" is zero then __write should perform flushing of
 * internal buffers, if any.  In this case "handle" can be -1 to
 * indicate that all handles should be flushed.
 *
 * The template implementation below assumes that the application
 * provides the function "MyLowLevelPutchar".  It should return the
 * character written, or -1 on failure.
 *
 ********************/

#include <LowLevelIOInterface.h>
#include "gd32f30x_usart.h"//根据自己的串口文件修改(STM32或其他同理)
#include <stdio.h>
#include <stdint.h>

#pragma module_name = "?__write"

int MyLowLevelPutchar(int x)
{
  comSendChar(COM1, x);
  
  return x;
 
}

/*
 * 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)
{
  /* Remove the #if #endif pair to enable the implementation */
#if 1

  size_t nChars = 0;

  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;
  }

  for (/* Empty */; size != 0; --size)
  {
    if (MyLowLevelPutchar(*buffer++) < 0)
    {
      return _LLIO_ERROR;
    }

    ++nChars;
  }

  return nChars;

#else

  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;

#endif

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置iar9.30.1环境,您需要按照以下步骤进行操作: 1. 首先,确保您已经下载并安装了IAR Embedded Workbench软件的9.30.1版本。您可以从IAR Systems官方网站下载安装程序。 2. 打开IAR Embedded Workbench软件,并创建一个新的工程或打开您现有的工程。 3. 在工程窗口中,右键单击您的工程,并选择“Options...”选项。 4. 在弹出的选项对话框中,选择您的目标设备。如果您的目标设备不在列表中,您可以选择“Add...”按钮并添加设备描述文件。 5. 在选项对话框的左侧面板中,选择“Debugger”选项,并确保您选择了正确的调试器。如果您使用的是J-Link调试器,您可以选择“Segger J-Link”并配置连接参数。 6. 在选项对话框的左侧面板中,选择“C/C++ Compiler”选项,并确保您选择了正确的编译器。根据您的需求配置编译器选项,如优化级别、警告级别等。 7. 在选项对话框的左侧面板中,选择“Linker”选项,并配置链接器选项。根据您的需求配置堆栈大小、堆大小、链接脚本等。 8. 完成配置后,点击“OK”按钮保存更改并关闭选项对话框。 9. 现在您可以编译和调试您的工程了。选择“Build”菜单中的“Make”选项编译工程。如果您需要调试工程,您可以选择“Debug”菜单中的“Start Without Download”选项启动调试会话。 请注意,具体的环境配置步骤可能会根据您的具体需求和目标设备而有所不同。上述步骤仅作为一般指南,您可能需要参考IAR Embedded Workbench的用户手册或官方文档以获取更详细的配置信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值