发新帖本帖赏金 20.00元(功能说明)我要提问
123下一页
返回列表
打印
[APM32F4]

优雅使用IAR9.3调用printf打印信息

[复制链接]
5103|58
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 kai迪皮 于 2022-6-23 17:09 编辑

#申请原创# @21小跑堂

前言

最近发现IAR 发布了新版本9.30.1,在新版本中Geehy的众多MCU都完成了支持。



我这边刚好有APM32F407IG的MINIBOARD,想着使用IAR编译一下工程下载运行一下程序。不料却发现了问题,APM32F4xx_SDK_V1.1中的IAR例程是基于 IAR8.5制作的,关于printf的重定向都已经在工程内进行了设置。使用8.5的工程也可以正常使用printf功能。但在9.x的IAR对printf重定向有了新的要求,导致IAR 9.x版本无法运行含有printf功能的例程。

本文档就解决APM32F4xx_SDK_V1.1中使用IAR9.3进行编译使用printf功能进行记录分享。

值得注意的是:

- printf重定向前需完成对应串口的初始化操作。

1 IAR 8.x的printf重定向

IAR 8.x的printf重定向与Keil并无差异,仅需在内部的任意一个C文件中重定向printf后设置工程的相应参数即可完成。

1. 重定向printf代码如下(发送串口为串口1,头文件需包含stdio.h)
/* Includes */
#include "stdio.h"


/*!
* [url=home.php?mod=space&uid=247401]@brief[/url]       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*/
int fputc(int ch, FILE *f)
{
    /** send a byte of data to the serial port */
    USART_TxData(DEBUG_USART,(uint8_t)ch);

    /** wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return (ch);
}


2. 设置工程相应参数,将General Option 的 Library Configuration 选项卡下Library选择为“Full”,CMSIS 项目勾选“Use CMSIS”。

  

至此程序中便可以正常使用printf函数。

2 IAR 9.x的printf重定向

通过查阅IAR的开发文档。(在Help选项卡下打开“C/C++ Development Guide”)



在开发文档的“BRIEFLY ABOUT RETARGETING”章节,我们可以看到IAR9.x要求我们使用printf时需要重定向__write函数。



重定向的详细内容请查阅文档,此处不在赘述。这里给出参考操作。

1. 在源码目录下,新建“write.c”文件用于存放我们重定向的代码。

2. 打开需要重定向的工程,在工程中添加我们刚刚新建的“write.c”。



3. 编辑“write.c”文件,添加重定向代码。代码内容如下。

/*******************
*
* 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 "Board.h"
#include "apm32f4xx.h"

#pragma module_name = "?__write"

uint8_t USART_Transmit(USART_T* 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(USART_Transmit(USART1,(uint8_t *)buffer,size) == 1)
  {
    return size;
  }
  else
  {
    return _LLIO_ERROR;
  }
}

uint8_t USART_Transmit(USART_T* usart, uint8_t *pdata, uint16_t Size)
{
  uint8_t ch = 0;
  uint16_t i = 0;
  uint16_t timeout = 0x1000;
  
  for(i=0;i<Size;i++)
  {
    ch = pdata[i];
   
    /** send a byte of data to the serial port */
    USART_TxData(usart,(uint8_t)ch);

    /** wait for the data to be send  */
    while ((USART_ReadStatusFlag(usart, USART_FLAG_TXBE) == RESET) && (timeout -- ));
   
    if(timeout == 0)
    {
      return 0;
    }
   
    timeout = 0x1000;
   
  }
  
  return 1;
}


    __write函数为IAR要求我们进行重定向的函数,这里使用的发送串口是串口1,若需要使用其他串口,请相应修改“USART_Transmit(USART1,(uint8_t *)buffer,size) == 1”中的USART1参数。

    USART_Transmit函数是为了便捷使用发送功能进行定义的函数,其采用的是轮训发送的操作,有超时设置,返回1为发送成功,返回0为发送失败。

至此程序中便可以正常使用printf函数。



以上便是APM32F4xx_SDK_V1.1中的IAR工程使用9.30打开后如何使用printf进行打印的全部内容。

