# STM32 USB: detecting connection and disconnection

Using STM32F4 USB Mass Storage Controller example in Device Mode, how do you detect connections and disconnections with a host controller?

I tried polling the VBUS pin state yet it could be high from a wall adapter or high without any communication with the host.

Is there a register to check? I noticed DSTS (status I guess?) in the USB library structs but couldn't find its documentation nor any useful comments in the code.

• take this question to the STM forum, email their tech support. Is this a custom PCB with STM32 on it, or a discovery/other dev board? – KyranF Oct 13 '14 at 22:32
• Seeing USB bus activity would be a strong clue. – Chris Stratton Jan 20 '15 at 17:27

You could detect the connection and disconnection from this file:

usbd_core.c

and the API for it is this

USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef  *pdev)
USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef  *pdev)


I am not sure about USB Mass Storage Class but in the CDC class, the 2 APIs above detect the USB connection and disconnection, maybe this helps

Things to note:

• Connection > when the physical USB cable is plugged into the USB port
• Disconnection > when the physical USB cable is unplugged from the USB port

I used STMCubeMX to generate the USB CDC class.

• usbd_core.c has USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev) { return USBD_OK; } ...How does this work? – cp.engr Dec 2 '16 at 21:36
• OP is asking how to detect the connection, not what to do after it's detected. – cp.engr Dec 2 '16 at 21:37
• This seems the right answer, but AFAICS that code is only active if USB is OTG. – Ezequiel Garcia Sep 6 '17 at 15:14
• But these callbacks are already implemented in usbd_core.c and you can't add user code there. So how can the event be handled by the user? – Alaa M. Oct 4 '18 at 6:52
• I mean if you're using CubeMx it will overwrite your code every time you hit the generate button. – Alaa M. Oct 4 '18 at 7:27

The answer is very simple. Just like you tried VBUS, try DSTS, and see what happens! Good luck.

• This is scarcely an answer. The question states that DSTS is not clearly documented. Do you have any insight to add? Or are you suggesting just logging the value to see what happens to happen, without bothering to understand what it represents? – Chris Stratton Oct 21 '14 at 18:36
• Presumably @Guill is talking about this: "Bit0 SUSPSTS:Suspendstatus In device mode, this bit is set as long as a Suspend condition is detected on the USB. The core enters the Suspended state when there is no activity on the USB data lines for a period of 3 ms. The core comes out of the suspend: – When there is an activity on the USB data lines" – Daniel Apr 4 '16 at 3:54
• Agreed, though, as a pro struggling with this awful USB core and associated documentation, this is unnecessarily teasing. – Daniel Apr 4 '16 at 3:55
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
{
/* Inform USB library that core enters in suspend Mode. */
USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
......


in usbd_conf.c

Function calls on usb cable disconnecting

(tested on Mass Storage device, CubeMX)