@@ -361,11 +361,24 @@ func runProgramAction(pme *packagemanager.Explorer,
361361 uploadProperties .Set ("build.project_name" , sketchName )
362362 }
363363
364+ // Force port wait to make easier to unbrick boards like the Arduino Leonardo, or similar with native USB,
365+ // when a sketch causes a crash and the native USB serial port is lost.
366+ // See https://github.com/arduino/arduino-cli/issues/1943 for the details.
367+ //
368+ // In order to trigger the forced serial-port-wait the following conditions must be met:
369+ // - No upload port specified (protocol == "default")
370+ // - "upload.wait_for_upload_port" == true (developers requested the touch + port wait)
371+ // - "upload.tool.serial" not defained, or
372+ // "upload.tool.serial" is the same as "upload.tool.default"
373+ forcedSerialPortWait := port .Protocol == "default" && // this is the value when no port is specified
374+ uploadProperties .GetBoolean ("upload.wait_for_upload_port" ) &&
375+ (! uploadProperties .ContainsKey ("upload.tool.serial" ) ||
376+ uploadProperties .Get ("upload.tool.serial" ) == uploadProperties .Get ("upload.tool.default" ))
377+
364378 // If not using programmer perform some action required
365379 // to set the board in bootloader mode
366380 actualPort := port
367- if programmer == nil && ! burnBootloader && port .Protocol == "serial" {
368-
381+ if programmer == nil && ! burnBootloader && (port .Protocol == "serial" || forcedSerialPortWait ) {
369382 // Perform reset via 1200bps touch if requested and wait for upload port also if requested.
370383 touch := uploadProperties .GetBoolean ("upload.use_1200bps_touch" )
371384 wait := false
@@ -425,7 +438,7 @@ func runProgramAction(pme *packagemanager.Explorer,
425438 if actualPort .Address != "" {
426439 // Set serial port property
427440 uploadProperties .Set ("serial.port" , actualPort .Address )
428- if actualPort .Protocol == "serial" {
441+ if actualPort .Protocol == "serial" || actualPort . Protocol == "default" {
429442 // This must be done only for serial ports
430443 portFile := strings .TrimPrefix (actualPort .Address , "/dev/" )
431444 uploadProperties .Set ("serial.port.file" , portFile )
0 commit comments