From 61c0e1157fcd40fac6e03cd22e0c65b16b4d9aea Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 18 Jan 2024 11:14:28 -0600 Subject: [PATCH 1/2] working on improving DFU --- Source/v2/Meadow.CLI.Core/DFU/DfuContext.cs | 99 +++++++++---------- .../Firmware/FirmwareWriter.cs | 56 +++++++++++ .../v2/Meadow.CLI.Core/Meadow.CLI.Core.csproj | 3 + Source/v2/Meadow.UsbLib/LibUsbDevice.cs | 13 +-- 4 files changed, 112 insertions(+), 59 deletions(-) create mode 100644 Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs diff --git a/Source/v2/Meadow.CLI.Core/DFU/DfuContext.cs b/Source/v2/Meadow.CLI.Core/DFU/DfuContext.cs index c42c2ff7..216fb844 100644 --- a/Source/v2/Meadow.CLI.Core/DFU/DfuContext.cs +++ b/Source/v2/Meadow.CLI.Core/DFU/DfuContext.cs @@ -1,57 +1,54 @@ -using System; -using System.Collections.Generic; -using DfuSharp; +using DfuSharp; -namespace MeadowCLI +namespace MeadowCLI; + +public class DfuContext { - public class DfuContext + private List validVendorIDs = new List { - private List validVendorIDs = new List - { - 0x22B1, // secret labs - 0x1B9F, // ghi - 0x05A, // who knows - 0x0483 // bootloader - }; - - // --------------------------- INSTANCE - public static DfuContext? Current; - - public static void Init() - { - Current = new DfuContext(); - Current._context = new Context(); - } - - public static void Dispose() - { - Current?._context?.Dispose(); - } - // --------------------------- INSTANCE - - private Context? _context; - - public List? GetDevices() - { - if (_context != null) - return _context.GetDfuDevices(validVendorIDs); - else - return null; - } - - public bool HasCapability(Capabilities caps) - { - if (_context != null) - return _context.HasCapability(caps); - else - return false; - } - - public void BeginListeningForHotplugEvents() - { - if (_context != null) - _context.BeginListeningForHotplugEvents(); - } + 0x22B1, // secret labs + 0x1B9F, // ghi + 0x05A, // who knows + 0x0483 // bootloader + }; + + // --------------------------- INSTANCE + public static DfuContext? Current; + public static void Init() + { + Current = new DfuContext(); + Current._context = new Context(); } + + public static void Dispose() + { + Current?._context?.Dispose(); + } + // --------------------------- INSTANCE + + private Context? _context; + + public List? GetDevices() + { + if (_context != null) + return _context.GetDfuDevices(validVendorIDs); + else + return null; + } + + public bool HasCapability(Capabilities caps) + { + if (_context != null) + return _context.HasCapability(caps); + else + return false; + } + + public void BeginListeningForHotplugEvents() + { + if (_context != null) + _context.BeginListeningForHotplugEvents(); + } + } \ No newline at end of file diff --git a/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs b/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs new file mode 100644 index 00000000..35dae8f2 --- /dev/null +++ b/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs @@ -0,0 +1,56 @@ +using Meadow.CLI.Core.Internals.Dfu; +using Meadow.LibUsb; +using Microsoft.Extensions.Logging; + +namespace MeadowCLI; + +public class FirmwareWriter +{ + public IEnumerable GetLibUsbDevices(bool useLegacyLibUsb = false) + { + ILibUsbProvider provider; + + if (useLegacyLibUsb) + { + provider = new ClassicLibUsbProvider(); + } + else + { + provider = new LibUsbProvider(); + } + + return provider.GetDevicesInBootloaderMode(); + } + + public bool IsDfuDeviceAvailable(bool useLegacyLibUsb = false) + { + try + { + return GetLibUsbDevices(useLegacyLibUsb).Count() > 0; + } + catch + { + return false; + } + } + + public async Task WriteOsWithDfu(string osFile, ILogger? logger = null, bool useLegacyLibUsb = false) + { + var devices = GetLibUsbDevices(useLegacyLibUsb); + + switch (devices.Count()) + { + case 0: throw new Exception("No device found in bootloader mode"); + case 1: break; + default: throw new Exception("Multiple devices found in bootloader mode - only connect one device"); + } + + var serialNumber = devices.First().GetDeviceSerialNumber(); + + await DfuUtils.FlashFile( + osFile, + serialNumber, + logger: logger, + format: DfuUtils.DfuFlashFormat.ConsoleOut); + } +} \ No newline at end of file diff --git a/Source/v2/Meadow.CLI.Core/Meadow.CLI.Core.csproj b/Source/v2/Meadow.CLI.Core/Meadow.CLI.Core.csproj index e1a160a8..c2c1bd24 100644 --- a/Source/v2/Meadow.CLI.Core/Meadow.CLI.Core.csproj +++ b/Source/v2/Meadow.CLI.Core/Meadow.CLI.Core.csproj @@ -17,6 +17,9 @@ + + + diff --git a/Source/v2/Meadow.UsbLib/LibUsbDevice.cs b/Source/v2/Meadow.UsbLib/LibUsbDevice.cs index 7c5e4398..d0a78185 100644 --- a/Source/v2/Meadow.UsbLib/LibUsbDevice.cs +++ b/Source/v2/Meadow.UsbLib/LibUsbDevice.cs @@ -18,14 +18,11 @@ static LibUsbProvider() public List GetDevicesInBootloaderMode() { - if (_devices == null) - { - _devices = _context - .List() - .Where(d => d.Info.VendorId == UsbBootLoaderVendorID) - .Select(d => new LibUsbDevice(d)) - .ToList(); - } + _devices = _context + .List() + .Where(d => d.Info.VendorId == UsbBootLoaderVendorID) + .Select(d => new LibUsbDevice(d)) + .ToList(); return _devices; } From 3514ddffa6a24d530011773ddea7dbc6eae2f5d9 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 18 Jan 2024 17:22:30 -0600 Subject: [PATCH 2/2] add debug writes for viz --- Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs | 1 + Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs b/Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs index dde5938b..e43b6774 100644 --- a/Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs +++ b/Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs @@ -55,6 +55,7 @@ private static void FormatDfuOutput(string logLine, ILogger? logger, DfuFlashFor } else //Console out { + Debug.WriteLine(logLine); Console.Write(logLine); Console.Write(logLine.Contains("%") ? "\r" : "\r\n"); diff --git a/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs b/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs index 35dae8f2..e8abb06d 100644 --- a/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs +++ b/Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs @@ -1,6 +1,7 @@ using Meadow.CLI.Core.Internals.Dfu; using Meadow.LibUsb; using Microsoft.Extensions.Logging; +using System.Diagnostics; namespace MeadowCLI; @@ -47,6 +48,8 @@ public async Task WriteOsWithDfu(string osFile, ILogger? logger = null, bool use var serialNumber = devices.First().GetDeviceSerialNumber(); + Debug.WriteLine($"DFU Writing file {osFile}"); + await DfuUtils.FlashFile( osFile, serialNumber,