-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
164 lines (131 loc) · 3.7 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
FirmwareUploader.go - A firmware uploader for the WiFi101 module.
Copyright (c) 2015 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 sara
import (
"fmt"
"github.com/arduino/FirmwareUploader/programmers/bossac"
"github.com/arduino/FirmwareUploader/utils/context"
"io/ioutil"
"log"
"strconv"
"time"
)
var f *Flasher
var payloadSize uint16
var programmer context.Programmer
func Run(ctx *context.Context) {
programmer := bossac.NewBossac(ctx)
if ctx.FWUploaderBinary != "" {
log.Println("Flashing firmware uploader sara")
if err := programmer.Flash(ctx.FWUploaderBinary, nil); err != nil {
log.Fatal(err)
}
}
log.Println("Connecting to programmer")
if _f, err := OpenFlasher(ctx.PortName); err != nil {
log.Fatal(err)
} else {
f = _f
}
time.Sleep(2 * time.Second)
// Synchronize with programmer
log.Println("Sync with programmer")
if err := f.Hello(); err != nil {
log.Fatal(err)
}
// Check maximum supported payload size
log.Println("Reading actual firmware version")
if fwVersion, err := f.GetFwVersion(); err != nil {
log.Fatal(err)
} else {
log.Println("Initial firmware version: " + fwVersion)
}
payloadSize = 128
if ctx.FirmwareFile != "" {
if err := flashFirmware(ctx); err != nil {
log.Fatal(err)
}
}
if fwVersion, err := f.GetFwVersion(); err != nil {
log.Fatal(err)
} else {
log.Println("After applying update firmware version: " + fwVersion)
}
f.Close()
if ctx.BinaryToRestore != "" {
log.Println("Restoring previous sketch")
if err := programmer.Flash(ctx.BinaryToRestore, nil); err != nil {
log.Fatal(err)
}
}
}
func flashFirmware(ctx *context.Context) error {
FirmwareOffset := 0x0000
log.Printf("Flashing firmware from '%v'", ctx.FirmwareFile)
fwData, err := ioutil.ReadFile(ctx.FirmwareFile)
if err != nil {
return err
}
_, err = f.Expect("AT+ULSTFILE", "+ULSTFILE:", 1000)
if err != nil {
return err
}
_, err = f.Expect("AT+UDWNFILE=\"UPDATE.BIN\","+strconv.Itoa(len(fwData))+",\"FOAT\"", ">", 20000)
if err != nil {
return err
}
err = flashChunk(FirmwareOffset, fwData)
if err != nil {
return err
}
time.Sleep(1 * time.Second)
_, err = f.Expect("", "OK", 1000)
if err != nil {
return err
}
_, err = f.Expect("AT+UFWINSTALL", "OK", 60000)
if err != nil {
return err
}
time.Sleep(10 * time.Second)
// wait up to 20 minutes trying to ping the module. After 20 minutes signal the error
start := time.Now()
for time.Since(start) < time.Minute*20 {
err = f.Hello()
if err == nil {
return nil
}
time.Sleep(1 * time.Second)
}
return err
}
func flashChunk(offset int, buffer []byte) error {
chunkSize := int(payloadSize)
bufferLength := len(buffer)
for i := 0; i < bufferLength; i += chunkSize {
fmt.Printf("\rFlashing: " + strconv.Itoa((i*100)/bufferLength) + "%%")
start := i
end := i + chunkSize
if end > bufferLength {
end = bufferLength
}
if err := f.Write(uint32(offset+i), buffer[start:end]); err != nil {
return err
}
//time.Sleep(1 * time.Millisecond)
}
return nil
}