Skip to content

Commit ba49148

Browse files
deadprogramaykevl
authored andcommitted
flash: support flashing boards using Mass Storage Device (MSD) operation for uf2 and daplink bootloaders
Signed-off-by: Ron Evans <[email protected]>
1 parent 02facb8 commit ba49148

File tree

9 files changed

+122
-37
lines changed

9 files changed

+122
-37
lines changed

main.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,26 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
431431
time.Sleep(3 * time.Second)
432432
}
433433

434+
// this flashing method copies the binary data to a Mass Storage Device (msd)
435+
if spec.FlashMethod == "msd" {
436+
switch fileExt {
437+
case ".uf2":
438+
err := flashUF2UsingMSD(spec.FlashVolume, tmppath)
439+
if err != nil {
440+
return &commandError{"failed to flash", tmppath, err}
441+
}
442+
return nil
443+
case ".hex":
444+
err := flashHexUsingMSD(spec.FlashVolume, tmppath)
445+
if err != nil {
446+
return &commandError{"failed to flash", tmppath, err}
447+
}
448+
return nil
449+
default:
450+
return errors.New("mass storage device flashing currently only supports uf2 and hex")
451+
}
452+
}
453+
434454
// Create the command.
435455
flashCmd := spec.Flasher
436456
fileToken := "{" + fileExt[1:] + "}"
@@ -574,6 +594,42 @@ func touchSerialPortAt1200bps(port string) error {
574594
return nil
575595
}
576596

597+
func flashUF2UsingMSD(volume, tmppath string) error {
598+
// find standard UF2 info path
599+
infoPath := "/media/*/" + volume + "/INFO_UF2.TXT"
600+
if runtime.GOOS == "darwin" {
601+
infoPath = "/Volumes/" + volume + "/INFO_UF2.TXT"
602+
}
603+
604+
d, err := filepath.Glob(infoPath)
605+
if err != nil {
606+
return err
607+
}
608+
if d == nil {
609+
return errors.New("unable to locate UF2 device:" + volume)
610+
}
611+
612+
return moveFile(tmppath, filepath.Dir(d[0])+"/flash.uf2")
613+
}
614+
615+
func flashHexUsingMSD(volume, tmppath string) error {
616+
// find expected volume path
617+
destPath := "/media/*/" + volume
618+
if runtime.GOOS == "darwin" {
619+
destPath = "/Volumes/" + volume
620+
}
621+
622+
d, err := filepath.Glob(destPath)
623+
if err != nil {
624+
return err
625+
}
626+
if d == nil {
627+
return errors.New("unable to locate device:" + volume)
628+
}
629+
630+
return moveFile(tmppath, d[0]+"/flash.hex")
631+
}
632+
577633
// parseSize converts a human-readable size (with k/m/g suffix) into a plain
578634
// number.
579635
func parseSize(s string) (int64, error) {

target.go

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,29 @@ var TINYGOROOT string
2626
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/struct.TargetOptions.html
2727
// https://github.com/shepmaster/rust-arduino-blink-led-no-core-with-cargo/blob/master/blink/arduino.json
2828
type TargetSpec struct {
29-
Inherits []string `json:"inherits"`
30-
Triple string `json:"llvm-target"`
31-
CPU string `json:"cpu"`
32-
Features []string `json:"features"`
33-
GOOS string `json:"goos"`
34-
GOARCH string `json:"goarch"`
35-
BuildTags []string `json:"build-tags"`
36-
GC string `json:"gc"`
37-
Scheduler string `json:"scheduler"`
38-
Compiler string `json:"compiler"`
39-
Linker string `json:"linker"`
40-
RTLib string `json:"rtlib"` // compiler runtime library (libgcc, compiler-rt)
41-
CFlags []string `json:"cflags"`
42-
LDFlags []string `json:"ldflags"`
43-
ExtraFiles []string `json:"extra-files"`
44-
Emulator []string `json:"emulator"`
45-
Flasher string `json:"flash"`
46-
OCDDaemon []string `json:"ocd-daemon"`
47-
GDB string `json:"gdb"`
48-
GDBCmds []string `json:"gdb-initial-cmds"`
49-
PortReset string `json:"flash-1200-bps-reset"`
29+
Inherits []string `json:"inherits"`
30+
Triple string `json:"llvm-target"`
31+
CPU string `json:"cpu"`
32+
Features []string `json:"features"`
33+
GOOS string `json:"goos"`
34+
GOARCH string `json:"goarch"`
35+
BuildTags []string `json:"build-tags"`
36+
GC string `json:"gc"`
37+
Scheduler string `json:"scheduler"`
38+
Compiler string `json:"compiler"`
39+
Linker string `json:"linker"`
40+
RTLib string `json:"rtlib"` // compiler runtime library (libgcc, compiler-rt)
41+
CFlags []string `json:"cflags"`
42+
LDFlags []string `json:"ldflags"`
43+
ExtraFiles []string `json:"extra-files"`
44+
Emulator []string `json:"emulator"`
45+
Flasher string `json:"flash"`
46+
OCDDaemon []string `json:"ocd-daemon"`
47+
GDB string `json:"gdb"`
48+
GDBCmds []string `json:"gdb-initial-cmds"`
49+
PortReset string `json:"flash-1200-bps-reset"`
50+
FlashMethod string `json:"flash-method"`
51+
FlashVolume string `json:"flash-msd-volume-name"`
5052
}
5153

5254
// copyProperties copies all properties that are set in spec2 into itself.
@@ -104,6 +106,12 @@ func (spec *TargetSpec) copyProperties(spec2 *TargetSpec) {
104106
if spec2.PortReset != "" {
105107
spec.PortReset = spec2.PortReset
106108
}
109+
if spec2.FlashMethod != "" {
110+
spec.FlashMethod = spec2.FlashMethod
111+
}
112+
if spec2.FlashVolume != "" {
113+
spec.FlashVolume = spec2.FlashVolume
114+
}
107115
}
108116

109117
// load reads a target specification from the JSON in the given io.Reader. It
@@ -233,15 +241,16 @@ func defaultTarget(goos, goarch, triple string) (*TargetSpec, error) {
233241
// No target spec available. Use the default one, useful on most systems
234242
// with a regular OS.
235243
spec := TargetSpec{
236-
Triple: triple,
237-
GOOS: goos,
238-
GOARCH: goarch,
239-
BuildTags: []string{goos, goarch},
240-
Compiler: "clang",
241-
Linker: "cc",
242-
GDB: "gdb",
243-
GDBCmds: []string{"run"},
244-
PortReset: "false",
244+
Triple: triple,
245+
GOOS: goos,
246+
GOARCH: goarch,
247+
BuildTags: []string{goos, goarch},
248+
Compiler: "clang",
249+
Linker: "cc",
250+
GDB: "gdb",
251+
GDBCmds: []string{"run"},
252+
PortReset: "false",
253+
FlashMethod: "command",
245254
}
246255
if goos == "darwin" {
247256
spec.LDFlags = append(spec.LDFlags, "-Wl,-dead_strip")

targets/circuitplay-express.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"inherits": ["atsamd21g18a"],
33
"build-tags": ["sam", "atsamd21g18a", "circuitplay_express"],
4-
"flash": "uf2conv.py {bin}"
4+
"flash": "{uf2}",
5+
"flash-1200-bps-reset": "true",
6+
"flash-method": "msd",
7+
"flash-msd-volume-name": "CPLAYBOOT"
58
}

targets/feather-m0.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"inherits": ["atsamd21g18a"],
33
"build-tags": ["sam", "atsamd21g18a", "feather_m0"],
4-
"flash": "bossac -d -i -e -w -v -R --port={port} --offset=0x2000 {hex}"
4+
"flash": "{uf2}",
5+
"flash-1200-bps-reset": "true",
6+
"flash-method": "msd",
7+
"flash-msd-volume-name": "FEATHERBOOT"
58
}

targets/hifive1b.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
"build-tags": ["hifive1b"],
44
"ldflags": [
55
"-T", "targets/hifive1b.ld"
6-
]
6+
],
7+
"flash": "{hex}",
8+
"flash-method": "msd",
9+
"flash-msd-volume-name": "HiFive"
710
}

