大家單片機玩得很溜了,平時也很少去關注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;
}
審核編輯 :李倩
-
芯片技術
+關注
關注
1文章
161瀏覽量
17594 -
APM
+關注
關注
1文章
71瀏覽量
13045 -
SDK
+關注
關注
3文章
1045瀏覽量
46267 -
Printf
+關注
關注
0文章
83瀏覽量
13732
原文標題:APM32芯得 EP.07 | APM32F407 使用IAR9.3調用printf打印信息
文章出處:【微信號:geehysemi,微信公眾號:Geehy極海半導體】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論