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)