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):
system_stm32xxxx.c Core clock

Go to Options Image for a hint for a target and go toDebug tab. Ensure that you use ST- Link Debugger


Debug tab ST- Link Debugger


Ensure that SWD is used


SWD Port setting Debug Keil

Go to Trace tab and setup next settings (use Core Clock that you found before, e.g. 16000000 Hz = 16.0 MHz):


Core clock frequency on Trace tab

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

stm32l1xx.h

#stm32
0
Ivan Borshchov profile picture
Jan 10, 2017
by Ivan Borshchov
Did it help you?
Yes !
No

Best related