Skip to content

fix: add full ITM initialization for STM32F4 SWD trace compatibility#8

Open
Biancaa-R wants to merge 3 commits intoniekiran:masterfrom
Biancaa-R:patch-1
Open

fix: add full ITM initialization for STM32F4 SWD trace compatibility#8
Biancaa-R wants to merge 3 commits intoniekiran:masterfrom
Biancaa-R:patch-1

Conversation

@Biancaa-R
Copy link

@Biancaa-R Biancaa-R commented Oct 25, 2025

For the ITM debugging ,
The implemented code didnt work on my STM32F407G Disc board .
In the initially provided code ,there is

  • The earlier ITM_SendChar() implementation only enabled TRCENA (DEMCR bit 24)

  • As it happened along the transfer. -> while (!(ITM_STIMULUS_PORT0 & 1)); wasnt set .

  • This patch adds an explicit ITM_Init() function that happens before hand.

Without these steps, the ITM hardware didnt work in my case.
and printf() redirection via ITM remains non-functional.
image

A separate ITM_Init() function has been added to perform full trace setup.
This ensures proper SWO output on STM32F407G-DISC boards under the latest

This approach maintains backward compatibility while ensuring consistent
trace output on all supported IDE versions.
(In the video it had worked on your side ,could be due to versions of STM32Cube ide not sure.)

  • This method works on the latest cube ide.

  • proof of working

image
  • Init implementation:
image

The issue being:
Your ITM_SendChar() contains an initial busy-wait

// Wait until ITM TxFIFO is ready (bit 0 is 1)
while (!(ITM_STIMULUS_PORT0 & 1)); // <-- this can hang forever
//Enable TRCENA
DEMCR |= ( 1 << 24);
...

If ITM/TPIU isn't enabled yet, that first while will block forever .

  • To ensure immediate response .
    Replacing the function with this safer version (still minimal, keeps your behavior but avoids the hang):
  • Typecasting of the message 8 -> 32.
    // Write to ITM stimulus port 0
    // Note: The port is 32-bit, but we only write a byte.
    ITM_STIMULUS_PORT0 = (uint32_t)ch;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant