Skip to content

Commit

Permalink
Bootloader detection improvements (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark authored and drashna committed Nov 4, 2019
1 parent 296c0ad commit 6382a52
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 73 deletions.
76 changes: 18 additions & 58 deletions windows/QMK Toolbox/Flashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public enum Chipset
Caterina,
Stm32,
Kiibohd,
Avrisp,
Usbasp,
AvrIsp,
UsbAsp,
UsbTiny,
BootloadHID,
BootloadHid,
AtmelSamBa,
NumberOfChipsets
};
Expand Down Expand Up @@ -65,47 +65,7 @@ public Flashing(Printing printer)
_printer = printer;
EmbeddedResourceHelper.ExtractResources(_resources);

/*
var query = new System.Management.SelectQuery("Win32_SystemDriver") { Condition = "Name = 'libusb0'" };
var searcher = new System.Management.ManagementObjectSearcher(query);
var drivers = searcher.Get();
if (drivers.Count > 0)
{
printer.Print("libusb0 driver found on system", MessageType.Info);
}
else
{
printer.Print("libusb0 driver not found - attempting to install", MessageType.Info);
if (Directory.Exists(Path.Combine(Application.LocalUserAppDataPath, "dfu-prog-usb-1.2.2")))
Directory.Delete(Path.Combine(Application.LocalUserAppDataPath, "dfu-prog-usb-1.2.2"), true);
ZipFile.ExtractToDirectory(Path.Combine(Application.LocalUserAppDataPath, "dfu-prog-usb-1.2.2.zip"), Application.LocalUserAppDataPath);
var size = 0;
var success = Program.SetupCopyOEMInf(Path.Combine(Application.LocalUserAppDataPath,
"dfu-prog-usb-1.2.2",
"atmel_usb_dfu.inf"),
"",
Program.OemSourceMediaType.SpostNone,
Program.OemCopyStyle.SpCopyNewer,
null,
0,
ref size,
null);
if (!success)
{
var errorCode = Marshal.GetLastWin32Error();
var errorString = new Win32Exception(errorCode).Message;
printer.Print("Error: " + errorString, MessageType.Error);
}
}
*/

_process = new Process();
//process.EnableRaisingEvents = true;
//process.OutputDataReceived += OnOutputDataReceived;
//process.ErrorDataReceived += OnErrorDataReceived;
_startInfo = new ProcessStartInfo
{
UseShellExecute = false,
Expand Down Expand Up @@ -187,14 +147,14 @@ public void Flash(string mcu, string file)
FlashStm32(mcu, file);
if (Usb.CanFlash(Chipset.Kiibohd))
FlashKiibohd(file);
if (Usb.CanFlash(Chipset.Avrisp))
FlashAvrisp(mcu, file);
if (Usb.CanFlash(Chipset.Usbasp))
FlashUsbasp(mcu, file);
if (Usb.CanFlash(Chipset.AvrIsp))
FlashAvrIsp(mcu, file);
if (Usb.CanFlash(Chipset.UsbAsp))
FlashUsbAsp(mcu, file);
if (Usb.CanFlash(Chipset.UsbTiny))
FlashUsbTiny(mcu, file);
if (Usb.CanFlash(Chipset.BootloadHID))
FlashBootloadHID(file);
if (Usb.CanFlash(Chipset.BootloadHid))
FlashBootloadHid(file);
if (Usb.CanFlash(Chipset.AtmelSamBa))
FlashAtmelSamBa(file);
}
Expand All @@ -205,8 +165,8 @@ public void Reset(string mcu)
ResetDfu(mcu);
if (Usb.CanFlash(Chipset.Halfkay))
ResetHalfkay(mcu);
if (Usb.CanFlash(Chipset.BootloadHID))
ResetBootloadHID();
if (Usb.CanFlash(Chipset.BootloadHid))
ResetBootloadHid();
}

public void EepromReset(string mcu)
Expand All @@ -215,8 +175,8 @@ public void EepromReset(string mcu)
EepromResetDfu(mcu);
if (Usb.CanFlash(Chipset.Caterina))
EepromResetCaterina(mcu);
if (Usb.CanFlash(Chipset.Usbasp))
EepromResetUsbasp(mcu);
if (Usb.CanFlash(Chipset.UsbAsp))
EepromResetUsbAsp(mcu);
}

