@@ -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}
0 commit comments