PDA

View Full Version : disconnect USB - is it possible?



keymuu
- 10th November 2008, 13:09
Hi there!

PicBasicPro has four usb commands, read, write, init and service. Clean and simple indeed. However, one command is missing, DISCONNECT!
Is it possible to disconnect your current usb session without touching the cable?

When you plug an USB device you hear ding-dong and when you unplug it you hear dong-ding. This quit convenient for the users...

Let's say that you have a cable connected between your PIC and your PC, then you turn on your PIC, in the code first there will be USBINIT and after that you should call quit frequently USBSERVICE and very shortly after this you will hear the ding-dong sound. I do not know how many times you have to call USBINIT before you hear that ding-dong sound. Do you know that?

I would need to disconnect (hear the dong-ding sound) from my application, without unplugging the cable. How is that possible? :confused:

I tried skipping the call of USBSERVICE, but that did not do the trick. :mad:
I believe that you really have to hear the dong-ding sound before you really can believe that your application has been unplugged/disconnected.

If you can, please help...

Rob
- 10th November 2008, 17:25
Hi,


I do not know how many times you have to call USBINIT before you hear that ding-dong sound. Do you know that?


this can vary. I will reply tomorrow with a good example of how to do this unless somebody else replies first but for now look up posts from Darrell Taylor regarding the USB stuff as a hint.



I would need to disconnect (hear the dong-ding sound) from my application, without unplugging the cable. How is that possible? :confused:


Try:



UCON.3 = 0 ' UCON register bit 3 (USBEN) enables/disables
' the USB - see page 164 18F4550 datasheeet


Hope this helps in the mean time

Cheers

Rob

mister_e
- 10th November 2008, 18:14
Totally untested thought...
what happen if you stop calling USBSERVICE?

thereafter, what may happen if you use the method shown in the famous known "POST 148"


' snip
usb_device_state var byte EXT
CONFIGURED_STATE CON EXT

DoUSBinit:
pause 500
usbinit ' initialise USB...
repeat ' kick-start it
usbservice
until usb_device_state = CONFIGURED_STATE

POST 148
http://www.picbasic.co.uk/forum/showpost.php?p=30682&postcount=148

EDIT 1:
Here's the complete list for usb_device_state var


; USB Device States - To be used with [byte usb_device_state]
#define DETACHED_STATE 0
#define ATTACHED_STATE 1
#define POWERED_STATE 2
#define DEFAULT_STATE 3
#define ADR_PENDING_STATE 4
#define ADDRESS_STATE 5
#define CONFIGURED_STATE 6


EDIT:2
I don't think you can relly on that ding-dong noise... check USBDemo, it has this fake led showing if the device is connected or not. An another alternative is to use USBDeview...
http://www.nirsoft.net/utils/usb_devices_view.html

keymuu
- 10th November 2008, 21:18
Hi,
this can vary. I will reply tomorrow with a good example of how to do this unless somebody else replies first but for now look up posts from Darrell Taylor regarding the USB stuff as a hint.

Try:



UCON.3 = 0 ' UCON register bit 3 (USBEN) enables/disables
' the USB - see page 164 18F4550 datasheeet

Thank you Rob !!! :)

UCON.3 ' USB Control Register, USB Module Enable Bit,0=Detached, datasheet 17.2.1
That did do the trick, nice and smooth, without any problems, without any other lines of code... very nice indeed


Totally untested thought...
what happen if you stop calling USBSERVICE?
Nothing, no dong-ding, nothing you could measure. Or maybe you can, bat because I did not hear the dong-ding I skipped rest of the "testing"...:o


thereafter, what may happen if you use the method shown in the famous known "POST 148"
I will take a closer look at it in the future. At the moment I'm quite happy with that one line solution...

Thank you again Rob and Mister_e thank you indeed :)

Rob
- 11th November 2008, 10:30
Hey Keymuu,

glad that worked for you. The stuff to read that I was going to point you to today is the post that Steve is referring to above - this will help you achieve USB enumeration correctly.

Good luck

Rob

mister_e
- 11th November 2008, 17:15
Yup, all good stuff to know. One day i may need to do it as well.

Squibcakes
- 25th February 2010, 07:56
Thank you Rob !!! :)

UCON.3 ' USB Control Register, USB Module Enable Bit,0=Detached, datasheet 17.2.1
That did do the trick, nice and smooth, without any problems, without any other lines of code... very nice indeed


Nothing, no dong-ding, nothing you could measure. Or maybe you can, bat because I did not hear the dong-ding I skipped rest of the "testing"...:o


I will take a closer look at it in the future. At the moment I'm quite happy with that one line solution...

Thank you again Rob and Mister_e thank you indeed :)

Guys,

Having trouble compiling UCON.3= 0

Getting a syntax error with PB2.47. Anyone else?

Squib

FinchPJ
- 24th August 2010, 10:59
Guys,

Having trouble compiling UCON.3= 0

Getting a syntax error with PB2.47. Anyone else?

Squib

I had the same problem - have now bought PBP2.6 upgrade but have not tested yet but I got around this in assembler where UCON is recognised:

T0CON.7 = 0 ' Disable USBservice interrupts
' UCON.1 = 1 ' UCON not defined in PBP 2.46
@ clrf UCON ; Disable USB & detach from bus
@ clrf UIE ; Mask all USB interrupts

Peter

PS I have Timer0 running on 1 mSec interrupts - the Interrrupt handler simply doing an USBservice and returning - turned this off here also

FinchPJ
- 24th August 2010, 19:32
I have just had a chance to check this out and it works fine - add:

UCON var byte EXT
to your PBP 2.46 program and it should solve the problem! You can now refer to for example:

UCON.3 = 0
Peter