Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to get DFU->HCOM transition to not die #448

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa
Settings = settingsManager;
}

private double _lastWriteProgress = -1;

private async Task<IMeadowConnection?> GetConnectionAndDisableRuntime()
{
var connection = await GetCurrentConnection();
Expand All @@ -52,7 +54,25 @@ public FirmwareWriteCommand(ISettingsManager settingsManager, FileManager fileMa
connection.FileWriteProgress += (s, e) =>
{
var p = (e.completed / (double)e.total) * 100d;
Console?.Output.Write($"Writing {e.fileName}: {p:0}% \r");
if (p == _lastWriteProgress) { return; }

_lastWriteProgress = p;

Logger?.LogInformation($"Writing {e.fileName}: {p:0}% {(p < 100 ? "\n" : "\r\n")}");
};
connection.DeviceMessageReceived += (s, e) =>
{
if (e.message.Contains("% downloaded"))
{ // don't echo this, as we're already reporting % written
}
else
{
Logger?.LogInformation(e.message);
}
};
connection.ConnectionMessage += (s, message) =>
{
Logger?.LogInformation(message);
};

Logger?.LogInformation("Disabling device runtime...");
Expand All @@ -65,13 +85,12 @@ private bool RequiresDfuForRuntimeUpdates(DeviceInfo info)
{
if (System.Version.TryParse(info.OsVersion, out var version))
{
switch (version.Major)
return version.Major switch
{
case 0: return true;
case 1:
return version.Minor < 8;
default: return false;
}
0 => true,
1 => version.Minor < 8,
_ => false,
};
}

return true;
Expand All @@ -81,13 +100,12 @@ private bool RequiresDfuForEspUpdates(DeviceInfo info)
{
if (System.Version.TryParse(info.OsVersion, out var version))
{
switch (version.Major)
return version.Major switch
{
case 0: return true;
case 1:
return version.Minor < 9;
default: return false;
}
0 => true,
1 => version.Minor < 9,
_ => false,
};
}

return true;
Expand Down Expand Up @@ -158,6 +176,7 @@ protected override async ValueTask ExecuteCommand()
if (dfuDevice != null)
{
Logger?.LogInformation($"DFU device detected. Using DFU to write OS");
UseDfu = true;
}
else
{
Expand All @@ -174,7 +193,9 @@ protected override async ValueTask ExecuteCommand()
return;
}

connection = await GetCurrentConnection();
dfuDevice?.Dispose();

connection = await GetConnectionAndDisableRuntime();
await connection!.WaitForMeadowAttach();

}
Expand Down Expand Up @@ -222,6 +243,8 @@ protected override async ValueTask ExecuteCommand()

if (UseDfu || RequiresDfuForRuntimeUpdates(deviceInfo))
{
await connection!.Device!.RuntimeDisable(CancellationToken);

write_runtime:
if (!await connection!.Device!.WriteRuntime(rtpath, CancellationToken))
{
Expand All @@ -246,21 +269,6 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi
if (connection == null) return; // couldn't find a connected device
}

connection.DeviceMessageReceived += (s, e) =>
{
if (e.message.Contains("% downloaded"))
{ // don't echo this, as we're already reporting % written
}
else
{
Logger?.LogInformation(e.message);
}
};
connection.ConnectionMessage += (s, message) =>
{
Logger?.LogInformation(message);
};

Logger?.LogInformation($"{Environment.NewLine}Writing Coprocessor files...");

string[] fileList;
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/MeadowConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public MeadowConnectionManager(ISettingsManager settingsManager)

if (route == null)
{
throw new Exception("No 'route' configuration set");
throw new Exception($"No 'route' configuration set.{Environment.NewLine}Use the `meadow config route` command. For example:{Environment.NewLine} > meadow config route COM5");
}

// TODO: support connection changing (CLI does this rarely as it creates a new connection with each command)
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.UsbLib.Core/ILibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface ILibUsbProvider
List<ILibUsbDevice> GetDevicesInBootloaderMode();
}

public interface ILibUsbDevice
public interface ILibUsbDevice : IDisposable
{
string GetDeviceSerialNumber();
}
5 changes: 5 additions & 0 deletions Source/v2/Meadow.UsbLib/LibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public LibUsbDevice(IUsbDevice usbDevice)
_device = usbDevice;
}

public void Dispose()
{
_device?.Dispose();
}

public string GetDeviceSerialNumber()
{
var serialNumber = string.Empty;
Expand Down
5 changes: 5 additions & 0 deletions Source/v2/Meadow.UsbLibClassic/ClassicLibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public ClassicLibUsbDevice(UsbRegistry usbDevice)
_device = usbDevice;
}

public void Dispose()
{
_device.Device.Close();
}

public string GetDeviceSerialNumber()
{
if (_device != null && _device.DeviceProperties != null)
Expand Down
Loading