From 106fb08a6ff4cef3c45aaa505aaf467903871cc5 Mon Sep 17 00:00:00 2001 From: Umberto Baldi Date: Thu, 1 Jul 2021 15:55:30 +0200 Subject: [PATCH 1/2] harden upload on windows pt2 (was using port before touch) --- cli/firmware/flash.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cli/firmware/flash.go b/cli/firmware/flash.go index aa0ecddb..f29f8138 100644 --- a/cli/firmware/flash.go +++ b/cli/firmware/flash.go @@ -151,19 +151,8 @@ func run(cmd *cobra.Command, args []string) { loaderSketch := strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "") - uploaderCommand := board.GetUploaderCommand() - uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String())) - uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", address) - uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch) - - commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false) - if err != nil { - feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err) - os.Exit(errorcodes.ErrGeneric) - } - for retry := 1; retry <= int(retries); retry++ { - err = updateFirmware(board, commandLine, moduleName, firmwareFile) + err = updateFirmware(board, loaderSketch, moduleName, uploadToolDir, firmwareFile) if err == nil { logrus.Info("Operation completed: success! :-)") break @@ -178,13 +167,13 @@ func run(cmd *cobra.Command, args []string) { } } -func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, moduleName string, firmwareFile *paths.Path) error { +func updateFirmware(board *firmwareindex.IndexBoard, loaderSketch, moduleName string, uploadToolDir, firmwareFile *paths.Path) error { var err error // Check if board needs a 1200bps touch for upload uploadPort := address if board.UploadTouch { logrus.Info("Putting board into bootloader mode") - newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil) + newUploadPort, err := serialutils.Reset(uploadPort, board.UploadWait, nil) if err != nil { return fmt.Errorf("error during firmware flashing: missing board address. %s", err) } @@ -194,6 +183,17 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul } } + uploaderCommand := board.GetUploaderCommand() + uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String())) + uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", uploadPort) + uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch) + + commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false) + if err != nil { + feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err) + os.Exit(errorcodes.ErrGeneric) + } + // Flash loader Sketch programmerOut := new(bytes.Buffer) programmerErr := new(bytes.Buffer) From 1dc10a815ed539d12febf60d993191e352a8158a Mon Sep 17 00:00:00 2001 From: Umberto Baldi Date: Thu, 1 Jul 2021 17:41:08 +0200 Subject: [PATCH 2/2] the correct address is used once the board comes back from bootloader mode --- cli/firmware/flash.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cli/firmware/flash.go b/cli/firmware/flash.go index f29f8138..e7d78355 100644 --- a/cli/firmware/flash.go +++ b/cli/firmware/flash.go @@ -170,22 +170,22 @@ func run(cmd *cobra.Command, args []string) { func updateFirmware(board *firmwareindex.IndexBoard, loaderSketch, moduleName string, uploadToolDir, firmwareFile *paths.Path) error { var err error // Check if board needs a 1200bps touch for upload - uploadPort := address + bootloaderPort := address if board.UploadTouch { logrus.Info("Putting board into bootloader mode") - newUploadPort, err := serialutils.Reset(uploadPort, board.UploadWait, nil) + newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil) if err != nil { return fmt.Errorf("error during firmware flashing: missing board address. %s", err) } if newUploadPort != "" { logrus.Infof("Found port to upload Loader: %s", newUploadPort) - uploadPort = newUploadPort + bootloaderPort = newUploadPort } } uploaderCommand := board.GetUploaderCommand() uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String())) - uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", uploadPort) + uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", bootloaderPort) uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch) commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false) @@ -208,15 +208,16 @@ func updateFirmware(board *firmwareindex.IndexBoard, loaderSketch, moduleName st // Wait a bit after flashing the loader sketch for the board to become // available again. - time.Sleep(2 * time.Second) + time.Sleep(3 * time.Second) // Get flasher depending on which module to use var f flasher.Flasher switch moduleName { case "NINA": - f, err = flasher.NewNinaFlasher(uploadPort) + // we use address and not bootloaderPort because the board should not be in bootloader mode + f, err = flasher.NewNinaFlasher(address) case "WINC1500": - f, err = flasher.NewWincFlasher(uploadPort) + f, err = flasher.NewWincFlasher(address) default: err = fmt.Errorf("unknown module: %s", moduleName) feedback.Errorf("Error during firmware flashing: %s", err)