Skip to content

Commit a7ccb9d

Browse files
authored
Fixes for ESP32-S2 targets (#91)
1 parent 97c30fc commit a7ccb9d

File tree

13 files changed

+147
-42
lines changed

13 files changed

+147
-42
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ When using nanoff you can add `--target MY_TARGET_NAME_HERE` to use a specific i
9797
```console
9898
No target name was provided! Using 'ESP32_REV0' based on the device characteristics.
9999
```
100-
100+
101+
>Note: Please note that for ESP32-S2 targets is not possible to safely determine what's the best image to use. For this reason it's mandatory providing the appropriate target name with `--target MY_TARGET_NAME_HERE`.
102+
101103
Some ESP32 boards have issues entering bootloader mode. This can be usually overcome by holding down the BOOT/FLASH button in the board.
102104
In case nanoff detects this situation the following warning is shown:
103105

@@ -115,13 +117,13 @@ To update the firmware of an ESP32 target connected to COM31, to the latest avai
115117
nanoff --update --target ESP32_PSRAM_REV0 --serialport COM31
116118
```
117119

118-
### Update the firmware of an ESP_WROVER_KIT with a local CLR file
120+
### Update the firmware of an ESP32-S2 KALUGA 1 with a local CLR file
119121

120-
To update the firmware of an ESP_WROVER_KIT target connected to COM31 with a local CLR file (for example from a build).
122+
To update the firmware of an ESP32-S2 KALUGA 1 target connected to COM31 with a local CLR file (for example from a build).
121123
This file has to be a binary file with a valid CLR from a build. No other checks or validations are performed on the file content.
122124

123125
```console
124-
nanoff --update --target ESP_WROVER_KIT --serialport COM31 --clrfile "C:\nf-interpreter\build\nanoCLR.bin"
126+
nanoff --update --target KALUGA_1 --serialport COM31 --clrfile "C:\nf-interpreter\build\nanoCLR.bin"
125127
```
126128

127129
You can adjust the name of the core image you want to use. Refer to the previous section to get the full list.
19.5 KB
Binary file not shown.
3 KB
Binary file not shown.
3 KB
Binary file not shown.
3 KB
Binary file not shown.
149 KB
Binary file not shown.

nanoFirmwareFlasher/Esp32Operations.cs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ internal static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync
9898
bool updateCLRfile = !string.IsNullOrEmpty(clrFile);
9999

100100
// perform sanity checks for the specified target against the connected device details
101-
if (esp32Device.ChipType != "ESP32")
101+
if (esp32Device.ChipType != "ESP32" &&
102+
esp32Device.ChipType != "ESP32-S2")
102103
{
103104
// connected to a device not supported
104105
Console.ForegroundColor = ConsoleColor.Yellow;
@@ -113,27 +114,45 @@ internal static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync
113114
// if a target name wasn't specified try to guess from the device characteristics
114115
if (string.IsNullOrEmpty(targetName))
115116
{
116-
if (esp32Device.ChipName.Contains("PICO"))
117+
if (esp32Device.ChipType == "ESP32")
117118
{
118-
targetName = "ESP32_PICO";
119-
}
120-
else
121-
{
122-
var revisionSuffix = "REV0";
123-
var psRamSegment = "";
124-
125-
if (esp32Device.ChipName.Contains("revision 3"))
119+
if (esp32Device.ChipName.Contains("PICO"))
126120
{
127-
revisionSuffix = "REV3";
121+
targetName = "ESP32_PICO";
128122
}
129-
130-
if(esp32Device.PSRamAvailable == PSRamAvailability.Yes)
123+
else
131124
{
132-
psRamSegment = "_PSRAM";
125+
var revisionSuffix = "REV0";
126+
var psRamSegment = "";
127+
128+
if (esp32Device.ChipName.Contains("revision 3"))
129+
{
130+
revisionSuffix = "REV3";
131+
}
132+
133+
if (esp32Device.PSRamAvailable == PSRamAvailability.Yes)
134+
{
135+
psRamSegment = "_PSRAM";
136+
}
137+
138+
// compose target name
139+
targetName = $"ESP32{psRamSegment}_{revisionSuffix}";
133140
}
141+
}
142+
else if (esp32Device.ChipType == "ESP32-S2")
143+
{
144+
// can't guess with certainty for this series, better have the user provide
145+
// a target name
146+
147+
Console.ForegroundColor = ConsoleColor.Red;
148+
149+
Console.WriteLine("");
150+
Console.WriteLine($"No target name was provided! Please provide an appropriate one adding this option '--target MY_ESP32_S2_TARGET'.");
151+
Console.WriteLine("");
134152

135-
// compose target name
136-
targetName = $"ESP32{psRamSegment}_{revisionSuffix}";
153+
Console.ForegroundColor = ConsoleColor.White;
154+
155+
return ExitCodes.E9000;
137156
}
138157

139158
Console.ForegroundColor = ConsoleColor.Blue;
@@ -310,6 +329,21 @@ internal static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync
310329
{
311330
Console.ForegroundColor = ConsoleColor.Green;
312331
Console.WriteLine("OK");
332+
333+
// warn user if reboot is not possible
334+
if (espTool.CouldntResetTarget)
335+
{
336+
Console.ForegroundColor = ConsoleColor.Yellow;
337+
338+
Console.WriteLine("");
339+
Console.WriteLine("**********************************************");
340+
Console.WriteLine("The connected device is in 'download mode'.");
341+
Console.WriteLine("Please reset the chip manually to run nanoCLR.");
342+
Console.WriteLine("**********************************************");
343+
Console.WriteLine("");
344+
345+
Console.ForegroundColor = ConsoleColor.White;
346+
}
313347
}
314348
else
315349
{

nanoFirmwareFlasher/EspTool.cs

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ namespace nanoFramework.Tools.FirmwareFlasher
2121
/// </summary>
2222
internal partial class EspTool
2323
{
24+
private string _esptoolMessage;
25+
2426
/// <summary>
2527
/// The serial port over which all the communication goes
2628
/// </summary>
2729
private readonly string _serialPort = null;
2830

2931
/// <summary>
30-
/// The baud rate for the serial port. The default comming from <see cref="Options.BaudRate"/> is 9216000.
32+
/// The baud rate for the serial port. The default comming from <see cref="Options.BaudRate"/>.
3133
/// </summary>
3234
private int _baudRate = 0;
3335

@@ -77,6 +79,11 @@ internal partial class EspTool
7779
/// </summary>
7880
public VerbosityLevel Verbosity { get; internal set; } = VerbosityLevel.Normal;
7981

82+
/// <summary>
83+
/// Flag to report if the target couldn't be reset after flashing it.
84+
/// </summary>
85+
public bool CouldntResetTarget;
86+
8087
// ESP32 chip type to connect to.
8188
// Default is 'auto'. It's replaced with the actual chip type after detection to improve operations.
8289
internal string _chipType = "auto";
@@ -139,7 +146,9 @@ internal EspTool(
139146
/// Tries reading ESP32 device details.
140147
/// </summary>
141148
/// <returns>The filled info structure with all the information about the connected ESP32 device or null if an error occured</returns>
142-
internal Esp32DeviceInfo GetDeviceDetails(bool requireFlashSize = true)
149+
internal Esp32DeviceInfo GetDeviceDetails(
150+
string targetName,
151+
bool requireFlashSize = true)
143152
{
144153
string messages;
145154

@@ -152,7 +161,7 @@ internal Esp32DeviceInfo GetDeviceDetails(bool requireFlashSize = true)
152161
// execute flash_id command and parse the result
153162
if (!RunEspTool(
154163
"flash_id",
155-
true,
164+
false,
156165
true,
157166
false,
158167
null,
@@ -168,7 +177,7 @@ internal Esp32DeviceInfo GetDeviceDetails(bool requireFlashSize = true)
168177
// try again now without the stub
169178
if (!RunEspTool(
170179
"flash_id",
171-
false,
180+
true,
172181
true,
173182
false,
174183
null,
@@ -217,16 +226,26 @@ internal Esp32DeviceInfo GetDeviceDetails(bool requireFlashSize = true)
217226
_chipType = chipType.ToLower().Replace("-", "");
218227

219228
// try to find out if PSRAM is present
220-
PSRamAvailability psramIsAvailable;
229+
PSRamAvailability psramIsAvailable = PSRamAvailability.Unknown;
221230

222231
if (name.Contains("PICO"))
223232
{
224233
// PICO's don't have PSRAM, so don't even bother
225234
psramIsAvailable = PSRamAvailability.No;
226235
}
236+
else if (name.Contains("ESP32-S2")
237+
&& targetName == "FEATHER_S2")
238+
{
239+
// FEATHER_S2's have PSRAM, so don't even bother
240+
psramIsAvailable = PSRamAvailability.Yes;
241+
}
227242
else
228243
{
229-
psramIsAvailable = FindPSRamAvailable();
244+
// if a target name was provided, don't bother to check PSRAM
245+
if (targetName == null)
246+
{
247+
psramIsAvailable = FindPSRamAvailable();
248+
}
230249
}
231250

232251
if (Verbosity >= VerbosityLevel.Normal)
@@ -274,12 +293,22 @@ private PSRamAvailability FindPSRamAvailable()
274293
{ 0x8000, Path.Combine(Program.ExecutingPath, $"{_chipType}bootloader", $"partitions_{Esp32DeviceInfo.GetFlashSizeAsString(_flashSize).ToLowerInvariant()}.bin") }
275294
};
276295

277-
if (WriteFlash(bootloaderPartition) == ExitCodes.OK)
296+
// need to use standard baud rate here because of boards put in download mode
297+
if (WriteFlash(bootloaderPartition, true) == ExitCodes.OK)
278298
{
299+
// check if the
300+
if(_esptoolMessage.Contains("esptool.py can not exit the download mode over USB"))
301+
{
302+
// this board was put on download mode manually, can't run the test app...
303+
304+
return PSRamAvailability.Unknown;
305+
}
306+
279307
try
280308
{
281309
// open COM port and grab output
282-
SerialPort espDevice = new SerialPort(_serialPort, _baudRate);
310+
// force baud rate to 115200 (standard baud rate for boootloader)
311+
SerialPort espDevice = new SerialPort(_serialPort, 115200);
283312
espDevice.Open();
284313

285314
if (espDevice.IsOpen)
@@ -327,7 +356,7 @@ internal void BackupFlash(string backupFilename,
327356
// execute read_flash command and parse the result; progress message can be found be searching for backspaces (ASCII code 8)
328357
if (!RunEspTool(
329358
$"read_flash 0 0x{flashSize:X} \"{backupFilename}\"",
330-
false,
359+
true,
331360
false,
332361
false,
333362
(char)8,
@@ -358,7 +387,7 @@ internal ExitCodes EraseFlash()
358387
if (!RunEspTool(
359388
"erase_flash",
360389
false,
361-
false,
390+
true,
362391
false,
363392
null,
364393
out string messages))
@@ -415,7 +444,9 @@ internal ExitCodes EraseFlashSegment(uint startAddress, uint length)
415444
/// </summary>
416445
/// <param name="partsToWrite">dictionary which keys are the start addresses and the values are the complete filenames (the bin files)</param>
417446
/// <returns>true if successful</returns>
418-
internal ExitCodes WriteFlash(Dictionary<int, string> partsToWrite)
447+
internal ExitCodes WriteFlash(
448+
Dictionary<int, string> partsToWrite,
449+
bool useStandardBaudrate = false)
419450
{
420451
// put the parts to flash together and prepare the regex for parsing the output
421452
var partsArguments = new StringBuilder();
@@ -445,7 +476,7 @@ internal ExitCodes WriteFlash(Dictionary<int, string> partsToWrite)
445476
if (!RunEspTool(
446477
$"write_flash --flash_mode {_flashMode} --flash_freq {_flashFrequency}m --flash_size {flashSize} {partsArguments.ToString().Trim()}",
447478
false,
448-
false,
479+
useStandardBaudrate,
449480
true,
450481
'\r',
451482
out string messages))
@@ -467,6 +498,9 @@ internal ExitCodes WriteFlash(Dictionary<int, string> partsToWrite)
467498
}
468499
}
469500

501+
// check if there is any mention of not being able to run the app
502+
CouldntResetTarget = messages.Contains("To run the app, reset the chip manually");
503+
470504
return ExitCodes.OK;
471505
}
472506

@@ -475,6 +509,7 @@ internal ExitCodes WriteFlash(Dictionary<int, string> partsToWrite)
475509
/// </summary>
476510
/// <param name="commandWithArguments">the esptool command (e.g. write_flash) incl. all arguments (if needed)</param>
477511
/// <param name="noStub">if true --no-stub will be added; the chip_id, read_mac and flash_id commands can be quicker executes without uploading the stub program to the chip</param>
512+
/// <param name="useStandardBaudRate">If <see langword="true"/> the tool will use the standard baud rate to connect to the chip.</param>
478513
/// <param name="hardResetAfterCommand">if true the chip will execute a hard reset via DTR signal</param>
479514
/// <param name="progressTestChar">If not null: After each of this char a progress message will be printed out</param>
480515
/// <param name="messages">StandardOutput and StandardError messages that the esptool prints out</param>
@@ -487,6 +522,9 @@ private bool RunEspTool(
487522
char? progressTestChar,
488523
out string messages)
489524
{
525+
// reset message
526+
_esptoolMessage = string.Empty;
527+
490528
// create the process start info
491529
// if we can directly talk to the ROM bootloader without a stub program use the --no-stub option
492530
// --nostub requires to not change the baudrate (ROM doesn't support changing baud rate. Keeping initial baud rate 115200)
@@ -510,8 +548,9 @@ private bool RunEspTool(
510548
}
511549

512550
// prepare the process start of the esptool
513-
string appName = string.Empty;
514-
string appDir = string.Empty;
551+
string appName;
552+
string appDir;
553+
515554
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
516555
{
517556
appName = "esptool.exe";
@@ -631,11 +670,31 @@ private bool RunEspTool(
631670

632671
messages = messageBuilder.ToString();
633672

673+
// save output messages
674+
_esptoolMessage = messages;
675+
634676
// if the stub program was used then we don't need to transfer ist again
635677
_isStubActive = !noStub;
636678

637-
// true if exit code was 0 (success)
638-
return espTool.ExitCode == 0;
679+
if(espTool.ExitCode == 0)
680+
{
681+
// exit code was 0 (success), all good
682+
return true;
683+
}
684+
else
685+
{
686+
// need to look for specific error messages to do a safe guess if execution is as expected
687+
if(messages.Contains("esptool.py can not exit the download mode over USB") ||
688+
messages.Contains("Staying in bootloader.") )
689+
{
690+
// we are probably good with this as we can't do much about it...
691+
return true;
692+
}
693+
else
694+
{
695+
return false;
696+
}
697+
}
639698
}
640699

641700
private void ProcessConnectPattern(StringBuilder messageBuilder)

nanoFirmwareFlasher/FirmwarePackage.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,11 @@ protected async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync()
428428
if (Verbosity >= VerbosityLevel.Normal)
429429
{
430430
Console.ForegroundColor = ConsoleColor.Yellow;
431+
432+
Console.WriteLine("");
431433
Console.WriteLine($"Updating to {Version}");
434+
Console.WriteLine("");
435+
432436
Console.ForegroundColor = ConsoleColor.White;
433437
}
434438

nanoFirmwareFlasher/Options.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class Options
101101
[Option(
102102
"baud",
103103
Required = false,
104-
Default = 921600,
104+
Default = 1500000,
105105
HelpText = "Baud rate to use for the serial port.")]
106106
public int BaudRate { get; set; }
107107

0 commit comments

Comments
 (0)