cancel
Showing results for 
Search instead for 
Did you mean: 

USB cable detection

qwer.asdf
Senior
Posted on July 01, 2015 at 12:01

I use a custom STM32F407 board.

I want to detect USB FS cable presence (no matter to pc or charger). Here is what I did. First, the VBUS pin is initialized in usb_bsp.c USB_OTG_BSP_Init function, I didn't change anything here:

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);

I don't know why an input pin's OType is configured as open drain, but whatever. Next, in my code, I used to detect whether the cable is plugged using this function:

int
is_usb_vbus_present(
void
) {
return
(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == Bit_SET) ? 1 : 0;
}

When I just plug and unplug the cable, it's working, I get 1 from that function when the cable is plugged and 0 when it's unplugged. But when I initialize the device as USB device (e.g. VCP) then I keep getting 1 from that function even after unplugging the cable. What am I doing wrong? Or are there other, better ways of detecting USB cable (to turn on/off the battery charger). Thank you. #usb-otg
4 REPLIES 4
jpeacock
Associate II
Posted on July 02, 2015 at 19:48

If your board functions only as a ''B'' peripheral then using VBUS is sufficient to determine if a cable is plugged in.  But if you have an OTG USB port on your board it's more complicated.

Because OTG can also function as an ''A'' host by default the connector has to source VBUS from it's own power supply so that an external ''B'' peripheral has power to start up.  Chances are that's why you see VBUS on all the time once the OTG port is enabled.

OTG uses the fifth pin on the Micro-USB connector, the device ID, to determine if it should act as ''A'' or ''B''.  If the device ID pin is low then the external device is an ''A'', so the local VBUS is turned off and VBUS pin has incoming power from the remote device.  If the device ID is high then the external is a ''B'' device, which requires VBUS power from your board.

So if you want to determine if there's a connection watch for either a low device ID or SOFs and a high device ID.

  Jack Peacock
qwer.asdf
Senior
Posted on July 03, 2015 at 09:55

peacock.jack.003, thank you for your answer..

Unfortunately only the VBUS, DP and DM pins of the USB connector are connected to the microcontroller, the ID and SOF pins are not connected (when I was designing the board CubeMX didn't select those pins as needed pins when I selected USB FS in device only mode) so I use the ID and SOF pins for other purposes and I have disabled their initialization as alternate function pins in the usb_bsp.c file. Also, USE_HOST_MODE is not defined and the

USB_OTG_BSP_ConfigVBUS function is not being called, so why would the microcontroller activate the internal vbus sourcing?

What do you think, is there a way for me to solve this problem without redesigning the hardware? Thank you so much.

jpeacock
Associate II
Posted on July 06, 2015 at 13:58

Without an ID pin you really can't use the connector in OTG mode.  You have no way to know if VBUS should be sourced locally or is incoming from a remote device.

SOF is the start of frame in the USB protocol, not a pin.  If you receive SOFs you know there's something at the other end of the connector, sending SOF marks to your board.

I don't see any resolution to your problem.  It appears your hardware is set up as an OTG USB port, but if you don't use it that way you'll have to disable the VBUS supply to the connector and run in device mode only, no host support.  In that case you can use VBUS to detect a connection, but you won't be able to act as a host.

  Jack Peacock
qwer.asdf
Senior
Posted on July 08, 2015 at 13:12

I'm not using it in OTG mode, I use it only as USB device in CDC mode or MSC mode, no host mode is required.