Skip to main content
Associate
April 1, 2026
Question

PRINTING not working when sending data to SWV ITM

  • April 1, 2026
  • 3 replies
  • 242 views
 
I am trying to print data but nothing seems to be working , I do not have cables to use COM, but I am sending data using SWV ITM, anyone who might know what's wrong , thanks!. 
/* USER CODE BEGIN Header */

/**

******************************************************************************

* @file : main.c

* @brief : Main program body

******************************************************************************

*/

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "cmsis_os.h"

#include "usb_host.h"

#include "stdio.h"

#include "core_cm4.h"



/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;

I2S_HandleTypeDef hi2s3;

RTC_HandleTypeDef hrtc;

SPI_HandleTypeDef hspi1;

UART_HandleTypeDef huart2;



/* Definitions for defaultTask */

osThreadId_t defaultTaskHandle;

const osThreadAttr_t defaultTask_attributes = {

.name = "defaultTask",

.stack_size = 256 * 4,

.priority = (osPriority_t) osPriorityLow,

};



/* Definitions for rtcTask */

osThreadId_t rtcTaskHandle;

const osThreadAttr_t rtcTask_attributes = {

.name = "rtcTask",

.stack_size = 768 * 4, /* printf needs stack */

.priority = (osPriority_t) osPriorityNormal,

};



/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

static void MX_I2S3_Init(void);

static void MX_SPI1_Init(void);

static void MX_RTC_Init(void);

static void MX_USART2_UART_Init(void);

void StartDefaultTask(void *argument);



/* USER CODE BEGIN PFP */

static void vTaskRTC(void *argument);

static void RTC_SetDateTime_Default(void);

/* USER CODE END PFP */

static void ITM_Enable_Port0(void)

{

CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;

ITM->LAR = 0xC5ACCE55UL;

ITM->TER = 0x00000001UL; /* stimulus port 0 */

ITM->TCR = ITM_TCR_ITMENA_Msk;

}



int __io_putchar(int ch)

{

ITM_SendChar((uint32_t)ch);



return ch;

}

/* USER CODE BEGIN 0 */







static void RTC_SetDateTime_Default(void)

{

RTC_TimeTypeDef sTime = {0};

RTC_DateTypeDef sDate = {0};



sTime.Hours = 0;

sTime.Minutes = 0;

sTime.Seconds = 0;

sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

sTime.StoreOperation = RTC_STOREOPERATION_RESET;

if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)

Error_Handler();



sDate.WeekDay = RTC_WEEKDAY_TUESDAY;

sDate.Month = RTC_MONTH_MARCH;

sDate.Date = 31;

sDate.Year = 26; /* 2026 */

if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)

Error_Handler();

}



static void vTaskRTC(void *argument)

{

(void)argument;







// RTC_SetDateTime_Default();



for (;;)

{

RTC_TimeTypeDef sTime;

RTC_DateTypeDef sDate;



HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);

HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);



printf("Time: %02u:%02u:%02u\r\n", sTime.Hours, sTime.Minutes, sTime.Seconds);

printf("Date: 20%02u-%02u-%02u\r\n", sDate.Year, sDate.Month, sDate.Date);



HAL_GPIO_WritePin(GPIOD, LD3_Pin | LD4_Pin | LD5_Pin | LD6_Pin, GPIO_PIN_RESET);



if (sTime.Seconds < 15)

HAL_GPIO_WritePin(GPIOD, LD4_Pin, GPIO_PIN_SET); /* green */

else if (sTime.Seconds < 30)

HAL_GPIO_WritePin(GPIOD, LD3_Pin, GPIO_PIN_SET); /* orange */

else if (sTime.Seconds < 45)

HAL_GPIO_WritePin(GPIOD, LD5_Pin, GPIO_PIN_SET); /* red */

else

HAL_GPIO_WritePin(GPIOD, LD6_Pin, GPIO_PIN_SET); /* blue */



osDelay(500);

}

}

/* USER CODE END 0 */



int main(void)

{

HAL_Init();

ITM_Enable_Port0();

SystemClock_Config();



MX_GPIO_Init();

MX_I2C1_Init();

MX_I2S3_Init();

MX_SPI1_Init();

MX_USART2_UART_Init();

MX_RTC_Init();



osKernelInitialize();



defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

rtcTaskHandle = osThreadNew(vTaskRTC, NULL, &rtcTask_attributes);

if (rtcTaskHandle == NULL)

Error_Handler();



osKernelStart();



while (1) { }

}



