Skip to content

Commit d7fcbb9

Browse files
committed
Added BurnBootloder action
1 parent 2f8cb39 commit d7fcbb9

File tree

10 files changed

+597
-163
lines changed

10 files changed

+597
-163
lines changed

Diff for: cli/burnbootloader/burnbootloader.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package burnbootloader
17+
18+
import (
19+
"context"
20+
"os"
21+
22+
"github.com/arduino/arduino-cli/cli/errorcodes"
23+
"github.com/arduino/arduino-cli/cli/feedback"
24+
"github.com/arduino/arduino-cli/cli/instance"
25+
"github.com/arduino/arduino-cli/commands/upload"
26+
rpc "github.com/arduino/arduino-cli/rpc/commands"
27+
"github.com/arduino/arduino-cli/table"
28+
"github.com/arduino/go-paths-helper"
29+
"github.com/sirupsen/logrus"
30+
"github.com/spf13/cobra"
31+
)
32+
33+
var (
34+
fqbn string
35+
port string
36+
verbose bool
37+
verify bool
38+
importDir string
39+
programmer string
40+
burnBootloader bool
41+
)
42+
43+
// NewCommand created a new `burn-bootloader` command
44+
func NewCommand() *cobra.Command {
45+
burnBootloaderCommand := &cobra.Command{
46+
Use: "burn-bootloader",
47+
Short: "Upload the bootloader.",
48+
Long: "Upload the bootloader on the board using an external programmer.",
49+
Example: " " + os.Args[0] + " burn-bootloader -b arduino:avr:uno -P atmel-ice",
50+
Args: cobra.MaximumNArgs(1),
51+
Run: run,
52+
}
53+
54+
burnBootloaderCommand.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
55+
burnBootloaderCommand.Flags().StringVarP(&port, "port", "p", "", "Upload port, e.g.: COM10 or /dev/ttyACM0")
56+
burnBootloaderCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
57+
burnBootloaderCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Turns on verbose mode.")
58+
burnBootloaderCommand.Flags().StringVarP(&programmer, "programmer", "P", "", "Use the specified programmer to upload or 'list' to list supported programmers.")
59+
60+
return burnBootloaderCommand
61+
}
62+
63+
func run(command *cobra.Command, args []string) {
64+
instance, err := instance.CreateInstance()
65+
if err != nil {
66+
feedback.Errorf("Error during Upload: %v", err)
67+
os.Exit(errorcodes.ErrGeneric)
68+
}
69+
70+
if programmer == "list" {
71+
resp, err := upload.ListProgrammersAvailableForUpload(context.Background(), &rpc.ListProgrammersAvailableForUploadReq{
72+
Instance: instance,
73+
Fqbn: fqbn,
74+
})
75+
if err != nil {
76+
feedback.Errorf("Error listing programmers: %v", err)
77+
os.Exit(errorcodes.ErrGeneric)
78+
}
79+
feedback.PrintResult(&programmersList{
80+
Programmers: resp.GetProgrammers(),
81+
})
82+
os.Exit(0)
83+
}
84+
85+
if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderReq{
86+
Instance: instance,
87+
Fqbn: fqbn,
88+
Port: port,
89+
Verbose: verbose,
90+
Verify: verify,
91+
Programmer: programmer,
92+
}, os.Stdout, os.Stderr); err != nil {
93+
feedback.Errorf("Error during Upload: %v", err)
94+
os.Exit(errorcodes.ErrGeneric)
95+
}
96+
os.Exit(0)
97+
}
98+
99+
// initSketchPath returns the current working directory
100+
func initSketchPath(sketchPath *paths.Path) *paths.Path {
101+
if sketchPath != nil {
102+
return sketchPath
103+
}
104+
105+
wd, err := paths.Getwd()
106+
if err != nil {
107+
feedback.Errorf("Couldn't get current working directory: %v", err)
108+
os.Exit(errorcodes.ErrGeneric)
109+
}
110+
logrus.Infof("Reading sketch from dir: %s", wd)
111+
return wd
112+
}
113+
114+
type programmersList struct {
115+
Programmers []*rpc.Programmer
116+
}
117+
118+
func (p *programmersList) Data() interface{} {
119+
return p.Programmers
120+
}
121+
122+
func (p *programmersList) String() string {
123+
t := table.New()
124+
t.SetHeader("ID", "Programmer Name", "Platform")
125+
for _, prog := range p.Programmers {
126+
t.AddRow(prog.GetId(), prog.GetName(), prog.GetPlatform())
127+
}
128+
return t.Render()
129+
}

