STM32 CAN and USB cannot run at the same time, USB Device Sense? - Page 1
Forum section: electronics / general

author: jnz
author_rank: Frequent Contributor
timestamp: May 27, 2015, 03:47:38 pm
content: Since my STM32 parts use shared SRAM between the two peripherals, I need a way of detecting when the user plugs a USB device in or plugs my device into the host.You'd think the part where my module is a device would be easy because I could just look for 5v on the USB, but that's got to power my chip in this case, so it'll need to be tied to the 5v lines all over the board.Device, I'm really not sure. I need to look and see how the D+ / D- lines work. Maybe the are pulled high/low and when attached to a host I can use those lines as GPIO to detect a connection is there and switch from CAN Bus Mode to USB OTG Mode?Any suggestions on this? Most of my devices will be using CAN almost exclusively and should only shut that down when a USB Host or Device is attached. Kind of annoying they didn't make provisions to just allow for both to run at the same time!

author: Brutte
author_rank: Frequent Contributor
timestamp: May 27, 2015, 05:43:25 pm
content: Both D lines are pulled low by the USB host. IIRC that is 10x the 1R5 required by the device to PU. Even if you configure D+/D- in AF, you can still read IDR.As for the device being plugged in, same thing applies here. It would PU one of D lines so an EXTI on D is a good choice to sense a device.

author: Jeroen3
author_rank: Super Contributor
timestamp: May 27, 2015, 06:03:01 pm
content: So you have a bus powered device?You should be careful with the voltage levels of D+ and D- to prevent this from happening constantly:

author: jnz
author_rank: Frequent Contributor
timestamp: May 27, 2015, 06:38:45 pm
content: Quote from: Brutte on May 27, 2015, 05:43:25 pmOk...Host pulls down at 15K, so if I'm host, I can check for high on those lines by looking at IDR or EXTI/External Interrupt watching for rising edge of the device's pullup, that's easy enough. I'm pulling down and watching for voltage to come up.But I'm hungry and not following this for device... If I plan on being the device and can not count on just powering up into USB Mode by the sheer existence of power being there, I will be pulling up with a 1.5k that's built into some controllers, so I'll always see high on my D+/- lines even when plugged into a host right?

author: jnz
author_rank: Frequent Contributor
timestamp: May 27, 2015, 06:39:46 pm
content: Quote from: Jeroen3 on May 27, 2015, 06:03:01 pmI will occasionally be a USB powered device, and a self-powered device. What do you recommend to prevent that error? While in USB mode, it should be a very low power circuit, it's not "doing" anything except taking data in via USB MSC

author: Brutte
author_rank: Frequent Contributor
timestamp: May 27, 2015, 08:44:54 pm
content: QuoteIf it is a bus powered device then if it boots then it means there is an USB host connected.If it is a self-powered USB device then just sense voltage on VUSB pin. Then disable CAN, reconfigure USB, enable 1k5 PU on D+ and that is it.

author: jnz
author_rank: Frequent Contributor
timestamp: May 27, 2015, 08:49:49 pm
content: Quote from: Brutte on May 27, 2015, 08:44:54 pmIt's not that easy. It's bus powered when it's removed from it's working enviroment (for programming, adjusting, whatever). And will be self-powered when it's in it's functional home. I can't be sure anyone will want to drag a laptop out to it, it's much easier to just unplug it, take it to a PC, plug in and adjust the settings file, then re-install it.So when it boots, I won't know if it's home and self-powered or away from home and powered via VBUS.

author: jnz
author_rank: Frequent Contributor
timestamp: May 27, 2015, 09:04:48 pm
content: I'm starting to wonder if a VERY weak pullup on device side temporarily in place of the 1.5K pullups would work?200k-1m pullup on device D+/- line, when plugged in with be pulled down by the host's 15k. So at 3.3V, when a device is plugged in, I would see the line go down to under .2V or so, easily low enough to trigger a low state / zero in IDR.Then once I detect a low I can go turn on the 1.5K pullup to set FullSpeed/Highspeed mode, shut down CAN, and start the USB module yea? I'd need to see if a 200-1m pullup would mess anything up but I kinda doubt it.Since it's a very weak pullup I think it would probably be a fair idea to "debounce" it to make sure it wasn't just a transient error. After being sure it's really driven low enough (plugged in), shut the weak pullup off.Thoughts?EDIT: And for anyone in the future that might stumble over this... The "connectivity" line of STM32 parts do not have this CAN/USB simultaneous issue, there is separate RAM for each module. I could step up to a STM32F105/7 part and not have to deal with any of this. For this project, the much smaller and cheaper chips are more ideal for me, provided there is a solution that may work.

author: timb
author_rank: Super Contributor
timestamp: May 31, 2015, 10:48:46 pm
content: Put a diode coming from the 5V USB line and measure behind it to tell when you're plugged in. Then use a FET or USB Power Switch (TI makes good ones) to provide power in device mode.Sent from my Tablet