Hello
I am using LPC1768 for one of the project and i need to detect if the USB device is plugged in. How is it possible that i detect something like a pen drive being inserted.
I will be using the controller to interact to the PC as well as with a pendrive and i dont intend to use OTG. I need to have a option which will be able to detect the connection type.
Regards Prakash
Why not?
Isn't that the obvious way to go for something that needs to be both a Host and a Device...?!
> I will be using the controller to interact to the PC as well as with a pendrive and i dont intend to use OTG.
If you drop the OTG option, your device has to mount two USB receptacles, one for host, another for device. Depending on the type of USB function (Device/Host/OTG), the type of receptacle is determined.
USB spec provides these receptacle options for each function. Device: standard-B, mini-B or micro-B Host : standard-A OTG : micro-AB
Typical circuits around USB receptacles (Device/Host/OTG) are found in this Olimex schematic for LPC-1766STK (pin-compatible to LPC1768). www.olimex.com/.../LPC-1766STK-SCH.pdf
[1] Connection As seen on this schematic, Device/Host/OTG functions on the chip share single pair of D+/D- ports (on the schematic, jumper blocks are mounted). To connect two receptacles to this ports, there are two ways. a) Direct connection b) Analog switch
a) Direct connection The two receptacles and the ports are directly connected each other. In this case, your device should have a mechanical shutter in front of the receptacles, so that users should plug to just one receptacle at a time.
b) Analog switch The receptacles are connected to the chip ports over analog switches, respectively. For bus switch of Full-speed USB, these analog switches are available.
Analog Devices www.analog.com/.../usb_switch.html
ON semiconductor www.onsemi.com/.../parametrics.do
Fairchild www.fairchildsemi.com/.../USBGuide_ENG_pgs.pdf
Maxim MAX4906 www.maxim-ic.com/.../3020 etc.
Host/Device mode (ie. active receptacle) is switched by a menu on the display of your device, or automatic detection of plug-in.
[2] Plug-in detection 1) Host plug-in detection on Device mode The voltage at VBUS pin of B receptacle is supplied to a MCU VBUS pin over resistor divider. (see USB_DEVICE section of above schematic)
2) Device plug-in detection on Host mode VBUS power should be supplied to the A receptacle. Plug-in of device is detected on the LPC1768 chip by D+/D- line voltage change.
[3] OTG - micro-AB receptacle To plug in a USB stick to this receptacle, an adapter (Micro-A plug to Standard-A receptacle) is required. Also, to connect the device to PC, Micro-B cable (Micro-B plug to Standard-A plug) is needed. These cables are not so popular on the market, yet. You may want to attach these accessories to your product set for user's convenience.
You'll fine similar discussion on Microchip USB forum www.microchip.com/.../tm.aspx
Tsuneo
Hello Tsuneo,
Thanks for your detail response.
My observation is as follow
>1) Host plug-in detection on Device mode >The voltage at VBUS pin of B receptacle is supplied to a MCU VBUS pin over resistor divider.
The voltage at VBUS pin is 5V while plug-in the cable.
>2) Device plug-in detection on Host mode >VBUS power should be supplied to the A receptacle. >Plug-in of device is detected on the LPC1768 chip by D+/D- line voltage change.
There is no changes on D+/D- line while plug-in of usb stick.
Is there any other method to detect the plug-in of USB Stick?
> There is no changes on D+/D- line while plug-in of usb stick.
Huh? How did you measure it?
In Host mode, D+/D- lines are pulled down by 15k Ohm resistors on the host side, respectively. When a device is plugged in, and when VBUS (5V) is supplied to the device, the device pulls up D+ (or D-) line by a 1k5 resistor. It causes voltage change at D+ (or D-) line.
It's the standard way for the host to detect device connection.
>In Host mode, D+/D- lines are pulled down by 15k Ohm resistors on the host side, respectively. When a device is plugged in, and when VBUS (5V) is supplied to the device, the device pulls up D+ (or D-) line by a 1k5 resistor. It causes voltage change at D+ (or D-) line.
It would be great if you can check the schematic, which is at below link and give suggetion
www.flickr.com/.../53850468@N08/5058618433/
-Prakash
Your Host circuit is almost similar to Olimex one. Which option do you take, a) direct connection, or b) Connection over analog switches?
When you take b) over analog switches, the Olimex circuits around the receptacles are applied as is. Just replace the jumper blocks with analog switches.
But when you take a) direct option, the 15k pull-down resistors on D+/D- lines are switched by Nch-MOSFETs or NPN transistors in series, respectively. These transistors are enabled on the Host mode by a spare port of the MCU. Sorry, I've forgot to write on this modification in above post.
For direct option I have drawn the circuit and the link of this circuit is
www.flickr.com/.../53850468@N08/5061175399/
In this circuit below two transistors will be used for switching.
Although i am not quite sure if i can use the transistor part on top of the circuit to detect the device (pen drive) plug in. If i am detecing the insertion in interrupt mode then even after detection, there will be variations on the line while data transfer and this can cause unpredictable interrupts.
Kindly let me know if what i am trying is right.
And are there any changes that are supposed to be made while the controller is used a end device. There is a series resistor (33ohm) in series on D+ and D- line. This is for end device.
The transistor switches for 15k pulldowns are fine.
> Although i am not quite sure if i can use the transistor part on top of the circuit to detect the device (pen drive) plug in.
For Host mode, you don't need any extra circuit for device plug-in detection. It is integrated in the host controller on the LPC176x chip. For a) Direct connection option, a sliding door in front of the USB receptacles is necessary, so that just single receptacle is exposed to users at a time. You may need to detect the door position, to switch USB mode between Host and Device automatically.
For b) Over analog switches option, such a sliding door is not required. Both receptacles are always exposed to users.
I made the hardware change as you suggest for Direct connection option.
The code is as follows.
if(IS_USB_CABLE_CONNECTED()) { if(!(device_and_host_switch_flag == DEVICE)) { LPC_USB->OTGStCtrl = 0; enumerated_flag = DEVICE; USB_Init(); // USB Initialization USB_Connect(TRUE); // USB Connect while(!USB_Configuration); // wait until USB is configured device_and_host_switch_flag = DEVICE; } } else if(is_usb_stick_connected()) { if(!(device_and_host_switch_flag == HOST)) { enumerated_flag = HOST; Host_Init(); /* Initialize the lpc17xx host device_and_host_switch_flag = HOST; /* Do data transfer */ LPC_GPIO0->FIOSET = BIT4 | BIT5; /*switch on trasistors connected to D+ and D- /* rc = Host_EnumDev(); /* Enumerate the device connected*/ if (rc == OK) { /* Initialize the mass storage and scsi interfaces */ rc = MS_Init( &blkSize, &numBlks, inquiryResult ); if (rc == OK) { rc = FAT_Init(); /* Initialize the FAT16 file system */ if (rc == OK) { Main_Copy(); //Copy MSREAD.TXT to MSWRITE.TXT } } } LPC_GPIO0->FIOCLR = BIT4 | BIT5;/*switch off trasistors connected to D+ and D- */ } } else { device_and_host_switch_flag = USB_NONE; }
The USB Device is enumerated fine for above code. But when I remove the USB cable and connect the USB stick host fails to enumerate.
If swap the LPC_GPIO0->FIOSET = BIT4 | BIT5; /* and LPC_GPIO0->FIOCLR = BIT4 | BIT5; /* */ then HOST is enumerate fine but during USB_Init(); the code gets abort.
After detecting the USB stick, software should turn on the both trasistors before transferring data and after completion of data transfer software should turn off the both trasistors right?
The USB Host circuit is at following link
www.flickr.com/.../53850468@N08/5058618433/in/photostream/
In above circuit I am using only below two transistors.
And USB Device circuit is at following link
www.flickr.com/.../53850468@N08/5071190151/
Is there any mistake in circuit?
Kindly give your suggession.
In a) Direct connection method, you can't switch device/host role by plug in to the connector. The current role (host/device) is detected by a switch attached to a sliding door in front of the USB receptacles. When the door switch detects role change, firmware disable current role, and it calls stack initialization for each role, host or device. In addition, before the host initialization, the firmware enables D+/D- pull-downs. Also, device initialization is preceded by disabling D+/D- pull-downs.
For a) Direct connection method, a sliding door is necessary so that users don't plug in to both receptacles at a time.
Anyway, try host and device separately, first.
On the host circuit of above post, Is the D+/D- pull-down resistors (R8/R9) on the first figure are switched by NPN transistors, as you show in the second figure?
On the device circuit, Replace NPN transistor (Q1) for the D+ pull-up with PNP. On the NPN emitter-follower circuit, V<sub>CE</sub> doesn't saturate, less than V<sub>BE</sub>. You may take this circuit from some KEIL board, but it is wrong one.
Hello Tsuneo
Is the D+/D- pull-down resistors (R8/R9) on the first figure are switched by NPN transistors, as you show in the second figure?
I am switching ON these two NPN transistors and trying to communicate with the pen drive.
It can be seen that Q1 that is being used is already a PNP configuration.
As you rightly suggested, I have tested the HOST and the device code saparatly and everything seems to work fine.
It would be very helpful if you can post the relevent changes. probably a circuit would be very helpful.
>> On the device circuit, >> Replace NPN transistor (Q1) for the D+ pull-up with PNP.
> It can be seen that Q1 that is being used is already a PNP configuration.
Aha surely, It's PNP. But the emitter and the collector are swapped on the schematic.
>Aha surely, It's PNP. >But the emitter and the collector are swapped on the schematic.
The schematic I have send of USB device is working fine.