-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathflasher.go
114 lines (92 loc) · 2.72 KB
/
flasher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
FirmwareUploader
Copyright (c) 2021 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package flasher
import (
"fmt"
"io"
"time"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"go.bug.st/serial"
)
type CommandData struct {
Command byte
Address uint32
Value uint32
Payload []byte
}
func (d CommandData) String() string {
return fmt.Sprintf("%+v, %+v, %+v, %+v", d.Command, d.Address, d.Value, d.Payload)
}
type FlasherError struct {
err string
}
func (e FlasherError) Error() string {
return e.err
}
type Flasher interface {
FlashFirmware(firmwareFile *paths.Path, flasherOut io.Writer) error
FlashCertificates(certificatePaths *paths.PathList, URLs []string, flasherOut io.Writer) error
Close() error
hello() error
write(address uint32, buffer []byte) error
flashChunk(offset int, buffer []byte) error
getMaximumPayloadSize() (uint16, error)
serialFillBuffer(buffer []byte) error
sendCommand(data CommandData) error
}
// http://www.ni.com/product-documentation/54548/en/
// Standard baud rates supported by most serial ports
var baudRates = []int{
115200,
57600,
56000,
38400,
}
func openSerial(portAddress string) (serial.Port, error) {
var lastError error
for _, baudRate := range baudRates {
port, err := serial.Open(portAddress, &serial.Mode{BaudRate: baudRate})
if err != nil {
lastError = err
// Try another baudrate
continue
}
logrus.Infof("Opened port %s at %d", portAddress, baudRate)
if err := port.SetReadTimeout(30 * time.Second); err != nil {
err = fmt.Errorf("could not set timeout on serial port: %s", err)
logrus.Error(err)
return nil, err
}
return port, nil
}
return nil, lastError
}
type FlashResult struct {
Programmer *ExecOutput `json:"programmer"`
Flasher *ExecOutput `json:"flasher"`
}
type ExecOutput struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
}
func (r *FlashResult) Data() interface{} {
return r
}
func (r *FlashResult) String() string {
// The output is already printed via os.Stdout/os.Stdin
return ""
}