cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USB Virtual com implementation

Alexander Gornik
Associate II

Hello,

Using CubeMX, I have created USB CDC device for FS and started using the CDC.

By modifying CDC_Receive_FS() I am able to receive all data sent form the PC over USB and put the data in my buffer

Also (while the USB cable is connected and PC terminal is opened, I am able to send rarely data to the PC using CDC_Transmit_FS().

The next stage is sending many data via USB (e.g., my trace messages).

For this purpose I need to know when the USB cable is connected or disconnected and when PC is ready to accept my data (at least the PC port is opened) . So I will be able stop and resume my data sending

For USB connection/disconnection I use USER code sections of HAL_PCD_ResumeCallback() and HAL_PCD_SuspendCallback(). Is it OK? (HAL_PCD_DisconnectCallback has no USER CODE section)

My main questions are:

  1. How I can know (via what user modifiable callback) that The PC port was opened - closed)
  2. When I send data via CDC_Transmit_FS(), what modifiable callback/s inform about the status of the transmission (success or failed)

When I write "modifiable" I means that the code will not be lost every time when the Cube Mx recreates the project

Thanks, Alex Gornik

2 REPLIES 2
Pavel A.
Evangelist III

For 1: you can track when the host resets you, does enumeration and sends you some configuration requests. After this you can consider the connection active. But still the host can be lagging receiving data, you should be able to handle this (for example, set a timeout).

2: see function USBD_CDC_TransmitPacket in usbd_cdc.c. It can return three outcomes: USBD_OK (send complete), USBD_BUSY (TX still pending) or USBD_FAIL (error).

Function USBD_CDC_DataIn is the callback that signals TX completion.

Be careful with the Cube, always have your previous code backed up before you let Cube to mess with it. Review and merge changes. (yes, boring but better be safe than sorry).

Good luck.

-- pa

Alexander Gornik
Associate II
Hello, Pavel
1. As I understand, when sending:
a. Send the packet via USBD_CDC_TransmitPacket() and check the result
b. Start the timer (or use SysTick timer to check pdev->pClassData !=
NULL && hdcd->TxState == 0
c. In case if success continue sending
2. About port open/close on the PC
a. In previous implementation of the USB I was able to check CTS (on –
PC port opened, off – closed)
b. Is it possible to check CTS status via STM32 USB library. If yes,
how?
RGRDS, Alex

_____