Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit eac8b5d

Browse files
author
Brian Baltz
committed
Adding Go source from github.com/facchinm/arduino101load
Signed-off-by: Brian Baltz <[email protected]>
1 parent d29774c commit eac8b5d

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

arduino101load/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# arduino101load
2+
multiplatform launcher for Arduino101 dfu-util flashing utility
3+
4+
## Compiling
5+
6+
* Download go package from [here](https://golang.org/dl/) or using your package manager
7+
* `cd` into the root folder of this project
8+
* execute
9+
```bash
10+
export GOPATH=$PWD
11+
go get
12+
go build
13+
```
14+
to produce a binary of `arduino101load` for your architecture.
15+
16+
To cross compile for different OS/architecture combinations, execute
17+
```bash
18+
GOOS=windows GOARCH=386 go build #windows
19+
GOOS=darwin GOARCH=amd64 go build #osx
20+
GOOS=linux GOARCH=386 go build #linux_x86
21+
GOOS=linux GOARCH=amd64 go build #linux_x86-64
22+
```

arduino101load/main.go

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"github.com/codeskyblue/go-sh"
7+
"io"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
"runtime"
12+
"strings"
13+
"time"
14+
)
15+
16+
var verbose bool
17+
18+
func PrintlnVerbose(a ...interface{}) {
19+
if verbose {
20+
fmt.Println(a...)
21+
}
22+
}
23+
24+
func main() {
25+
fmt.Println("Starting download script...")
26+
27+
// ARG 1: Path to binaries
28+
// ARG 2: BIN File to download
29+
// ARG 3: TTY port to use.
30+
// ARG 4: quiet/verbose
31+
// path may contain \ need to change all to /
32+
33+
args := os.Args[1:]
34+
35+
bin_path := args[0]
36+
dfu := bin_path + "/dfu-util"
37+
dfu = filepath.ToSlash(dfu)
38+
dfu_flags := "-d,8087:0ABA"
39+
40+
bin_file_name := args[1]
41+
42+
com_port := args[2]
43+
verbosity := args[3]
44+
45+
if verbosity == "quiet" {
46+
verbose = false
47+
} else {
48+
verbose = true
49+
}
50+
51+
PrintlnVerbose("Args to shell:", args)
52+
PrintlnVerbose("Serial Port: " + com_port)
53+
PrintlnVerbose("BIN FILE " + bin_file_name)
54+
55+
counter := 0
56+
board_found := false
57+
58+
if runtime.GOOS == "darwin" {
59+
library_path := os.Getenv("DYLD_LIBRARY_PATH")
60+
if !strings.Contains(library_path, bin_path) {
61+
os.Setenv("DYLD_LIBRARY_PATH", bin_path+":"+library_path)
62+
}
63+
}
64+
65+
for counter < 10 && board_found == false {
66+
PrintlnVerbose("Waiting for device...")
67+
out, err := sh.Command(dfu, dfu_flags, "-l").Output()
68+
if err != nil {
69+
fmt.Println(err)
70+
os.Exit(1)
71+
}
72+
if counter == 4 {
73+
fmt.Println("Flashing is taking longer than expected")
74+
fmt.Println("Try pressing MASTER_RESET button")
75+
}
76+
if strings.Contains(string(out), "sensor_core") {
77+
board_found = true
78+
PrintlnVerbose("Device found!")
79+
break
80+
}
81+
time.Sleep(1000 * time.Millisecond)
82+
counter++
83+
}
84+
85+
if board_found == false {
86+
fmt.Println("ERROR: Timed out waiting for Arduino 101 on " + com_port)
87+
os.Exit(1)
88+
}
89+
90+
dfu_download := []string{dfu, dfu_flags, "-D", bin_file_name, "-v", "--alt", "7", "-R"}
91+
92+
oscmd := exec.Command(dfu_download[0], dfu_download[1:]...)
93+
94+
tellCommandNotToSpawnShell(oscmd)
95+
96+
stdout, _ := oscmd.StdoutPipe()
97+
98+
stderr, _ := oscmd.StderrPipe()
99+
100+
multi := io.MultiReader(stderr, stdout)
101+
102+
err := oscmd.Start()
103+
104+
in := bufio.NewScanner(multi)
105+
106+
in.Split(bufio.ScanLines)
107+
108+
for in.Scan() {
109+
PrintlnVerbose(in.Text())
110+
}
111+
112+
err = oscmd.Wait()
113+
114+
if err == nil {
115+
fmt.Println("SUCCESS: Sketch will execute in about 5 seconds.")
116+
os.Exit(0)
117+
} else {
118+
fmt.Println("ERROR: Upload failed on " + com_port)
119+
os.Exit(1)
120+
}
121+
}

arduino101load/util_unix.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// +build linux darwin
2+
3+
package main
4+
5+
import (
6+
"os/exec"
7+
)
8+
9+
func tellCommandNotToSpawnShell(_ *exec.Cmd) {
10+
}

arduino101load/util_windows.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import (
4+
"os/exec"
5+
"syscall"
6+
)
7+
8+
func tellCommandNotToSpawnShell(oscmd *exec.Cmd) {
9+
oscmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
10+
}

0 commit comments

Comments
 (0)