@@ -35,6 +35,7 @@ import (
35
35
"github.com/arduino/arduino-cli/arduino/serialutils"
36
36
"github.com/arduino/arduino-cli/cli/errorcodes"
37
37
"github.com/arduino/arduino-cli/cli/feedback"
38
+ "github.com/arduino/go-paths-helper"
38
39
"github.com/arduino/go-properties-orderedmap"
39
40
"github.com/sirupsen/logrus"
40
41
"github.com/spf13/cobra"
44
45
fqbn string
45
46
address string
46
47
module string
48
+ retries uint8
47
49
)
48
50
49
51
// NewCommand created a new `version` command
@@ -63,6 +65,7 @@ func NewFlashCommand() *cobra.Command {
63
65
command .Flags ().StringVarP (& fqbn , "fqbn" , "b" , "" , "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect" )
64
66
command .Flags ().StringVarP (& address , "address" , "a" , "" , "Upload port, e.g.: COM10, /dev/ttyACM0" )
65
67
command .Flags ().StringVarP (& module , "module" , "m" , "" , "Firmware module ID, e.g.: WINC1500, NINA" )
68
+ command .Flags ().Uint8Var (& retries , "retries" , 9 , "Number of retries in case of upload failure (default 9)" )
66
69
return command
67
70
}
68
71
@@ -159,14 +162,31 @@ func run(cmd *cobra.Command, args []string) {
159
162
os .Exit (errorcodes .ErrGeneric )
160
163
}
161
164
165
+ for retry := 1 ; retry <= int (retries ); retry ++ {
166
+ err = updateFirmware (board , commandLine , moduleName , firmwareFile )
167
+ if err == nil {
168
+ logrus .Info ("Operation completed: success! :-)" )
169
+ break
170
+ }
171
+ feedback .Error (err )
172
+ if retry == int (retries ) {
173
+ logrus .Fatal ("Operation failed. :-(" )
174
+ }
175
+ logrus .Info ("Waiting 1 second before retrying..." )
176
+ time .Sleep (time .Second )
177
+ logrus .Infof ("Retrying upload (%d of %d)" , retry , retries )
178
+ }
179
+ }
180
+
181
+ func updateFirmware (board * firmwareindex.IndexBoard , commandLine []string , moduleName string , firmwareFile * paths.Path ) error {
182
+ var err error
162
183
// Check if board needs a 1200bps touch for upload
163
184
uploadPort := address
164
185
if board .UploadTouch {
165
186
logrus .Info ("Putting board into bootloader mode" )
166
187
newUploadPort , err := serialutils .Reset (address , board .UploadWait , nil )
167
188
if err != nil {
168
- feedback .Errorf ("Error during firmware flashing: missing board address" )
169
- os .Exit (errorcodes .ErrGeneric )
189
+ return fmt .Errorf ("error during firmware flashing: missing board address. %s" , err )
170
190
}
171
191
if newUploadPort != "" {
172
192
logrus .Infof ("Found port to upload Loader: %s" , newUploadPort )
@@ -183,8 +203,7 @@ func run(cmd *cobra.Command, args []string) {
183
203
err = programmer .Flash (commandLine , os .Stdout , os .Stderr )
184
204
}
185
205
if err != nil {
186
- feedback .Errorf ("Error during firmware flashing: %s" , err )
187
- os .Exit (errorcodes .ErrGeneric )
206
+ return fmt .Errorf ("error during loader sketch flashing: %s" , err )
188
207
}
189
208
190
209
// Wait a bit after flashing the loader sketch for the board to become
@@ -202,10 +221,12 @@ func run(cmd *cobra.Command, args []string) {
202
221
f , err = flasher .NewWincFlasher (uploadPort )
203
222
default :
204
223
err = fmt .Errorf ("unknown module: %s" , moduleName )
224
+ feedback .Errorf ("Error during firmware flashing: %s" , err )
225
+ os .Exit (errorcodes .ErrGeneric )
205
226
}
206
227
if err != nil {
207
228
feedback .Errorf ("Error during firmware flashing: %s" , err )
208
- os . Exit ( errorcodes . ErrGeneric )
229
+ return err
209
230
}
210
231
defer f .Close ()
211
232
@@ -218,7 +239,6 @@ func run(cmd *cobra.Command, args []string) {
218
239
err = f .FlashFirmware (firmwareFile , os .Stdout )
219
240
}
220
241
if err != nil {
221
- feedback .Errorf ("Error during firmware flashing: %s" , err )
222
242
flasherErr .Write ([]byte (fmt .Sprintf ("Error during firmware flashing: %s" , err )))
223
243
}
224
244
@@ -233,8 +253,8 @@ func run(cmd *cobra.Command, args []string) {
233
253
Stderr : flasherErr .String (),
234
254
}),
235
255
})
236
- // Exit if something went wrong but after printing
237
256
if err != nil {
238
- os . Exit ( errorcodes . ErrGeneric )
257
+ return fmt . Errorf ( "error during firmware flashing: %s" , err )
239
258
}
259
+ return nil
240
260
}
0 commit comments