@@ -26,7 +26,7 @@ internal partial class EspTool
2626 private readonly string _serialPort = null ;
2727
2828 /// <summary>
29- /// The baud rate for the serial port; 921600 baud is the default
29+ /// The baud rate for the serial port. The default comming from <see cref="Options.BaudRate"/> is 9216000.
3030 /// </summary>
3131 private readonly int _baudRate = 0 ;
3232
@@ -76,6 +76,10 @@ internal partial class EspTool
7676 /// </summary>
7777 public VerbosityLevel Verbosity { get ; internal set ; } = VerbosityLevel . Normal ;
7878
79+ // ESP32 chip type to connect to.
80+ // Default is 'auto'. It's replaced with the actual chip type after detection to improve operations.
81+ internal string _chipType = "auto" ;
82+
7983 /// <summary>
8084 /// Constructor
8185 /// </summary>
@@ -123,7 +127,7 @@ internal EspTool(
123127 /// Tries reading ESP32 device details.
124128 /// </summary>
125129 /// <returns>The filled info structure with all the information about the connected ESP32 device or null if an error occured</returns>
126- internal Esp32DeviceInfo GetDeviceDetails ( )
130+ internal Esp32DeviceInfo GetDeviceDetails ( bool requireFlashSize = true )
127131 {
128132 string messages ;
129133
@@ -134,11 +138,34 @@ internal Esp32DeviceInfo GetDeviceDetails()
134138 }
135139
136140 // execute flash_id command and parse the result
137- if ( ! RunEspTool ( "flash_id" , true , false , null , out messages ) )
141+ if ( ! RunEspTool (
142+ "flash_id" ,
143+ true ,
144+ true ,
145+ false ,
146+ null ,
147+ out messages ) )
138148 {
139149 throw new EspToolExecutionException ( messages ) ;
140150 }
141151
152+ // check if we got flash size (in case we need it)
153+ if ( requireFlashSize
154+ && messages . Contains ( "Detected flash size: Unknown" ) )
155+ {
156+ // try again now without the stub
157+ if ( ! RunEspTool (
158+ "flash_id" ,
159+ false ,
160+ true ,
161+ false ,
162+ null ,
163+ out messages ) )
164+ {
165+ throw new EspToolExecutionException ( messages ) ;
166+ }
167+ }
168+
142169 if ( Verbosity >= VerbosityLevel . Normal )
143170 {
144171 Console . ForegroundColor = ConsoleColor . Green ;
@@ -163,14 +190,26 @@ internal Esp32DeviceInfo GetDeviceDetails()
163190 string size = match . Groups [ "size" ] . ToString ( ) . Trim ( ) ;
164191
165192 // collect and return all information
166- // convert the flash size into bytes
193+ // try to convert the flash size into bytes
167194 string unit = size . Substring ( size . Length - 2 ) . ToUpperInvariant ( ) ;
168- _flashSize = int . Parse ( size . Remove ( size . Length - 2 ) ) * unit switch
195+
196+ if ( int . TryParse ( size . Remove ( size . Length - 2 ) , out _flashSize ) )
169197 {
170- "MB" => 0x100000 ,
171- "KB" => 0x400 ,
172- _ => 1 ,
173- } ;
198+ _flashSize *= unit switch
199+ {
200+ "MB" => 0x100000 ,
201+ "KB" => 0x400 ,
202+ _ => 1 ,
203+ } ;
204+ }
205+ else
206+ {
207+ throw new EspToolExecutionException ( "Can't read flash size from device" ) ;
208+ }
209+
210+ // update chip type
211+ // lower case, no hifen
212+ _chipType = chipType . ToLower ( ) . Replace ( "-" , "" ) ;
174213
175214 return new Esp32DeviceInfo (
176215 chipType ,
@@ -193,7 +232,13 @@ internal void BackupFlash(string backupFilename,
193232 int flashSize )
194233 {
195234 // execute read_flash command and parse the result; progress message can be found be searching for backspaces (ASCII code 8)
196- if ( ! RunEspTool ( $ "read_flash 0 0x{ flashSize : X} \" { backupFilename } \" ", false , false , ( char ) 8 , out string messages ) )
235+ if ( ! RunEspTool (
236+ $ "read_flash 0 0x{ flashSize : X} \" { backupFilename } \" ",
237+ false ,
238+ false ,
239+ false ,
240+ ( char ) 8 ,
241+ out string messages ) )
197242 {
198243 throw new ReadEsp32FlashException ( messages ) ;
199244 }
@@ -217,7 +262,13 @@ internal void BackupFlash(string backupFilename,
217262 internal ExitCodes EraseFlash ( )
218263 {
219264 // execute erase_flash command and parse the result
220- if ( ! RunEspTool ( "erase_flash" , false , false , null , out string messages ) )
265+ if ( ! RunEspTool (
266+ "erase_flash" ,
267+ false ,
268+ false ,
269+ false ,
270+ null ,
271+ out string messages ) )
221272 {
222273 throw new EraseEsp32FlashException ( messages ) ;
223274 }
@@ -241,7 +292,13 @@ internal ExitCodes EraseFlashSegment(uint startAddress, uint length)
241292 // esptool takes care of validating this so no need to perform any sanity check before executing the command
242293
243294 // execute erase_flash command and parse the result
244- if ( ! RunEspTool ( $ "erase_region 0x{ startAddress : X} 0x{ length : X} ", false , false , null , out string messages ) )
295+ if ( ! RunEspTool (
296+ $ "erase_region 0x{ startAddress : X} 0x{ length : X} ",
297+ false ,
298+ false ,
299+ false ,
300+ null ,
301+ out string messages ) )
245302 {
246303 throw new EraseEsp32FlashException ( messages ) ;
247304 }
@@ -292,7 +349,13 @@ internal ExitCodes WriteFlash(Dictionary<int, string> partsToWrite)
292349 } ;
293350
294351 // execute write_flash command and parse the result; progress message can be found be searching for linefeed
295- if ( ! RunEspTool ( $ "write_flash --flash_mode { _flashMode } --flash_freq { _flashFrequency } m --flash_size { flashSize } { partsArguments . ToString ( ) . Trim ( ) } ", false , true , '\r ' , out string messages ) )
352+ if ( ! RunEspTool (
353+ $ "write_flash --flash_mode { _flashMode } --flash_freq { _flashFrequency } m --flash_size { flashSize } { partsArguments . ToString ( ) . Trim ( ) } ",
354+ false ,
355+ false ,
356+ true ,
357+ '\r ' ,
358+ out string messages ) )
296359 {
297360 throw new WriteEsp32FlashException ( messages ) ;
298361 }
@@ -325,7 +388,8 @@ internal ExitCodes WriteFlash(Dictionary<int, string> partsToWrite)
325388 /// <returns>true if the esptool exit code was 0; false otherwise</returns>
326389 private bool RunEspTool (
327390 string commandWithArguments ,
328- bool noStub ,
391+ bool noStub ,
392+ bool useStandardBaudRate ,
329393 bool hardResetAfterCommand ,
330394 char ? progressTestChar ,
331395 out string messages )
@@ -345,13 +409,16 @@ private bool RunEspTool(
345409 }
346410 else
347411 {
348- // using the stub that supports changing the baudrate
349- baudRateParameter = $ "--baud { _baudRate } ";
412+ if ( ! useStandardBaudRate )
413+ {
414+ // using the stub that supports changing the baudrate
415+ baudRateParameter = $ "--baud { _baudRate } ";
416+ }
350417 }
351418
352419 // prepare the process start of the esptool
353420 Process espTool = new Process ( ) ;
354- string parameter = $ "--port { _serialPort } { baudRateParameter } --chip auto { noStubParameter } { beforeParameter } --after { afterParameter } { commandWithArguments } ";
421+ string parameter = $ "--port { _serialPort } { baudRateParameter } --chip { _chipType } { noStubParameter } { beforeParameter } --after { afterParameter } { commandWithArguments } ";
355422 espTool . StartInfo = new ProcessStartInfo ( Path . Combine ( Program . ExecutingPath , "esptool" , "esptool.exe" ) , parameter )
356423 {
357424 WorkingDirectory = Path . Combine ( Program . ExecutingPath , "esptool" ) ,
0 commit comments