Skip to content

Commit 688ad63

Browse files
committed
Implemented burn-bootloader
1 parent 13cf07f commit 688ad63

File tree

4 files changed

+120
-56
lines changed

4 files changed

+120
-56
lines changed

cli/upload/upload.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ import (
3131
)
3232

3333
var (
34-
fqbn string
35-
port string
36-
verbose bool
37-
verify bool
38-
importDir string
39-
programmer string
34+
fqbn string
35+
port string
36+
verbose bool
37+
verify bool
38+
importDir string
39+
programmer string
40+
burnBootloader bool
4041
)
4142

4243
// NewCommand created a new `upload` command
@@ -56,6 +57,7 @@ func NewCommand() *cobra.Command {
5657
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
5758
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")
5859
uploadCommand.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload or 'list' to list supported programmers.")
60+
uploadCommand.Flags().BoolVar(&burnBootloader, "burn-bootloader", false, "Burn bootloader (some platforms performs chip-erase too).")
5961

6062
return uploadCommand
6163
}
@@ -88,6 +90,24 @@ func run(command *cobra.Command, args []string) {
8890
}
8991
sketchPath := initSketchPath(path)
9092

93+
if burnBootloader {
94+
if _, err := upload.Upload(context.Background(), &rpc.UploadReq{
95+
Instance: instance,
96+
Fqbn: fqbn,
97+
SketchPath: sketchPath.String(),
98+
Port: port,
99+
Verbose: verbose,
100+
Verify: verify,
101+
ImportDir: importDir,
102+
Programmer: programmer,
103+
BurnBootloader: true,
104+
}, os.Stdout, os.Stderr); err != nil {
105+
feedback.Errorf("Error during Upload: %v", err)
106+
os.Exit(errorcodes.ErrGeneric)
107+
}
108+
os.Exit(0)
109+
}
110+
91111
if _, err := upload.Upload(context.Background(), &rpc.UploadReq{
92112
Instance: instance,
93113
Fqbn: fqbn,

commands/upload/upload.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
9090
var uploadToolName string
9191
var uploadToolPlatform *cores.PlatformRelease
9292
var programmer *cores.Programmer
93-
if programmerID := req.GetProgrammer(); programmerID != "" {
93+
94+
burnBootloader := req.GetBurnBootloader()
95+
if burnBootloader {
96+
uploadToolName = boardProperties.Get("bootloader.tool")
97+
uploadToolPlatform = boardPlatform
98+
if uploadToolName == "" {
99+
return nil, fmt.Errorf("cannot get programmer tool: undefined 'bootloader.tool' in boards.txt")
100+
}
101+
} else if programmerID := req.GetProgrammer(); programmerID != "" {
94102
programmer = boardPlatform.Programmers[programmerID]
95103
if programmer == nil {
96104
// Try to find the programmer in the referenced build platform
@@ -153,13 +161,25 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
153161
if v, ok := uploadProperties.GetOk("program.params.verbose"); ok {
154162
uploadProperties.Set("program.verbose", v)
155163
}
164+
if v, ok := uploadProperties.GetOk("erase.params.verbose"); ok {
165+
uploadProperties.Set("erase.verbose", v)
166+
}
167+
if v, ok := uploadProperties.GetOk("bootloader.params.verbose"); ok {
168+
uploadProperties.Set("bootloader.verbose", v)
169+
}
156170
} else {
157171
if v, ok := uploadProperties.GetOk("upload.params.quiet"); ok {
158172
uploadProperties.Set("upload.verbose", v)
159173
}
160174
if v, ok := uploadProperties.GetOk("program.params.quiet"); ok {
161175
uploadProperties.Set("program.verbose", v)
162176
}
177+
if v, ok := uploadProperties.GetOk("erase.params.quiet"); ok {
178+
uploadProperties.Set("erase.verbose", v)
179+
}
180+
if v, ok := uploadProperties.GetOk("bootloader.params.quiet"); ok {
181+
uploadProperties.Set("bootloader.verbose", v)
182+
}
163183
}
164184

165185
// Set properties for verify
@@ -172,29 +192,31 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
172192
}
173193

174194
var importPath *paths.Path
175-
if importDir := req.GetImportDir(); importDir != "" {
176-
importPath = paths.New(importDir)
177-
} else {
178-
// TODO: Create a function to obtain importPath from sketch
179-
importPath = sketch.FullPath
180-
// Add FQBN (without configs part) to export path
181-
fqbnSuffix := strings.Replace(fqbn.StringWithoutConfig(), ":", ".", -1)
182-
importPath = importPath.Join("build").Join(fqbnSuffix)
183-
}
195+
if !burnBootloader {
196+
if importDir := req.GetImportDir(); importDir != "" {
197+
importPath = paths.New(importDir)
198+
} else {
199+
// TODO: Create a function to obtain importPath from sketch
200+
importPath = sketch.FullPath
201+
// Add FQBN (without configs part) to export path
202+
fqbnSuffix := strings.Replace(fqbn.StringWithoutConfig(), ":", ".", -1)
203+
importPath = importPath.Join("build").Join(fqbnSuffix)
204+
}
184205

185-
if !importPath.Exist() {
186-
return nil, fmt.Errorf("compiled sketch not found in %s", importPath)
187-
}
188-
if !importPath.IsDir() {
189-
return nil, fmt.Errorf("expected compiled sketch in directory %s, but is a file instead", importPath)
206+
if !importPath.Exist() {
207+
return nil, fmt.Errorf("compiled sketch not found in %s", importPath)
208+
}
209+
if !importPath.IsDir() {
210+
return nil, fmt.Errorf("expected compiled sketch in directory %s, but is a file instead", importPath)
211+
}
212+
uploadProperties.SetPath("build.path", importPath)
213+
uploadProperties.Set("build.project_name", sketch.Name+".ino")
190214
}
191-
uploadProperties.SetPath("build.path", importPath)
192-
uploadProperties.Set("build.project_name", sketch.Name+".ino")
193215

194216
// If not using programmer perform some action required
195217
// to set the board in bootloader mode
196218
actualPort := port
197-
if programmer == nil {
219+
if programmer == nil && !burnBootloader {
198220
// Perform reset via 1200bps touch if requested
199221
if uploadProperties.GetBoolean("upload.use_1200bps_touch") {
200222
ports, err := serial.GetPortsList()
@@ -250,8 +272,14 @@ func Upload(ctx context.Context, req *rpc.UploadReq, outStream io.Writer, errStr
250272
}
251273

252274
// Build recipe for upload
253-
var recipe string
254-
if programmer != nil {
275+
if burnBootloader {
276+
if err := runTool("erase.pattern", uploadProperties, outStream, errStream, req.GetVerbose()); err != nil {
277+
return nil, fmt.Errorf("chip erase error: %s", err)
278+
}
279+
if err := runTool("bootloader.pattern", uploadProperties, outStream, errStream, req.GetVerbose()); err != nil {
280+
return nil, fmt.Errorf("burn bootloader error: %s", err)
281+
}
282+
} else if programmer != nil {
255283
if err := runTool("program.pattern", uploadProperties, outStream, errStream, req.GetVerbose()); err != nil {
256284
return nil, fmt.Errorf("programming error: %s", err)
257285
}

rpc/commands/upload.pb.go

Lines changed: 42 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/commands/upload.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ message UploadReq {
4444
// not specified, the standard build directory under `sketch_path` is used.
4545
string import_dir = 8;
4646
string programmer = 9;
47+
// Set to true if you want to program the bootloader. This option requires a
48+
// programmer. If sketch_path and import_path are empty only the bootloader is
49+
// uploaded.
50+
bool burn_bootloader = 10;
4751
}
4852

4953
message UploadResp {

0 commit comments

Comments
 (0)