Enable printf via ST-Link in Keil for STM32
Configure ST-Link connection
You should use SWD
interface (not JTAG!) for connecting ST-Link to your STM32 chip. For this use Next pins: SWDIO
SWCLK
SWO
(used serial terminal for printf
) GND
Configure Keil
Open system_stm32xxxx.c
and find what Core clock
frequency used (HCLK
):
Go to Options
for a target and go toDebug
tab. Ensure that you use ST- Link Debugger
Ensure that SWD
is used
Go to Trace
tab and setup next settings (use Core Clock that you found before, e.g. 16000000
Hz = 16.0
MHz):
Firmware part
Create file Retarget.c
and add next:
#include <stdio.h>
#include <stdint.h>
#include "stm32l1xx.h"
#pragma import(use_no_semihosting_swi)
struct FILE {
int handle;
};
FILE stdout;
FILE stdin;
volatile int32_t ITM_RxBuffer;
int fputc(int ch, FILE *f) {
ITM_SendChar(ch);
return(ch);
}
void _sys_exit(int return_code) {
return ;
}
Instead of "stm32l1xx.h"
in #include
you should use your platform h file. You can found it name by unfolding system_stm32xxxx.c
in Project view