http://home.eeworld.com.cn/my/space-uid-338727-blogid-47176.htmlphp
// 發送數據 int fputc(int ch, FILE *f) { USART_SendData(USART1, (unsigned char) ch);// USART1 能夠換成 USART2 等 while (!(USART1->SR & USART_FLAG_TXE)); return (ch); } // 接收數據 int GetKey (void)
{ while (!(USART1->SR & USART_FLAG_RXNE)); return ((int)(USART1->DR & 0x1FF)); }
#include <stdio.h> #include <rt_misc.h> #pragma import(__use_no_semihosting_swi) extern int SendChar(int ch); // 聲明外部函數,在main文件中定義 extern int GetKey(void); struct __FILE { int handle; // Add whatever you need here }; FILE __stdout; FILE __stdin; int fputc(int ch, FILE *f) { return (SendChar(ch)); } int fgetc(FILE *f) { return (SendChar(GetKey())); } void _ttywrch(int ch) { SendChar (ch); } int ferror(FILE *f) { // Your implementation of ferror return EOF; } void _sys_exit(int return_code) { label: goto label; // endless loop }
三、在main文件中添加定義如下兩個函數html
int SendChar (int ch) { while (!(USART1->SR & USART_FLAG_TXE)); // USART1 可換成你程序中通訊的串口 USART1->DR = (ch & 0x1FF); return (ch); } int GetKey (void) { while (!(USART1->SR & USART_FLAG_RXNE)); return ((int)(USART1->DR & 0x1FF)); }
至此完成配置,能夠在main文件中隨意使用 printf 。編程
http://wojiushiwolxw.spaces.eepw.com.cn/articles/article/item/92847less
標準庫函數的默認輸出設備是顯示器,要實如今串口或LCD輸出,必須重定義標準庫函數裏調用的與輸出設備相關的函數.異步
例如:printf輸出到串口,須要將fputc裏面的輸出指向串口(重定向),方法以下:ide
只要本身添加一個int fputc(int ch, FILE *f)函數,可以輸出字符就能夠了#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */
PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(USART1, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); return ch; }
因printf()之類的函數,使用了半主機模式。使用標準庫會致使程序沒法運行,如下是解決方法:函數
方法1.使用微庫,由於使用微庫的話,不會使用半主機模式.oop
方法2.仍然使用標準庫,在主程序添加下面代碼:
#pragma import(__use_no_semihosting) _sys_exit(int x) { x = x; } struct __FILE { int handle; }; FILE __stdout;
General Options -- Library Configuration -- Library : Full < file descriptor support >post
#include <stdio.h>
#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) #else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) #endif /* __GNUC__ */ PUTCHAR_PROTOTYPE { /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_SendData(USART1, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); return ch; }
General Options -- Library Configuration -- Library : Normal < NO file descriptor support >ui
#include <stdio.h> size_t __write(int handle, const unsigned char * buffer, size_t size) { // byte by byte write } size_t __dwrite(int handle, const unsigned char * buffer, size_t size) { // buffer[ 0x50 ] }
Buffered Terminal Output : Enabled
xxwritebuffered.c
#define STORE_SIZE 80 static size_t storeLen = 0; static unsigned char store[STORE_SIZE];
uint8_t store[ 0x50 ];
uint32_t storelen;
printf() --> __dwrite() : buffer[0x50]
Buffered Terminal Output : Disabled
printf() --> __write(), byte by byte
自定義輸出緩衝區
#define LOG_MAX_STR_LEN 512 void log_printf( const char * fmt, ... ) { char log_buf[ LOG_MAX_STR_LEN ]; va_list args; va_start( args, fmt ); int count = vsnprintf( log_buf, LOG_MAX_STR_LEN, fmt, args ); va_end( args ); // If an output error is encountered, a negative value is returned. if ( count < 0 ) return; // "123456" [123456][0X] : count = 6, n = 8 // "1234567" [1234567][0] : count = 7, n = 8 // "12345678" [1234567][0] : count = 8, n = 8 // "123456789" [1234567][0] : count = 9, n = 8 if ( count >= LOG_MAX_STR_LEN ) count = LOG_MAX_STR_LEN - 1; // now log_buf is C string with the terminating null character __write(0, log_buf, count ); }
log_printf --> __write(), bufferred
http://leon0820.blog.51cto.com/5893766/1440146
在程序的調試過程當中,除了那些高大上的調試手段外,printf無疑是咱們最熟悉最順手的調試方法。
經過使用printf,咱們能夠很方便很直觀的獲取當前程序的運行狀態。
printf()函數是格式化輸出函數, 通常用於向標準輸出設備按規定格式輸出信息。
可是在單片機開發中,通常狀況下並不存在標準輸出設備,所以咱們須要將printf的輸出信息重定向,也就是輸出到其餘輸出設備中去。
在stm32平臺上實現重定向的方式有兩種,重定向至UART,或者經過JTAG的SW模式將printf重定向至SWO引腳輸出。
代碼以下:在對UART進行初始化後,經過以下代碼對printf進行重定向
int fputc(int ch, FILE *f) { USART_SendData(USART1, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} return ch; }
1.在源碼中添加對ITM端口寄存器的定義
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n))) #define ITM_Port16(n) (*((volatile unsigned short*)(0xE0000000+4*n))) #define ITM_Port32(n) (*((volatile unsigned long *)(0xE0000000+4*n))) #define DEMCR (*((volatile unsigned long *)(0xE000EDFC))) #define TRCENA 0x01000000
2.經過以下代碼將printf的輸出重定向至ITM的Port 0
int fputc(int ch, FILE *f) { if (DEMCR & TRCENA) { while (ITM_Port32(0) == 0); ITM_Port8(0) = ch; } return(ch); }
3.經過printf輸出調試信息
printf("AD value = 0x%04X\r\n", AD_value);
4.將Jtag設置爲SW模式,並設置ITM的Port 0 獲取信息。
http://www.dashashi.com/index.php/2014/03/1488
printf在命令行編程的時候是很是經常使用的,雖然是個老函數,可是功能強大,經久不衰
51等8位單片機因爲RAM比較小,棧就比較小,跑printf比較吃力,
可是STM32這種32位單片機跑printf就很容易了,而做爲一種調試手段,printf十分方便、直觀。
比較常見的方法是把printf重定向到串口,不過這須要外接一個串口線,比較麻煩。
其實STM32自帶的SWO口是可以異步輸出數據的,並且不須要外接什麼設備,
ST-LINK/J-Link等帶SWO口的調試器都支持。
下面以STM32F4Discovery開發板+GCC爲例說明。
根據這裏的方法,也能夠把printf定位到其餘外設。
PS:IAR在編譯選項裏自帶了printf via SWO的功能,就不須要外加設置了。
http://community.silabs.com/t5/Microcontroller-How-to-Guides/SWO-printf-in-IAR/td-p/98257
首先來講說怎麼把信息輸出到SWO口,一句話搞定。
ITM_SendChar(ch);
這是在core_cm4.h(若是是F1系列的那就是core_cm3.h)中定義的內聯函數。
不過不須要特地去include這個頭文件,經過#include "stm32f4xx.h"就間接地將core_cm4.h包含進來。
不過提及來,ITM這個東西其實嚴格來講是Cortex-M提供的一個特性,而不是STM32。
利用這個函數把信息輸出到SWO口以後再打開St-Link Utility,
在菜單裏找到ST-LINK→Printf via SWO Viewer就會彈出一個窗口,
設置System Clock爲單片機內核頻率,點Start就能看到輸出的信息了。
接下來就是把printf函數輸出的字符串重定向過去了。
因爲單片機的外設功能是根據需求變化的,編譯器不可能肯定printf須要用到的外設資源,
因而乎它就乾脆留了個接口,也就是_write函數,
固然除了_write函數以外還有_read等其餘函數,不過這裏咱們用不到。
其聲明爲 int _write(int fd, char* ptr, int len);
關於_write函數,說簡單點,就是全部涉及到輸出字符串的函數,
好比printf和putchar(),最終都會跑到_write函數,這裏fd是文件標識符,說開來就比較複雜了,
這裏咱們用獲得的就只有STDOUT_FILENO跟STDERR_FILENO,
其中前一個是標準輸出的文件標識符的預約義變量,後一個是錯誤輸出的文件標識符預約義變量。
第二個變量ptr是須要輸出的字符串首地址,len就是輸出長度。
當咱們調用printf函數後,C運行庫會把輸入變量轉換爲最終須要輸出的字符串,
而後調用_write函數,將結果輸出。咱們的工做就是實現一個_write函數。
新建一個_write.c文件,內容以下:
#include <stdio.h> #include <unistd.h> #include "stm32f10x.h" #ifdef _DEBUG int _write(int fd, char* ptr, int len) { if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { int i = 0; while(i<len) ITM_SendChar(ptr[i++]); } return len; } #endif
加了個#ifdef _DEBUG 的效果是未加 _DEBUG 定義的時候就忽略下面的東西,
由於這東西主要是用在調試階段,RELEASE版本里面都用不到了,並且多少仍是會影響速度。
其餘東西就很簡單了- -不須要多說明了吧。
直接編譯能經過,可是連接會報錯,提示沒法找到_read之類的一堆函數。
在連接腳本的下面libgcc.a ( * )後面加上libnosys.a ( * ),就不會報錯了。
具體緣由涉及到Cortex-M3使用的newlib庫的實現,就不具體展開了。
好吧好吧,其實我也不知道。
若是想把信息定位到串口,能夠直接把ITM_SendChar改爲相應的串口函數,
也能夠利用DMA,先把數據拷貝到DMA緩衝區,讓DMA自動傳數據,提升響應速度。
http://blog.sina.com.cn/s/blog_79b01f6601018ymr.html
1) 加入stdio.h,這樣你就能夠調用printf函數了
2) 使能SWO輸出
使能SWO輸出。最簡單的辦法就是將以下的函數拷貝到你的工程裏面,而且在mian函數初始化以後調用該函數。
void setupSWO(void) { uint32_t *dwt_ctrl = (uint32_t *) 0xE0001000; uint32_t *tpiu_prescaler = (uint32_t *) 0xE0040010; uint32_t *tpiu_protocol = (uint32_t *) 0xE00400F0; CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; GPIO->ROUTE |= GPIO_ROUTE_SWOPEN; #if defined(_EFM32_GIANT_FAMILY) GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC0; GPIO->P[5].MODEL &= ~(_GPIO_P_MODEL_MODE2_MASK); GPIO->P[5].MODEL |= GPIO_P_MODEL_MODE2_PUSHPULL; #else GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC1; GPIO->P[2].MODEH &= ~(_GPIO_P_MODEH_MODE15_MASK); GPIO->P[2].MODEH |= GPIO_P_MODEH_MODE15_PUSHPULL; #endif CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN; while(!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY)); CoreDebug->DHCSR |= 1; CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; *dwt_ctrl = 0x400113FF; *tpiu_prescaler = 0xf; *tpiu_protocol = 2; ITM->LAR = 0xC5ACCE55; ITM->TCR = 0x10009; }
3) 配置Keil的工程選項
打開Keil的工程配置,選擇Debug頁面,選擇仿真器爲Cortex-M/R J-Link/J-Trace, 並點擊仿真器選項邊上的setting選項,打開具體的設置窗口。
在打開的窗口中,切換到Trace頁面,選中Enable,而且設置Core Clock爲14MHz,分頻選項爲Core Clock/16。詳情以下:
4) 在初始化SWO函數以後的地方,使用printf函數進行輸出。例如printf("Hello world")。
5) 在你的工程裏面,須要添加以下的函數:
struct __FILE
{
int handle;
};
FILE __stdout; FILE __stdin;
int fputc(int ch, FILE *f)
{ ITM_SendChar(ch); return(ch); }
6) 編譯你的代碼,而且進入Debug狀態
7) 打開Keil的printf-view窗口, 經過 View -> Serial Windows -> Debug(printf) View
8) 點擊運行以後,在Debug (printf) View裏便可查看
http://www.keil.com/support/man/docs/jlink/jlink_trace_itm_viewer.htm