Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
Binary file modified Arduino/hardware/tools/avr/bin/avrdude.exe
Binary file not shown.
12,857 changes: 8,029 additions & 4,828 deletions Arduino/hardware/tools/avr/etc/avrdude.conf

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Boards/arduino_mega.board.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"$schema": "./mfboard.schema.json",
"AvrDudeSettings": {
"Attempts": 3,
"Device": "atmega2560",
"BaudRate": "115200",
"BaudRates": [ "115200" ],
"Programmer": "wiring",
"FirmwareBaseName": "mobiflight_mega",
"ResetFirmwareFile": "reset.arduino_mega_1_0_2.hex",
Expand Down
3 changes: 2 additions & 1 deletion Boards/arduino_micro.board.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"$schema": "./mfboard.schema.json",
"AvrDudeSettings": {
"BaudRate": "57600",
"Attempts": 3,
"BaudRates": [ "57600" ],
"Device": "atmega32u4",
"FirmwareBaseName": "mobiflight_micro",
"Programmer": "avr109",
Expand Down
3 changes: 2 additions & 1 deletion Boards/arduino_uno.board.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"$schema": "./mfboard.schema.json",
"AvrDudeSettings": {
"Attempts": 3,
"Device": "atmega328p",
"BaudRate": "115200",
"BaudRates": [ "115200" ],
"Programmer": "arduino",
"FirmwareBaseName": "mobiflight_uno",
"Timeout": 15000,
Expand Down
40 changes: 33 additions & 7 deletions Boards/mfboard.schema.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"$id": "https://mobiflight.com/mfboard.schema.json",
"title": "Root",
"type": "object",
"required": ["Connection", "HardwareIds", "Info", "ModuleLimits", "Pins"],
"required": [ "Connection", "HardwareIds", "Info", "ModuleLimits", "Pins" ],
"properties": {
"AvrDudeSettings": {
"$id": "#root/AvrDudeSettings",
"title": "Avrdudesettings",
"type": "object",
"description": "Settings for flashing Arduino devices with avrdude.",
"required": ["BaudRate", "Device", "FirmwareBaseName", "Programmer"],
"required": [ "Attempts", "BaudRates", "Device", "FirmwareBaseName", "Programmer" ],
"properties": {
"Attempts": {
"$id": "#root/AvrDudeSettings/Attempts",
"title": "Attempts",
"description": "Number of times AvrDude should retry connecting to the device.",
"type": "integer"
},
"BaudRate": {
"$id": "#root/AvrDudeSettings/BaudRate",
"title": "Baudrate",
"description": "Baud rate to use with AvrDude.",
"deprecated": true,
"description": "Baud rate to use with AvrDude. Deprecated. Use BaudRates instead.",
"type": "string"
},
"BaudRates": {
"$id": "#root/AvrDude/Settings/BaudRates",
"title": " Baudrates",
"description": "All supported baud rates to use with AvrDude.",
"type": "array",
"default": [],
"items": {
"$id": "#root/BaudRates/items",
"title": "Items",
"description": "A baud rate for communicating with the device.",
"type": "string"
}
},
"Device": {
"$id": "#root/AvrDudeSettings/Device",
"title": "Device",
Expand Down Expand Up @@ -107,6 +127,13 @@
"title": "Messagesize",
"description": "Maximum size of a CmdMessenger message, in bytes.",
"type": "integer"
},
"TimeoutForFirmwareUpdate": {
"$id": "#root/Connection/TimeoutForFirmwareUpdate",
"title": "Timeoutforfirmwareupdate",
"description": "Number of milliseconds to wait for the firmware update to complete before attempting to call GetInfo on the board. Default 15000.",
"type": "integer",
"default": 15000
}
}
},
Expand Down Expand Up @@ -176,7 +203,6 @@
"MaxOutputs",
"MaxServos",
"MaxSteppers",
"MaxInputShifters",
"MaxInputMultiplexer"
],
"properties": {
Expand Down Expand Up @@ -258,7 +284,7 @@
"$id": "#root/Pins/items",
"title": "Items",
"type": "object",
"required": ["isAnalog", "isPWM", "isI2C", "Pin"],
"required": [ "isAnalog", "isPWM", "isI2C", "Pin" ],
"properties": {
"isAnalog": {
"$id": "#root/Pins/items/isAnalog",
Expand All @@ -282,7 +308,7 @@
"$id": "#root/Pins/items/Name",
"title": "Name",
"description": "Optional display name for the pin.",
"examples": ["A1"],
"examples": [ "A1" ],
"type": "string"
},
"Pin": {
Expand Down
39 changes: 38 additions & 1 deletion MobiFlight/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;

namespace MobiFlight
{
Expand All @@ -12,10 +13,20 @@ namespace MobiFlight
public class AvrDudeSettings
{
/// <summary>
/// Baud rate to use with AvrDude.
/// Number of times AvrDude should retry connecting to the device.
/// </summary>
public int Attempts;

/// <summary>
/// Baud rate to use with AvrDude. Deprecated. Use BaudRates instead.
/// </summary>
public String BaudRate;

/// <summary>
/// All supported baud rates to use with AvrDude.
/// </summary>
public List<String> BaudRates;

/// <summary>
/// AvrDude device type for the device.
/// </summary>
Expand Down Expand Up @@ -91,6 +102,11 @@ public class Connection
/// Maximum size of a CmdMessenger message, in bytes.
/// </summary>
public int MessageSize;

/// <summary>
/// Number of milliseconds to wait for the firmware update to complete before attempting to call GetInfo on the board.
/// </summary>
public int TimeoutForFirmwareUpdate = 15000;
}

/// <summary>
Expand Down Expand Up @@ -212,6 +228,27 @@ public class Board
/// </summary>
public List<MobiFlightPin> Pins;

/// <summary>
/// Migrates board definitions from older versions to newer versions.
/// </summary>
public void Migrate()
{
// Migrate AvrDudeSettings from older versions.
if (AvrDudeSettings != null)
{
// Older versions of boards only specified a single baud rate. Handle the case where
// an old file was loaded by migrating the BaudRate value into the BaudRates array.
if (!String.IsNullOrEmpty(AvrDudeSettings.BaudRate) && AvrDudeSettings.BaudRates == null)
{
AvrDudeSettings.BaudRates = new List<string>()
{
AvrDudeSettings.BaudRate
};
}

}
}

public override string ToString()
{
return $"{Info.MobiFlightType} ({Info.FriendlyName})";
Expand Down
1 change: 1 addition & 0 deletions MobiFlight/BoardDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static void Load()
try
{
var board = JsonConvert.DeserializeObject<Board>(File.ReadAllText(definitionFile));
board.Migrate();
boards.Add(board);
Log.Instance.log($"Loaded board definition for {board.Info.MobiFlightType} ({board.Info.FriendlyName})", LogSeverity.Info);
}
Expand Down
54 changes: 27 additions & 27 deletions MobiFlight/MobiFlightFirmwareUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ public static bool UpdateFirmware(MobiFlightModule module, String FirmwareName)

public static void RunAvrDude(String Port, Board board, String FirmwareName)
{
String ArduinoChip = board.AvrDudeSettings.Device;
String Bytes = board.AvrDudeSettings.BaudRate;
String C = board.AvrDudeSettings.Programmer;
String message = "";

if (!IsValidFirmwareFilepath(FirmwarePath + "\\" + FirmwareName))
Expand All @@ -107,33 +104,36 @@ public static void RunAvrDude(String Port, Board board, String FirmwareName)

String FullAvrDudePath = $@"{ArduinoIdePath}\{AvrPath}";

var proc1 = new ProcessStartInfo();
string anyCommand =
$@"-C""{FullAvrDudePath}\etc\avrdude.conf"" {verboseLevel} -p{ArduinoChip} -c{C} -P{Port} -b{Bytes} -D -Uflash:w:""{FirmwarePath}\{FirmwareName}"":i";
proc1.UseShellExecute = true;
proc1.WorkingDirectory = $@"""{FullAvrDudePath}""";
proc1.FileName = $@"""{FullAvrDudePath}\bin\avrdude""";
proc1.Arguments = anyCommand;
proc1.WindowStyle = ProcessWindowStyle.Hidden;
Log.Instance.log($"{proc1.FileName}.", LogSeverity.Debug);
Log.Instance.log($"{anyCommand}.", LogSeverity.Debug);
Process p = Process.Start(proc1);
if (p.WaitForExit(board.AvrDudeSettings.Timeout))
foreach (var baudRate in board.AvrDudeSettings.BaudRates)
{
Log.Instance.log($"Firmware upload exit code: {p.ExitCode}.", LogSeverity.Debug);
// everything OK
if (p.ExitCode == 0) return;

// process terminated but with an error.
message = $"ExitCode: {p.ExitCode} => Something went wrong when flashing with command \n {proc1.FileName} {anyCommand}.";
} else
{
// we timed out;
p.Kill();
message = $"avrdude timed out! Something went wrong when flashing with command \n {proc1.FileName} {anyCommand}.";
var proc1 = new ProcessStartInfo();
string anyCommand =
$@"-C""{FullAvrDudePath}\etc\avrdude.conf"" {verboseLevel} -x attempts={board.AvrDudeSettings.Attempts} -p{board.AvrDudeSettings.Device} -c{board.AvrDudeSettings.Programmer} -P{Port} -b{baudRate} -D -Uflash:w:""{FirmwarePath}\{FirmwareName}"":i";
proc1.UseShellExecute = true;
proc1.WorkingDirectory = $@"""{FullAvrDudePath}""";
proc1.FileName = $@"""{FullAvrDudePath}\bin\avrdude""";
proc1.Arguments = anyCommand;
proc1.WindowStyle = ProcessWindowStyle.Hidden;
Log.Instance.log($"{proc1.FileName} {anyCommand}", LogSeverity.Debug);
Process p = Process.Start(proc1);
if (p.WaitForExit(board.AvrDudeSettings.Timeout))
{
Log.Instance.log($"Firmware upload exit code: {p.ExitCode}.", LogSeverity.Debug);
// everything OK
if (p.ExitCode == 0) return;

// process terminated but with an error.
message = $"ExitCode: {p.ExitCode} => Something went wrong when flashing with command \n {proc1.FileName} {anyCommand}.";
}
else
{
// we timed out;
p.Kill();
message = $"avrdude timed out! Something went wrong when flashing with command \n {proc1.FileName} {anyCommand}.";
}
Log.Instance.log(message, LogSeverity.Error);
}

Log.Instance.log(message, LogSeverity.Error);
throw new Exception(message);
}
}
Expand Down
3 changes: 1 addition & 2 deletions UI/Forms/FirmwareUpdateProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ async void UpdateOrResetModule(MobiFlightModule module, bool IsUpdate)
MobiFlightFirmwareUpdater.ArduinoIdePath = arduinoIdePath;
MobiFlightFirmwareUpdater.FirmwarePath = firmwarePath;

int timeout = 15000;
var task = Task<bool>.Run(() => {
bool UpdateResult;
if (IsUpdate)
Expand All @@ -130,7 +129,7 @@ async void UpdateOrResetModule(MobiFlightModule module, bool IsUpdate)
UpdateResult = MobiFlightFirmwareUpdater.Reset(module);
return UpdateResult;
});
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
if (await Task.WhenAny(task, Task.Delay(module.Board.Connection.TimeoutForFirmwareUpdate)) == task)
{
NumberOfModulesForFirmwareUpdate--;

Expand Down