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
ST Employee

Hello @Ratul99 and welcome to the ST Community,

What board are you using? ST board? Custom board? blue/black pill board? and how did you connect the board to a PC with a detailed hardware description.

Thank you

PS: please use </> button to paste your code: see this post. I'm editing your post.

 

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

+

You did the printf retarget to the ITM not to the serial port. 

ITM_SendChar((*ptr++));

Look at this example for STM32CubeF1 package:

https://github.com/STMicroelectronics/STM32CubeF1/tree/master/Projects/STM32F103RB-Nucleo/Examples/UART/UART_Printf

Look also at this thread: https://community.st.com/t5/stm32cubemx-mcus/how-to-retarget-printf-on-uart/td-p/380053

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.

Hello @mƎALLEm 

 

Thank you for the response. As you have asked we are using a custom board and for the debugging part we have used ST Link V2. If anything more details needed please feel free to ask.

Regards

Ratul Kar

Hi @mƎALLEm 

I will look into this and let you know if I can get any results out of it.

Thanks again

Ratul


@Ratul99 wrote:

we are using a custom board


Then please post the schematic.

 


@Ratul99 wrote:

for the debugging part we have used ST Link V2.


Is it genuine ?

https://community.st.com/t5/stm32-mcus/how-to-recognize-a-genuine-st-link-v2-versus-a-cloned-one/ta-p/49471

In particular: https://community.st.com/t5/stm32-mcus/how-to-recognize-a-genuine-st-link-v2-versus-a-cloned-one/tac-p/703663/highlight/true#M965

 


@Ratul99 wrote:

I encounter an error that prevents me from seeing the expected output


What error, exactly?

 


@Ratul99 wrote:

If anything more details needed please feel free to ask.


Please see: How to write your question to maximize your chances to find a solution.

You haven't said what host system you're using, nor what IDE version.

 

PS:

This is a public forum, visible to anyone & everyone on the internet - so you might want to re-think publishing your email address ...

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.

Before adding the complexities of printf redirection, be sure that you can get basic direct UART IO working first.

https://community.st.com/t5/stm32-mcus/how-to-redirect-the-printf-function-to-a-uart-for-debug-messages/tac-p/733969/highlight/true#M1096

 

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.

The ITM would come out of PB3 SWO via the SWV Viewer in STM32 Cube Programmer or your debugger.

The standalone ST-LINK/V2 doesn't provide a USART via a VCP

For your F103 to provide a VCP you'd need to compile as a USB VCP Device and communicate with that and not the USART, but wouldn't really allow for the type of blocking in-line interaction you're attempting. 

Consider a cheap USB to CMOS Serial dongle for USART comms

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Andrew Neil 

Thank you for the response. So as you have requested I have attached it below for your reference. Also I'm using Win 11 as the host system and the STM32CUBEIDE version is 1.18.0.

Hello @Andrew Neil 

So the reference you have shared that is for STM32FXXXX series but my custom board have STM32F103XXX series MCU. Is that makes any difference because to be honest I do not know much about this.

Also when I'm trying see the output using only the usb port. For that I have installed the ST VCP from the STM official website on my windows pc having win 11. Now after debugging when I try to view view the output using putty I need to select the port number where my STM32 board is connected through the usb but it is not showing anything and also under the Universal Serial Bus Controller part it is showing as unknown device with a yellow caution sign. Now for this work I have only enabled the USB and the Serial Wire pins on my STM32F103C8T6 MCU. Now when I have checked the forum about this Virtual COM Port solution somebody was asking for the ST VCP updated version for Win 10 or higher version and their are not much replies so I am unable to find any solution because the ST VCP official has the win 7/8 versions available on the ST official website. If you have any solution please guide me through this problem.