cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042 Nucleo USB detect connection and disconnection

hans23
Associate II
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?

5 REPLIES 5
Ben K
Senior III
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.

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

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

Posted on September 05, 2017 at 17:28

Hi Werner,

So to rephrase, you don't want to detect the USB connection itself, but the availability of your MIDI driver on the host, is that correct?

In that case you should create a kind of notification mechanism from your PC driver, for example sending a specific encapsulated command on the control endpoint (e.g. a CDC_SEND_BREAK), which then the device could recognize in the EP0_RxReady callout and begin the data transfer. Alternatively, if your MIDI interface is only an input for the PC, you can use the data OUT endpoint for this purpose as well.

Posted on September 05, 2017 at 19:33

Hi Ben,

yes, i want to know that the class compliant midi driver on my win10 pc is running (i dont use my own special driver => so i cannot send any notification data to ep0!) and can receive data from my midi interface!

My midi interface can send data to my win10 pc and also receive data from my win10 pc!

Problem is, that i should only send data to my win10 pc, if the class compliant midi driver is working and can receive data!

Best regards 

Werner