2025-08-05 11:23 PM - edited 2025-08-05 11:24 PM
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:
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.
Any help would be appreciated.