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-07 2:13 AM
@Ratul99 wrote:Hello @Andrew Neil
So the reference you have shared that is for STM32FXXXX series but my custom board have STM32F103XXX series MCU.
Sorry, which reference are you referring to? This one:
Yes, the bit about redirecting printf (strictly, stdout) to a UART is entirely generic to any STM32.
Again, the first thing you need to do is to get basic, direct UART output working (eg, using HAL_UART_Transmit).
Don't get into the added complications of printf before you have that working.
@Ratul99 wrote:I'm trying see the output using only the usb port.
To make that USB port work, you will need to have a USB stack with CDC-ACM implemented & working in your STM32. Have you done that?
2025-04-07 2:48 AM
yes I'm talking about this reference.
@Andrew Neil wrote:
To make that USB port work, you will need to have a USB stack with CDC-ACM implemented & working in your STM32. Have you done that?
Sure I would love to go through this process to be sure that the USB is working or not. If you have any reference on how to check the USB port and I would be very happy try this. Thank you for your suggestion.
2025-04-07 2:55 AM
Hello,
You didn't tel which board you are using?
You said you are using STM32F103C8T6 and the anlt Nucleo board available is the NUCLEO-F103RB
Could you please provide which board are you using? ST board? custom board? Blue/Black pill?
2025-04-07 2:56 AM
@Ratul99 wrote:Sure I would love to go through this process to be sure that the USB is working or not.
Again, it's not going to do anything unless your project has the code in it.
Does your project have that code in it?
Before trying to get this working on a custom board, did you get it working on an ST Board (or any other known-working board) ?
Did you study Application Note AN4879, Introduction to USB hardware and PCB guidelines using STM32 MCUs:
Also AN2586, Getting started with STM32F10xxx hardware development:
2025-04-07 3:09 AM - edited 2025-04-07 3:12 AM
I think there is a confusion here.
According to you previous statement:
@Ratul99 wrote:
Hello @Andrew Neil
So the reference you have shared that is for STM32FXXXX series but my custom board have STM32F103XXX series MCU.
So you are using a custom board and you are using UART2 on PA2 and PA3, with VCP!
VCP usage doesn't make sense here.
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.
2- Use USART but need to connect an external USB/UART bridge like this one: https://www.waveshare.com/cp2102-usb-uart-board-type-a.htm
2025-04-07 4:52 AM
Thank you for the suggestions. I will go through the Application Note AN4879 and try the UART process.
2025-04-07 4:54 AM
Hello
Sorry for late reply. Actually I'm using a custom made board. As you have suggested NUCLEO-F103RB, I will try to check that also.
2025-04-07 5:31 AM
Hello
So let me correct you that, the custom board that I'm using is enable with UART1 on PB6 and PB7 and I do not know which pin can be used for VCP or are you talking about something else. So till now I have faced issue with the VCP because my pc is not detecting the COM port which I have checked on the device manager. Now as you have suggested I have ordered a external USB to Serial wire connector like this
Is it will be okay or it has to be the one that you have suggested. Please have a look.
Apart from this I would like to highlight that my coding skills are not upto the mark, so if you can help me with any similar type of reference code through which I can see the output of my custom STM32 board via UART will be helpful.
2025-04-07 6:03 AM - edited 2025-04-07 6:14 AM
@Ratul99 wrote:I do not know which pin can be used for VCP .
As the name suggests, a VCP is a Virtual COM Port.
The "Virtual" means that it does not exist as a physical device - so there are no pins dedicated to it.
The physical link is USB; the virtual COM Port is carried over the USB protocol.
@Ratul99 wrote:I have faced issue with the VCP because my pc is not detecting the COM port .
As already noted, a VCP requires you to have USB CDC-ACM firmware installed & running in your STM32.
Without that, there is no VCP - so the PC could not possibly detect one!
@Ratul99 wrote:my pc is not detecting the COM port which I have checked on the device manager. .
You mean you have checked in Device Manager, and there is no VCP shown there?
@Ratul99 wrote:I have ordered a external USB to Serial wire connector like this
That one should be fine.
It will connect to UART pins on your STM32 - not to the STM32's USB.
@Ratul99 wrote:Apart from this I would like to highlight that my coding skills are not upto the mark
Note that USB is quite an advanced topic - not really a beginner project!
You will be better off just using the STM32's UART, and connecting via a USB-to-UART adaptor (such as the one you have ordered).
This is a key reason why these USB-to-UART adaptors exist - precisely so that you do not have to get bogged down in the complexities of implementing USB in your own firmware!
PS:
STM32 USB-to-UART +-----------+ +-----------+ | | | | | ....| |... | | U: | | :U ..| +------+ | A: TX+---------->+TX :A : U| | | | R: RX+<----------+RX :R : S+<================>+ PC | | T: | | :T : B| | | | ... | |... ..| +------+ | | | | +-----------+ +-----------+
2025-04-07 6:37 AM - edited 2025-04-07 6:39 AM
@Ratul99 wrote:
Hello
Now as you have suggested I have ordered a external USB to Serial wire connector like this
Is it will be okay or it has to be the one that you have suggested. Please have a look.
The module you've ordered is OK as the Rx/Tx pins are 3.3V level.
From the link you provided:
Now forget about printf for the moment and try to send characters with USART2 using:
HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, HAL_MAX_DELAY);
If it works, you hardware and setting are good. Then you can retarget printf over USART2. You can refer to this example running on NUCLEO-F103.
See also this article.
Hope that helps.