Does anyone know how I can detect if my USB device is connected to a Host or not?
Thx Steven
Do you have a Keil MCB board or different board?
I have the MCBSTM32F200 board and using the internal USB1.0 PHY
Hi Steven,
my suggestion for your case would be that you solder out USBLC6-4 ICs because they prevent VBUS going low when USB is disconnected. Also, you need to define USBD_VBUS_DETECT=1 for compilation of USBD_FS_STM32F2xx.c and USBD_HS_STM32F2xx.c which allows detection of VBUS changes.
Now, if all previous is working as expected, from user API you would call USBD_Configured which would return "true" if device was connected and successfully enumerated otherwise it would return "false" (also in case you connect charger, your device would never get enumerated so this function will be retuning "false" all the time).
For SOF variant, you would need to change USBD_FS_STM32F3xx.c and USBD_HS_STM32F3xx.c manually and add SOF interrupt handling in IRQ routine and provide information to your user application directly from IRQ routine.
Hi Milorad
I previously used USB_Configured and it works as you discribed but the problem is that when the host gets disconnected (pulling off the cable) this function still returns 'true'.
Yes, that is because of board design, there is USBLC6-4 chip which serves for protection of USB lines but combined with schottky diode D2 or D3 it prevents VBUS voltage to drop low when USB cable is disconnected and that is the reason why driver can not recognize the disconnect event thus it never provides it to middle layer which does not reset the configured information and keeps returning "true" on USBD_Configured.
my fault, I should have read the beginning of your post. The ESD diode was keeping Vusb high. Thx for all!