Skip to content

Commit 27df656

Browse files
authored
Improve deployment operation for ESP32 (#256)
1 parent da96a08 commit 27df656

File tree

2 files changed

+140
-14
lines changed

2 files changed

+140
-14
lines changed

nanoFirmwareFlasher.Library/Esp32Operations.cs

Lines changed: 137 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,18 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
391391

392392
string applicationBinary = new FileInfo(applicationPath).FullName;
393393

394-
// add DEPLOYMENT partition with the address provided in the command OR the address from the partition table
395-
firmware.FlashPartitions = new Dictionary<int, string>()
394+
// check for empty flash partitions
395+
if (firmware.FlashPartitions is null)
396396
{
397-
{
397+
firmware.FlashPartitions = [];
398+
}
399+
400+
// add DEPLOYMENT partition with the address provided in the command OR the address from the partition table
401+
firmware.FlashPartitions.Add(
398402
address != 0 ? (int)address : firmware.DeploymentPartitionAddress,
399403
applicationBinary
400-
}
401-
};
404+
);
405+
402406
}
403407
else
404408
{
@@ -532,5 +536,133 @@ public static async System.Threading.Tasks.Task<ExitCodes> UpdateFirmwareAsync(
532536

533537
return operationResult;
534538
}
539+
540+
/// <summary>
541+
/// Deplay application on a ESP32 device.
542+
/// </summary>
543+
/// <param name="espTool"><see cref="EspTool"/> to use when performing update.</param>
544+
/// <param name="esp32Device"><see cref="Esp32DeviceInfo"/> of device to update.</param>
545+
/// <param name="targetName">Name of the target to update.</param>
546+
/// <param name="applicationPath">Path to application to update along with the firmware update.</param>
547+
/// <param name="deploymentAddress">Flash address to use when deploying an aplication.</param>
548+
/// <param name="verbosity">Set verbosity level of progress and error messages.</param>
549+
/// <param name="partitionTableSize">Size of partition table.</param>
550+
/// <returns>The <see cref="ExitCodes"/> with the operation result.</returns>
551+
public static async System.Threading.Tasks.Task<ExitCodes> DeployApplicationAsync(
552+
EspTool espTool,
553+
Esp32DeviceInfo esp32Device,
554+
string targetName,
555+
string applicationPath,
556+
string deploymentAddress,
557+
VerbosityLevel verbosity,
558+
PartitionTableSize? partitionTableSize)
559+
{
560+
var operationResult = ExitCodes.OK;
561+
uint address = 0;
562+
563+
// perform sanity checks for the specified target against the connected device details
564+
if (esp32Device.ChipType != "ESP32" &&
565+
esp32Device.ChipType != "ESP32-S2" &&
566+
esp32Device.ChipType != "ESP32-C3" &&
567+
esp32Device.ChipType != "ESP32-S3")
568+
{
569+
// connected to a device not supported
570+
Console.ForegroundColor = ConsoleColor.Yellow;
571+
Console.WriteLine("");
572+
Console.WriteLine("******************************* WARNING *******************************");
573+
Console.WriteLine("Seems that the connected device is not supported by .NET nanoFramework");
574+
Console.WriteLine("Most likely it won't boot");
575+
Console.WriteLine("************************************************************************");
576+
Console.WriteLine("");
577+
}
578+
579+
Esp32Firmware firmware = new Esp32Firmware(
580+
targetName,
581+
null,
582+
false,
583+
partitionTableSize)
584+
{
585+
Verbosity = verbosity
586+
};
587+
588+
// include application file?
589+
if (!string.IsNullOrEmpty(applicationPath))
590+
{
591+
// check application file
592+
if (File.Exists(applicationPath))
593+
{
594+
// this operation includes a deployment image
595+
// try parsing the deployment address from parameter, if provided
596+
if (!string.IsNullOrEmpty(deploymentAddress))
597+
{
598+
// need to remove the leading 0x and to specify that hexadecimal values are allowed
599+
if (!uint.TryParse(deploymentAddress.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier, System.Globalization.CultureInfo.InvariantCulture, out address))
600+
{
601+
return ExitCodes.E9009;
602+
}
603+
}
604+
605+
string applicationBinary = new FileInfo(applicationPath).FullName;
606+
607+
// check for empty flash partitions
608+
if (firmware.FlashPartitions is null)
609+
{
610+
firmware.FlashPartitions = [];
611+
}
612+
613+
// add DEPLOYMENT partition with the address provided in the command OR the address from the partition table
614+
firmware.FlashPartitions.Add(
615+
address != 0 ? (int)address : firmware.DeploymentPartitionAddress,
616+
applicationBinary
617+
);
618+
}
619+
else
620+
{
621+
return ExitCodes.E9008;
622+
}
623+
}
624+
625+
Console.ForegroundColor = ConsoleColor.White;
626+
627+
if (verbosity >= VerbosityLevel.Normal)
628+
{
629+
Console.Write($"Flashing deployment partition...");
630+
}
631+
632+
// write to flash
633+
operationResult = espTool.WriteFlash(firmware.FlashPartitions);
634+
635+
if (operationResult == ExitCodes.OK)
636+
{
637+
if (verbosity >= VerbosityLevel.Normal)
638+
{
639+
Console.ForegroundColor = ConsoleColor.Green;
640+
Console.WriteLine("OK".PadRight(110));
641+
642+
// warn user if reboot is not possible
643+
if (espTool.CouldntResetTarget)
644+
{
645+
Console.ForegroundColor = ConsoleColor.Yellow;
646+
647+
Console.WriteLine("");
648+
Console.WriteLine("**********************************************");
649+
Console.WriteLine("The connected device is in 'download mode'.");
650+
Console.WriteLine("Please reset the chip manually to run nanoCLR.");
651+
Console.WriteLine("**********************************************");
652+
Console.WriteLine("");
653+
654+
Console.ForegroundColor = ConsoleColor.White;
655+
}
656+
}
657+
else
658+
{
659+
Console.WriteLine("");
660+
}
661+
}
662+
663+
Console.ForegroundColor = ConsoleColor.White;
664+
665+
return operationResult;
666+
}
535667
}
536668
}

nanoFirmwareFlasher.Tool/Esp32Manager.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ public async Task<ExitCodes> ProcessAsync()
158158
updateAndDeploy = true;
159159
}
160160

161-
// it's OK to deploy after a successful update
162-
if (_options.Deploy)
161+
// deploy without update
162+
if (_options.Deploy && !_options.Update)
163163
{
164164
// need to take care of flash address
165165
string appFlashAddress = string.Empty;
@@ -172,18 +172,12 @@ public async Task<ExitCodes> ProcessAsync()
172172

173173
// this to flash a deployment image without updating the firmware
174174
// write flash
175-
var exitCode = await Esp32Operations.UpdateFirmwareAsync(
175+
var exitCode = await Esp32Operations.DeployApplicationAsync(
176176
espTool,
177177
esp32Device,
178178
_options.TargetName,
179-
false,
180-
null,
181-
false,
182179
_options.DeploymentImage,
183180
appFlashAddress,
184-
null,
185-
!_options.FitCheck,
186-
_options.MassErase,
187181
_verbosityLevel,
188182
_options.Esp32PartitionTableSize);
189183

0 commit comments

Comments
 (0)