2025-04-05 3:44 AM - edited 2025-04-08 1:33 AM
Greeting Everyone,
I am facing an issue with my STM32 board having STM32F103C8T6 as MCU, while trying to see a normal "Hello World" output. After debugging the board, I attempted to connect it to my PC via the serial port. To ensure proper communication, I installed the necessary Virtual COM Port drivers from the STM32 website.
However, my PC is not detecting the COM port to which the STM32 board is connected. Additionally, when I try to view the output on the Debugger Console within STM32CubeIDE, I encounter an error that prevents me from seeing the expected output.
I have written the code below that I'm using to print the Hello World which I have found online. Also I do not know if their is any problem on the code. With this I have also attached the Pin Configuration and Clock configuration details below as images.
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include <stdio.h>
#include <stdint.h>
#include <stm32f1xx_hal_conf.h>
void SystemClock_Config(void);
int _write(int file, char *ptr, int len)
{
int i=0;
for(i=0;i<len;i++)
ITM_SendChar((*ptr++));
return len;
}
uint8_t count = 0;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
count ++;
printf("Hello World count = %d \n",count);
HAL_Delay(250);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
Could you please assist me in resolving this issue? I would appreciate any guidance or troubleshooting steps you can provide to help me get the serial communication working and view the output correctly.
Thank you for your support!
Best regards,
Ratul Kar
2025-04-08 5:30 AM
@mƎALLEm wrote:
So the solution:
1- Either to implement a VCP on your MCU using the USB library. In that case no need to use USART but only USB. See for example this video.
So the video link that you have shared I have tried to check that but still it was not detection the USB Port on my pc as I have checked in the Device Manager. So if any other solution is available for to see the output using the USB please let know because it is like weeks and I'm still stuck in this. I have also tried some other methods I have found over the internet.
2025-04-08 5:40 AM - edited 2025-04-08 8:09 AM
(USB-related - moved to other thread)
2025-04-08 6:44 AM
Not clear what you did?
Did you change your mind: dropped the usage of USART2 and you are using USB-CDC?
If it's the case that's another question and we need to create another thread in a specific forum board and keep this one for the USART2 question.
2025-04-08 7:16 AM
@mƎALLEm wrote:keep this one for the USART2 question.
It's all rather confused, but I think this thread was originally about USB-CDC approach ?
It seems that @Ratul99 didn't originally (and still doesn't?) understand that it requires firmware in the STM32 to do that?
2025-04-08 7:41 AM
@Andrew Neil wrote:
It's all rather confused, but I think this thread was originally about USB-CDC approach ?
I don't think so. He's doing the printf retarget but in a wrong way:
int _write(int file, char *ptr, int len)
{
int i=0;
for(i=0;i<len;i++)
ITM_SendChar((*ptr++));
return len;
}
And he already shared the pinout config using USART2:
2025-04-08 7:48 AM - edited 2025-04-08 8:13 AM
I think I was mistaken when I merged the threads:
Indeed the second thread is talking about USB_CDC.
I will split the threads then ..
@Ratul99 sorry I merged your threads I thought they contain the same subject/question. So let's keep this thread for USART usage and printf retarget ..
And this one for USB-CDC / Virtual comport implementation
2025-04-10 8:27 AM
Are you trying to use messages over the VCP for diagnostic purposes or is this communication with the PC part of your actual application and function?