cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USB-CDC to create a virtual comport

Ratul99
Associate II

Hello Everyone

So I'm using a custom made STM32 board having STM32F103C8T6 MCU and after debugging the board using ST Link V2 when I try view the output through USB on my pc using the virtual COM Port and it is not working. As well as I have tried to view the output on the debugger console but it is showing temporary breakpoint for HAL_Init() . If their is any other way to view the output I would love to try and also the drivers I have install from STM32 website are for Win 7,8 versions and when I am connecting my board to my PC and opened the device manager to check the COM Port, in their under Universal Serial BUS Controller it is showing as a Unknown Device with a yellow caution sign. Do i need to have the latest drivers to view the output on Serial Wire mode?? and where can I find the latest driver for Win 11. I have attached the Pin configuration of my board and the code I am using below such that it can help you to understand the problem I am going through. Please guide me through the issue such that I can solve this problem. Also if possible please guide me through how to view the output such as simple Hello World print using the UART or any other possible way to see the output.Pin Config.pngCode1.pngcode2.pngcode3.png

Thank you for your support.

Best Regards

Ratul

 

3 REPLIES 3

This got confused with your other thread about using the UART:

https://community.st.com/t5/stm32-mcus-products/unable-to-view-output-from-stm32-board-via-serial-port/td-p/790510

 

USB is much more complex that a UART - so you really should complete getting the UART working first.

This also makes it a much stronger recommendation that you start on a genuine ST Dev Board - rather than unknown custom hardware.

 

Some points from the other thread:

Please re-post the code using the </> button.

Tell us what debugging you've done.

Does your project

  1. Successfully build - no errors; no warnings ?
  2. Successfully download to your board ?

Have you done Basic USB connectivity tests:

  • Are you sure it's a good USB cable - does it work with other devices?
  • Are you sure it's a data cable - not just a charging cable?
  • Does the PC see anything at all when you plug in? Does it give any beeps, error messages, etc?
  • Are you sure all connections are good?
  • It looks like you're intending for your STM32 to be powered from USB - is it actually getting power?

 

On implementing USB within an STM32:

Application Note AN4879Introduction to USB hardware and PCB guidelines using STM32 MCUs:

https://www.st.com/resource/en/application_note/an4879-introduction-to-usb-hardware-and-pcb-guidelines-using-stm32-mcus-stmicroelectronics.pdf

 

Also AN2586Getting started with STM32F10xxx hardware development:

https://www.st.com/resource/en/application_note/an2586-getting-started-with-stm32f10xxx-hardware-development-stmicroelectronics.pdf

ST Wiki - Introduction to USB with STM32

MOOC - STM32 USB Training

 via: https://community.st.com/t5/stm32-mcus/how-to-use-stmicroelectronics-classic-usb-device-middleware-with/ta-p/599274

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 Thank you for your concern.

So I will be answering your you question 

  1. Successfully build - no errors; no warnings ?Yes.

 

    • build ok.png

 

  1. Successfully download to your board ?Yes.debug complition.png

 

So I have attached the screen shoots to support my answer.

 

 

  • Are you sure it's a good USB cable - does it work with other devices?Yes
  • Are you sure it's a data cable - not just a charging cable?Yes
  • Does the PC see anything at all when you plug in? Does it give any beeps, error messages, etc?Yes when I connect the STM32 board using USB their is this unknown USB detected message was there for the first few time now when I connect the board to my pc It does not show any message but in the device manager It was showing as Unknown USB Device ( Device Descriptor request Failed). So I am stuck in this part only also I have attached a snap below about the response I'm getting.

usb not working.png

 

  • Are you sure all connections are good?By question what connections do you want know. If it is about the ST Link V2 to my STM32 board then I this it is Fine because otherwise the debug process would not be completed successfully. Please feel free to ask me if you need any more details. To support you with more details I have attached the custom STM32 PCB connection pdf below.
  • t looks like you're intending for your STM32 to be powered from USB - is it actually getting power?Yes. I have checked the UART I2C SWD port pins using multimeter and the value was 3.27V. Is the value need to be accurately 3V3 because the value I have measured is slightly lower than 3V3??

 

So this is the that I'm using for my STM32 Custom board. Please look into this any if any changes required let me know and also if their is any way round I would love to try that.

 

#include "main.h"
#include "stdint.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"


void SystemClock_Config(void);
static void MX_GPIO_Init(void);


int main(void)
{


  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USB_DEVICE_Init();

  HAL_Delay(1000);

  uint8_t msg[] = "Hello, World from STM32 via USB CDC!\r\n";

  CDC_Transmit_FS(msg, sizeof(msg) - 1);

  while (1)
  {

  }

}


void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}


static void MX_GPIO_Init(void)
{

  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();


}


void Error_Handler(void)
{

  __disable_irq();
  while (1)
  {
  }

}

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)
{

}
#endif /* USE_FULL_ASSERT */

Regards 

Ratul


@Ratul99 wrote:

So this is the that I'm using for my STM32 Custom board.  


Again, start off with a genuine ST board - get the software working on known-good hardware first.

Once you have it working on known-good hardware, then you can move on to debugging your custom hardware.

 


@Ratul99 wrote:

So I have attached the screen shoots to support my answer.


Thanks. It would be nice to crop the images to just show the pertinent details - rather than the entire screen.

https://www.techrepublic.com/article/how-to-take-screenshots-in-windows-10/#:~:text=full%20screen%20screenshot.-,How%20to%20capture%20a%20screen%20selection%20in%20Windows%2010%20with%20keyboard%20shortcuts,-If%20you%20just

 


@Ratul99 wrote:
  • It was showing as Unknown USB Device ( Device Descriptor request Failed). 

So look into why your code is failing to give a valid Device Descriptor ...

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.