Skip to content

Commit 62cc30c

Browse files
authored
Replace DFU implementation with ST Cube Programmer (#79)
1 parent 34325da commit 62cc30c

File tree

13 files changed

+729
-616
lines changed

13 files changed

+729
-616
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ It makes use of several 3rd party tools:
1414

1515
- Espressif esptool
1616
You can find the esptool and licensing information on the repository [here](http://github.com/espressif/esptool).
17-
- QMKDfuSe
18-
Tool based on STM DfusSe tool. You can find the source, licensing information and documentation [here](https://github.com/qmk/qmk_dfuse).
1917
- STM32 Cube Programmer
2018
You can find the source, licensing information and documentation [here](https://www.st.com/en/development-tools/stm32cubeprog.html).
2119
- Texas Instruments Uniflash

lib/stdfu/qmk-dfuse.exe

-3.33 MB
Binary file not shown.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Runtime.Serialization;
8+
9+
namespace nanoFramework.Tools.FirmwareFlasher
10+
{
11+
/// <summary>
12+
/// Couldn't open the specified DFU device.
13+
/// </summary>
14+
[Serializable]
15+
internal class CantConnectToDfuDeviceException : Exception
16+
{
17+
public CantConnectToDfuDeviceException()
18+
{
19+
}
20+
21+
public CantConnectToDfuDeviceException(string message) : base(message)
22+
{
23+
}
24+
25+
public CantConnectToDfuDeviceException(string message, Exception innerException) : base(message, innerException)
26+
{
27+
}
28+
29+
protected CantConnectToDfuDeviceException(SerializationInfo info, StreamingContext context) : base(info, context)
30+
{
31+
}
32+
}
33+
}

nanoFirmwareFlasher/Exceptions/CantOpenDfuDeviceException.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

nanoFirmwareFlasher/ExitCodes.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public enum ExitCodes
3434
/// <summary>
3535
/// Error flashing DFU dvice
3636
/// </summary>
37-
[Display(Name = "Error flashing DFU dvice.")]
37+
[Display(Name = "Error flashing DFU device.")]
3838
E1003 = 1003,
3939

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

46+
/// <summary>
47+
/// Can't connect to specified DFU device.
48+
/// </summary>
49+
[Display(Name = "Can't connect to specified DFU device. Make sure it's connected and that the ID is correct.")]
50+
E1005 = 1005,
51+
52+
/// <summary>
53+
/// Failed to start execution on the connected device.
54+
/// </summary>
55+
[Display(Name = "Failed to start execition on the connected device.")]
56+
E1006 = 1006,
57+
4658
////////////////////////
4759
// ESP32 tools Errors //
4860
////////////////////////

nanoFirmwareFlasher/Options.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ public class Options
1313
{
1414
#region STM32 DFU options
1515

16-
[Option(
17-
"dfufile",
18-
Required = false,
19-
Default = null,
20-
HelpText = "DFU file to be flashed into the device.")]
21-
public string DfuFile { get; set; }
22-
2316
[Option(
2417
"listdfu",
2518
Required = false,

nanoFirmwareFlasher/Program.cs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
231231
// DFU related
232232
else if (
233233
o.ListDevicesInDfuMode ||
234-
!string.IsNullOrEmpty(o.DfuDeviceId) ||
235-
!string.IsNullOrEmpty(o.DfuFile))
234+
!string.IsNullOrEmpty(o.DfuDeviceId))
236235
{
237236
o.Platform = "stm32";
238237
}
@@ -520,11 +519,11 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
520519

521520
if (o.ListDevicesInDfuMode)
522521
{
523-
var connecteDevices = StmDfuDevice.ListDfuDevices();
522+
var connecteDevices = StmDfuDevice.ListDevices();
524523

525524
Console.ForegroundColor = ConsoleColor.Cyan;
526525

527-
if (connecteDevices.Count == 0)
526+
if (connecteDevices.Count() == 0)
528527
{
529528
Console.ForegroundColor = ConsoleColor.Yellow;
530529
Console.WriteLine("No DFU devices found");
@@ -533,9 +532,9 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
533532
{
534533
Console.WriteLine("-- Connected DFU devices --");
535534

536-
foreach (string deviceId in connecteDevices)
535+
foreach ((string serial, string device) device in connecteDevices)
537536
{
538-
Console.WriteLine(deviceId);
537+
Console.WriteLine($"{device.serial} @ {device.device}");
539538
}
540539

541540
Console.WriteLine("---------------------------");
@@ -588,55 +587,65 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
588587
return;
589588
}
590589

591-
var connectedStDfuDevices = StmDfuDevice.ListDfuDevices();
590+
var connectedStDfuDevices = StmDfuDevice.ListDevices();
592591
var connectedStJtagDevices = StmJtagDevice.ListDevices();
593592

594-
if (!string.IsNullOrEmpty(o.DfuFile) && connectedStDfuDevices.Count != 0)
593+
if (o.BinFile.Any() &&
594+
o.HexFile.Any() &&
595+
connectedStDfuDevices.Count != 0)
595596
{
596-
// there is a DFU file argument, so follow DFU path
597-
var dfuDevice = new StmDfuDevice(o.DfuDeviceId);
598597

599-
if (!dfuDevice.DevicePresent)
598+
#region STM32 DFU options
599+
600+
try
600601
{
601-
// no DFU device found
602+
var dfuDevice = new StmDfuDevice(o.DfuDeviceId);
602603

603-
// done here, this command has no further processing
604-
_exitCode = ExitCodes.E1000;
604+
if (!dfuDevice.DevicePresent)
605+
{
606+
// no JTAG device found
605607

606-
return;
607-
}
608+
// done here, this command has no further processing
609+
_exitCode = ExitCodes.E5001;
608610

609-
if (_verbosityLevel >= VerbosityLevel.Normal)
610-
{
611-
Console.WriteLine($"Connected to DFU device with ID { dfuDevice.DeviceId }");
612-
}
611+
return;
612+
}
613613

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

617-
// get mass erase option
618-
dfuDevice.DoMassErase = o.MassErase;
619+
// set verbosity
620+
dfuDevice.Verbosity = _verbosityLevel;
619621

620-
try
621-
{
622-
dfuDevice.FlashDfuFile(o.DfuFile);
622+
// get mass erase option
623+
dfuDevice.DoMassErase = o.MassErase;
623624

624-
// done here, this command has no further processing
625-
_exitCode = ExitCodes.OK;
625+
if (o.HexFile.Any())
626+
{
627+
_exitCode = dfuDevice.FlashHexFiles(o.HexFile);
626628

627-
return;
628-
}
629-
catch (DfuFileDoesNotExistException)
630-
{
631-
// DFU file doesn't exist
632-
_exitCode = ExitCodes.E1002;
629+
// done here
630+
return;
631+
}
632+
633+
if (o.BinFile.Any())
634+
{
635+
_exitCode = dfuDevice.FlashBinFiles(o.BinFile, o.FlashAddress);
636+
637+
// done here
638+
return;
639+
}
633640
}
634-
catch (Exception ex)
641+
catch (CantConnectToDfuDeviceException)
635642
{
636-
// exception with DFU operation
637-
_exitCode = ExitCodes.E1003;
638-
_extraMessage = ex.Message;
643+
// done here, this command has no further processing
644+
_exitCode = ExitCodes.E1005;
639645
}
646+
647+
#endregion
648+
640649
}
641650
else if (
642651
o.BinFile.Any() &&
@@ -702,7 +711,7 @@ static async Task RunOptionsAndReturnExitCodeAsync(Options o)
702711
// update operation requested?
703712
if (o.Update)
704713
{
705-
// this to update the device with fw from Cloudsmith
714+
// this to update the device with fw from CloudSmith
706715

707716
// need to take care of flash address
708717
string appFlashAddress = null;

nanoFirmwareFlasher/Stm32Firmware.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace nanoFramework.Tools.FirmwareFlasher
1515
/// </summary>
1616
internal class Stm32Firmware : FirmwarePackage
1717
{
18+
[Obsolete("This property is discontinued and it will be removed in a future version.")]
1819
public bool HasDfuPackage => !string.IsNullOrEmpty(DfuPackage);
1920

2021
public string NanoBooterFile { get; private set; }
@@ -52,5 +53,54 @@ public Stm32Firmware(
5253

5354
return executionResult;
5455
}
56+
57+
public uint GetdBooterStartAddress()
58+
{
59+
uint address;
60+
61+
// find out what's the CLR block start
62+
63+
// do this by reading the HEX format file...
64+
var textLines = File.ReadAllLines(NanoBooterFile);
65+
66+
// ... and decoding the start address
67+
var addressRecord = textLines.FirstOrDefault();
68+
69+
// 1st line is an Extended Segment Address Records (HEX86)
70+
// format ":02000004FFFFFC"
71+
72+
// perform sanity checks
73+
if (addressRecord == null ||
74+
addressRecord.Length != 15 ||
75+
addressRecord.Substring(0, 9) != ":02000004")
76+
{
77+
// wrong format
78+
throw new FormatException("Wrong data in nanoBooter file");
79+
}
80+
81+
// looking good, grab the upper 16bits
82+
address = (uint)int.Parse(addressRecord.Substring(9, 4), System.Globalization.NumberStyles.HexNumber);
83+
address <<= 16;
84+
85+
// now the 2nd line to get the lower 16 bits of the address
86+
addressRecord = textLines.Skip(1).FirstOrDefault();
87+
88+
// 2nd line is a Data Record
89+
// format ":10246200464C5549442050524F46494C4500464C33"
90+
91+
// perform sanity checks
92+
if (addressRecord == null ||
93+
addressRecord.Substring(0, 1) != ":" ||
94+
addressRecord.Length < 7)
95+
{
96+
// wrong format
97+
throw new FormatException("Wrong data in nanoBooter file");
98+
}
99+
100+
// looking good, grab the lower 16bits
101+
address += (uint)int.Parse(addressRecord.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);
102+
103+
return address;
104+
}
55105
}
56106
}

0 commit comments

Comments
 (0)