Skip to content

Commit 32a72e0

Browse files
reboot logic now read the fw version to decide which logic to apply
1 parent 0023bef commit 32a72e0

File tree

3 files changed

+37
-42
lines changed

3 files changed

+37
-42
lines changed

hid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func rebootUsingHID() error {
2121
b[0] = 0
2222
b[1] = 0xAA
2323
if _, err := d.SendFeatureReport(b); err != nil {
24-
return err
24+
return fmt.Errorf("send HID command: %v", err)
2525
}
2626

2727
return nil

main.go

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/arduino/uno-r4-wifi-fwuploader-plugin/certificate"
1515
"github.com/arduino/uno-r4-wifi-fwuploader-plugin/serial"
1616
semver "go.bug.st/relaxed-semver"
17+
serialx "go.bug.st/serial"
1718
)
1819

1920
const (
@@ -138,24 +139,13 @@ func (p *unoR4WifiPlugin) GetFirmwareVersion(portAddress string, feedback *helpe
138139
return nil, err
139140
}
140141

141-
port, err := serial.Open(serial.Port(portAddress))
142+
port, err := serial.Open(portAddress)
142143
if err != nil {
143144
return nil, err
144145
}
145146
defer port.Close()
146147

147-
if _, err := port.Write([]byte(string(serial.VersionCommand))); err != nil {
148-
return nil, fmt.Errorf("write to serial port: %v", err)
149-
}
150-
151-
var version string
152-
scanner := bufio.NewScanner(port)
153-
for scanner.Scan() {
154-
version = scanner.Text()
155-
break
156-
}
157-
158-
return semver.ParseRelaxed(version), nil
148+
return getFirmwareVersion(port)
159149
}
160150

161151
func (p *unoR4WifiPlugin) reboot(portAddress *string, feedback *helper.PluginFeedback) error {
@@ -169,48 +159,40 @@ func (p *unoR4WifiPlugin) reboot(portAddress *string, feedback *helper.PluginFee
169159
return fmt.Errorf("upload commands sketch: %v", err)
170160
}
171161

172-
port, err := serial.Open(serial.Port(*portAddress))
162+
fmt.Fprintf(feedback.Out(), "\nWaiting to flash the binary...\n")
163+
164+
port, err := serial.Open(*portAddress)
173165
if err != nil {
174166
return err
175167
}
176-
if err := serial.SendCommandAndClose(port, serial.RebootCommand); err != nil {
177-
return err
178-
}
179-
180-
fmt.Fprintf(feedback.Out(), "Waiting to flash the binary...\n")
181168

182-
time.Sleep(3 * time.Second)
183-
184-
// On Windows, when a board is successfully rebooted in esp32 mode, it will change the serial port.
185-
// Every 250ms we're watching for new ports, if a new one is found we return that otherwise
186-
// we'll wait the the 10 seconds timeout expiration.
187-
newPort, changed, err := allSerialPorts.NewPort()
169+
// Get version to decide if we need to reboot with hid or not
170+
version, err := getFirmwareVersion(port)
188171
if err != nil {
189172
return err
190173
}
191-
if changed {
192-
*portAddress = newPort
193-
}
194-
195-
// Older firmware version (v0.1.0) do not support rebooting using the command sketch.
196-
// So we use HID to reboot. We're consciosly ignoring the error because for boards
197-
// running a firmware >= v0.2.0 will alaways throw an HID error as we're already in
198-
// esp32 mode.
199-
_ = rebootUsingHID()
200174

201-
time.Sleep(3 * time.Second)
175+
// Older firmware version (v0.1.0) can be rebooted only with HID.
176+
if version.LessThanOrEqual(semver.ParseRelaxed("0.1.0")) {
177+
if err := rebootUsingHID(); err != nil {
178+
return err
179+
}
180+
} else {
181+
if err := serial.SendCommandAndClose(port, serial.RebootCommand); err != nil {
182+
return err
183+
}
184+
}
202185

203-
// On Windows, when a board is successfully rebooted in esp32 mode, it will change the serial port.
186+
// When a board is successfully rebooted in esp32 mode, it might change the serial port.
204187
// Every 250ms we're watching for new ports, if a new one is found we return that otherwise
205188
// we'll wait the the 10 seconds timeout expiration.
206-
newPort, changed, err = allSerialPorts.NewPort()
189+
newPort, changed, err := allSerialPorts.NewPort()
207190
if err != nil {
208191
return err
209192
}
210193
if changed {
211194
*portAddress = newPort
212195
}
213-
214196
return nil
215197
}
216198

@@ -241,3 +223,18 @@ func (p *unoR4WifiPlugin) uploadCommandsSketch(portAddress string, feedback *hel
241223
time.Sleep(1 * time.Second)
242224
return nil
243225
}
226+
227+
func getFirmwareVersion(port serialx.Port) (*semver.RelaxedVersion, error) {
228+
if _, err := port.Write([]byte(string(serial.VersionCommand))); err != nil {
229+
return nil, fmt.Errorf("write to serial port: %v", err)
230+
}
231+
232+
var version string
233+
scanner := bufio.NewScanner(port)
234+
for scanner.Scan() {
235+
version = scanner.Text()
236+
break
237+
}
238+
239+
return semver.ParseRelaxed(version), nil
240+
}

serial/serial.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ const (
1414
VersionCommand Command = "v\n\r"
1515
)
1616

17-
type Port string
18-
19-
func Open(portAddress Port) (serial.Port, error) {
17+
func Open(portAddress string) (serial.Port, error) {
2018
return serial.Open(string(portAddress), &serial.Mode{
2119
BaudRate: 9600,
2220
Parity: serial.NoParity,

0 commit comments

Comments
 (0)