Skip to content

Commit 0033831

Browse files
authored
harden the flashing process on win pt2 (#76)
* harden upload on windows pt2 (was using port before touch) * the correct address is used once the board comes back from bootloader mode
1 parent 0a35f6f commit 0033831

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

cli/firmware/flash.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,8 @@ func run(cmd *cobra.Command, args []string) {
151151

152152
loaderSketch := strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "")
153153

154-
uploaderCommand := board.GetUploaderCommand()
155-
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String()))
156-
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", address)
157-
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch)
158-
159-
commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
160-
if err != nil {
161-
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
162-
os.Exit(errorcodes.ErrGeneric)
163-
}
164-
165154
for retry := 1; retry <= int(retries); retry++ {
166-
err = updateFirmware(board, commandLine, moduleName, firmwareFile)
155+
err = updateFirmware(board, loaderSketch, moduleName, uploadToolDir, firmwareFile)
167156
if err == nil {
168157
logrus.Info("Operation completed: success! :-)")
169158
break
@@ -178,10 +167,10 @@ func run(cmd *cobra.Command, args []string) {
178167
}
179168
}
180169

181-
func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, moduleName string, firmwareFile *paths.Path) error {
170+
func updateFirmware(board *firmwareindex.IndexBoard, loaderSketch, moduleName string, uploadToolDir, firmwareFile *paths.Path) error {
182171
var err error
183172
// Check if board needs a 1200bps touch for upload
184-
uploadPort := address
173+
bootloaderPort := address
185174
if board.UploadTouch {
186175
logrus.Info("Putting board into bootloader mode")
187176
newUploadPort, err := serialutils.Reset(address, board.UploadWait, nil)
@@ -190,10 +179,21 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
190179
}
191180
if newUploadPort != "" {
192181
logrus.Infof("Found port to upload Loader: %s", newUploadPort)
193-
uploadPort = newUploadPort
182+
bootloaderPort = newUploadPort
194183
}
195184
}
196185

186+
uploaderCommand := board.GetUploaderCommand()
187+
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{tool_dir}", filepath.FromSlash(uploadToolDir.String()))
188+
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{serial.port.file}", bootloaderPort)
189+
uploaderCommand = strings.ReplaceAll(uploaderCommand, "{loader.sketch}", loaderSketch)
190+
191+
commandLine, err := properties.SplitQuotedString(uploaderCommand, "\"", false)
192+
if err != nil {
193+
feedback.Errorf(`Error splitting command line "%s": %s`, uploaderCommand, err)
194+
os.Exit(errorcodes.ErrGeneric)
195+
}
196+
197197
// Flash loader Sketch
198198
programmerOut := new(bytes.Buffer)
199199
programmerErr := new(bytes.Buffer)
@@ -208,15 +208,16 @@ func updateFirmware(board *firmwareindex.IndexBoard, commandLine []string, modul
208208

209209
// Wait a bit after flashing the loader sketch for the board to become
210210
// available again.
211-
time.Sleep(2 * time.Second)
211+
time.Sleep(3 * time.Second)
212212

213213
// Get flasher depending on which module to use
214214
var f flasher.Flasher
215215
switch moduleName {
216216
case "NINA":
217-
f, err = flasher.NewNinaFlasher(uploadPort)
217+
// we use address and not bootloaderPort because the board should not be in bootloader mode
218+
f, err = flasher.NewNinaFlasher(address)
218219
case "WINC1500":
219-
f, err = flasher.NewWincFlasher(uploadPort)
220+
f, err = flasher.NewWincFlasher(address)
220221
default:
221222
err = fmt.Errorf("unknown module: %s", moduleName)
222223
feedback.Errorf("Error during firmware flashing: %s", err)

0 commit comments

Comments
 (0)