USB Client initialization crashes -

I have strip down the USB client configuration as much as I could. This version will crash with in 2 or 3 attempts. The new behavior is that the program is restarted on its own, see debug statements.


public class Program
 {
	public static string SerialNumber = "FFFFFFFFFFF";
	private const int WRITE_EP = 1;
	private const int READ_EP = 2;
        
        public static void Main()
        {
		UsbController[] controllers = UsbController.GetControllers();

		Debug.Print("begin program");
		Configuration.DeviceDescriptor device = new Configuration.DeviceDescriptor(0xDEAD, 0x0110, 0x0200);
		device.bDeviceClass = 0xFF;     // Vendor defined class
		device.bDeviceSubClass = 0xFF;     // Vendor defined subclass
		device.bMaxPacketSize0 = 64;        // Maximum packet size of EP0
		device.iManufacturer = 1;        // String #1 is manufacturer name (see string descriptors below)
		device.iProduct = 2;        // String #2 is product name
		device.iSerialNumber = 3;        // String #3 is the serial number

		// Create the endpoints
		Configuration.Endpoint writeEP = new Configuration.Endpoint(WRITE_EP, Configuration.Endpoint.ATTRIB_Write |     Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Synchronous);//Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Write);
		Configuration.Endpoint readEP = new Configuration.Endpoint(READ_EP, Configuration.Endpoint.ATTRIB_Read | Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Synchronous);// Configuration.Endpoint.ATTRIB_Bulk | Configuration.Endpoint.ATTRIB_Read);

		Configuration.Endpoint[] usbEndpoints = new Configuration.Endpoint[] { writeEP, readEP };

		// Set up the USB interface
		Configuration.UsbInterface usbInterface = new Configuration.UsbInterface(0, usbEndpoints);

		// Create array of USB interfaces
		Configuration.UsbInterface[] usbInterfaces = new Configuration.UsbInterface[] { usbInterface };

		// Create configuration descriptor
		Configuration.ConfigurationDescriptor config = new Configuration.ConfigurationDescriptor(200, usbInterfaces);

		// Create the string descriptors
		Configuration.StringDescriptor manufacturerName = new Configuration.StringDescriptor(1, "YourName");
		Configuration.StringDescriptor productName = new Configuration.StringDescriptor(2, "YourProductName");
		Configuration.StringDescriptor serialNumber = new Configuration.StringDescriptor(3, "0000-0" + USB.SerialNumber.Substring(0, 3) + "-" + USB.SerialNumber.Substring(3, 4) + "-" + USB.SerialNumber.Substring(7, 4));

		// Create the final configuration
		Configuration configuration = new Configuration();

		configuration.descriptors = new Configuration.Descriptor[] {device, config, manufacturerName,
                                                                        productName, serialNumber};
		try
		{
			// Set the configuration
			Debug.Print("begin usb controller configuration");
			controllers[0].Configuration = configuration;
			Debug.Print("end usb controller configuration");

			if (UsbController.ConfigError.ConfigOK != controllers[0].ConfigurationError)
			{
				Debug.Assert(true, "USB controller failed configuration");
			}

			// If all ok, start the USB controller.
			controllers[0].Start();
			Debug.Print("usb controller started");
			}
			       catch (ArgumentException)
			{
				Debug.Print("Can't configure USB controller, error " + controllers[0].ConfigurationError.ToString());
			}
			Thread.Sleep(Timeout.Infinite);
        }
    }

The debug statements when a crash happens:
begin program
begin usb controller configuration
begin program
begin usb controller configuration
end usb controller configuration
usb controller started

Windows Device Manager reports a - Unknown USB Device (Link in Compliance Mode)

should we switch to serial debug mode before start usb client?

@ Dat - Yes, we need to use serial debug mode, sometimes the configuration statement succeeds and I am able to set break points, inspect variables as expected.

My environment is VS2013 on Windows 8.1, I have seen this issue with G80, and G400 platforms. Both the 2015 R1 and 2016 R1 Pre-release both have this issue.

@ PHITEK -

add sleep(100) somewhere before controllers[0].Configuration = configuration; or just like this:

 Thread.Sleep(100);
                controllers[0].Configuration = configuration;

@ Dat - Ok I’ll give that a try this evening.

Just curious does this prevent the crash when single stepping over the initialization code with the Sleep(100) statement add? Guess I can try that as well.

@ Dat - Adding the sleep statement did not help, 9 out of 10 tries failed with the crash. This was tested on a G80 platform.

Happy to help with the testing of this issue.

@ PHITEK -

Hi, I can reproduce the issue only in G80. That because once usb was config, some variables aren’t completely reset after a soft reset. The workaround now is, every time you are going to deploy your application, hit the reset button (hard reset).

In begin of your main function, should have a piece of code like this:

public static void Main()
        {
            while (button.Read())
            {

                Thread.Sleep(100);
                led.Write(!led.Read());

            }
            // start your application
             .......
}

So usb configuration isn’t run until you hit the button, that is easier for debug. For final version you can remove them.
We will fix this issue in next release.

@ Dat - Ok I will give that a try this evening. Plus I will try on the G400 and possibly the G120 as we are moving to the G400 platform.

That’s good news this can be fixed for the next release.

Thanks @ Dat

@ Dat - Yes, waiting for the button pressed helps with the G80 platform. It does not crash as consistently now.

I tried the G400 and so far it works without the wait on pushbutton. I’ll continue to test the USB with serial port debugging over the next few days.

Thanks for looking at this issue.