cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to View Output from STM32 Board via Serial Port

Ratul99
Associate II

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


pin configuration.pngclock configuration.png

26 REPLIES 26

@mƎALLEm 


@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.




 

(USB-related - moved to other thread)

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.

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.

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

@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?

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.

@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:

mALLEm_0-1744123265285.png

 

 

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
mƎALLEm
ST Employee

I think I was mistaken when I merged the threads:

mALLEm_0-1744123615369.png

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

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Carl_G
Senior II

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?