From 6382a526e2bb15edc2587432340f636f32deabea Mon Sep 17 00:00:00 2001 From: fauxpark Date: Mon, 4 Nov 2019 15:09:58 +1100 Subject: [PATCH] Bootloader detection improvements (#129) --- windows/QMK Toolbox/Flashing.cs | 76 ++++++++----------------------- windows/QMK Toolbox/MainWindow.cs | 4 +- windows/QMK Toolbox/USB.cs | 27 +++++------ 3 files changed, 34 insertions(+), 73 deletions(-) diff --git a/windows/QMK Toolbox/Flashing.cs b/windows/QMK Toolbox/Flashing.cs index 05e2d9ea5f..a16f0e88c7 100644 --- a/windows/QMK Toolbox/Flashing.cs +++ b/windows/QMK Toolbox/Flashing.cs @@ -22,10 +22,10 @@ public enum Chipset Caterina, Stm32, Kiibohd, - Avrisp, - Usbasp, + AvrIsp, + UsbAsp, UsbTiny, - BootloadHID, + BootloadHid, AtmelSamBa, NumberOfChipsets }; @@ -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, @@ -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); } @@ -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) @@ -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) @@ -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"); @@ -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); @@ -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}\""); diff --git a/windows/QMK Toolbox/MainWindow.cs b/windows/QMK Toolbox/MainWindow.cs index ce2771e781..1789c6f1b5 100644 --- a/windows/QMK Toolbox/MainWindow.cs +++ b/windows/QMK Toolbox/MainWindow.cs @@ -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(); @@ -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); } } } diff --git a/windows/QMK Toolbox/USB.cs b/windows/QMK Toolbox/USB.cs index 6465651062..369c26d932 100644 --- a/windows/QMK Toolbox/USB.cs +++ b/windows/QMK Toolbox/USB.cs @@ -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 @@ -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); @@ -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 { @@ -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; } @@ -141,4 +142,4 @@ public bool AreDevicesAvailable() return available; } } -} \ No newline at end of file +}