2019-01-17 05:38 AM
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:
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
2019-01-17 06:10 AM
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
2019-01-17 06:26 AM