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

大家单片机玩得很溜了,平时也很少去关注vdd波形,整机测试无异常就完事了。也确实,芯片技术发展多年  前言  最近发现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
#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;
}


灵伴科技获重金投资,ABB 收购 Sevensense 加强机器人感知
工程车辆无线遥控快速实现方案
预计物联网商机会在2025年报到
电容 的耐压和绝缘电阻
为什么电脑开机会自动打开文件或者文件夹
APM32F4xx_SDK_V1.1中使用IAR9.3进行编译使用printf功能
测温枪的原理是怎样的
简易病房床位呼叫器电路图
激发无限创新灵感 Intel Edison登陆Mouser
Google浏览器已经具有安全保存密码并填写所需空间的功能
全新的物联网操作系统AWorks IoT OS
317芯片引脚怎么看
米动手环2体验 到底好不好用
微软推Surface Connect转USB Type-C适配器,售价19.99美元起
基于火花塞离子电流检测技术设计应用
MAX19790应用电路--250MHz至4000MHz、双
史陶比尔公司推出适用于汽车行业的激光切割机器人
探讨FPGA的无线传感网络信道波形整形滤波器
中兴王继刚:在5G行业应用中需加强行业核心资产的安全
Web3.0展望