Skip to main content
hans23
Associate II
September 2, 2017
Question

STM32F042 Nucleo USB detect connection and disconnection

  • September 2, 2017
  • 5 replies
  • 1773 views
Posted on September 02, 2017 at 14:55

Hi all,

i have a problem with STM32F042 Nucleo and USB Connect detection.

I made my own MIDI Interface with CUBEMX (generated a CDC Projekt and changed it with help from some internet sites to MIDI) and it works well. I can attach and deattach the USB connector and it works!

I can send data to the pc and the pc can send data to my Interface => tested with MIDIOX!

But i need to detect, that USB Connection to my window pc is established, because then i can only send data to the pc. In the moment i send without checking the connection!

But Problem:

If i connect my MIDI Interface to the pc with power off and then switch power on, i cannot detect the connection!

I found in 'usbd_core.c' the following functions:

/**

* @brief USBD_DevConnected

* Handle device connection event

* @param pdev: device instance

* @retval status

*/

USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev)

{

return USBD_OK;

}

/**

* @brief USBD_DevDisconnected

* Handle device disconnection event

* @param pdev: device instance

* @retval status

*/

USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev)

{

/* Free Class Resources */

pdev->dev_state = USBD_STATE_DEFAULT;

pdev->pClass->DeInit(pdev, pdev->dev_config);

return USBD_OK;

}

But these function are never called inside my STM32CUBE projekt => so they dont work!

How can secure detect the USB Connection?

    This topic has been closed for replies.

    5 replies

    Ben K
    Senior III
    September 3, 2017
    Posted on September 03, 2017 at 16:59

    These connected callbacks are only intended for OTG capable devices where you have built-in VBUS detection.

    I would suggest you to only start sending data when the USB device state has changed to 'configured', i.e. when the host has specified which of the available configurations should the device use. (Until that happens the device is not even supposed to open its non-control endpoints.) The STM32 USBD library calls the class specific Init() at this step.

    Detecting the disconnect of the device is more complicated, but I don't think you even need it for your application.

    hans23
    hans23Author
    Associate II
    September 3, 2017
    Posted on September 03, 2017 at 20:23

    Hi Ben,

    Thank you for the fast answer!

    I will test it as fast as possible!

    i think this is a solution for me!

    i dont need to know disconnect!

    Best regards from germany

    Werner

    hans23
    hans23Author
    Associate II
    September 4, 2017
    Posted on September 04, 2017 at 16:46

    Hi Ben,

    bad news!

    I used a global variable 'g_midi_connected', that was init to value 'NO'.

    In my main program i initialize my USB Device and then i wait, until this variable is 'YES'.

    If  my 'class specific Init()' is called from the USB Device Library i set this variable to 'YES'.

    Then i start a delay of 2 seconds and now i send to the WIN10 PC my first MIDI Data. This does not work, because the connection from my MIDI Interface to the PC is not finished in this moment! I increased this delaytime until it worked! This is approx 60s!!!!!

    This is the time from power on, until my windows 10 displays the windows desktop and is ready for work!

    So i can work with this solution, but it is not good, because disconnect and then reconnect of my MIDI Interface now has a delay until it works of 60s!

    Do you know a better solution?

    I must know, when windows is ready for receive data from my MIDI Interface!

    Best regards

    Werner