cancel
Showing results for 
Search instead for 
Did you mean: 

USB CDC Bug in CubeMX firmware

richard239955
Associate II
Posted on April 11, 2014 at 10:07

Hey everybody,

in order to initialize a working Virtual Com Port with CubeMX on the STM32F4 Discovery, I had to change

   pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));

to

   pdev->pClassData = (void *) USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));

Without the type cast pClassData would always be null, which will make the parent function (USBD_CDC_Init in usbd_cdc.c) return before initializing the interface.

Btw I used firmware 1.1.0.

EDIT: Sorry, that was too fast. This line was actually not the root cause, it was the following line in USB_CDC.h

   &sharpdefine CDC_DATA_HS_MAX_PACKET_SIZE        512

Changing that value to 256 made it work for me now. In case anyone is interested, here's a small code example for a vcp mirror. Add this code to CDC_Receive in usb_cdc_if.c

    static uint8_t buff_RX[256];

    static uint8_t buff_TX[256];

    

    int i = 0;

    

    for (i = 0; i < *Len; i++)

        buff_TX[i] = buff_RX[i];

    

    USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &buff_TX[0], *Len);

    USBD_CDC_TransmitPacket(&hUsbDeviceFS);

    

    USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &buff_RX[0]);

    USBD_CDC_ReceivePacket(&hUsbDeviceFS);

Also you will have to add

    extern USBD_HandleTypeDef hUsbDeviceFS;

to this file.

 

#usb #cdc #cdc #cdc #cdc_receive_fs #full-echo #usb-cdc #vcp #vcp
49 REPLIES 49
rc
Associate II
Posted on June 25, 2015 at 21:48

It took me a quite a while to get CDC serial USB up and running reliably but it is now solid. A couple of things that gave me issues:

 o outgoing buffer to TX must be address aligned to 4 bytes!

 o don't call anything unless something is plugged in

I used STM32CubeMX to generate the code for me for the STM32F429I-DISCO board. I used the HS usb because that is the PHY connected to the onboard USB connector. All the code runs independent of everything else and runs from its interrupts so all you have to do is call two words for transmit and interface with two words for receive. Pretty nice when it works.

On the aligned buffers, if it wasn't on a 4 byte boundary, then when the content was transmitted, it was padded with zeroes and the bytes on the end got cut off. Weird bug and not documented.

There is a handler structure which will be zero if nothing is plugged into the USB port so one must check for that before transmitting or you'll crash.

This is all the code I needed to interface to the USB code. Transmission came from a buffer which is setup in USBD_CDC_SetTxBuffer and sending is done by calling USBD_CDC_TransmitPacket. A hook is needed in the USB code CDC_Receive_HS to call the receive routine which I just push to a queue and then a call is made to the USB code USBD_CDC_ReceivePacket to let it know you have processed the packet:

// CDC USB

void usbRxData(uint8_t* Buf, uint32_t *Len)

{

    Long n = *Len;

    while (n--)

        pushbq(*Buf++, usbLink.rxq);

    USBD_CDC_ReceivePacket(hUsbDevice_1); // must tell usb that you are done

}

void usbServiceTx(sfpLink_t *link)

{

    if (link->sfpBytesToTx) { // check for bytes to go out

        if (hUsbDevice_1) { // must check: gets initialized when usb port plugs in

            USBD_CDC_SetTxBuffer(hUsbDevice_1, link->sfpTxPtr, link->sfpBytesToTx);

            if (USBD_OK == USBD_CDC_TransmitPacket(hUsbDevice_1)) {

                link->sfpBytesToTx = 0; 

            }

        }

    }

}

And I had to add a hook into the receiver in usbd_cdc_if.c:

static int8_t CDC_Receive_HS (uint8_t* Buf, uint32_t *Len)

{

  /* USER CODE BEGIN 11 */ 

  usbRxData(Buf, Len);   // call back to my code

  return (USBD_OK);

  /* USER CODE END 11 */ 

}

mahesh
Associate
Posted on July 07, 2015 at 07:27

Hi All, 

I am able to receive and send data using virtual comport but only one time . If we try to receive data multiple time data is not receiving . I have genarated code using cubeMX.

Any one faced same issue .? what may be the cause . 

kolos
Associate
Posted on July 29, 2015 at 13:16

Hy Jla!

I'm facing the exactly same problem with my STM32F407 disco board.

I can sand any char array from the terminal, the controller receives it, but does not respond to anything after that.

Have you found a solution?

I'm desperate please help! 🙂
kolos
Associate
Posted on July 29, 2015 at 13:24

