ITM printf trace
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 7:26 AM
I have a STM32L432 Nucleo-32 board and I'm trying to use ITM printf trace.
I created new project with configuration: "Targeted Project type: Empty" "targeted language: C++". I'm not using HAL libraries.
In the syscalls.c I added following code:
//Debug Exception and Monitor Control Register base address
#define DEMCR *((volatile uint32_t*) 0xE000EDFCU )
/* ITM register addresses */
#define ITM_STIMULUS_PORT0 *((volatile uint32_t*) 0xE0000000 )
#define ITM_TRACE_EN *((volatile uint32_t*) 0xE0000E00 )
void ITM_SendChar(uint8_t ch)
{
//Enable TRCENA
DEMCR |= ( 1 << 24);
//enable stimulus port 0
ITM_TRACE_EN |= ( 1 << 0);
// read FIFO status in bit [0]:
while(!(ITM_STIMULUS_PORT0 & 1));
//Write to ITM stimulus port0
ITM_STIMULUS_PORT0 = ch;
}
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
(void)file;
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
ITM_SendChar(*ptr++);
}
return len;
}
I didn't make any changes to the Clock and as far as I know the default clock for STM32L432 is 4MHz. Here is the debug and ITM configuration:
I also started recording in ITM Data Console window.
I don't see any traces in the SWV ITM Data Console window. Any ideas why? What am I doing wrong?
- Labels:
-
STM32L4 series
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 7:37 AM
As for doing printf via UART, I would strongly suggest that you get this going direct first.
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 7:49 AM
@Andrew Neil I read that post already before. I did the same steps as in that post. However I'm not using CubeMX, I'm not using STM32Cube as Project Type.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 7:53 AM
So what are you using?
If it's GCC-based, it should be the same.
Again, try to get the ITM output to work direct first - before adding the complications of printf, etc ...
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 8:05 AM
I am using STM32CubeIDE, but when creating the project I select "empty" project type. With this configuration, the CubeMX file is not created, neither HAL/configuration files related to CubeMX.
What do you mean by "get the ITM output to work direct"?
I tried also to use SWV trace log and SWV statistical profiling, following the STM's instructions from this video but also without success.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-10 7:33 PM - edited ‎2025-04-10 7:34 PM
Please start from some "UART printf" example for your board/MCU, or generate a new project with Cube (yes, with HAL, you will remove unneeded later). Especially do not add any code to touch the DBG and ITM. Let the host (debugger) to configure DBG. Make sure the ITM prints work. Then play with the code and change it as you like.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-11 12:00 AM
@DPiór.1 wrote:What do you mean by "get the ITM output to work direct"?
Just make sure that you can output stuff by calling ITM_SendChar direct - without going via printf
Maybe, @Pavel A. suggested, get printf working via UART first, then switch to SWO.
A complex system designed from scratch never works and cannot be patched up to make it work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-04-11 4:22 AM
Have you enabled SWO? This is the initialisation code I use for a G4 device.
// enable the SWO pin in async. mode
DBGMCU->CR &= ~DBGMCU_CR_TRACE_MODE;
DBGMCU->CR |= DBGMCU_CR_TRACE_IOEN;
// enable DWT and ITM
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
TPI->CSPSR = 1; // Port width = 1 bit
TPI->SPPR = 2; // UART encoding
TPI->ACPR = SystemCoreClock / 2000000 - 1; // Clock divider
TPI->FFCR &= ~TPI_FFCR_EnFCont_Msk; // No formatter
ITM->LAR = 0xC5ACCE55; // Unlock the ITM registers
ITM->TCR = ITM_TCR_ITMENA_Msk // Enable the ITM
| ITM_TCR_TraceBusID_Msk; // Set the stream identifier
ITM->TPR = 1; // Enable unprivileged access to ports 7:0
ITM->TER = 1; // Enable trace port 0
