Detect USB connected

Status
Not open for further replies.
You may be chasing your tail on the OTG stuff, I've the impression that OTG is related to the MK20DX.... being the host to some other USB client like device and I am not sure it will give you an interrupt you can use to wake with if it becomes a client on a USB bus mastered by the OS on a PC.

That said, I've been wrong before and that should be said too, about now, probably :)

...

Also, looking at the mk20dx128.h file, those status registers are there, but they don't seem to match the manual...I assume if it is 8 bit and the field value is 7, then you use 0x07, but in that file is usually has 0x80.

...

I think you are looking at a field referring to a bit number in which case hexadecimal 0x80 is 0b10000000 is 128 in decimal.

I think that to cut to the chase you should probably go with the resistor dividor to get the project done to an extent in which you are ready to release to the lab sooner. I also think that an alternative I'd happily enough vote for is to make the end user press the program button after plugging the Teensy in and running the Teensy Loader on the PC.
 
I think I will go with the button push, since it does not matter if they have the correct COM port selected, they can plug it in, try to upload the code....wait for the error msg, then push the button.

I will then just make 2 sets of code, one for calibration, where it won't sleep and they can access the serial port (since will use labview to collect and determine calibration values). And another that will be updated and loaded onto the device before being sent out.

Thanks to everyone for the help. Attached is an image of the board I made for this device.
IMG_20140626_081309.jpg
 
Also, looking at the mk20dx128.h file, those status registers are there, but they don't seem to match the manual...I assume if it is 8 bit and the field value is 7, then you use 0x07, but in that file is usually has 0x80.

The 7th bit would be a decimal 64, which is 0x80 in hex. I'm not very good with binary math, but there are ways of testing a register to find out whether 0x80 is a component of it, and this would tell you whether that particular bit is set in the register (wouldn't the test be something like 'if (REGISTER_TO_TEST & 0x80) {print "worky!"}'?). Unfortunately, I can't tell exactly what those BITBAND_ functions are doing (maybe trying to reference the proper memory location?), but my first guess would be to try changing your code from:
Code:
if ((BITBAND_U32(SIM_SCGC4, USB0_OTGSTAT_ID)) == 0x00)Serial.println("Worky");

To:
Code:
if ((BITBAND_U32(SIM_SCGC4, USB0_OTGSTAT_ID)) & 0x80)Serial.println("Worky");

Assuming you've chosen the proper register value (which, from the code, I would guess is a memory location) and I'm right about my guesses as to what the code means (which I'm not at all sure of), then that might solve your problem. If, however, the register always comes back equal to "0x00", then none of the bits (which could be anywhere from 8 to 32 different boolean values) are "true".
 
Code:
if ((BITBAND_U32(SIM_SCGC4, USB0_OTGSTAT_ID)) & 0x80)Serial.println("Worky");

This won't work, this macro "BITBAND_U32(x, n)" is meant to atomically read or (set/clear) certain bits in a 32bit register. Its similar to the bitSet(x,n) arduino function but you can also read if a certian bit in the register is set or cleared atomically, but remember it is for registers ONLY.

Where:

  1. x = register ex...SIM_SCGC4
  2. n = decimal position of the bit starting with least significant bit as 0

In the Teensy 3.1 and Teensy 3.0 manual it shows that the SIM_SCGC4 register has a bit to turn on or off the usb clock, that bit is at position 18 decimal in the SIM_SCGC4 register. So the correct way to use this macro would be:
Code:
#define USB0_OTGSTAT_ID 18

/*************************************** read bit example***************************************/
if ( BITBAND_U32( SIM_SCGC4, USB0_OTGSTAT_ID ) == 1 ) Serial.println("Worky");

or...

if ( BITBAND_U32( SIM_SCGC4, USB0_OTGSTAT_ID ) == 0 ) do something...// can't print because the clock is off

/************************************** set/clear bit example************************************/
//set bit
BITBAND_U32( SIM_SCGC4, USB0_OTGSTAT_ID ) = 1;

//clear bit
BITBAND_U32( SIM_SCGC4, USB0_OTGSTAT_ID ) = 0;

I just used 0x12 instead of 18 decimal for the bit position to confuse you;)
 
Thanks for the info.

I played around with it a bit last night, seeing if I could read the bits on the register that might indicate that the USB is plugged in, but really didn't understand how to indicate the correct bit position.
I just assumed there would be an easy way to programmatically indicate the USB was plugged in, however it seems that is not the case.

Thanks again for all the help and clarifications.
 
Hello everyone,

I have a very similar question. I have a running prototype (with a really packed PCB) and want to detect if a USB cable is plugged in to initialise the serial connection only when its used. It seams the serial connection is using quite a bit of power!? Am I right there?
The device is powered by a LiPo batterie and the usb connection is used to charge this LiPo, but not to power the Teensy directly. So I can't use the voltage as indicator for a plugged in USB cable.

Is there an elegant way to accomplish the detection in the software?
Anyone made progress with the OTG status?
 
Hey,

I have just solved my own question. I have an OLED display connected to print out the status. Here is the code:
Code:
      display.clearDisplay();
      display.setTextSize(1);
      for (int i=0;i<8;i++) {
        display.setCursor(10+6*i,4);
        display.print(i);
        display.setCursor(10+6*i,12);
        display.print(bitRead(USB0_OTGSTAT,i));
      }
      
      if (!bitRead(USB0_OTGSTAT,5)) {
        display.setCursor(10,22);
        display.print("USB connected");
      } else {
        display.setCursor(10,22);
        display.print("USB disconnected");
      }
      display.display();

I found out that bit 5 turns to "1" if USB is not connected.
I will go on testing this, but for now it's looking good ;)

USB0_OTGSTAT is defined in mk20dx128.h so you don't have do define anything. All you need is this on line of code:
Code:
bitRead(USB0_OTGSTAT,5)
 
Last edited:
Status
Not open for further replies.
Back
Top