0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

APM32F4xx_SDK_V1.1中使用IAR9.3进行编译使用printf功能

Geehy极海半导体 来源:21ic论坛 作者:21ic 2022-08-11 10:36 次阅读

单片机玩得很溜了,平时也很少去关注VDD波形,整机测试无异常就完事了。也确实,芯片技术发展多年 前言 最近发现IAR 发布了新版本9.30.1,在新版本中Geehy的众多MCU都完成了支持。 16e0dcf2-191d-11ed-ba43-dac502259ad0.png  我这边刚好有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”。

170b6ff8-191d-11ed-ba43-dac502259ad0.jpg

至此程序中便可以正常使用printf函数。 2 IAR 9.x的printf重定向 通过查阅IAR的开发文档。(在Help选项卡下打开“C/C++ Development Guide”)

1727e4da-191d-11ed-ba43-dac502259ad0.jpg

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

17419bdc-191d-11ed-ba43-dac502259ad0.png

重定向的详细内容请查阅文档,此处不在赘述。这里给出参考操作。 1. 在源码目录下,新建“write.c”文件用于存放我们重定向的代码。 2. 打开需要重定向的工程,在工程中添加我们刚刚新建的“write.c”。

1775fbc0-191d-11ed-ba43-dac502259ad0.jpg

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

#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

{

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进行打印的全部内容。

审核编辑 :李倩


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 芯片技术
    +关注

    关注

    1

    文章

    156

    浏览量

    17019
  • APM
    APM
    +关注

    关注

    0

    文章

    69

    浏览量

    12856
  • SDK
    SDK
    +关注

    关注

    3

    文章

    966

    浏览量

    44682
  • Printf
    +关注

    关注

    0

    文章

    79

    浏览量

    13478

原文标题:APM32芯得 EP.07 | APM32F407 使用IAR9.3调用printf打印信息

文章出处:【微信号:geehysemi,微信公众号:Geehy极海半导体】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    在macos下SW4STM32编译,调用printf异常怎么解决?

    刚开始在MACOS下开发STM32的程序,现在出现了一些很奇怪的问题,看是否大家有遇到这样的问题。 开发配置如下 1.STM32cube427,STM32Cube FW_F4 V
    发表于 04-17 06:48

    先楫半导体 hpm_sdk v1.5.0 正式发布

    先楫半导体 hpm_sdk v1.5.0 正式发布!功能升级更强大 版本更新概况 新支持的IDE IAR Embedded Workbench for RISC-
    发表于 04-08 11:14

    STM32G4XX不能够像STM32F1XX STM32F4XX那样对GPIO进行位带操作呢?

    请教下,在STM32G4XX系列里面,GPIO挂载在AHB2总线上面,地址在0x4800 0000. 是不是也就意味着,STM32G4XX不能够在像STM32F1XX STM32F4XX
    发表于 03-21 07:16

    AT32F4xx I2C使用中断进行主机发送从机接收

    AT32F4xx I2C使用中断进行主机发送从机接收演示AT32F4xx I2C使用中断进行主机发送数据从机接收数据通信。
    发表于 10-27 08:30

    AT32F4xx I2C使用中断进行主机接收从机发送

    AT32F4xx I2C使用中断进行主机接收从机发送演示AT32F4xx I2C使用中断进行主机接收数据从机发送数据通信。
    发表于 10-27 08:29

    AT32F4xx_I2C使用DMA进行主机接收从机发送

    AT32F4xx_I2C使用DMA进行主机接收从机发送演示AT32F4xx I2C使用DMA进行主机接收数据从机发送数据通信。
    发表于 10-27 06:53

    AT32F4xx MPU的使用

    AT32F4xx MPU的使用简单介绍对Memory Protection Unit功能如何使用和配置。
    发表于 10-26 06:47

    AT32F4xx_I2C使用DMA进行主机发送从机接收

    AT32F4xx_I2C使用DMA进行主机发送从机接收演示AT32F4xx I2C使用DMA进行主机发送数据从机接收数据通信。
    发表于 10-26 06:04

    IAR9.3以上版本调试注意事项

    IAR 9.3以上版本调试注意事项
    发表于 10-23 07:40

    AT32F4xx ERTC和RTC的毫秒计时功能

    AT32F4xx RTC/ERTC 毫秒计时演示AT32F4xx ERTC和RTC的毫秒计时功能
    发表于 10-19 08:08

    瑞芯微RK3568|SDK开发之Buildroot编译

    Buildroot查询帮助查看buildroot的详细编译命令,如下所示。图1.1编译文件系统以上命令为,配置buildroot对应的默认配置defconfig,然后编译。注:buil
    的头像 发表于 10-08 10:38 607次阅读
    瑞芯微RK3568|<b class='flag-5'>SDK</b>开发之Buildroot<b class='flag-5'>编译</b>

    【Milk-V Duo 开发板首发免费试用】环境搭建、编译、运行

    sudo apt update sudo apt install libssl1.1 3.进行编译,执行一键编译 cd duo-buildroot-
    发表于 07-23 17:32

    ML51 IAR Compile printf出错是什么原因?怎么解决?

    刚刚用IAR搭起来ML51的环境,编译sample code时,发现每次编译printf,就报如下错误: Error[Pe167]: argument of type \"char
    发表于 06-21 08:35

    IAR Embedded Workbench中进行ARM+RISC-V多核调试

    在之前的文章,我们介绍了如何在IAR Embedded Workbench for Arm中进行多核调试,其中所有的CPU内核都是基于ARM架构。近些年来,随着RISC-V的兴起,不
    发表于 06-14 16:55

    为什么不能在esp8266 sdk_v1.4.0中使用PIN_FUNC_SELECT宏?

    为什么我不能在 esp8266 sdk_v1.4.0 中使用 PIN_FUNC_SELECT 宏。它在 eagle_soc.h 定义。每当我使用这个宏将我的引脚设置为 gpios 时,就会出现错误
    发表于 06-12 07:39