Skip to content

harden the flashing process on win pt2 #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions cli/firmware/flash.go
Original file line number Diff line number Diff line change
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