flash: add ability to perform 1200baud port reset for MCUs that can detect this in order to go into bootloader mode for flashing without pressing any buttons. Also add support for this to the Arduino Nano33 IoT board target

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans
2019-10-11 11:34:08 +02:00
committed by Ayke
parent 4164c39a4a
commit 02facb8568
5 changed files with 41 additions and 1 deletions
+25
View File
@@ -15,10 +15,13 @@ import (
"strconv"
"strings"
"syscall"
"time"
"github.com/tinygo-org/tinygo/compiler"
"github.com/tinygo-org/tinygo/interp"
"github.com/tinygo-org/tinygo/loader"
serial "go.bug.st/serial.v1"
)
// commandError is an error type to wrap os/exec.Command errors. This provides
@@ -418,6 +421,16 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
return errors.New("no flash command specified - did you miss a -target flag?")
}
// do we need port reset to put MCU into bootloader mode?
if spec.PortReset == "true" {
err := touchSerialPortAt1200bps(port)
if err != nil {
return &commandError{"failed to reset port", tmppath, err}
}
// give the target MCU a chance to restart into bootloader
time.Sleep(3 * time.Second)
}
// Create the command.
flashCmd := spec.Flasher
fileToken := "{" + fileExt[1:] + "}"
@@ -549,6 +562,18 @@ func Run(pkgName, target string, config *BuildConfig) error {
})
}
func touchSerialPortAt1200bps(port string) error {
// Open port
p, err := serial.Open(port, &serial.Mode{BaudRate: 1200})
if err != nil {
return fmt.Errorf("opening port: %s", err)
}
defer p.Close()
p.SetDTR(false)
return nil
}
// parseSize converts a human-readable size (with k/m/g suffix) into a plain
// number.
func parseSize(s string) (int64, error) {