From 1f5e4e79aa6b691a54fbfac76b8d1f83650aff4d Mon Sep 17 00:00:00 2001 From: Olaf Flebbe Date: Thu, 3 Jun 2021 14:27:33 +0200 Subject: [PATCH] support flashing pca10059 from windows --- builder/build.go | 12 ++++++++++++ builder/nrfutil.go | 27 +++++++++++++++++++++++++++ compileopts/config.go | 5 +++++ main.go | 2 ++ targets/pca10059.json | 3 ++- 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 builder/nrfutil.go diff --git a/builder/build.go b/builder/build.go index bdb5a1e7..5ef7b3e5 100644 --- a/builder/build.go +++ b/builder/build.go @@ -639,6 +639,18 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil if err != nil { return err } + case "nrf-dfu": + // special format for nrfutil for Nordic chips + tmphexpath := filepath.Join(dir, "main.hex") + err := objcopy(executable, tmphexpath, "hex") + if err != nil { + return err + } + tmppath = filepath.Join(dir, "main"+outext) + err = makeDFUFirmwareImage(config.Options, tmphexpath, tmppath) + if err != nil { + return err + } default: return fmt.Errorf("unknown output binary format: %s", outputBinaryFormat) } diff --git a/builder/nrfutil.go b/builder/nrfutil.go new file mode 100644 index 00000000..55aef45e --- /dev/null +++ b/builder/nrfutil.go @@ -0,0 +1,27 @@ +package builder + +import ( + "fmt" + "io/ioutil" + "os/exec" + + "github.com/tinygo-org/tinygo/compileopts" +) + +// https://infocenter.nordicsemi.com/index.jsp?topic=%2Fug_nrfutil%2FUG%2Fnrfutil%2Fnrfutil_intro.html + +func makeDFUFirmwareImage(options *compileopts.Options, infile, outfile string) error { + cmdLine := []string{"nrfutil", "pkg", "generate", "--hw-version", "52", "--sd-req", "0x0", "--debug-mode", "--application", infile, outfile} + + if options.PrintCommands != nil { + options.PrintCommands(cmdLine[0], cmdLine[1:]...) + } + + cmd := exec.Command(cmdLine[0], cmdLine[1:]...) + cmd.Stdout = ioutil.Discard + err := cmd.Run() + if err != nil { + return fmt.Errorf("could not run nrfutil pkg generate: %w", err) + } + return nil +} diff --git a/compileopts/config.go b/compileopts/config.go index d5f39266..d29616f0 100644 --- a/compileopts/config.go +++ b/compileopts/config.go @@ -254,6 +254,11 @@ func (c *Config) BinaryFormat(ext string) string { // More information: // https://github.com/Microsoft/uf2 return "uf2" + case ".zip": + if c.Target.BinaryFormat != "" { + return c.Target.BinaryFormat + } + return "zip" default: // Use the ELF format for unrecognized file formats. return "elf" diff --git a/main.go b/main.go index 88d7cfac..c85feb69 100644 --- a/main.go +++ b/main.go @@ -275,6 +275,8 @@ func Flash(pkgName, port string, options *compileopts.Options) error { fileExt = ".bin" case strings.Contains(config.Target.FlashCommand, "{uf2}"): fileExt = ".uf2" + case strings.Contains(config.Target.FlashCommand, "{zip}"): + fileExt = ".zip" default: return errors.New("invalid target file - did you forget the {hex} token in the 'flash-command' section?") } diff --git a/targets/pca10059.json b/targets/pca10059.json index efab1b6e..5a8eb826 100644 --- a/targets/pca10059.json +++ b/targets/pca10059.json @@ -2,5 +2,6 @@ "inherits": ["nrf52840"], "build-tags": ["pca10059"], "linkerscript": "targets/pca10059.ld", - "flash-command": "nrfutil pkg generate --hw-version 52 --sd-req 0x0 --application {hex} --application-version 1 /tmp/tinygo_$$.zip && nrfutil dfu usb-serial -pkg /tmp/tinygo_$$.zip -p {port} -b 115200 && rm -f /tmp/tinygo_$$.zip" + "binary-format": "nrf-dfu", + "flash-command": "nrfutil dfu usb-serial -pkg {zip} -p {port} -b 115200" }