/**

* @brief System Clock Configuration

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};



__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);



/* HSE + PLL for SYSCLK, LSI ON for RTC source */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSI;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.LSIState = RCC_LSI_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 8;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 7;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

Error_Handler();



RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK

| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)

Error_Handler();

}



static void MX_RTC_Init(void)

{

/* Assumes CubeMX configured RTC clock source = LSI.



hrtc.Instance = RTC;

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 127;

hrtc.Init.SynchPrediv = 255;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

if (HAL_RTC_Init(&hrtc) != HAL_OK)

Error_Handler();

}



static void MX_USART2_UART_Init(void)

{

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(&huart2) != HAL_OK)

Error_Handler();

}





static void MX_I2C1_Init(void)

{

hi2c1.Instance = I2C1;

hi2c1.Init.ClockSpeed = 100000;

hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

hi2c1.Init.OwnAddress1 = 0;

hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

hi2c1.Init.OwnAddress2 = 0;

hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

if (HAL_I2C_Init(&hi2c1) != HAL_OK)

Error_Handler();

}



static void MX_I2S3_Init(void)

{

hi2s3.Instance = SPI3;

hi2s3.Init.Mode = I2S_MODE_MASTER_TX;

hi2s3.Init.Standard = I2S_STANDARD_PHILIPS;

hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;

hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_ENABLE;

hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_96K;

hi2s3.Init.CPOL = I2S_CPOL_LOW;

hi2s3.Init.ClockSource = I2S_CLOCK_PLL;

hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;

if (HAL_I2S_Init(&hi2s3) != HAL_OK)

Error_Handler();

}



static void MX_SPI1_Init(void)

{

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 10;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

Error_Handler();

}



static void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};



__HAL_RCC_GPIOE_CLK_ENABLE();

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOH_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

__HAL_RCC_GPIOD_CLK_ENABLE();



HAL_GPIO_WritePin(GPIOD, LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin|Audio_RST_Pin, GPIO_PIN_RESET);



GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin|LD5_Pin|LD6_Pin|Audio_RST_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}



void StartDefaultTask(void *argument)

{

(void)argument;



// MX_USB_HOST_Init();



for (;;)

{

osDelay(1);

}

}



void Error_Handler(void) // cath the eeror

{

__disable_irq();

while (1)

{

HAL_GPIO_TogglePin(GPIOD, LD5_Pin); /* blink red if any init fails */

for (volatile uint32_t i = 0; i < 250000; ++i) {}

}

}



#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)

{

(void)file; (void)line;

}

#endif

 

3 replies

KING2Author
Associate
April 1, 2026
 

When I upload the code it works as excepted on the SMT32F407-DISC1 board, but timestamps can not show in the SWV interface 

Screenshot 2026-04-01 152503.png

Andrew Neil
Super User
April 2, 2026

Welcome to the forum.

Please supply more details of your setup - see: How to write your question to maximize your chances to find a solution

 


@KING2 wrote:

works as expected on the SMT32F407-DISC1 board

So compare & contrast your non-working setup with the DISC1 board

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Technical Moderator
April 6, 2026

Hello @KING2, and welcome to ST Community!

To enable timestamps during debugging, click on the wrench icon in the debug toolbar and check the option Enable Timestamps.

Capture d'écran 2026-04-06 104544.png

Best regards,

To improve visibility of answered topics, please click 'Accept as Solution' on the reply that resolved your issue or answered your question.
Andrew Neil
Super User
April 7, 2026

@KING2 wrote:
 
I do not have cables to use COM

 


@KING2 wrote:
 

SMT32F407-DISC1


The User Manual tells you how to add the VCP (Virtual COM Port)  connection:

Image1.png

https://www.st.com/resource/en/user_manual/um1472-discovery-kit-with-stm32f407vg-mcu-stmicroelectronics.pdf#page=18

via: https://www.st.com/en/evaluation-tools/stm32f4discovery.html#documentation

 

Alternatively, just add an external USB-to-UART adaptor

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.