2020-01-09 11:42 PM
I've been trying to get a virtual COM port working on my NUCLEO-L073RZ for a couple of days now.
I've used STM32CubeMX to set up my chip in USB Device mode and configured the USB_DEVICE middleware to act as a VCP with default settings. I also made sure to fix the clock configuration so it can drive the USB peripheral at the proper speed. I've left Minimum Heap Size at 0x200 and Minimum Stack Size at 0x400. In my main.c, I included
#include "usbd_cdc_if.h"
and I added the following to my main infinite loop
uint8_t buffer[] = "Hello, World!\r\n";
CDC_Transmit_FS(buffer, sizeof(buffer));
HAL_Delay(1000);
I then hooked up PA12 to USB-DP and PA11 to USB-DM, as described in the chip pinout. When I compile and download the code to my board, Linux cannot enumerate the USB device. In dmesg, I get the following prints.
[ 9157.764155] usb 3-10.4.3: new full-speed USB device number 66 using xhci_hcd
[ 9157.864133] usb 3-10.4.3: device descriptor read/64, error -32
[ 9158.072148] usb 3-10.4.3: device descriptor read/64, error -32
[ 9158.280084] usb 3-10.4.3: new full-speed USB device number 67 using xhci_hcd
[ 9158.380134] usb 3-10.4.3: device descriptor read/64, error -32
[ 9158.588076] usb 3-10.4.3: device descriptor read/64, error -32
[ 9158.696075] usb 3-10.4-port3: attempt power cycle
[ 9159.320077] usb 3-10.4.3: new full-speed USB device number 68 using xhci_hcd
[ 9159.320175] usb 3-10.4.3: Device not responding to setup address.
[ 9159.528163] usb 3-10.4.3: Device not responding to setup address.
[ 9159.736053] usb 3-10.4.3: device not accepting address 68, error -71
[ 9159.836051] usb 3-10.4.3: new full-speed USB device number 69 using xhci_hcd
[ 9159.836152] usb 3-10.4.3: Device not responding to setup address.
[ 9160.044133] usb 3-10.4.3: Device not responding to setup address.
[ 9160.252005] usb 3-10.4.3: device not accepting address 69, error -71
[ 9160.252070] usb 3-10.4-port3: unable to enumerate USB device
Similarly, Windows will see the USB device but it will report the error "Device Descriptor Request Failed" in the Device manager.
I've run and stepped through the code, the whole USB initialization process is returning successes at every point and the Error_Handler() function never gets called. I've tried using a 1.5k pull up to 3.3v on USB-DP but that doesn't work either. (The datasheet lists that the pull up is internal anyway). I probed the signal, and there is zero data on either line. The only thing that happens is USB-DP gets pulled to 3.3v on boot up.
I have been trying to debug this for days and I have no leads. According to the debugger, everything is hunky dory. The only think I can think of is there's some kind of initialization process that wasn't included in code generation, but it looks like everything is here. There is no USB example project for my board.
I'm running Version 1.0.1 of STM32CubeIDE and Version 5.2.1.201906050926 of STM32CubeMX. My OS is debian but I tried it on Windows 8 too.
Here's the code I think is relevant:
main.c
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_USB_DEVICE_Init();
while (1)
{
uint8_t buffer[] = "Hello, World!\r\n";
CDC_Transmit_FS(buffer, sizeof(buffer));
HAL_Delay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_12;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
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_HSI;
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_0) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_USB;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = LD2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
usb_device.c
#include "usb_device.h"
#include "usbd_core.h"
#include "usbd_desc.h"
#include "usbd_cdc.h"
#include "usbd_cdc_if.h"
USBD_HandleTypeDef hUsbDeviceFS;
void MX_USB_DEVICE_Init(void)
{
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK)
{
Error_Handler();
}
if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK)
{
Error_Handler();
}
if (USBD_Start(&hUsbDeviceFS) != USBD_OK)
{
Error_Handler();
}
}
usbd_desc.c, USB #define'd parameters only
#define USBD_VID 1155
#define USBD_LANGID_STRING 1033
#define USBD_MANUFACTURER_STRING "STMicroelectronics"
#define USBD_PID_FS 22336
#define USBD_PRODUCT_STRING_FS "STM32 Virtual ComPort"
#define USBD_CONFIGURATION_STRING_FS "CDC Config"
#define USBD_INTERFACE_STRING_FS "CDC Interface"
2020-01-18 02:55 AM
nothing issue with firmware side , may be issue relatd to be vbus
have your usb devie is self powerd ,then no use of vbus
otherwise you have to use vbus for power supply
2022-06-25 12:38 PM
@GHaac. 1
I know it's long time from your question, but if you have solved the issue please share the solution here. I'm stucked in a similar situation!
2023-03-30 09:13 AM
I had the same problem. In my case, the root cause was that I had configured the clock tree to feed 52MHz to my USB peripheral. I downgraded it to 48MHz and it worked.