Skip to content

Commit

Permalink
Replace DFU implementation with ST Cube Programmer (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Aug 25, 2021
1 parent 34325da commit 62cc30c
Show file tree
Hide file tree
Showing 13 changed files with 729 additions and 616 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ It makes use of several 3rd party tools:

- Espressif esptool
You can find the esptool and licensing information on the repository [here](http://github.com/espressif/esptool).
- QMKDfuSe
Tool based on STM DfusSe tool. You can find the source, licensing information and documentation [here](https://github.com/qmk/qmk_dfuse).
- STM32 Cube Programmer
You can find the source, licensing information and documentation [here](https://www.st.com/en/development-tools/stm32cubeprog.html).
- Texas Instruments Uniflash
Expand Down
Binary file removed lib/stdfu/qmk-dfuse.exe
Binary file not shown.
33 changes: 33 additions & 0 deletions nanoFirmwareFlasher/Exceptions/CantConnectToDfuDeviceException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using System;
using System.Runtime.Serialization;

namespace nanoFramework.Tools.FirmwareFlasher
{
/// <summary>
/// Couldn't open the specified DFU device.
/// </summary>
[Serializable]
internal class CantConnectToDfuDeviceException : Exception
{
public CantConnectToDfuDeviceException()
{
}

public CantConnectToDfuDeviceException(string message) : base(message)
{
}

public CantConnectToDfuDeviceException(string message, Exception innerException) : base(message, innerException)
{
}

protected CantConnectToDfuDeviceException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
36 changes: 0 additions & 36 deletions nanoFirmwareFlasher/Exceptions/CantOpenDfuDeviceException.cs

This file was deleted.

14 changes: 13 additions & 1 deletion nanoFirmwareFlasher/ExitCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum ExitCodes
/// <summary>
/// Error flashing DFU dvice
/// </summary>
[Display(Name = "Error flashing DFU dvice.")]
[Display(Name = "Error flashing DFU device.")]
E1003 = 1003,

/// <summary>
Expand All @@ -43,6 +43,18 @@ public enum ExitCodes
[Display(Name = "Firmware package doesn't have a DFU package.")]
E1004 = 1004,

/// <summary>
/// Can't connect to specified DFU device.
/// </summary>
[Display(Name = "Can't connect to specified DFU device. Make sure it's connected and that the ID is correct.")]
E1005 = 1005,

/// <summary>
/// Failed to start execution on the connected device.
/// </summary>
[Display(Name = "Failed to start execition on the connected device.")]
E1006 = 1006,

////////////////////////
// ESP32 tools Errors //
////////////////////////
Expand Down
7 changes: 0 additions & 7 deletions nanoFirmwareFlasher/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ public class Options
{
#region STM32 DFU options

[Option(
"dfufile",
Required = false,
Default = null,
HelpText = "DFU file to be flashed into the device.")]
public string DfuFile { get; set; }

[Option(
"listdfu",
Required = false,
Expand Down
89 changes: 49 additions & 40 deletions nanoFirmwareFlasher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
// DFU related
else if (
o.ListDevicesInDfuMode ||
!string.IsNullOrEmpty(o.DfuDeviceId) ||
!string.IsNullOrEmpty(o.DfuFile))
!string.IsNullOrEmpty(o.DfuDeviceId))
{
o.Platform = "stm32";
}
Expand Down Expand Up @@ -520,11 +519,11 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)

if (o.ListDevicesInDfuMode)
{
var connecteDevices = StmDfuDevice.ListDfuDevices();
var connecteDevices = StmDfuDevice.ListDevices();

Console.ForegroundColor = ConsoleColor.Cyan;

if (connecteDevices.Count == 0)
if (connecteDevices.Count() == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("No DFU devices found");
Expand All @@ -533,9 +532,9 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
{
Console.WriteLine("-- Connected DFU devices --");

foreach (string deviceId in connecteDevices)
foreach ((string serial, string device) device in connecteDevices)
{
Console.WriteLine(deviceId);
Console.WriteLine($"{device.serial} @ {device.device}");
}

Console.WriteLine("---------------------------");
Expand Down Expand Up @@ -588,55 +587,65 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
return;
}

var connectedStDfuDevices = StmDfuDevice.ListDfuDevices();
var connectedStDfuDevices = StmDfuDevice.ListDevices();
var connectedStJtagDevices = StmJtagDevice.ListDevices();

if (!string.IsNullOrEmpty(o.DfuFile) && connectedStDfuDevices.Count != 0)
if (o.BinFile.Any() &&
o.HexFile.Any() &&
connectedStDfuDevices.Count != 0)
{
// there is a DFU file argument, so follow DFU path
var dfuDevice = new StmDfuDevice(o.DfuDeviceId);

if (!dfuDevice.DevicePresent)
#region STM32 DFU options

try
{
// no DFU device found
var dfuDevice = new StmDfuDevice(o.DfuDeviceId);

// done here, this command has no further processing
_exitCode = ExitCodes.E1000;
if (!dfuDevice.DevicePresent)
{
// no JTAG device found

return;
}
// done here, this command has no further processing
_exitCode = ExitCodes.E5001;

if (_verbosityLevel >= VerbosityLevel.Normal)
{
Console.WriteLine($"Connected to DFU device with ID { dfuDevice.DeviceId }");
}
return;
}

// set verbosity
dfuDevice.Verbosity = _verbosityLevel;
if (_verbosityLevel >= VerbosityLevel.Normal)
{
Console.WriteLine($"Connected to JTAG device with ID { dfuDevice.DfuId }");
}

// get mass erase option
dfuDevice.DoMassErase = o.MassErase;
// set verbosity
dfuDevice.Verbosity = _verbosityLevel;

try
{
dfuDevice.FlashDfuFile(o.DfuFile);
// get mass erase option
dfuDevice.DoMassErase = o.MassErase;

// done here, this command has no further processing
_exitCode = ExitCodes.OK;
if (o.HexFile.Any())
{
_exitCode = dfuDevice.FlashHexFiles(o.HexFile);

return;
}
catch (DfuFileDoesNotExistException)
{
// DFU file doesn't exist
_exitCode = ExitCodes.E1002;
// done here
return;
}

if (o.BinFile.Any())
{
_exitCode = dfuDevice.FlashBinFiles(o.BinFile, o.FlashAddress);

// done here
return;
}
}
catch (Exception ex)
catch (CantConnectToDfuDeviceException)
{
// exception with DFU operation
_exitCode = ExitCodes.E1003;
_extraMessage = ex.Message;
// done here, this command has no further processing
_exitCode = ExitCodes.E1005;
}

#endregion

}
else if (
o.BinFile.Any() &&
Expand Down Expand Up @@ -702,7 +711,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
// update operation requested?
if (o.Update)
{
// this to update the device with fw from Cloudsmith
// this to update the device with fw from CloudSmith

// need to take care of flash address
string appFlashAddress = null;
Expand Down
50 changes: 50 additions & 0 deletions nanoFirmwareFlasher/Stm32Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace nanoFramework.Tools.FirmwareFlasher
/// </summary>
internal class Stm32Firmware : FirmwarePackage
{
[Obsolete("This property is discontinued and it will be removed in a future version.")]
public bool HasDfuPackage => !string.IsNullOrEmpty(DfuPackage);

public string NanoBooterFile { get; private set; }
Expand Down Expand Up @@ -52,5 +53,54 @@ public Stm32Firmware(

return executionResult;
}

public uint GetdBooterStartAddress()
{
uint address;

// find out what's the CLR block start

// do this by reading the HEX format file...
var textLines = File.ReadAllLines(NanoBooterFile);

// ... and decoding the start address
var addressRecord = textLines.FirstOrDefault();

// 1st line is an Extended Segment Address Records (HEX86)
// format ":02000004FFFFFC"

// perform sanity checks
if (addressRecord == null ||
addressRecord.Length != 15 ||
addressRecord.Substring(0, 9) != ":02000004")
{
// wrong format
throw new FormatException("Wrong data in nanoBooter file");
}

// looking good, grab the upper 16bits
address = (uint)int.Parse(addressRecord.Substring(9, 4), System.Globalization.NumberStyles.HexNumber);
address <<= 16;

// now the 2nd line to get the lower 16 bits of the address
addressRecord = textLines.Skip(1).FirstOrDefault();

// 2nd line is a Data Record
// format ":10246200464C5549442050524F46494C4500464C33"

// perform sanity checks
if (addressRecord == null ||
addressRecord.Substring(0, 1) != ":" ||
addressRecord.Length < 7)
{
// wrong format
throw new FormatException("Wrong data in nanoBooter file");
}

// looking good, grab the lower 16bits
address += (uint)int.Parse(addressRecord.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);

return address;
}
}
}
Loading

0 comments on commit 62cc30c

Please sign in to comment.