cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407 ITM

paulsmitton9157
Associate II
Posted on July 01, 2015 at 17:36

Hello

I want to use ITM Port 0 on a STM32F407 with a Keil µVision I wrote

printf(''TEST'');

with the following hook

int fputc(int c, FILE *stream)
{
return(ITM_SendChar(c));
}
static __INLINE uint32_t ITM_SendChar (uint32_t ch)
{
if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) && /* Trace enabled */
(ITM->TCR & ITM_TCR_ITMENA_Msk) && /* ITM enabled */
(ITM->TER & (1UL << 
0
) ) ) /* ITM Port #0 enabled */
{
while (ITM->PORT[0].u32 == 0);
ITM->PORT[0].u8 = (uint8_t) ch;
}
return (ch);
}

I reached the line

ITM->PORT[0].u8 = (uint8_t) ch;

but On Keil's Debug (printf) Viewer window nothing appeared I then tried to add some init function:

/* Debug MCU registers base address */
#define DBGMCU_BASE ((uint32_t )0xE0042000)
#define DBGMCU ((DBGMCU_TypeDef *) DBGMCU_BASE)
void itm_init(void) {
uint32_t SWOSpeed = 3000000;
uint32_t SWOPrescaler = (120000000 / SWOSpeed) - 1; // SWOSpeed in Hz
CoreDebug->DEMCR = 1 << 
CoreDebug_DEMCR_TRCENA_Pos
;
DBGMCU->CR = 0x00000027; //Enabling TRACE_IOEN, DBG_STANDBY, DBG_STOP, DBG_SLEEP
//Set TPIU register->Selected pinprotocol = 10b: Serial Wire Output - NRZ, 01b = SerialWire Output (Manchester)
*((volatile unsigned *) 0xE00400F0) = 0x00000002; // ''Selected PIN Protocol Register'': Select which protocol to use for trace output (2: SWO)
//Set TPIU -> Async Clock Prescaler Register [bits 0-12]
*((volatile unsigned *) 0xE0040010) = SWOPrescaler; // ''Async Clock Prescaler Register''. Scale the baud rate of the asynchronous output
//Lock Access Register
*((volatile unsigned *) 0xE0000FB0) = 0xC5ACCE55; // ITM Lock Access Register, C5ACCE55 enables more write access to Control Register 0xE00 :: 0xFFC
*((volatile unsigned *) 0xE0000E80) = 0x0001000D; // ITM Trace Control Register
*((volatile unsigned *) 0xE0000E40) = 0x0000000F; // ITM Trace Privilege Register
*((volatile unsigned *) 0xE0000E00) = 0x00000001; // ITM Trace Enable Register. Enabled tracing on stimulus ports. One bit per stimulus port.
*((volatile unsigned *) 0xE0001000) = 0x400003FE; // DWT_CTRL
//And this is really tricky!
*((volatile unsigned *) 0xE0040304) = 0x00000100; // Formatter and Flush Control Register
}

without any improvement.... Does anybody have an idea ? #stm32f407 #itm
3 REPLIES 3
Posted on July 01, 2015 at 17:55

You're trying too hard. Are you running at 120 MHz?

So, not clear from post which board or SWD debugger you're using. Does it support SWV? On DISCO/NUCLEO boards it's important to make sure the SWO/PB3 pin gets to the ST-LINK, so check the solder-bridge configurations. In Keil, make sure you go to the Debugger's option settings, and go to the Trace pane, turning on Trace, and making sure the FREQUENCY matches what you're clocking your chip at.

/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32F429%20Debug%20Viewer%20Usage&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=121

/**************************************************************************************/
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
ITM_SendChar(ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
ch = 1;
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
ITM_SendChar(ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
/**************************************************************************************/

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on July 01, 2015 at 18:01

More on point, with images, code

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Getting%20printf%20on%20STM32F407%20Discovery%20board&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=44]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FGetting%20printf%20on%20STM32F407%20Discovery%20board&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=44

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
paulsmitton9157
Associate II
Posted on July 17, 2015 at 16:05

Hello,

Thanks for your replies

In fact everything worked fine, my problem was that on my board the SWO line is also used for the external SPI flash, so as soon as it was initialized the SWO is not more active....

Sorry for this

misinterpretation

of my problem

regards