Skip to content

Commit 3dd4f75

Browse files
authored
Add auto-detection for ESP32 targets (#87)
1 parent d53e46c commit 3dd4f75

14 files changed

+214
-38
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ nanoff --help
8484

8585
## ESP32 usage examples
8686

87-
>Note: some ESP32 boards have issues entering bootloader mode. This can be usually overcome by holding down the BOOT/FLASH button in the board.
87+
There are multiple ESP32 images available, some are build specifically for a target. Please check out the [list](https://github.com/nanoframework/nf-interpreter#firmware-for-reference-boards).
88+
89+
The ESP32_PSRAM_REV0 image will just work for any variant of the ESP32 series, with or without PSRAM, and for all silicon revisions.
90+
You can read more about the differences between the various images [here](https://docs.nanoframework.net/content/reference-targets/esp32.html).
91+
92+
When using nanoff you can add `--target MY_TARGET_NAME_HERE` to use a specific image. If, instead, you just specify the platform with `--platform esp32` nanoff will choose the most appropriate image depending on the features of the device that's connected. Output similar to this one will show to advise what's the image about to be used:
93+
94+
```console
95+
No target name was provided! Using 'ESP32_REV0' based on the device characteristics.
96+
```
97+
98+
Some ESP32 boards have issues entering bootloader mode. This can be usually overcome by holding down the BOOT/FLASH button in the board.
8899
In case nanoff detects this situation the following warning is shown:
89100

90101
```console
@@ -99,9 +110,6 @@ To update the firmware of an ESP32 target connected to COM31, to the latest avai
99110
nanoff --update --target ESP32_PSRAM_REV0 --serialport COM31
100111
```
101112

102-
103-
> Note: there are multiple ESP32 images. The ESP32_PSRAM_REV0 image will just work for any variant of the ESP32 series, with or without PSRAM. Dedicated images are also available, check out the [list](https://github.com/nanoframework/nf-interpreter#firmware-for-reference-boards) for details.
104-
105113
### Update the firmware of an ESP_WROVER_KIT with a local CLR file
106114

107115
To update the firmware of an ESP_WROVER_KIT target connected to COM31 with a local CLR file (for example from a build).

lib/esp32bootloader/bootloader.bin

25 KB
Binary file not shown.
3 KB
Binary file not shown.
3 KB
Binary file not shown.
3 KB
Binary file not shown.
3 KB
Binary file not shown.
206 KB
Binary file not shown.

nanoFirmwareFlasher/Esp32DeviceInfo.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ internal class Esp32DeviceInfo
5959
/// </summary>
6060
internal int FlashSize { get; }
6161

62+
/// <summary>
63+
/// Availability of PSRAM on the device.
64+
/// </summary>
65+
internal PSRamAvailability PSRamAvailable { get; }
66+
6267
/// <summary>
6368
/// Constructor
6469
/// </summary>
@@ -77,7 +82,8 @@ internal Esp32DeviceInfo(
7782
string macAddress,
7883
byte flashManufacturerId,
7984
short flashDeviceModelId,
80-
int flashSize)
85+
int flashSize,
86+
PSRamAvailability psramAvailability)
8187
{
8288
ChipType = chipType;
8389
ChipName = chipName;
@@ -87,11 +93,17 @@ internal Esp32DeviceInfo(
8793
FlashManufacturerId = flashManufacturerId;
8894
FlashDeviceId = flashDeviceModelId;
8995
FlashSize = flashSize;
96+
PSRamAvailable = psramAvailability;
9097
}
9198

9299
internal string GetFlashSizeAsString()
93100
{
94-
return FlashSize >= 0x10000 ? $"{ FlashSize / 0x100000 }MB" : $"{ FlashSize / 0x400 }kB";
101+
return GetFlashSizeAsString(FlashSize);
102+
}
103+
104+
public static string GetFlashSizeAsString(int flashSize)
105+
{
106+
return flashSize >= 0x10000 ? $"{ flashSize / 0x100000 }MB" : $"{ flashSize / 0x400 }kB";
95107
}
96108

97109
/// <inheritdoc/>
@@ -113,6 +125,22 @@ public override string ToString()
113125

114126
deviceInfo.AppendLine($"Features { Features }");
115127
deviceInfo.AppendLine($"Flash size { GetFlashSizeAsString() } { GetFlashDeviceId() } from { GetFlashManufacturer() } (manufacturer 0x{ FlashManufacturerId } device 0x{ FlashDeviceId })");
128+
129+
switch(PSRamAvailable)
130+
{
131+
case PSRamAvailability.Unknown:
132+
deviceInfo.AppendLine($"PSRAM: unknown");
133+
break;
134+
135+
case PSRamAvailability.Yes:
136+
deviceInfo.AppendLine($"PSRAM: available");
137+
break;
138+
139+
case PSRamAvailability.No:
140+
deviceInfo.AppendLine($"PSRAM: not available");
141+
break;
142+
}
143+
116144
deviceInfo.AppendLine($"Crystal { Crystal }");
117145
deviceInfo.AppendLine($"MAC { MacAddress }");
118146

nanoFirmwareFlasher/Esp32Firmware.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ internal async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync(in
5757
// if specified, partition table size overrides flash size.
5858
flashSize = (int)_partitionTableSize * 0x100000;
5959
}
60-
var humanReadable = flashSize >= 0x10000 ? $"{ flashSize / 0x100000 }MB" : $"{ flashSize / 0x400 }kB";
6160

6261
// check if the option to override the partition table was set
6362

6463
if (!SupportedFlashSizes.Contains(flashSize))
6564
{
6665
if (Verbosity >= VerbosityLevel.Detailed)
6766
{
68-
Console.WriteLine($"There is no firmware available for ESP32 with {humanReadable} flash size!{Environment.NewLine}Only the following flash sizes are supported: {string.Join(", ", SupportedFlashSizes.Select(size => size >= 0x10000 ? $"{ size / 0x100000 }MB" : $"{ size / 0x400 }kB."))}");
67+
Console.WriteLine($"There is no firmware available for ESP32 with {Esp32DeviceInfo.GetFlashSizeAsString(flashSize)} flash size!{Environment.NewLine}Only the following flash sizes are supported: {string.Join(", ", SupportedFlashSizes.Select(size => size >= 0x10000 ? $"{ size / 0x100000 }MB" : $"{ size / 0x400 }kB."))}");
6968
}
7069

7170
return ExitCodes.E4001;
@@ -88,7 +87,7 @@ internal async System.Threading.Tasks.Task<ExitCodes> DownloadAndExtractAsync(in
8887
{ CLRAddress, Path.Combine(LocationPath, "nanoCLR.bin") },
8988

9089
// partition table goes to 0x8000; there are partition tables for 2MB, 4MB, 8MB and 16MB flash sizes
91-
{ 0x8000, Path.Combine(LocationPath, $"partitions_{humanReadable.ToLowerInvariant()}.bin") }
90+
{ 0x8000, Path.Combine(LocationPath, $"partitions_{Esp32DeviceInfo.GetFlashSizeAsString(flashSize).ToLowerInvariant()}.bin") }
9291
};
9392
}
9493

nanoFirmwareFlasher/Esp32Operations.cs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ namespace nanoFramework.Tools.FirmwareFlasher
1212
{
1313
internal class Esp32Operations
1414
{
15-
// This is the only official ESP32 target available, so it's OK to use this as the target
16-
// name whenever ESP32 is the specified platform
17-
private const string _esp32TargetName = "ESP32_WROOM_32";
18-
1915
public static ExitCodes BackupFlash(
2016
EspTool tool,
2117
Esp32DeviceInfo device,
@@ -101,27 +97,56 @@ internal static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync
10197
uint address = 0;
10298
bool updateCLRfile = !string.IsNullOrEmpty(clrFile);
10399

104-
// if a target name wasn't specified use the default (and only available) ESP32 target
105-
if (string.IsNullOrEmpty(targetName))
100+
// perform sanity checks for the specified target against the connected device details
101+
if (esp32Device.ChipType != "ESP32")
106102
{
107-
targetName = _esp32TargetName;
103+
// connected to a device not supported
104+
Console.ForegroundColor = ConsoleColor.Yellow;
105+
Console.WriteLine("");
106+
Console.WriteLine("******************************* WARNING *******************************");
107+
Console.WriteLine("Seems that the connected device is not supported by .NET nanoFramework");
108+
Console.WriteLine("Most likely it won't boot");
109+
Console.WriteLine("************************************************************************");
110+
Console.WriteLine("");
108111
}
109112

110-
if (fitCheck)
113+
// if a target name wasn't specified try to guess from the device characteristics
114+
if (string.IsNullOrEmpty(targetName))
111115
{
112-
// perform sanity checks for the specified target agains the connected device details
113-
if (esp32Device.ChipType != "ESP32")
116+
if (esp32Device.ChipName.Contains("PICO"))
114117
{
115-
// connected to a device not supported
116-
Console.ForegroundColor = ConsoleColor.Yellow;
117-
Console.WriteLine("");
118-
Console.WriteLine("******************************* WARNING *******************************");
119-
Console.WriteLine("Seems that the connected device is not supported by .NET nanoFramework");
120-
Console.WriteLine("Most likely it won't boot");
121-
Console.WriteLine("************************************************************************");
122-
Console.WriteLine("");
118+
targetName = "ESP32_PICO";
119+
}
120+
else
121+
{
122+
var revisionSuffix = "REV0";
123+
var psRamSegment = "";
124+
125+
if (esp32Device.ChipName.Contains("revision 3"))
126+
{
127+
revisionSuffix = "REV3";
128+
}
129+
130+
if(esp32Device.PSRamAvailable == PSRamAvailability.Yes)
131+
{
132+
psRamSegment = "_PSRAM";
133+
}
134+
135+
// compose target name
136+
targetName = $"ESP32{psRamSegment}_{revisionSuffix}";
123137
}
124138

139+
Console.ForegroundColor = ConsoleColor.Blue;
140+
141+
Console.WriteLine("");
142+
Console.WriteLine($"No target name was provided! Using '{targetName}' based on the device characteristics.");
143+
Console.WriteLine("");
144+
145+
Console.ForegroundColor = ConsoleColor.White;
146+
}
147+
148+
if (fitCheck)
149+
{
125150
if (targetName.Contains("ESP32_WROOM_32_V3") &&
126151
(esp32Device.ChipName.Contains("revision 0") ||
127152
esp32Device.ChipName.Contains("revision 1") ||

0 commit comments

Comments
 (0)