Skip to content

Commit 02facb8

Browse files
deadprogramaykevl
authored andcommitted
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>
1 parent 4164c39 commit 02facb8

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ go 1.11
44

55
require (
66
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2
7+
github.com/creack/goselect v0.1.0 // indirect
78
github.com/marcinbor85/gohex v0.0.0-20180128172054-7a43cd876e46
9+
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45
10+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
811
golang.org/x/tools v0.0.0-20190227180812-8dcc6e70cdef
912
tinygo.org/x/go-llvm v0.0.0-20190818154551-95bc4ffe1add
1013
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2 h1:oMCHnXa6CCCafdPDbMh/lWRhRByN0VFLvv+g+ayx1SI=
22
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI=
3+
github.com/creack/goselect v0.1.0 h1:4QiXIhcpSQF50XGaBsFzesjwX/1qOY5bOveQPmN9CXY=
4+
github.com/creack/goselect v0.1.0/go.mod h1:gHrIcH/9UZDn2qgeTUeW5K9eZsVYCH6/60J/FHysWyE=
35
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
46
github.com/marcinbor85/gohex v0.0.0-20180128172054-7a43cd876e46 h1:wXG2bA8fO7Vv7lLk2PihFMTqmbT173Tje39oKzQ50Mo=
57
github.com/marcinbor85/gohex v0.0.0-20180128172054-7a43cd876e46/go.mod h1:Pb6XcsXyropB9LNHhnqaknG/vEwYztLkQzVCHv8sQ3M=
8+
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=
9+
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
610
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
711
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
812
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
14+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
915
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1016
golang.org/x/tools v0.0.0-20190227180812-8dcc6e70cdef h1:ymc9FeDom3RIEA3coKokSllBB1hRcMT0tZ1W3Jf9Ids=
1117
golang.org/x/tools v0.0.0-20190227180812-8dcc6e70cdef/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ import (
1515
"strconv"
1616
"strings"
1717
"syscall"
18+
"time"
1819

1920
"github.com/tinygo-org/tinygo/compiler"
2021
"github.com/tinygo-org/tinygo/interp"
2122
"github.com/tinygo-org/tinygo/loader"
23+
24+
serial "go.bug.st/serial.v1"
2225
)
2326

2427
// 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 {
418421
return errors.New("no flash command specified - did you miss a -target flag?")
419422
}
420423

424+
// do we need port reset to put MCU into bootloader mode?
425+
if spec.PortReset == "true" {
426+
err := touchSerialPortAt1200bps(port)
427+
if err != nil {
428+
return &commandError{"failed to reset port", tmppath, err}
429+
}
430+
// give the target MCU a chance to restart into bootloader
431+
time.Sleep(3 * time.Second)
432+
}
433+
421434
// Create the command.
422435
flashCmd := spec.Flasher
423436
fileToken := "{" + fileExt[1:] + "}"
@@ -549,6 +562,18 @@ func Run(pkgName, target string, config *BuildConfig) error {
549562
})
550563
}
551564

565+
func touchSerialPortAt1200bps(port string) error {
566+
// Open port
567+
p, err := serial.Open(port, &serial.Mode{BaudRate: 1200})
568+
if err != nil {
569+
return fmt.Errorf("opening port: %s", err)
570+
}
571+
defer p.Close()
572+
573+
p.SetDTR(false)
574+
return nil
575+
}
576+
552577
// parseSize converts a human-readable size (with k/m/g suffix) into a plain
553578
// number.
554579
func parseSize(s string) (int64, error) {

target.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type TargetSpec struct {
4646
OCDDaemon []string `json:"ocd-daemon"`
4747
GDB string `json:"gdb"`
4848
GDBCmds []string `json:"gdb-initial-cmds"`
49+
PortReset string `json:"flash-1200-bps-reset"`
4950
}
5051

5152
// copyProperties copies all properties that are set in spec2 into itself.
@@ -100,6 +101,9 @@ func (spec *TargetSpec) copyProperties(spec2 *TargetSpec) {
100101
if len(spec2.GDBCmds) != 0 {
101102
spec.GDBCmds = spec2.GDBCmds
102103
}
104+
if spec2.PortReset != "" {
105+
spec.PortReset = spec2.PortReset
106+
}
103107
}
104108

105109
// load reads a target specification from the JSON in the given io.Reader. It
@@ -237,6 +241,7 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
237241
Linker: "cc",
238242
GDB: "gdb",
239243
GDBCmds: []string{"run"},
244+
PortReset: "false",
240245
}
241246
if goos == "darwin" {
242247
spec.LDFlags = append(spec.LDFlags, "-Wl,-dead_strip")

targets/arduino-nano33.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"inherits": ["atsamd21g18a"],
33
"build-tags": ["sam", "atsamd21g18a", "arduino_nano33"],
4-
"flash": "bossac -d -i -e -w -v -R --port={port} --offset=0x2000 {bin}"
4+
"flash": "bossac -d -i -e -w -v -R --port={port} --offset=0x2000 {bin}",
5+
"flash-1200-bps-reset": "true"
56
}

0 commit comments

Comments
 (0)