Does anyone know how I can detect if my USB device is connected to a Host or not?
Thx Steven
I don't know, can you measure/sense the voltage on the supply, or get interrupts from the USB controller indicating transactions on the data bus?
Could you provide any more information for example:
- which platform (microcontroller/board) you are using - which software you are using ARM(Keil) middleware or other
Since I want to know when a USB Host is attached observing Vusb isn't a solution b/c it also rises when a charger is connected. But thx anyway.
I'm using a STM32F2 with ARM Middleware.
Hello Steven, you could frequently poll the status of the stack using USBH_GetDeviceStatus, depending on what you are trying to achieve: www.keil.com/.../group__usbh__core_functions.html
Matthias
Well if monitoring VBUS is problematic, you can monitor Start Of Frame interrupts, only potential problem is that if host puts your device into suspend mode SOF will not be generated during suspended bus.
@Milorad How can I get access to the SOF Interrupt? I'm using ARM Middleware and just have access to USBD_Init, Connect, sendReport, etc.
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!
> Well if monitoring VBUS is problematic, you can monitor Start Of Frame interrupts, only potential problem is that if host puts your device into suspend mode SOF will not be generated during suspended bus.
Unfortunately, "SOF interrupt" is not an indication of bus connection in many USB MCUs. In such USB engine, "SOF interrupt" is internally generated by counter, synchronized to bus SOF timing (digital PLL). Even while no bus SOF appears, SOF interrupt generation continues. This mechanism is so-called "SOF recovery", to keep track over missing SOF on the bus, which is essential for USB audio/video implementation.
STM32F2xx/4xx is such a device.
Tsuneo
Tsuneo, are correct regarding SOF interrupts on STM32F, I have now tried this on real hardware and SOF were not generated until device was first connected on USB, but from that point on they continued regardless of USB cable being disconnected.
A little feedback from my side: I used the same design with the same diode as the eval board and after I changed the ESD diode to an other with a additional diode on Vusb and voila it works because the problem was that the USB D+ was keept high from the STM32f and was feeding its own detection line PA9.
Thx all for your help and hints