VBUS sensing doesn't work because internal pull-up is activated during USB init rather than when connected - STM32F072
Hi all, I just thought I'd add to the list of questions about USB! :grinning_face_with_sweat:
I read AN4879 and it says "when the host presence is detected by the device, the device connects its pull up resistor". This is very clear - first detect the host, second activate the pull-up. What actually happens in the Cube generated code though is that the USB device is initialised at boot and the embedded pull-up is immediately activated, whether a cable is attached or not.
This is triggered by a chain of function calls:
MX_USB_DEVICE_Init() -> USBD_Start() -> USBD_LL_Start() -> HAL_PCD_Start() -> USB_DevConnect() -> USBx->BCDR |= (uint16_t)USB_BCDR_DPPU;
This code is Cube generated so if I remove the call to USB_Start() it will just get added back in next time I make a change.
Should I call USBD_Stop() straight after the USBD_Start() or is there a different way to go about this? Then use my vbus detect pin to call USBD_Start() when the cable is plugged in.
A second and perhaps bigger issue this is causing is that, because I'm using a TVS device for protection, when the D+ pull-up is active it causes 2.56V to appear on the vbus pin (3.26V from the D+ line then 0.7V dropped across the TVS diode) this goes through a potential divider (R1=62k,R2=100k) and results in 1.57V on my vbus detect pin therefore I can detect the connected event but never detect a disconnect event as once the pull-up is active the vbus pins will never go below 1.57V and VIL is 1.36V.
I could swap the resistors so I get 1V when unplugged and 2V when plugged.
Has anyone else run into these issues? Is there a different way to detect connect/disconnect events? Do you just let the embedded pull-up go high upon USB init or do you override the default behaviour?
