Main Site Documentation

USB CDC problems


#1

Hi,

We have some issues with the USBH_SerialUSB CDC driver which I hope you can help us with.

In one case we have integrated an external IO board (KTA-223 USB/RS485 Relay IO Board , http://www.sparkfun.com/products/9526) and we are polling for information 2 times pr. second.

Very often we get an Exception during read:
Exception was thrown: System.Exception
GHIElectronics.NETMF.USBHost.USBH_SerialUSB::Read_Helper
GHIElectronics.NETMF.USBHost.USBH_SerialUSB::Read

What is this exception really? Is this a timeout ? Is this some low level IO error ? What should we do when we get this error? How can we get more details about what REALLY is the problem ?What Exceptions exists (ioerror, timeout, ++?)

What we have done is that after a number (>3) of such errors (without any successfull read or write in between) we delete the SerialUSB object , and re-create it. This is something that very often doesnt work. So to get the CDC communication to work we need to execute USBHostController.Reset().

I am not happy with this code, so I am hoping you can help me with some feedback on what the correct way should be to re-initalize the USB device after a failure.

And if you know about common problems with USB CDC please let me know.

bold]Write code:[/b]
len_sent = _io.Write(data, 0, len_data);

Read code:
count = _io.Read(buffer, 0, 1); // read 1 character

Reset USB code:
foreach (USBH_Device device in USBHostController.GetDevices())
{
if ((device.TYPE >= USBH_DeviceType.Serial_FTDI) && (device.TYPE <= USBH_DeviceType.Serial_Sierra_C885))
{
this.Detach(device);
}
}

            Logger.Warn("Resetting USBHostController!");
            USBHostController.Reset(); // Reset the controller                        
            Thread.Sleep(1000);

USB event handler code:

void USBHostController_DeviceDisconnectedEvent(USBH_Device device)
{
if ((device.TYPE >= USBH_DeviceType.Serial_FTDI) && (device.TYPE <= USBH_DeviceType.Serial_Sierra_C885))
{
this.Detach(device);
}
}

    void USBHostController_DeviceConnectedEvent(USBH_Device device)
    {
        if ((device.TYPE >= USBH_DeviceType.Serial_FTDI) && (device.TYPE <= USBH_DeviceType.Serial_Sierra_C885))
        {
            this.Attach(device);
        }
    }

Attach code:
private void Attach(USBH_Device device)
{
try
{
_io = new USBH_SerialUSB(device, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.None);
_io.ReadTimeout = 100;

            Logger.Debug("Attached IOController on USBDevice, device type =" + device.TYPE.ToString() + ", VendorID = " + device.VENDOR_ID.ToString());
        }
        catch (Exception ex)
        {
            Logger.Error("Failed to attach SerialUsbIOController!", ex);
        }
    }

#2

We have also implemented our own extrernal HW with similar interfaces as the KTA-223 interface. This HW is based on the Atmega AT90USB1287 chip. On this chip we have implemented a CDC interface - using the LUFA stack (http://www.fourwalledcubicle.com/LUFA.php).

We have implemented and tested this using a PC - and with the PC the CDC and device works perfect.

When I connect the device to the EM or ChipWorkx I get the USBHostController_DeviceConnectedEvent, and the device is also identifiect correctly (I see this on the HID and PID).

After that I am able to send the first byte array with the command, but reading from the device generates an System.Exception. Trying to write once more to the device results in a return value of 0 from the _io.Write() function (meaning no bytes written).

I am really unsure what may cause this, so some advice from you would be very helpfull.

Best regards,
Ronny


#3

Please “code tag” your code so it is readable.

I think there is a “get last error” in USB host drivers that may help.


#4

Thanks.

I have now implemented logging that dumps the error from “get last error”

 
 USBH_ERROR err = USBHostController.GetLastError();
  Logger.Error("Description: " + err.ToString());

Do you have a list of error numbers and what they mean ?

I tested with unplugging to force an error, and received thus number: 268435461

Regards,
Ronny


#5

Found the list of errorcodes :slight_smile:

http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/html/89777f35-e895-ca98-0649-287509cac58b.htm