Hy Jla!

I'm facing the same problem as you faced. I can receive one message through usb, but not any more without reseting the stm32f4 MCU.

Do you have a solution for it?

Please help I'm desperate.

Uros Males
Associate II
Posted on October 29, 2015 at 14:12

Hi kolos,

check [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/USB%20CDC%20Bug%20in%20CubeMX%20firmware&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=7680]this thread. 

Amel NASRI
ST Employee
Posted on October 30, 2015 at 10:28

Hi uros.m,

Do you mean another link? We are in a loop going back to this same discussion.

-Mayla-

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.

azevedocarlos
Associate
Posted on October 30, 2015 at 11:41

Hi all, it's my first time posting in this forum and I am doing it precisely because I'm facing some problems with USB. I never had any problem implementing new peripherals, until I had to use USB. :\

I am using my own board with a STM32F303 chip. I decided to use the cube for USB configuration and driver usage.

I started with some simple code to understand how each function works and didn't find any problem with ''CDC_Transmit_FS''. However, I don't get to understand how to implement the ''CDC_Receive_FS'' and how to use it. I know that it might be redundant asking for this. But does anyone actually managed to finish a project with a proper USB CDC implementation? Does anyone know how to use this function?

Thanks in advance!

Posted on November 02, 2015 at 11:30

Hibritzz,

I recommend you to refer to the example underSTM32Cube_FW_F3_V1.2.0\Projects\STM32303C_EVAL\Applications\USB_Device\CDC_Standalone. Besides, see theses[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/STM32%20Virtual%20Com%20Port%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=44]Link1 and

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/STm32F%20USB%20VCP%20example&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=754

.

-Shahrzad-

s54mtb
Associate II
Posted on November 20, 2015 at 21:52

I created CubeMX project for STM32F070F6, added CDC USB device. Then I added following code to CDC_Receive_FS dunction. 

USBD_CDC_ReceivePacket(&hUsbDeviceFS);

CDC_Transmit_FS(Buf, *Len);

All characters from terminal are echoed without problems. 

zhivko zivkovic
Associate III
Posted on December 27, 2017 at 11:14

I had same problem - for me using linux (ubuntu) trying to read usb data stream the problem reflected in dmesg output as this:

[54351.654920]

usb 1-1

: device not accepting address 2, error -62

[54351.822858]

usb 1-1

: new full-speed USB device number 6 using xhci_hcd

                                                       

[54351.823053]

usb 1-1

: Device not responding to setup address.

[54352.027064]

usb 1-1

: Device not responding to setup address.

[54352.230860]

usb 1-1

: device not accepting address 6, error -71

[54352.398876]

usb 1-1

: new full-speed USB device number 7 using xhci_hcd

                                                       

[54352.566899]

usb 1-1

: device descriptor read/64, error -71

[54352.838886]

usb 1-1

: device descriptor read/64, error -71                                                                     

[54353.110719]

usb 1-1

: new full-speed USB device number 8 using xhci_hcd

                                                       

[54353.278851]

usb 1-1

: device descriptor read/64, error -71

[54353.550828]

usb 1-1

: device descriptor read/64, error -71                                                                     

[54353.654892]

usb usb1-port1

: unable to enumerate USB device              

so after I changed usbd_cdc.h (generated by CUBEMX):

from

#define CDC_DATA_HS_MAX_PACKET_SIZE 512 /* Endpoint IN & OUT Packet size */

to

#define CDC_DATA_HS_MAX_PACKET_SIZE 256 /* Endpoint IN & OUT Packet size */

rebuiild and flash firmare to STM32F3, it resulted in dmesg:

[54776.500923]

usb 1-1

: new full-speed USB device number 9 using xhci_hcd

[54790.281970]

usb 1-1

: New USB device found, idVendor=0483, idProduct=5740

[54790.281982]

usb 1-1

: New USB device strings: Mfr=1, Product=2, SerialNumber=3

[54790.281990]

usb 1-1

: Product: STM32 Virtual ComPort

[54790.281996]

usb 1-1

: Manufacturer: STMicroelectronics

[54790.282001]

usb 1-1

: SerialNumber: 00000000001A

[54793.723570]

cdc_acm 1-1:1.0

: ttyACM2: USB ACM device

Now I can read successfuly from stm32f3 usb.

I was searching on internet for this problem with phrase '

device descriptor read/64, error -71',

so hopefully somebody will find this thread after this info was posted here.

I also noticed that in problem above

hUsbDeviceFS.dev_state == USBD_STATE_SUSPENDED.