Skip to main content
Explorer II
December 23, 2024
Question

Wrong echo from STM32F4 USB device

  • December 23, 2024
  • 2 replies
  • 3300 views

Hi all,

I'm developing a composite USB device on USB OTG port on a board based on STM32F429 chip.

The device has two interfaces: a custom device + a CDC device. I'm using USB_Device_Library code.

Windows host correctly shows 3 items: the composite device, the generic USB device with my custom label/name, a virtual serial port (COM8).

As test when I open the virtual port (by Putty) I see the three-linee welcome message and a prompt.

When I press a key on the terminal, the code is sent to the device by a BulkOut packet and the device, if the code is different from 0x0d, echoes the code by a BulkIn packet.

This is OK til I press the nine-th character on  the keyboard: as echo of this character I see garbage on the terminal.

Dumping the memory of the internal buffers I see that the device received the correct code and replied 1 byte with the correct code!

If I sniff the usb channel with WireShark I see a 7-bytes packet on BulkIn reply (!!).

My debug log shows that the register containing the number of chars to sent to the host (IN_EP_TSIZ) is 1 and the char written in the FIFO (in USB_WritePacket fn) is 0x39 '9':

 

 

RXFLVL OUT DATA_UPD
RXFLVL DATA_UPD EP buff: x200100c1 - count: 1
OEPINT
OEPINT XFRC ep1
[USBD_LL_DataOutStage]
[USBD_LL_DataOutStage] DataOut ep1 - idx=1
[terminal] echo '9'
[transmit] x20010128, len: 1
[USB_EPStartXfer] IN_EP_TSIZ x00080001
IEPINT
IEPINT TXFE
[USB_WritePacket] ep2 - len: 1
[USB_WritePacket] IN_EP_TSIZ x00080001
[USB_WritePacket] x39
IEPINT
IEPINT XFRC ep2
[USBD_LL_DataInStage] ep2
[USBD_CDC_DataIn] ep2 - id=1
[USBD_CDC_DataIn] endTx TxBuf: x20010128 - 39 -

 

 

 How it is possible that Wireshark and Windows terminal reveive a 7-bytes packet ??? (None of them are 0x39)

 

Thanks!

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    January 9, 2025

    Hi @SteMMo 

    The issue is likely linked to endpoint configuration or the way the USB stack in CDC class driver handles the transmission. Could you share your code to reproduce on my board Nucleo F439?

    SteMMoAuthor
    Explorer II
    January 29, 2025

    Hello!

    Which ep configuration/parameter/register I can check?

    In my case I'm using the IN 0x2 to send a single byte.

    I'm in the OTG_FS_IRQHandler -> HAL_PCD_IRQHandler() when the USB_OTG_DIEPINT_TXFE interrupt is received for EP2.

    Here I enter in the PCD_WriteEmptyTxFifo(), setup the ep variable mambers and len (=1) to call the USB_WritePacket() [stm32f4xx_ll_usb.c)] fn.

    Here the variables are ok; pSrc points the buffer containing the byte, len=1 and count32b=1.

    The registry values in the moment I write in the FIFO register (USB_WritePacket(..) in are:

    DIEPCTRL2: 0x80888040

    DIEPTSIZ2: 0x00080001

    On the host side (Windows and Wireshark) I see a BULK IN dev-to-host 0x82 packet of 7 bytes as DataLength.

    The routines are the 'standard' SDK involving stm32f4xx_hal_pcd.c and stm32f4xx_ll_usb.c files.

    Thanks!

    SteMMoAuthor
    Explorer II
    January 30, 2025

    Just for completeness:

    • I'm working on a composite device: the serial port device is one of the two exposed classes.
    • the first one is a custom device and it is working well
    • the EPOUT 0x01 (host to dev) is correct; when I open a serial terminal on host and press a key, I receive the correct byte on device, the problem seems to be on EPIN 0x82 (dev to host)
    • the replies of CONTROL packets are correct, so i guess EP0 is good

     

    Technical Moderator
    February 12, 2025