Skip to main content
Graduate II
November 25, 2023
Solved

USB Virtual Port Com as USB->Serial Converter

  • November 25, 2023
  • 4 replies
  • 7589 views

I am using a STM32F373 with the USB Middleware set to 'Communication Device Class (Virtual Port Com)'.

Is it possible to have it 'act' as a UART or SPI type port, or do I need to hook into the USB library to TX/RX data over USB?  

I currently use USART2 with a USB->SERIAL converter connected between my PC and STM32, but would like to eliminate the need to have it if I can find a way to get the built-in USB Middleware to act as a converter. 

I'm also looking at using a FTDI FT231XS-U IC as an on-board USB->Serial converter so only a plain USB cable is needed to connect the board.

Thanks,

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    There are typically USB CDC Device examples   https://github.com/STMicroelectronics/STM32CubeF3/tree/master/Projects/STM32373C_EVAL/Applications/USB_Device/CDC_Standalone

    You need to replace the USART plumbing with your own way of managing the data to/from the USB. ie buffers, parsing, responses, etc.

    https://github.com/STMicroelectronics/STM32CubeF3/blob/master/Projects/STM32373C_EVAL/Applications/USB_Device/CDC_Standalone/Src/usbd_cdc_interface.c

    4 replies

    Graduate II
    November 25, 2023
    Super User
    November 25, 2023

    > Is it possible to have it 'act' as a UART or SPI type port,

    USB isn't going to act like UART or SPI. It's USB and you need to implement the machinery that USB requires. In this case, the CDC device type.

    If you use a FT231 chip instead, you can just use a UART, but you will suffer some loss of speed as a result. May or may not be an issue.

    LMorr.3Author
    Graduate II
    November 26, 2023

    I was using USART's idle interrupt to detect when the PC was done sending a fixed sized packet of 15 bytes.  I now see that the USB Device in CDC mode's rx callback can be used in the same way as the USART Idle callback.  

    My only question; If the PC host sends 15 bytes, will the CDC_Receive_FS callback parameters Len and Buf  always be 15 bytes, or is there a chance the 15 bytes may be received in 2 events?  For example, is it possible the CDC_Receive_FS callback gets called twice, once with a Len of let say 10 bytes, and a second time with a length of 5 bytes?  I'm guessing the Len value will always be 15 bytes if that is what the PC host sends, and that is handled by USB CDC.  ( as long as i've set the max size USB can send to more than 15 bytes )

    Graduate II
    November 27, 2023

    No, USART, USB CDC and TCP are stream interfaces and doesn't have a concept of messages. Therefore bytes can come in an arbitrary sized blocks.