With FRDM KL46Z, how to turn off outputs when USB becomes disconnected?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

With FRDM KL46Z, how to turn off outputs when USB becomes disconnected?

1,031 Views
injunear
Contributor III

I have an Android tablet connected via USB to a FRDM KL46Z. How do I determine in the KL46Z when it loses the USB connection? I want to turn all outputs off at that point.

I tried using the USB1_OnError method in the Events.c file. I put an “extern” variable in a header file and I set the variable in the USB1_OnError method. I can then read the variable in my ProcessorExpert.c file and turn off the outputs accordingly, BUT, I don't know under what conditions USB1_OnError is called by the internal MCU programming. It does not appear to be called when the USB cable is disconnected.

When the USB cable is unplugged, my MCU program appears to hang. I don't know if an error is being generated at that point, or if the program is just waiting for a read or write function to complete.

Does anyone have any suggestions about how I can turn off the outputs when the USB cable is disconnected, or when there are any other USB disconnect issues?

If not, where I might find find the answers to these questions?

I am using the Processor Expert USB component in CW 10.5.

Thank you for you kind assistance.

0 Kudos
4 Replies

709 Views
injunear
Contributor III

Thank you for your suggestions, Erich.  Your program works as expected on my Win XP system. Also, I tried a KL46Z with your program connected to my Android tablet and the LEDs indicated correctly when the cable was unplugged, and then reconnected. Ditto Ubuntu 12.04 and Win XP on VirtualBox on Ubuntu. 

(It seems though, that even though the LED indicates the USB is reconnected the MCU no longer communicates with the terminal program.)


I have spent a great deal of time the last three days trying to figure this problem out. I probably tried a few hundred combinations of different code sequences to duplicate or cure the problem in both your program and mine. 


THE PROBLEM is that in order for the LEDs to indicate connect/disconnect status, the writing statement (such as CDC1_Send_Block or CDC1_Send_String) has to be inside the if(CDC1_GetCharsInRxBuf()!=0){...} statement.


I don't understand why this is but it is consistent.


In my program I have been writing to the USB at a point different from where I was reading from it. My USB reading and writing seemed to work fine for everything but this stupid USB disconnect issue. 


Now, at least, I know how to make the LEDs work right, even if it doesn't actually reestablish communication. I think I can do that from my Android tablet. 


Thanks for your help.

0 Kudos

709 Views
pavel_sadek
NXP Employee
NXP Employee

Hi,

I do not have the background about avialable methods for this, however I have a simple idea that can perhaps help:

if you remove the USB, the voltage directly on 5V input of USB disappears, what about to control this value by single input pin?

when it falls down, just switch off your outputs inside pin interrupt routine....

Pavel

709 Views
injunear
Contributor III

Pavel, That is an excellent alternative suggestion. Thank you.

0 Kudos

709 Views
BlackNight
NXP Employee
NXP Employee

Not sure if this applies to Android, of if (and how) Android is different. But what works for me under Windows7 is this:

while(CDC1_App_Task(cdc_buffer, sizeof(cdc_buffer))==ERR_BUSOFF) {
  /* device not enumerated */
LED1_Neg(); LED2_Off();
  WAIT1_Waitms(10);
}

I'm using the return code of App_Task() which I periodically check: if it returns ERR_BUSOFF then I know the cable is disconnected.