5
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – KyranF Oct 13 '14 at 22:32
  • \$\begingroup\$ Seeing USB bus activity would be a strong clue. \$\endgroup\$ – Chris Stratton Jan 20 '15 at 17:27
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ usbd_core.c has USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev) { return USBD_OK; } ...How does this work? \$\endgroup\$ – cp.engr Dec 2 '16 at 21:36
  • \$\begingroup\$ OP is asking how to detect the connection, not what to do after it's detected. \$\endgroup\$ – cp.engr Dec 2 '16 at 21:37
  • \$\begingroup\$ This seems the right answer, but AFAICS that code is only active if USB is OTG. \$\endgroup\$ – Ezequiel Garcia Sep 6 '17 at 15:14
  • \$\begingroup\$ 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? \$\endgroup\$ – Alaa M. Oct 4 '18 at 6:52
  • \$\begingroup\$ I mean if you're using CubeMx it will overwrite your code every time you hit the generate button. \$\endgroup\$ – Alaa M. Oct 4 '18 at 7:27
0
\$\begingroup\$

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

\$\endgroup\$
  • 4
    \$\begingroup\$ 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? \$\endgroup\$ – Chris Stratton Oct 21 '14 at 18:36
  • \$\begingroup\$ 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" \$\endgroup\$ – Daniel Apr 4 '16 at 3:54
  • \$\begingroup\$ Agreed, though, as a pro struggling with this awful USB core and associated documentation, this is unnecessarily teasing. \$\endgroup\$ – Daniel Apr 4 '16 at 3:55
0
\$\begingroup\$
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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.