Diff for: cli/cli.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/arduino/arduino-cli/cli/board"
26+
"github.com/arduino/arduino-cli/cli/burnbootloader"
2627
"github.com/arduino/arduino-cli/cli/cache"
2728
"github.com/arduino/arduino-cli/cli/compile"
2829
"github.com/arduino/arduino-cli/cli/config"
@@ -80,6 +81,7 @@ func createCliCommandTree(cmd *cobra.Command) {
8081
cmd.AddCommand(sketch.NewCommand())
8182
cmd.AddCommand(upload.NewCommand())
8283
cmd.AddCommand(debug.NewCommand())
84+
cmd.AddCommand(burnbootloader.NewCommand())
8385
cmd.AddCommand(version.NewCommand())
8486

8587
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print the logs on the standard output.")

Diff for: cli/upload/upload.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func NewCommand() *cobra.Command {
5757
uploadCommand.Flags().BoolVarP(&verify, "verify", "t", false, "Verify uploaded binary after the upload.")
5858
uploadCommand.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")
5959
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).")
6160

6261
return uploadCommand
6362
}
@@ -92,15 +91,14 @@ func run(command *cobra.Command, args []string) {
9291

9392
if burnBootloader {
9493
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,
94+
Instance: instance,
95+
Fqbn: fqbn,
96+
SketchPath: sketchPath.String(),
97+
Port: port,
98+
Verbose: verbose,
99+
Verify: verify,
100+
ImportDir: importDir,
101+
Programmer: programmer,
104102
}, os.Stdout, os.Stderr); err != nil {
105103
feedback.Errorf("Error during Upload: %v", err)
106104
os.Exit(errorcodes.ErrGeneric)

Diff for: commands/daemon/daemon.go

+13
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ func (s *ArduinoCoreServerImpl) Upload(req *rpc.UploadReq, stream rpc.ArduinoCor
216216
return stream.Send(resp)
217217
}
218218

219+
// BurnBootloader FIXMEDOC
220+
func (s *ArduinoCoreServerImpl) BurnBootloader(req *rpc.BurnBootloaderReq, stream rpc.ArduinoCore_BurnBootloaderServer) error {
221+
resp, err := upload.BurnBootloader(
222+
stream.Context(), req,
223+
utils.FeedStreamTo(func(data []byte) { stream.Send(&rpc.BurnBootloaderResp{OutStream: data}) }),
224+
utils.FeedStreamTo(func(data []byte) { stream.Send(&rpc.BurnBootloaderResp{ErrStream: data}) }),
225+
)
226+
if err != nil {
227+
return err
228+
}
229+
return stream.Send(resp)
230+
}
231+
219232
// ListProgrammersAvailableForUpload FIXMEDOC
220233
func (s *ArduinoCoreServerImpl) ListProgrammersAvailableForUpload(ctx context.Context, req *rpc.ListProgrammersAvailableForUploadReq) (*rpc.ListProgrammersAvailableForUploadResp, error) {
221234
return upload.ListProgrammersAvailableForUpload(ctx, req)

Diff for: commands/upload/burnbootloader.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package upload
17+
18+
import (
19+
"context"
20+
"io"
21+
22+
"github.com/arduino/arduino-cli/commands"
23+
rpc "github.com/arduino/arduino-cli/rpc/commands"
24+
"github.com/sirupsen/logrus"
25+
)
26+
27+
// BurnBootloader FIXMEDOC
28+
func BurnBootloader(ctx context.Context, req *rpc.BurnBootloaderReq, outStream io.Writer, errStream io.Writer) (*rpc.BurnBootloaderResp, error) {
29+
logrus.
30+
WithField("fqbn", req.GetFqbn()).
31+
WithField("port", req.GetPort()).
32+
WithField("programmer", req.GetProgrammer()).
33+
Trace("BurnBootloader started", req.GetFqbn())
34+
35+
pm := commands.GetPackageManager(req.GetInstance().GetId())
36+
37+
err := runProgramAction(
38+
pm,
39+
nil, // sketch
40+
"", // importDir
41+
req.GetFqbn(),
42+
req.GetPort(),
43+
req.GetProgrammer(),
44+
req.GetVerbose(),
45+
req.GetVerify(),
46+
true, // burnBootloader
47+
outStream,
48+
errStream,
49+
)
50+
if err != nil {
51+
return nil, err
52+
}
53+
return &rpc.BurnBootloaderResp{}, nil
54+
}

0 commit comments

Comments
 (0)