2014-04-11 01:07 AM
Hey everybody,
in order to initialize a working Virtual Com Port with CubeMX on the STM32F4 Discovery, I had to changepdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef));
topdev->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 512Changing 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 #vcp2015-06-25 12:48 PM
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 inI 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 */ }2015-07-06 10:27 PM
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 .2015-07-29 04:16 AM
2015-07-29 04:24 AM
2015-10-29 06:12 AM
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¤tviews=7680]this thread.2015-10-30 02:28 AM
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.
2015-10-30 03:41 AM
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!2015-11-02 02:30 AM
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¤tviews=44]Link1 and . -Shahrzad-2015-11-20 12:52 PM
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.2017-12-27 02:14 AM
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.