targets/itsybitsy-m0.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"inherits": ["atsamd21g18a"],
33
"build-tags": ["sam", "atsamd21g18a", "itsybitsy_m0"],
4-
"flash": "bossac -d -i -e -w -v -R --port={port} --offset=0x2000 {hex}"
4+
"flash": "{uf2}",
5+
"flash-1200-bps-reset": "true",
6+
"flash-method": "msd",
7+
"flash-msd-volume-name": "ITSYBOOT"
58
}

targets/itsybitsy-m4.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"inherits": ["atsamd51g19a"],
33
"build-tags": ["sam", "atsamd51g19a", "itsybitsy_m4"],
4-
"flash": "bossac -d -i -e -w -v -R --offset=0x4000 {bin}"
4+
"flash": "{uf2}",
5+
"flash-1200-bps-reset": "true",
6+
"flash-method": "msd",
7+
"flash-msd-volume-name": "ITSYM4BOOT"
58
}

targets/microbit.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"build-tags": ["microbit"],
44
"flash": "openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg -c 'program {hex} reset exit'",
55
"ocd-daemon": ["openocd", "-f", "interface/cmsis-dap.cfg", "-f", "target/nrf51.cfg"],
6-
"gdb-initial-cmds": ["target remote :3333", "monitor halt", "load", "monitor reset", "c"]
6+
"gdb-initial-cmds": ["target remote :3333", "monitor halt", "load", "monitor reset", "c"],
7+
"flash-method": "msd",
8+
"flash-msd-volume-name": "MICROBIT"
79
}

targets/trinket-m0.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"inherits": ["atsamd21e18a"],
33
"build-tags": ["sam", "atsamd21e18a", "trinket_m0"],
4-
"flash": "bossac -d -i -e -w -v -R --port={port} --offset=0x2000 {hex}"
4+
"flash": "{uf2}",
5+
"flash-1200-bps-reset": "true",
6+
"flash-method": "msd",
7+
"flash-msd-volume-name": "TRINKETBOOT"
58
}

0 commit comments

Comments
 (0)