Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] CDC.read freeze program if USB calble is disconnected
30-04-2013, 07:28 PM, (This post was last modified: 08-05-2013, 02:29 PM by moreno.)
#1
[SOLVED] CDC.read freeze program if USB calble is disconnected
Hi

I have a problem with CDC.read.

I write a small routine
Code:
void TaskRX()
{
//Funziona solo se il cavo è attaccato
  int Ctn=0;
  u8 Len = CDC.read(Buffer);
  if(Len > 0)
   {
    //NO SOH FOUND
    if (FSOH == false)
     {
      //Search for SOH
      for (Ctn = 0 ; Ctn < Len ; Ctn++)
       {
        if (Buffer[Ctn]==SOH)
         {
          FSOH=true;
          break;
         }
       }
     }
    //SOH Found
    if (FSOH == true)
     {
      //Search for EOH
      if (FrameLen+(Len-Ctn) > sizeof(Frame))
       {
        FrameLen=0;
        FSOH=FEOH=false;
        return;
       }
      for ( ; Ctn < Len ; Ctn++)
       {
        Frame[FrameLen++] = Buffer[Ctn];
        if (Buffer[Ctn] == EOH)
         {
          FEOH=true;
          break;
         }
       }
     }
   }
  if ( (FSOH==true) && (FEOH==true) )
   {
    for (Ctn = 0 ; Ctn < FrameLen ; Ctn++)
     CDC.printf("%c - %d\n\r",Frame[Ctn],Frame[Ctn]);
    //Comando #CA CR su CDC azzera contatori
    if ( (FrameLen == 4) && (Frame[1] == 'C')  && (Frame[2] == 'A') )
     {
      InitLog(1);
     }
    FrameLen=0;
    FSOH=FEOH=false;
   }
}
which receive command from CDC port to modify the state of the program, in this case clear the log data.

The code run without problem BUT.
The problems come if I start the program with the USB cable disconnected, in this case when CDC.read is called the first time the program freeze in a infinite loop.

The problem appear only with CDC.read, there is no problems using CDC.printf even if cable is disconnected.

There is a manner to check if the USB cable is connected and avoid to use CDC.read and freeze all??


Bye Bye, Moreno
Reply
08-05-2013, 08:41 AM, (This post was last modified: 08-05-2013, 08:58 AM by moreno.)
#2
RE: CDC.read freeze program if USB calble is disconnected
Hi

I found an easy solution to the problem.

I created this easy routine
Code:
BOOL CDC_USBConnected(void)
{
  return ( (U1OTGSTATbits.VBUSVD != 0) && (U1OTGSTATbits.SESVD != 0) );
}
Which check the status of U1OTGSTAT register and return true if VBUSVD and SESVD are set.

May be can be useful add this routine to the CDC library.


Now the program run correctly with and without USB cable, and run even if I disconnect the cable when the program run.
Remain the last problem, if I connect the USB cable when the program RUN the program freeze immediately and window signal a bad usb device.

Bye Bye, Moreno

Hi

Even the last problem was solved.

Code:
BOOL USB_Not_Connected = false;

BOOL CDC_USBConnected(void)
{
  if ( (U1OTGSTATbits.VBUSVD != 0) && (U1OTGSTATbits.SESVD != 0) )
   {
    if (USB_Not_Connected == true)
     {
      CDC_init();
//      CDC.init();
      USB_Not_Connected = false;
     }
    return(true);
   }
  else
   {
    USB_Not_Connected = true;
    return (false);
   }
}

I added a flag which signal USB disconnected, when the cable is connected the procedure exec an initialization of the USB port.


There is a BUG, CDC.init() ask for init_CDC instead of CDC_init.


Bye Bye, Moreno
Reply
18-06-2013, 06:23 PM, (This post was last modified: 18-06-2013, 06:27 PM by moreno.)
#3
RE: [SOLVED] CDC.read freeze program if USB calble is disconnected
Hi

Added this feature in 864 version.

A sample code for this function is:

Code:
void TaskRX()
{
  int Ctn=0;

  if (CDC.USBIsConnected() == false)
   return;

  u8 Len = CDC.read(Buffer);
  if(Len > 0)
   {
.................
.................

[postedit]
In the 865 version I correct even the BUG about init_CDC
[/postedit]

Bye Bye, Moreno
Reply
28-06-2013, 05:51 AM,
#4
RE: [SOLVED] CDC.read freeze program if USB calble is disconnected
Most users may not recognize the significance of this update, but anyone who use Pinguino for real life projects will know how valuable it is. Thank you.
Reply
28-06-2013, 07:08 AM,
#5
RE: [SOLVED] CDC.read freeze program if USB calble is disconnected
Hi

I'm happy which my contributions are useful and appreciated, Thank you.


Bye Bye, Moreno
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)