对于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);
- }
打开调试即可得到调试信息:
输出调试信息
测试代码:
*******************************************一天过去了***************************************
上面说了输出的功能,其实如果需要的话,重定向下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);
- }
- }