Skip to content

Commit

Permalink
harden the flashing process on win pt2 (#76)
Browse files Browse the repository at this point in the history
* harden upload on windows pt2 (was using port before touch)

* the correct address is used once the board comes back from bootloader mode
  • Loading branch information
umbynos committed Jul 1, 2021
1 parent 0a35f6f commit 0033831
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions cli/firmware/flash.go
Expand Up @@ -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
Expand All @@ -178,10 +167,10 @@ 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
bootloaderPort := address
if board.UploadTouch {
logrus.Info("Putting board into bootloader mode")
newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil)
Expand All @@ -190,10 +179,21 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
}
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}", bootloaderPort)
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)
Expand All @@ -208,15 +208,16 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul

// 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)
Expand Down

0 comments on commit 0033831

Please sign in to comment.