cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042 USB VCP not working

ajargans
Associate II
Posted on December 10, 2015 at 06:49

I have generated code with STM32CubeMX which should enable VCP support for STM32F042, but USB still doesn't work. Until USB device is initialized in code, windows shows device as ''Unknown USB Device (Device Descriptor Request Failed)'' and after calling USB Init function, device disappears.

I have repeated same procedure for F4 Discovery board and it works fine there.

 F4 Discovery and F042 source which is generated by CubeMX seems to be the same.

Where could the problem be? Am I missing something?

#stm32f04-cubemx #usb-stm32cube
9 REPLIES 9
ajargans
Associate II
Posted on January 02, 2016 at 09:25

Finally I managed to get it working, but only on USB 3.0 ports.

When I'm connecting my device to USB 2.0 ports, on Win7 it shows DeviceFailedEnumeration but on Win10 it doesn't show device at all.

jbeck17
Associate II
Posted on December 10, 2015 at 16:35

I'm not sure if this will help your issue, but I just developed a project using the F072 to do the same thing and had issues with not waiting long enough after init functions were called for USB. I needed to wait for it to fully enumerate and then do my stuff.

ajargans
Associate II
Posted on December 11, 2015 at 06:27

I added 10 second delay after 

MX_USB_DEVICE_Init

() but nothing changed.  Device still doesn't show up.

jbeck17
Associate II
Posted on December 11, 2015 at 14:28

What does it look like if you step line-by-line through your code? That may help in figuring what lines make it respond in what way. 

ajargans
Associate II
Posted on December 11, 2015 at 16:10

It's just the code generated by CubeMX:

int
main(
void
) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USB_DEVICE_Init();
while
(1);
}

AndMX_USB_DEVICE_Init() looks like this:

void
MX_USB_DEVICE_Init(
void
) {
USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);
USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
USBD_Start(&hUsbDeviceFS);
}


jbeck17
Associate II
Posted on December 11, 2015 at 16:22

Sure, I know what the Cube outputs since I made the same project through the cube. I am saying you need to run your code, and step line by line. Instead of just running the code, step to next line until you see things happening or not happening. What does it look like when you step through each line of the Cube's output. You need to narrow down where the issue is cropping up. I am not at my computer to run my example to remember exactly if I needed to do anything tricky but I will later.

rwmao
Senior
Posted on December 12, 2015 at 18:58

please follow the sample I built:

https://bitbucket.org/rwmao/cdc_onstm32f411rc/src/12de104e5bb561881af2c471d97ab3d46b30f6b4?at=master

There is a tuitorial and troubleshooting there.

I believe it will solve your problem.

the problem you have is what I have sometime ago.

ajargans
Associate II
Posted on December 24, 2015 at 13:26

I was missing 1.5k pull-up resistor. After adding it''Unknown USB Device (Device Descriptor Request Failed)'' error was gone. But now there is another problem. After adding pull-up resistor, program is now stuck atUSBD_Init() function. More precisely atHAL_PCD_Init() function at stm32f0xx_hal_pcd.c at theese lines:

hpcd->USB_Address = 0;
hpcd->State= HAL_PCD_STATE_READY;

It will pass this part only if pull-up resistor is removed.
tsuneo
Senior
Posted on December 25, 2015 at 12:11

Hi anrijs,

I tested STM32CubeMX (v4.12) - CubeF0 firmware pack (v1.4.0) on NUCLEO-F042K6 (STM32F042K6T6). Generated code makes this board enumerated by PC without any further modification. [ Hardware ] - USB connector PA11 (D-), PA12 (D+) and GND to a USB B-type receptacle. - No crystal: As F042 has ''crystal-less'' feature with CRS (Clock Recovery System), you don't need any external crystal for USB operation. - No external pull-up: Also, this USB engine has on-chip 1k5 resistor for D+ pull-up, any external pull-up is not required. [ CubeMX setting ] Start New Project at File menu, and set up these items. New Project Dialog Click Board Selector tab - select NUCLEO-F042K6 board and push OK Pinout tab RCC > CRS SYNC - CRS SYNC Source USB (pull-down menu) USB > Device (FS) (Check box) USB_DEVICE > Class For FS IP - Communication Device Class (pull-down menu) (USB_DEVICE on MiddleWare is enabled when USB - Device (FS) is checked) Clock Configuration tab - let CubeMX tune the setting automatically (automatic clock issues solver), following the dialog which appears when this tab is touched. Setting finishes. Generate Code on Project menu [ PC driver ] On Windows side, pre-install this driver before plug-in NUCLEO-F042K6 USB STM32 Virtual COM Port Driver

http://www.st.com/web/en/catalog/tools/PF257938

Or you may automatically get the driver from Windows Update over internet at the first device plug in. [ Customization ] 1) line coding A couple of PC terminal software (ex Hyperterminal) should fail to open this COM port, because the handling of Set/Get_line_coding request is up to users. Here is simple implementation.

usbd_cdc_if.c
static uint8_t lineCoding[7] // <------- add these three lines
// 115200bps, 1stop, no parity, 8bit
= {0x00, 0xC2, 0x01, 0x00, 0x00, 0x00, 0x08};
static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
{
/* USER CODE BEGIN 5 */
switch (cmd)
{
...
case CDC_SET_LINE_CODING: 
memcpy( lineCoding, pbuf, sizeof(lineCoding) ); // <-- add this line
break;
case CDC_GET_LINE_CODING: 
memcpy( pbuf, lineCoding, sizeof(lineCoding) ); // <-- add this line
break;

2) bulk IN/OUT transfer Device --> host (Tx) Fill a buffer with data, and pass the buffer to the stack by calling CDC_Transmit_FS(). This routine doesn't block the execution. To know completion of the transfer, - poll hUsbDevice_0->pClassData->TxState == 0 or - USBD_CDC_DataIn() (usb_cdc.c) is called by the stack at this timing. Modify this routine. Host --> device (Rx) When a packet comes from host, CDC_Receive_FS() callback in usbd_cdc_if.c is called by the stack. process data on the buffer in this callback. Generated code initially assigns these buffers for Tx and Rx (in CDC_Init_FS())

usbd_cdc_if.c
#define APP_RX_DATA_SIZE 4
#define APP_TX_DATA_SIZE 4
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];

You may use them, or assign another buffer for your custom purpose. Tsuneo