Skip to content

Commit d1ecdb3

Browse files
authored
Hard reset added for several ESP32 options missing it (#308)
1 parent ab17999 commit d1ecdb3

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

nanoFirmwareFlasher.Library/Esp32Operations.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public class Esp32Operations
2020
/// <param name="backupPath">Path to store backup image.</param>
2121
/// <param name="fileName">Name of backup file.</param>
2222
/// <param name="verbosity">Set verbosity level of progress and error messages.</param>
23+
/// <param name="hardResetAfterCommand">if true the chip will execute a hard reset via DTR signal</param>
2324
/// <returns>The <see cref="ExitCodes"/> with the operation result.</returns>
2425
public static ExitCodes BackupFlash(
2526
EspTool tool,
2627
Esp32DeviceInfo device,
2728
string backupPath,
2829
string fileName,
29-
VerbosityLevel verbosity)
30+
VerbosityLevel verbosity,
31+
bool hardResetAfterCommand = false)
3032
{
3133
// check for backup file without backup path
3234
if (!string.IsNullOrEmpty(fileName) &&
@@ -77,7 +79,7 @@ public static ExitCodes BackupFlash(
7779
OutputWriter.ForegroundColor = ConsoleColor.White;
7880
}
7981

80-
tool.BackupFlash(backupFilePath, device.FlashSize);
82+
tool.BackupFlash(backupFilePath, device.FlashSize, hardResetAfterCommand);
8183

8284
if (verbosity > VerbosityLevel.Quiet)
8385
{

nanoFirmwareFlasher.Library/EspTool.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
//
2-
// Copyright (c) .NET Foundation and Contributors
3-
// See LICENSE file in the project root for full license information.
4-
//
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
53

64
using System;
75
using System.Collections.Generic;
@@ -146,7 +144,8 @@ public EspTool(
146144
public Esp32DeviceInfo GetDeviceDetails(
147145
string targetName,
148146
bool requireFlashSize = true,
149-
bool forcePsRamCheck = false)
147+
bool forcePsRamCheck = false,
148+
bool hardResetAfterCommand = false)
150149
{
151150
string messages;
152151

@@ -161,7 +160,7 @@ public Esp32DeviceInfo GetDeviceDetails(
161160
"flash_id",
162161
false,
163162
true,
164-
false,
163+
hardResetAfterCommand && !requireFlashSize,
165164
null,
166165
out messages))
167166
{
@@ -182,14 +181,15 @@ public Esp32DeviceInfo GetDeviceDetails(
182181

183182
// check if we got flash size (in case we need it)
184183
if (requireFlashSize
185-
&& messages.Contains("Detected flash size: Unknown"))
184+
&& (messages.Contains("Detected flash size: Unknown") || hardResetAfterCommand))
186185
{
187186
// try again now without the stub
187+
// Also run this for the hardResetAfterCommand, as there is no way to use the tool for a reset only (esptool issue 910).
188188
if (!RunEspTool(
189189
"flash_id",
190190
true,
191191
true,
192-
false,
192+
hardResetAfterCommand,
193193
null,
194194
out messages))
195195
{
@@ -328,13 +328,13 @@ private PSRamAvailability FindPSRamAvailable(
328328
var bootloaderPartition = new Dictionary<int, string>
329329
{
330330
// bootloader goes to 0x1000, except for ESP32_S3, which goes to 0x0
331-
{ _chipType == "esp32s3" ? 0x0 : 0x1000, Path.Combine(Utilities.ExecutingPath, $"{_chipType}bootloader", "bootloader.bin") },
331+
{ _chipType == "esp32s3" ? 0x0 : 0x1000, Path.Combine(Utilities.ExecutingPath, $"{_chipType}bootloader", "bootloader.bin") },
332332

333-
// nanoCLR goes to 0x10000
334-
{ 0x10000, Path.Combine(Utilities.ExecutingPath, $"{_chipType}bootloader", "test_startup.bin") },
333+
// nanoCLR goes to 0x10000
334+
{ 0x10000, Path.Combine(Utilities.ExecutingPath, $"{_chipType}bootloader", "test_startup.bin") },
335335

336336
// partition table goes to 0x8000; there are partition tables for 2MB, 4MB, 8MB and 16MB flash sizes
337-
{ 0x8000, Path.Combine(Utilities.ExecutingPath, $"{_chipType}bootloader", $"partitions_{Esp32DeviceInfo.GetFlashSizeAsString(flashSize).ToLowerInvariant()}.bin") }
337+
{ 0x8000, Path.Combine(Utilities.ExecutingPath, $"{_chipType}bootloader", $"partitions_{Esp32DeviceInfo.GetFlashSizeAsString(flashSize).ToLowerInvariant()}.bin") }
338338
};
339339

340340
// need to use standard baud rate here because of boards put in download mode
@@ -439,16 +439,18 @@ private PSRamAvailability FindPSRamAvailable(
439439
/// </summary>
440440
/// <param name="backupFilename">Backup file including full path</param>
441441
/// <param name="flashSize">Flash size in bytes</param>
442+
/// <param name="hardResetAfterCommand">if true the chip will execute a hard reset via DTR signal</param>
442443
/// <returns>true if successful</returns>
443444
internal void BackupFlash(string backupFilename,
444-
int flashSize)
445+
int flashSize,
446+
bool hardResetAfterCommand = false)
445447
{
446448
// execute read_flash command and parse the result; progress message can be found be searching for backspaces (ASCII code 8)
447449
if (!RunEspTool(
448450
$"read_flash 0 0x{flashSize:X} \"{backupFilename}\"",
449451
true,
450452
false,
451-
false,
453+
hardResetAfterCommand,
452454
(char)8,
453455
out string messages))
454456
{

nanoFirmwareFlasher.Tool/Esp32Manager.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ private async Task<ExitCodes> DoProcessAsync()
7878
return ExitCodes.E4005;
7979
}
8080

81+
bool backupFlash = !string.IsNullOrEmpty(_options.BackupPath) ||
82+
!string.IsNullOrEmpty(_options.BackupFile);
83+
8184
Esp32DeviceInfo esp32Device;
8285

8386
if (espTool.ComPortAvailable)
@@ -86,7 +89,9 @@ private async Task<ExitCodes> DoProcessAsync()
8689
_options.TargetName,
8790
// if partition table size is specified, no need to get flash size
8891
_options.Esp32PartitionTableSize == null,
89-
_options.CheckPsRam);
92+
_options.CheckPsRam,
93+
(_options.DeviceDetails || _options.IdentifyFirmware) && !backupFlash
94+
);
9095
}
9196
else
9297
{
@@ -131,11 +136,16 @@ private async Task<ExitCodes> DoProcessAsync()
131136
// backup requested
132137
// Should backup be an unique operation => exit whatever success or not ?
133138
// In this case, should find how manage a NoOperationPerformedException info
134-
if (!string.IsNullOrEmpty(_options.BackupPath) ||
135-
!string.IsNullOrEmpty(_options.BackupFile))
139+
if (backupFlash)
136140
{
137141
// backup path specified, backup deployment
138-
var exitCode = Esp32Operations.BackupFlash(espTool, esp32Device, _options.BackupPath, _options.BackupFile, _verbosityLevel);
142+
var exitCode = Esp32Operations.BackupFlash(
143+
espTool,
144+
esp32Device,
145+
_options.BackupPath,
146+
_options.BackupFile,
147+
_verbosityLevel,
148+
_options.DeviceDetails || _options.IdentifyFirmware);
139149

140150
if (exitCode != ExitCodes.OK)
141151
{

0 commit comments

Comments
 (0)