Skip to content

Commit 307c2b2

Browse files
committed
Add flash-firmware command
1 parent b7ff19d commit 307c2b2

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

cli/flash_firmware/flash_firmware.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
FirmwareUploader
3+
Copyright (c) 2021 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package flash_firmware
21+
22+
import (
23+
"bytes"
24+
"os"
25+
26+
"github.com/arduino/FirmwareUploader/flasher"
27+
programmer "github.com/arduino/FirmwareUploader/programmers"
28+
"github.com/arduino/arduino-cli/arduino/serialutils"
29+
"github.com/arduino/arduino-cli/cli/errorcodes"
30+
"github.com/arduino/arduino-cli/cli/feedback"
31+
"github.com/spf13/cobra"
32+
)
33+
34+
var (
35+
fqbn string
36+
address string
37+
module string
38+
)
39+
40+
// NewCommand created a new `version` command
41+
func NewCommand() *cobra.Command {
42+
command := &cobra.Command{
43+
Use: "flash-firmware",
44+
Short: "Shows version number of FirmwareUploader.",
45+
Long: "Shows the version number of FirmwareUploader which is installed on your system.",
46+
Example: " " + os.Args[0] + " version",
47+
Args: cobra.NoArgs,
48+
Run: run,
49+
}
50+
51+
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:samd:mkr1000, arduino:mbed_nano:nanorp2040connect")
52+
command.Flags().StringVarP(&address, "address", "a", "", "Upload port, e.g.: COM10, /dev/ttyACM0")
53+
command.Flags().StringVarP(&module, "module", "m", "", "Firmware module ID, e.g.: WINC1500, NINA")
54+
return command
55+
}
56+
57+
func run(cmd *cobra.Command, args []string) {
58+
if fqbn == "" {
59+
feedback.Errorf("Error during firmware flashing: missing board fqbn")
60+
os.Exit(errorcodes.ErrGeneric)
61+
}
62+
63+
if address == "" {
64+
feedback.Errorf("Error during firmware flashing: missing board address")
65+
os.Exit(errorcodes.ErrGeneric)
66+
}
67+
68+
if module == "" {
69+
// TODO: Get firmware ID for board if not provided
70+
}
71+
72+
// TODO: Get firmware binary from given ID
73+
74+
// TODO: Get uploader executable path
75+
76+
// TODO: Get uploader command line
77+
commandLine := []string{""}
78+
79+
// TODO: Build uploader command line using uploader path, eventual config path and Loader Sketch binary
80+
81+
// TODO: Get 1200bps touch from upload properties
82+
use1200bpsTouch := false
83+
if use1200bpsTouch {
84+
feedback.Print("Putting board into bootloader mode")
85+
// TODO: Get waitForUploadPort from upload properties
86+
waitForUploadPort := false
87+
_, err := serialutils.Reset(address, waitForUploadPort, nil)
88+
if err != nil {
89+
// TODO
90+
}
91+
}
92+
93+
// TODO: Flash loader Sketch
94+
flashOut := new(bytes.Buffer)
95+
flashErr := new(bytes.Buffer)
96+
// TODO: Maybe this can be done differently?
97+
var err error
98+
// TODO: OutputFormat is not stored globally, we must store it globally since we need it
99+
OutputFormat := "json"
100+
if OutputFormat == "json" {
101+
err = programmer.Flash(commandLine, flashOut, flashErr)
102+
} else {
103+
err = programmer.Flash(commandLine, os.Stdout, os.Stderr)
104+
}
105+
if err != nil {
106+
// TODO
107+
}
108+
109+
// Get flasher depending on which module to use
110+
var f flasher.Flasher
111+
switch module {
112+
case "NINA":
113+
f, err = flasher.NewNinaFlasher(address)
114+
case "SARA":
115+
f, err = flasher.NewSaraFlasher(address)
116+
case "WINC":
117+
f, err = flasher.NewWincFlasher(address)
118+
}
119+
if err != nil {
120+
// TODO
121+
}
122+
defer f.Close()
123+
124+
// TODO: Flash firmware
125+
}

0 commit comments

Comments
 (0)