private void FlashDfu(string mcu, string file)
Expand All @@ -234,7 +194,7 @@ private void FlashDfu(string mcu, string file)

private void EepromResetCaterina(string mcu) => RunProcess("avrdude.exe", $"-p {mcu} -c avr109 -U eeprom:w:\"reset.eep\":i -P {CaterinaPort}");

private void EepromResetUsbasp(string mcu) => RunProcess("avrdude.exe", $"-p {mcu} -c usbasp -U eeprom:w:\"reset.eep\":i -P {CaterinaPort}");
private void EepromResetUsbAsp(string mcu) => RunProcess("avrdude.exe", $"-p {mcu} -c usbasp -U eeprom:w:\"reset.eep\":i -P {CaterinaPort}");

private void FlashHalfkay(string mcu, string file) => RunProcess("teensy_loader_cli.exe", $"-mmcu={mcu} \"{file}\" -v");

Expand All @@ -258,13 +218,13 @@ private void FlashKiibohd(string file)
}
}

private void FlashAvrisp(string mcu, string file)
private void FlashAvrIsp(string mcu, string file)
{
RunProcess("avrdude.exe", $"-p {mcu} -c avrisp -U flash:w:\"{file}\":i -P {CaterinaPort}");
_printer.Print("Flash complete", MessageType.Bootloader);
}

private void FlashUsbasp(string mcu, string file)
private void FlashUsbAsp(string mcu, string file)
{
RunProcess("avrdude.exe", $"-p {mcu} -c usbasp -U flash:w:\"{file}\":i");
_printer.Print("Flash complete", MessageType.Bootloader);
Expand All @@ -276,8 +236,8 @@ private void FlashUsbTiny(string mcu, string file)
_printer.Print("Flash complete", MessageType.Bootloader);
}

private void FlashBootloadHID(string file) => RunProcess("bootloadHID.exe", $"-r \"{file}\"");
private void ResetBootloadHID() => RunProcess("bootloadHID.exe", $"-r");
private void FlashBootloadHid(string file) => RunProcess("bootloadHID.exe", $"-r \"{file}\"");
private void ResetBootloadHid() => RunProcess("bootloadHID.exe", $"-r");

private void FlashAtmelSamBa(string file) => RunProcess("mdloader_windows.exe", $"-p {CaterinaPort} -D \"{file}\"");

Expand Down
4 changes: 2 additions & 2 deletions windows/QMK Toolbox/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ private void UpdateHidDevices(bool disconnected)

device.MonitorDeviceEvents = true;

_printer.Print($"{GetManufacturerString(device)}: {GetProductString(device)} connected -- {device.Attributes.VendorId:X4}:{device.Attributes.ProductId:X4}:{device.Attributes.Version:X4} ({GetParentIdPrefix(device)})", MessageType.Hid);
_printer.Print($"HID device connected: {GetManufacturerString(device)} {GetProductString(device)} ({device.Attributes.VendorId:X4}:{device.Attributes.ProductId:X4}:{device.Attributes.Version:X4})", MessageType.Hid);

device.ReadReport(OnReport);
device.CloseDevice();
Expand All @@ -415,7 +415,7 @@ private void UpdateHidDevices(bool disconnected)

