NOTICE: This website will be shut down in the near future. Product content has moved to nxp.com. Forum content and FAQs have been moved to community.nxp.com. We encourage you to create a user account on nxp.com to use the new community forums and access NXP microcontroller content. We greatly appreciate your contributions and look forward to seeing you at our new web location.

 

Detect USB connect/disconnect using USBD ROM Stack?

6 replies [Last post]
lpetersen
Offline
Joined: 2015-12-09
Posts: 4

I've implemented a USB Device MSC on an LPC1837, using LPCOpen 2.12 and based on the usbd_rom_msc_ram example (modified to use external memory).

I'm able to see the device show up in Windows as a drive, and format it through Windows. Now I need to be able to detect when the user has either 'ejected' the drive from Windows or physically disconnected USB so that I can access the files in memory. I can't seem to find documentation on an interrupt or status bit to poll. How can I detect USB connect/disconnect events using the USBD ROM stack?

Much appreciated!

0
Your rating: None

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
MikeSimmonds
Offline
Joined: 2012-09-18
Posts: 591

Read the OHCI document here

See the current connect status in the port[N] status register Sect 7.4.4

You need to ensure that the rhsc (root hub status change) bit is set in the hcInterruptEnable register. Sect 7.1.5

Then on an interrupt look at the connect status.

BTW, I never used the ROM driver (I don't think that the LPC1778 even has one!),
so I cannot give specifics to actually achieve the steps above. Presumably the docs on the various functions
and structures of the ROM API should help once you know what you are looking for.

HTH.

Cheers, Mike.

Old programmers never die. They go into infinite recursion.

Old programmers never die. They go into infinite recursion.

lpetersen
Offline
Joined: 2015-12-09
Posts: 4

Thanks Mike. That was helpful in pointing me in the right direction.

I found a status bit in USB0's PORTSC1_D register for CCS which sounded promising. Upon further investigation though, that status bit does not change even when physically disconnecting USB. According to the user manual -
"A zero indicates that the device did not attach successfully or was forcibly disconnected by the software writing a zero to the Run bit in the USBCMD register. It does not state the device being disconnected or suspended."

Which I found to be true. There is another bit in that same register, SUSP, which indicates when the device is in a suspended mode. It looks like I can use this to determine when USB has been physically disconnected (SUSP goes high, indicating that the device is suspended), but the status remains 'Not Suspended' even when I 'Eject' from Windows.

Does anyone know if I am going about this the right way? Is there a way to detect that windows 'ejected' the device even if it is still physically connected? Does the USBD ROM stack provide any handles into this?

Thanks!

MikeSimmonds
Offline
Joined: 2012-09-18
Posts: 591

My bad, I was going by memory and a cursory re-read of the OHCI manual.

In my Interrupt handler (when root hub status change is noted), I actually check
bit 16 of the PortStatus[N] register called CSC in the OHCI doc.
If this is set, then I capture the CCS bit I previously mentioned (bit 0)
and stash this in a (volatile) variable that the rest of the "C" code can see.

These bit names are confusing and easy to mis-spell aren't they.

Note: I cannot share: It is written in assembler and is also company confidential.

NB: remember to (a) clear the status change bit(s) in the PortStatus and
(b) clear the IrqStatus bit(s) in the InterruptStatus in the IRQ handler.

My "C" code polls the 'connected' variable (via a function) ...

static void
WaitInsert(void)
{
	static
	ulong	ftt = 1;
	uchar	wrk[128], s[256];
	ulong	rc;

	if (prvConnect == usbConnect)		// any change?
		return;							// nope

//--------------------------------------//
	prvConnect = usbConnect;			// remember change

        Do stuff depending on insert (1, non-zero) or remove (0) ...

Cheers, Mike.

Old programmers never die. They go into infinite recursion.

Old programmers never die. They go into infinite recursion.

MikeSimmonds
Offline
Joined: 2012-09-18
Posts: 591

Note: I am on LPC1778, my UM says that 5V from USB connector must be available
on USB_PWRDx pin for detection to work!

From UM10470 (LPC17x8) Chapter 6 Pin Configuration

Quote:
USB_PWRD1 — Power Status for USB port 1 (host power switch). When using the
chip in USB host mode, the USB_PWRD input must be enabled. The USB host
controller will only detect a device connect event when the port power bit is set in
the OHCI and the USB_PWRD bit is asserted for the corresponding port.

THIS LITTLE NUGGET SHOULD BE STATED IN THE USB HOST SECTION OF THE MANUALS!!

On our board (self powered) we feed in 5V to the pin via a 47K (I think, don't quote me) series resistor.

Cheers, Mike.

Old programmers never die. They go into infinite recursion.

Old programmers never die. They go into infinite recursion.

MikeSimmonds
Offline
Joined: 2012-09-18
Posts: 591

PLEASE IGNORE MY POSTS

I had assumed that you were implementing a HOST not a DEVICE.
I have no experience coding for device.

Sorry, Mike.

Old programmers never die. They go into infinite recursion.

Old programmers never die. They go into infinite recursion.

lpetersen
Offline
Joined: 2015-12-09
Posts: 4

Still looking for info on this, does anybody have any input for USB device using the ROM stack?

feedback