Skip to content

Commit

Permalink
Start to add menu option to run firmware on serial module
Browse files Browse the repository at this point in the history
Menu option is disabled for now.
  • Loading branch information
benlye committed Oct 31, 2020
1 parent d53d959 commit 84ae37d
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 1 deletion.
101 changes: 101 additions & 0 deletions src/flash-multi/Devices/SerialDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,106 @@ public static async Task WriteFlash(FlashMulti flashMulti, string fileName, stri
MessageBox.Show(Strings.succeededWritingFirmware, Strings.succeededWritingFirmware, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

/// <summary>
/// Launches the firmware on a serial MULTI-Module.
/// </summary>
/// <param name="flashMulti">An instance of the <see cref="FlashMulti"/> class.</param>
/// <param name="comPort">The COM port where the serial device can be found.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task<bool> RunFirmware(FlashMulti flashMulti, string comPort)
{
// Path to the flashing tool, stm32flash.exe
string command = ".\\tools\\stm32flash.exe";

// Baud rate for serial flash commands
int serialBaud = Properties.Settings.Default.SerialBaudRate;

// Arguments for the command line - will vary at each step of the process
string commandArgs;

// Variable to keep the return code from executed commands
int returnCode1 = -1;
int returnCode2 = -1;

// Write to the log
flashMulti.AppendLog($"{Strings.modeReading} {Strings.viaSerial}\r\n");

// Stop the serial monitor if it's active
SerialMonitor serialMonitor = null;
bool reconnectSerialMonitor = false;
if (Application.OpenForms.OfType<SerialMonitor>().Any())
{
Debug.WriteLine("Serial monitor window is open");
serialMonitor = Application.OpenForms.OfType<SerialMonitor>().First();
if (serialMonitor != null && serialMonitor.SerialPort != null && serialMonitor.SerialPort.IsOpen)
{
reconnectSerialMonitor = true;
Debug.WriteLine($"Serial monitor is connected to {serialMonitor.SerialPort.PortName}");

Debug.WriteLine($"Closing serial monitor connection to {serialMonitor.SerialPort.PortName}");
serialMonitor.SerialDisconnect();
}
}
else
{
Debug.WriteLine("Serial monitor is not open");
}

// Check if the port can be opened
if (!ComPort.CheckPort(comPort))
{
flashMulti.AppendLog($"{Strings.failedToOpenPort} {comPort}");
using (new CenterWinDialog(flashMulti))
{
MessageBox.Show($"{Strings.failedToOpenPort} {comPort}", Strings.dialogTitleRead, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

flashMulti.EnableControls(true);
return false;
}

// Write to the log
flashMulti.AppendLog($"[1/1] {Strings.progressReadingFlash}");

// Prepare the command line arguments for writing the firmware
commandArgs = $"-b {serialBaud} -g 0x8002000 {comPort}";

// Run the write command asynchronously and wait for it to finish
await Task.Run(() => { returnCode1 = RunCommand.Run(flashMulti, command, commandArgs); });

// Prepare the command line arguments for writing the firmware
commandArgs = $"-b {serialBaud} -g 0x8000000 {comPort}";

// Run the write command asynchronously and wait for it to finish
await Task.Run(() => { returnCode2 = RunCommand.Run(flashMulti, command, commandArgs); });

// Show an error message if the command failed for any reason
if (returnCode1 != 0 && returnCode2 != 0)
{
flashMulti.AppendLog($" {Strings.failed}");
using (new CenterWinDialog(flashMulti))
{
MessageBox.Show(Strings.failedToReadModule, Strings.dialogTitleRead, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

flashMulti.EnableControls(true);
return false;
}

// Reconnect the serial monitor if it was connected before
if (serialMonitor != null && serialMonitor.IsDisposed != true && reconnectSerialMonitor)
{
serialMonitor.SerialConnect(comPort);
}

// Write a success message to the log
flashMulti.AppendLog($" {Strings.done}\r\n\r\n");

// Re-enable the form controls
flashMulti.EnableControls(true);

return true;
}
}
}
11 changes: 10 additions & 1 deletion src/flash-multi/FlashMulti.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions src/flash-multi/FlashMulti.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,12 @@ public async void CheckControls()
if (selectedPort != null && selectedPort.ToString() != "USBasp" && selectedPort.ToString() != "DFU Device")
{
this.buttonSerialMonitor.Enabled = true;
this.runFirmwareToolStripMenuItem.Enabled = true;
}
else
{
this.buttonSerialMonitor.Enabled = false;
this.runFirmwareToolStripMenuItem.Enabled = false;
this.buttonRead.Enabled = false;
}

Expand Down Expand Up @@ -597,6 +599,7 @@ public async void CheckControls()
if (mapleCheck.DeviceFound && mapleCheck.UsbMode == true && selectedPort != null && selectedPort.ToString() == mapleComPort)
{
this.resetToDFUModeToolStripMenuItem.Enabled = true;
this.runFirmwareToolStripMenuItem.Enabled = false;
}
else
{
Expand Down Expand Up @@ -1077,6 +1080,15 @@ private async void ResetToDFUModeToolStripMenuItem_Click(object sender, EventArg
await this.ResetToDfuMode();
}

/// <summary>
/// Handles the user clicking the Run Firmare menu item.
/// </summary>
private async void RunFirmwareToolStripMenuItem_Click(object sender, EventArgs e)
{
// Run the module firmware
await this.RunMultiFirmware();
}

/// <summary>
/// Calls the device-specific method to read the module.
/// </summary>
Expand Down Expand Up @@ -1845,6 +1857,35 @@ private int RunDriverInstallerSync(string command, string args)
return returnCode;
}

private async Task RunMultiFirmware()
{
// Disable the buttons until this flash attempt is complete
Debug.WriteLine("Disabling the controls...");
this.EnableControls(false);

// Clear the output box
Debug.WriteLine("Clearing the output textboxes...");
this.textActivity.Clear();
this.textVerbose.Clear();
this.progressBar1.Value = 0;
this.outputLineBuffer = string.Empty;

/*
// Determine if we should use Maple device
MapleDevice mapleResult = MapleDevice.FindMaple();
// Determine if we should use a USBasp device
UsbAspDevice usbaspResult = UsbAspDevice.FindUsbAsp();
*/

// Get the selected COM port
string comPort = this.GetSelectedPort().ToString();

await SerialDevice.RunFirmware(this, comPort);

this.EnableControls(true);
}

/// <summary>
/// Async method to poulate the COM ports without blocking the UI.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/flash-multi/FlashMulti.resx
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,15 @@
<data name="upgradeBootloaderToolStripMenuItem.Text" xml:space="preserve">
<value>&amp;Flash Module Bootloader</value>
</data>
<data name="runFirmwareToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>219, 22</value>
</data>
<data name="runFirmwareToolStripMenuItem.Text" xml:space="preserve">
<value>Ru&amp;n Firmware</value>
</data>
<data name="runFirmwareToolStripMenuItem.Visible" type="System.Boolean, mscorlib">
<value>False</value>
</data>
<data name="actionsToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>180, 22</value>
</data>
Expand Down Expand Up @@ -1598,6 +1607,12 @@
<data name="&gt;&gt;upgradeBootloaderToolStripMenuItem.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;runFirmwareToolStripMenuItem.Name" xml:space="preserve">
<value>runFirmwareToolStripMenuItem</value>
</data>
<data name="&gt;&gt;runFirmwareToolStripMenuItem.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;helpToolStripMenuItem.Name" xml:space="preserve">
<value>helpToolStripMenuItem</value>
</data>
Expand Down

0 comments on commit 84ae37d

Please sign in to comment.