if (!deviceExists)
{
_printer.Print($"HID device disconnected -- {existingDevice.Attributes.VendorId:X4}:{existingDevice.Attributes.ProductId:X4}:{existingDevice.Attributes.Version:X4} ({GetParentIdPrefix(existingDevice)})", MessageType.Hid);
_printer.Print($"HID device disconnected ({existingDevice.Attributes.VendorId:X4}:{existingDevice.Attributes.ProductId:X4}:{existingDevice.Attributes.Version:X4})", MessageType.Hid);
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions windows/QMK Toolbox/USB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public bool DetectBootloaderFromCollection(ManagementObjectCollection collection

private static bool MatchPid(string did, ushort pid) => Regex.Match(did, $"USB.*PID_{pid:X4}.*").Success;

private static bool MatchRev(string did, ushort rev) => Regex.Match(did, $"USB.*REV_{rev:X4}.*").Success;

public bool DetectBootloader(ManagementBaseObject instance, bool connected = true)
{
var connectedString = connected ? "connected" : "disconnected";
var deviceId = instance.GetPropertyValue("DeviceID").ToString();
var hardwareIds = (System.String[])instance.GetPropertyValue("HardwareID");
var deviceId = hardwareIds[0];

var deviceidRegex = new Regex(@"VID_([0-9A-F]+).*PID_([0-9A-F]+)\\([0-9A-F]+)");
var vp = deviceidRegex.Match(instance.GetPropertyValue("DeviceID").ToString());
var deviceidRegex = new Regex(@"VID_([0-9A-F]+).*PID_([0-9A-F]+).*REV_([0-9A-F]+)");
var vp = deviceidRegex.Match(deviceId);
var vid = vp.Groups[1].ToString().PadLeft(4, '0');
var pid = vp.Groups[2].ToString().PadLeft(4, '0');
var ver = vp.Groups[3].ToString().PadLeft(4, '0');
var uni = instance.GetPropertyValue("ClassGuid").ToString();
var comRegex = new Regex("(COM[0-9]+)");
var rev = vp.Groups[3].ToString().PadLeft(4, '0');

string deviceName;
if (MatchVid(deviceId, 0x03EB) && MatchPid(deviceId, 0x6124)) // Detects Atmel SAM-BA VID & PID
Expand All @@ -59,7 +59,7 @@ public bool DetectBootloader(ManagementBaseObject instance, bool connected = tru
deviceName = "DFU";
_devicesAvailable[(int)Chipset.Dfu] += connected ? 1 : -1;
}
else if (MatchVid(deviceId, 0x2341) || MatchVid(deviceId, 0x1B4F) || MatchVid(deviceId, 0x239a)) // Detects Arduino Vendor ID, Sparkfun Vendor ID, Adafruit Vendor ID
else if (MatchVid(deviceId, 0x2341) || MatchVid(deviceId, 0x1B4F) || MatchVid(deviceId, 0x239A)) // Detects Arduino Vendor ID, Sparkfun Vendor ID, Adafruit Vendor ID
{
deviceName = "Caterina";
_flasher.CaterinaPort = GetComPort(deviceId);
Expand All @@ -84,13 +84,13 @@ public bool DetectBootloader(ManagementBaseObject instance, bool connected = tru
{
deviceName = "AVRISP";
_flasher.CaterinaPort = GetComPort(deviceId);
_devicesAvailable[(int)Chipset.Avrisp] += connected ? 1 : -1;
_devicesAvailable[(int)Chipset.AvrIsp] += connected ? 1 : -1;
}
else if (MatchVid(deviceId, 0x16C0) && MatchPid(deviceId, 0x05DC)) // Detects AVR USBAsp VID & PID
{
deviceName = "USBAsp";
_flasher.CaterinaPort = GetComPort(deviceId);
_devicesAvailable[(int)Chipset.Usbasp] += connected ? 1 : -1;
_devicesAvailable[(int)Chipset.UsbAsp] += connected ? 1 : -1;
}
else if (MatchVid(deviceId, 0x1781) && MatchPid(deviceId, 0x0C9F)) // Detects AVR Pocket ISP VID & PID
{
Expand All @@ -102,14 +102,15 @@ public bool DetectBootloader(ManagementBaseObject instance, bool connected = tru
else if (MatchVid(deviceId, 0x16C0) && MatchPid(deviceId, 0x05DF)) // Detects Objective Development VID & PID
{
deviceName = "BootloadHID";
_devicesAvailable[(int)Chipset.BootloadHID] += connected ? 1 : -1;
_devicesAvailable[(int)Chipset.BootloadHid] += connected ? 1 : -1;
}
else
{
return false;
}

_printer.Print($"{deviceName} device {connectedString}: {instance.GetPropertyValue("Name")} -- {vid}:{pid}:{ver} {uni}", MessageType.Bootloader);
var connectedString = connected ? "connected" : "disconnected";
_printer.Print($"{deviceName} device {connectedString}: {instance.GetPropertyValue("Manufacturer")} {instance.GetPropertyValue("Name")} ({vid}:{pid}:{rev})", MessageType.Bootloader);
return true;
}

Expand Down Expand Up @@ -141,4 +142,4 @@ public bool AreDevicesAvailable()
return available;
}
}
}
}

0 comments on commit 6382a52

Please sign in to comment.