對於stlink的驅動好像也有一些要求,我使用keil4帶的有點舊的stlink驅動就會提示驅動不支持,後來使用keil5帶的stlink驅動據可以了。
如果硬件和軟件都沒有問題了,還需要設置下kei:
首先打開Micro LIB
然後打開keil的Trace功能,具體設置如下:時鐘一定要對應上,否則是亂碼。
然後就是一些重定向printf的函數了,如果之前將printf重定向了串口,那麼要修改並添加如下代碼:
- #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
- struct __FILE { int handle; /* Add whatever needed */ };
- FILE __stdout;
- FILE __stdin;
- int fputc(int ch, FILE *f) {
- if (DEMCR & TRCENA) {
- while (ITM_Port32(0) == 0);
- ITM_Port8(0) = ch;
- }
- return(ch);
- }
打開調試即可得到調試信息:
輸出調試信息
測試代碼:
F303RE_SWO.rar(394.54 KB, 下載次數: 307)
*******************************************一天過去了***************************************
上面說了輸出的功能,其實如果需要的話,重定向下scanf()函數即可完成從Debug Viewer向程序輸入參數的,這在調試某些需要動態調整參數的程序裏面應該有幫助,比如按下某個按鍵,激活輸入功能,重新配置下參數,然後繼續運行程序。
調試的時候有個變量在一直檢測是否有數據輸入,如果有數據輸入,那麼輸入的數據就傳到這個變量,這個接口在內核的頭文件有相關的定義,我們只需要定義這個變量即可。
將下面的main.c代碼完整的替換掉上面的mian.c就可以使用了。
- #include "main.h"
- #include "string.h"
- //SWV test
- //2015-6-15
- //by creep
- #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
- struct __FILE { int handle; /* Add whatever needed */ };
- FILE __stdout;
- FILE __stdin;
- volatile int32_t ITM_RxBuffer=ITM_RXBUFFER_EMPTY;
- int fputc(int ch, FILE *f) {
- if (DEMCR & TRCENA) {
- while (ITM_Port32(0) == 0);
- ITM_Port8(0) = ch;
- }
- return(ch);
- }
- int fgetc(FILE *f)
- {
- while(ITM_CheckChar() == 0)
- {
- }
- return ITM_ReceiveChar();
- }
- u8 Write_buff[30] = "\n\r\n\rPlease enter your name:\n\r";
- u8 read_buff[50]= "";
- u8 name[100]="\n\rYour name is : ";
- int main(void)
- {
- static u8 led_sta = ON;
- LED_Init();
- delay_init();
- delay_ms(1000);
- while(1)
- {
- //將數據發送到Debug Viewer
- printf((const char*)Write_buff);
- memset(read_buff,0,50);
- //等待輸入
- scanf("%s",read_buff);
- //將姓名輸出
- printf((const char*)name);
- printf((const char*)read_buff);
- LED(led_sta);
- led_sta = !led_sta;
- delay_ms(1000);
- }
- }