APM32F4xx_SDK_V1.1_IAR 9.30.1.zip

298.52 KB

使用特权

评论回复

打赏榜单

21小跑堂 打赏了 20.00 元 2022-06-28
理由:恭喜通过原创文章审核!请多多加油哦!

评论
kai迪皮 2022-10-9 23:43 回复TA
@billzeng :一样的,主要修改一下USART_Transmit函数的内容实现即可。里面的内容使用ST的库进行实现。 
billzeng 2022-7-16 20:08 回复TA
@kai迪皮 :大师,STM32需要怎么改? 
billzeng 2022-7-16 20:06 回复TA
大师,STM32需要怎么改? 
kai迪皮 2022-7-1 09:29 回复TA
@21小跑堂 :谢谢支持 
21小跑堂 2022-6-28 16:01 回复TA
升级最新IAR软件,发现问题,解决问题,通过自己的努力解决最新版本printf重定向的问题。 
沙发
caigang13| | 2022-7-1 18:25 | 只看该作者
学习了,谢谢楼主分享。

使用特权

评论回复
板凳
chenjun89| | 2022-7-3 10:39 | 只看该作者
下载看看,谢谢分享。

使用特权

评论回复
地板
weifeng90| | 2022-7-4 08:16 | 只看该作者
IAR居然都更新到9版本了

使用特权

评论回复
5
linfelix| | 2022-7-4 15:32 | 只看该作者
自己重映射吧。   

使用特权

评论回复
评论
kai迪皮 2022-10-19 09:31 回复TA
是的,自己完成重映射 
6
selongli| | 2022-7-4 15:48 | 只看该作者
IAR效果怎么样   

使用特权

评论回复
7
七毛钱| | 2022-7-4 15:53 | 只看该作者
下载看看感谢分享

使用特权

评论回复
8
nomomy| | 2022-7-4 16:14 | 只看该作者
这个跟keil一样的。   

使用特权

评论回复
9
iyoum| | 2022-7-4 17:00 | 只看该作者
这个sprintf也可以使用。   

使用特权

评论回复
10
两只袜子| | 2022-7-5 15:07 | 只看该作者
我优雅的点开这篇文章优雅的看一看,哈哈

使用特权

评论回复
11
uytyu| | 2022-7-5 16:03 | 只看该作者
printf重定向与Keil并无差异  

使用特权

评论回复
12
bartonalfred| | 2022-7-5 17:04 | 只看该作者
是自带的库吗?  

使用特权

评论回复
评论
kai迪皮 2022-7-6 11:21 回复TA
APM32的官方标准库 
13
isseed| | 2022-7-5 17:56 | 只看该作者
printf应用还是比较多的。   

使用特权

评论回复
评论
kai迪皮 2022-7-6 11:21 回复TA
是的,printf很常用 
14
chenqianqian| | 2022-7-6 08:21 | 只看该作者
学习了,谢谢楼主分享经验。

使用特权

评论回复
15
cashrwood| | 2022-7-8 10:12 | 只看该作者
如何在C语言中用printf()输出某个值的地址?

使用特权

评论回复
16
i1mcu| | 2022-7-8 15:24 | 只看该作者
自动使用stdio标准输入输出的printf函数

使用特权

评论回复
17
xiaoyaozt| | 2022-7-8 16:16 | 只看该作者
怎样用一个printf函数输出多行字符

使用特权

评论回复
18
pl202| | 2022-7-9 13:09 | 只看该作者
printf是格式化输出函数,它可以直接打印十进制,八进制,十六进制,输出控制符分别为%d, %o, %x

使用特权

评论回复
19
plsbackup| | 2022-7-9 14:33 | 只看该作者
printf函数用来打印输出的调试信息

使用特权

评论回复
20
htmlme| | 2022-7-9 16:12 | 只看该作者
开发板中是如何实现printf这个打印的?

使用特权

评论回复
发新帖 本帖赏金 20.00元(功能说明)我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

31

主题

214

帖子

11

粉丝