cancel
Showing results for 
Search instead for 
Did you mean: 

Check SWV ITM trace from code

ak52
Associate III

Hello,

I am using the STM32F767ZI MCU and was wondering if it is possible to check if a debugger is connected or not and if SWV is enabled, to avoid the UN-necessary looping when a printf is encountered.

These are my requirements:

  1. Check if the user has connected a debugger
  2. Check if the user has enabled ITM trace from the debugger and connected to the correct port.(Port 0 in this case)
  3. If both points 1 & 2 are not done, avoid the looping when a printf is encountered.

So when printf is called it invokes the _write function. Inside the write function we call ITM_SendChar(); in a loop. It is this loop which i want to avoid.

This is what i have so far:

 

static inline bool IsDebuggerAttached(void)
{
  return (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk);
}

static inline bool ITM_Port0_IsEnabled(void)
{
  return ((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0U) && ((ITM->TER & 1U) != 0U);
}

int _write(int file, char *ptr, int len)
{
  (void)file;
  int DataIdx;

  // Only send to ITM if debugger is connected AND ITM is active
  if (!IsDebuggerAttached() || !ITM_Port0_IsEnabled())
  {
    HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET); //just to test
    return len;
  }

    for (DataIdx = 0; DataIdx < len; DataIdx++)
    {
      //__io_putchar(*ptr++);
	ITM_SendChar(*ptr++);
    }

  return len;
}

 

IsDebuggerAttached works are i am able to recognize if the debugger is connected or not by checking the DHCSR register.

But i am not able to check if SWV is enabled and the correct port is connected.

To test this , i disabled the SWV from the debug configurations, but the LED did not light up, and when i placed a break-point on ITM_SendChar(), it hit that breakpoint.

ak52_0-1754461007018.png

Any help would be appreciated.

 

 

0 REPLIES 0