Skip to content

Optimized 1200-bps touch and upload port detection. Fixed some rare upload-fail corner cases. #1267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Experimental: optimized touch + wait
  • Loading branch information
cmaglie committed Apr 19, 2021
commit a262ded8c91705f5cf011813f96726704e7b6ecc
75 changes: 75 additions & 0 deletions arduino/serialutils/serialutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package serialutils

import (
"fmt"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -127,3 +128,77 @@ func WaitForNewSerialPort() (string, error) {

return "", nil
}

// TouchAndWait FIXMEDOCS
func TouchAndWait(portToTouch string, wait bool) (string, error) {
getPortMap := func() (map[string]bool, error) {
ports, err := serial.GetPortsList()
if err != nil {
return nil, errors.WithMessage(err, "listing serial ports")
}
res := map[string]bool{}
for _, port := range ports {
res[port] = true
}
return res, nil
}

last, err := getPortMap()
fmt.Println("LAST:", last)
if err != nil {
return "", err
}

if portToTouch != "" {
fmt.Println("TOUCH:", portToTouch)
TouchSerialPortAt1200bps(portToTouch)
}

if !wait {
return "", nil
}
deadline := time.Now().Add(10 * time.Second)
for time.Now().Before(deadline) {
now, err := getPortMap()
fmt.Println("NOW:", now)
if err != nil {
return "", err
}
hasNewPorts := false
for p := range now {
if !last[p] {
hasNewPorts = true
break
}
}

if hasNewPorts {
fmt.Println("HAS NEW PORTS!")
// on OS X, if the port is opened too quickly after it is detected,
// a "Resource busy" error occurs, add a delay to workaround.
// This apply to other platforms as well.
time.Sleep(time.Second)

// Some boards have a glitch in the bootloader: some user experienced
// the USB serial port appearing and disappearing rapidly before
// settling.
// This check ensure that the port is stable after one second.
check, err := getPortMap()
fmt.Println("CHECK:", check)
if err != nil {
return "", err
}
for p := range check {
if !last[p] {
fmt.Println("FOUND:", p)
return p, nil // Found it!
}
}
}

last = now
time.Sleep(250 * time.Millisecond)
}

return "", nil
}
70 changes: 35 additions & 35 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.bug.st/serial"
)

// Upload FIXMEDOC
Expand Down Expand Up @@ -293,44 +292,45 @@ func runProgramAction(pm *packagemanager.PackageManager,
// to set the board in bootloader mode
actualPort := port
if programmer == nil && !burnBootloader {
// Perform reset via 1200bps touch if requested
if uploadProperties.GetBoolean("upload.use_1200bps_touch") {
if port == "" {
outStream.Write([]byte(fmt.Sprintln("Skipping 1200-bps touch reset: no serial port selected!")))
} else {
ports, err := serial.GetPortsList()
if err != nil {
return fmt.Errorf("cannot get serial port list: %s", err)
}
for _, p := range ports {
if p == port {
if verbose {
outStream.Write([]byte(fmt.Sprintf("Performing 1200-bps touch reset on serial port %s", p)))
outStream.Write([]byte(fmt.Sprintln()))
}
logrus.Infof("Touching port %s at 1200bps", port)
if err := serialutils.TouchSerialPortAt1200bps(p); err != nil {
outStream.Write([]byte(fmt.Sprintf("Cannot perform port reset: %s", err)))
outStream.Write([]byte(fmt.Sprintln()))
}
break
}
}
}
// Perform reset via 1200bps touch if requested and wait for upload port if requested.
touch := uploadProperties.GetBoolean("upload.use_1200bps_touch")
wait := uploadProperties.GetBoolean("upload.wait_for_upload_port")
if touch && port == "" {
outStream.Write([]byte(fmt.Sprintln("Skipping 1200-bps touch reset: no serial port selected!")))
}

// Wait for upload port if requested
if uploadProperties.GetBoolean("upload.wait_for_upload_port") {
if verbose {
outStream.Write([]byte(fmt.Sprintln("Waiting for upload port...")))
}

actualPort, err = serialutils.WaitForNewSerialPortOrDefaultTo(actualPort)
if err != nil {
return errors.WithMessage(err, "detecting serial port")
if newPort, err := serialutils.TouchAndWait(port, wait); err != nil {
} else {
if newPort != "" {
actualPort = newPort
}
}
}
// ports, err := serial.GetPortsList()
// for _, p := range ports {
// if p == port {
// if verbose {
// outStream.Write([]byte(fmt.Sprintf("Performing 1200-bps touch reset on serial port %s", p)))
// outStream.Write([]byte(fmt.Sprintln()))
// }
// logrus.Infof("Touching port %s at 1200bps", port)
// if err := serialutils.TouchSerialPortAt1200bps(p); err != nil {
// outStream.Write([]byte(fmt.Sprintf("Cannot perform port reset: %s", err)))
// outStream.Write([]byte(fmt.Sprintln()))
// }
// break
// }
// }
}

// Wait for upload port if requested
// if verbose {
// outStream.Write([]byte(fmt.Sprintln("Waiting for upload port...")))
// }

// actualPort, err = serialutils.WaitForNewSerialPortOrDefaultTo(actualPort)
// if err != nil {
// return errors.WithMessage(err, "detecting serial port")
// }

if port != "" {
// Set serial port property
Expand Down