@@ -2,78 +2,76 @@ package main
2
2
3
3
import (
4
4
"bufio"
5
+ "flag"
5
6
"fmt"
7
+ "github.com/kardianos/osext"
6
8
"github.com/mattn/go-shellwords"
7
9
"io"
8
10
"io/ioutil"
9
11
"os"
10
12
"os/exec"
11
13
"path/filepath"
12
14
"runtime"
15
+ "strconv"
13
16
"strings"
14
17
"time"
15
18
)
16
19
17
- var verbose bool
20
+ var (
21
+ verbose = flag .Bool ("v" , false , "Show verbose logging" )
22
+ quiet = flag .Bool ("q" , true , "Show quiet logging" )
23
+ force = flag .Bool ("f" , false , "Force firmware update" )
24
+ dfu_path = flag .String ("dfu" , "" , "Location of dfu-util binaries" )
25
+ bin_file_name = flag .String ("bin" , "" , "Location of sketch binary" )
26
+ com_port = flag .String ("port" , "" , "Upload serial port" )
27
+ ble_compliance_string = flag .String ("ble_fw_str" , "" , "BLE FW ID string" )
28
+ ble_compliance_offset = flag .Int ("ble_fw_pos" , 0 , "BLE FW ID offset" )
29
+ rtos_compliance_string = flag .String ("rots_fw_str" , "" , "RTOS FW ID string" )
30
+ rtos_compliance_offset = flag .Int ("rtos_fw_pos" , 0 , "RTOS FW ID offset" )
31
+ )
32
+
33
+ const Version = "1.8.0"
34
+
35
+ const dfu_flags = "-d,8087:0ABA"
36
+ const rtos_firmware = "quark.bin"
37
+ const ble_firmware = "ble_core.bin"
18
38
19
39
func PrintlnVerbose (a ... interface {}) {
20
- if verbose {
40
+ if * verbose {
21
41
fmt .Println (a ... )
22
42
}
23
43
}
24
44
25
- func main_load (args [] string ) {
45
+ func main_load () {
26
46
27
- // ARG 1: Path to binaries
28
- // ARG 2: BIN File to download
29
- // ARG 3: TTY port to use.
30
- // ARG 4: quiet/verbose
31
- // path may contain \ need to change all to /
32
-
33
- if len (args ) < 4 {
34
- fmt .Println ("Not enough arguments" )
47
+ if * dfu_path == "" {
48
+ fmt .Println ("Need to specify dfu-util location" )
35
49
os .Exit (1 )
36
50
}
37
51
38
- bin_path := args [0 ]
39
- dfu := bin_path + "/dfu-util"
40
- dfu = filepath .ToSlash (dfu )
41
- dfu_flags := "-d,8087:0ABA"
42
-
43
- bin_file_name := args [1 ]
44
-
45
- com_port := args [2 ]
46
- verbosity := args [3 ]
47
-
48
- ble_compliance_string := ""
49
- ble_compliance_offset := ""
50
- if len (args ) >= 5 {
51
- // Called by post 1.0.6 platform.txt
52
- ble_compliance_string = args [4 ]
53
- ble_compliance_offset = args [5 ]
52
+ if * bin_file_name == "" && * force == false {
53
+ fmt .Println ("Need to specify a binary location or force FW update" )
54
+ os .Exit (1 )
54
55
}
55
56
56
- if verbosity == "quiet" {
57
- verbose = false
58
- } else {
59
- verbose = true
60
- }
57
+ dfu := * dfu_path + "/dfu-util"
58
+ dfu = filepath .ToSlash (dfu )
61
59
62
- PrintlnVerbose ("Args to shell:" , args )
63
- PrintlnVerbose ("Serial Port: " + com_port )
64
- PrintlnVerbose ("BIN FILE " + bin_file_name )
60
+ PrintlnVerbose ("Serial Port: " + * com_port )
61
+ PrintlnVerbose ("BIN FILE " + * bin_file_name )
65
62
66
63
counter := 0
67
64
board_found := false
68
65
69
66
if runtime .GOOS == "darwin" {
70
67
library_path := os .Getenv ("DYLD_LIBRARY_PATH" )
71
- if ! strings .Contains (library_path , bin_path ) {
72
- os .Setenv ("DYLD_LIBRARY_PATH" , bin_path + ":" + library_path )
68
+ if ! strings .Contains (library_path , * dfu_path ) {
69
+ os .Setenv ("DYLD_LIBRARY_PATH" , * dfu_path + ":" + library_path )
73
70
}
74
71
}
75
72
76
73
dfu_search_command := []string {dfu , dfu_flags , "-l" }
74
+ var err error
77
75
78
76
for counter < 100 && board_found == false {
79
77
if counter % 10 == 0 {
@@ -98,11 +96,14 @@ func main_load(args []string) {
98
96
}
99
97
100
98
if board_found == false {
101
- fmt .Println ("ERROR: Timed out waiting for Arduino 101 on " + com_port )
99
+ fmt .Println ("ERROR: Timed out waiting for Arduino 101 on " + * com_port )
102
100
os .Exit (1 )
103
101
}
104
102
105
- if ble_compliance_string != "" {
103
+ needUpdateRTOS := false
104
+ needUpdateBLE := false
105
+
106
+ if * ble_compliance_string != "" {
106
107
107
108
// obtain a temporary filename
108
109
tmpfile , _ := ioutil .TempFile (os .TempDir (), "dfu" )
@@ -121,7 +122,7 @@ func main_load(args []string) {
121
122
os .Remove (tmpfile .Name ())
122
123
123
124
// download a piece of BLE firmware
124
- dfu_ble_dump_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "8" , "-K" , ble_compliance_offset }
125
+ dfu_ble_dump_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "8" , "-K" , strconv . Itoa ( * ble_compliance_offset ) }
125
126
126
127
err , _ , _ = launchCommandAndWaitForOutput (dfu_ble_dump_command , "" , false )
127
128
if err != nil {
@@ -130,29 +131,115 @@ func main_load(args []string) {
130
131
}
131
132
132
133
// check for BLE library compliance
133
- PrintlnVerbose ("Verifying BLE version:" , ble_compliance_string )
134
- found := searchBLEversionInDFU (tmpfile .Name (), ble_compliance_string )
134
+ PrintlnVerbose ("Verifying BLE version:" , * ble_compliance_string )
135
+ found := searchVersionInDFU (tmpfile .Name (), * ble_compliance_string )
136
+
137
+ // remove the temporary file
138
+ os .Remove (tmpfile .Name ())
139
+
140
+ if ! found {
141
+ needUpdateBLE = true
142
+ } else {
143
+ PrintlnVerbose ("BLE version: verified" )
144
+ }
145
+ }
146
+
147
+ if * rtos_compliance_string != "" {
148
+
149
+ // obtain a temporary filename
150
+ tmpfile , _ := ioutil .TempFile (os .TempDir (), "dfu" )
151
+ tmpfile .Close ()
152
+ os .Remove (tmpfile .Name ())
153
+
154
+ // reset DFU interface counter
155
+ dfu_reset_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "2" , "-K" , "1" }
156
+
157
+ err , _ , _ := launchCommandAndWaitForOutput (dfu_reset_command , "" , false )
158
+ if err != nil {
159
+ fmt .Println (err )
160
+ os .Exit (1 )
161
+ }
162
+
163
+ os .Remove (tmpfile .Name ())
164
+
165
+ // download a piece of RTOS firmware
166
+ dfu_rtos_dump_command := []string {dfu , dfu_flags , "-U" , tmpfile .Name (), "--alt" , "2" , "-K" , strconv .Itoa (* rtos_compliance_offset )}
167
+
168
+ err , _ , _ = launchCommandAndWaitForOutput (dfu_rtos_dump_command , "" , false )
169
+ if err != nil {
170
+ fmt .Println (err )
171
+ os .Exit (1 )
172
+ }
173
+
174
+ // check for BLE library compliance
175
+ PrintlnVerbose ("Verifying RTOS version:" , * rtos_compliance_string )
176
+ found := searchVersionInDFU (tmpfile .Name (), * rtos_compliance_string )
135
177
136
178
// remove the temporary file
137
179
os .Remove (tmpfile .Name ())
138
180
139
181
if ! found {
140
- fmt .Println ("!! BLE firmware version is not in sync with CurieBLE library !!" )
141
- fmt .Println ("* Set Programmer to \" Arduino/Genuino 101 Firmware Updater\" " )
142
- fmt .Println ("* Update it using \" Burn Bootloader\" menu" )
182
+ needUpdateRTOS = true
183
+ } else {
184
+ PrintlnVerbose ("RTOS version: verified" )
185
+ }
186
+ }
187
+
188
+ executablePath , _ := osext .ExecutableFolder ()
189
+ firmwarePath := executablePath + "/firmwares/"
190
+
191
+ // Save verbose flag
192
+ verbose_user := * verbose
193
+
194
+ if needUpdateBLE || * force == true {
195
+
196
+ * verbose = true
197
+ // flash current BLE firmware to partition 8
198
+ dfu_ble_flash_command := []string {dfu , dfu_flags , "-D" , firmwarePath + ble_firmware , "--alt" , "8" }
199
+
200
+ fmt .Println ("ATTENTION: BLE firmware is being flashed" )
201
+ fmt .Println ("DO NOT DISCONNECT THE BOARD" )
202
+
203
+ err , _ , _ = launchCommandAndWaitForOutput (dfu_ble_flash_command , "" , true )
204
+ if err != nil {
205
+ fmt .Println (err )
206
+ os .Exit (1 )
207
+ }
208
+ }
209
+
210
+ if needUpdateRTOS || * force == true {
211
+
212
+ * verbose = true
213
+ // flash current RTOS firmware to partition 2
214
+ dfu_rtos_flash_command := []string {dfu , dfu_flags , "-D" , firmwarePath + rtos_firmware , "--alt" , "2" }
215
+
216
+ fmt .Println ("ATTENTION: RTOS firmware is being flashed" )
217
+ fmt .Println ("DO NOT DISCONNECT THE BOARD" )
218
+
219
+ err , _ , _ = launchCommandAndWaitForOutput (dfu_rtos_flash_command , "" , true )
220
+ if err != nil {
221
+ fmt .Println (err )
143
222
os .Exit (1 )
144
223
}
145
- PrintlnVerbose ("BLE version: verified" )
146
224
}
147
225
148
- dfu_download := []string {dfu , dfu_flags , "-D" , bin_file_name , "-v" , "--alt" , "7" , "-R" }
149
- err , _ , _ := launchCommandAndWaitForOutput (dfu_download , "" , true )
226
+ // Restore verbose flag
227
+ * verbose = verbose_user
228
+
229
+ // Finally flash the sketch
230
+
231
+ if * bin_file_name == "" {
232
+ os .Exit (0 )
233
+ }
234
+
235
+ dfu_download := []string {dfu , dfu_flags , "-D" , * bin_file_name , "-v" , "--alt" , "7" , "-R" }
236
+ err , _ , _ = launchCommandAndWaitForOutput (dfu_download , "" , true )
150
237
151
238
if err == nil {
152
239
fmt .Println ("SUCCESS: Sketch will execute in about 5 seconds." )
153
240
os .Exit (0 )
154
241
} else {
155
- fmt .Println ("ERROR: Upload failed on " + com_port )
242
+ fmt .Println ("ERROR: Upload failed on " + * com_port )
156
243
os .Exit (1 )
157
244
}
158
245
}
@@ -164,7 +251,7 @@ func main_debug(args []string) {
164
251
os .Exit (1 )
165
252
}
166
253
167
- verbose = true
254
+ * verbose = true
168
255
169
256
type Command struct {
170
257
command string
@@ -205,24 +292,27 @@ func main_debug(args []string) {
205
292
}
206
293
207
294
func main () {
208
- name := os .Args [0 ]
209
- args := os .Args [1 :]
295
+ name := filepath .Base (os .Args [0 ])
296
+
297
+ flag .Parse ()
298
+
299
+ PrintlnVerbose (name + " " + Version + " - compiled with " + runtime .Version ())
210
300
211
301
if strings .Contains (name , "load" ) {
212
302
fmt .Println ("Starting download script..." )
213
- main_load (args )
303
+ main_load ()
214
304
}
215
305
216
306
if strings .Contains (name , "debug" ) {
217
307
fmt .Println ("Starting debug script..." )
218
- main_debug (args )
308
+ main_debug (os . Args [ 1 :] )
219
309
}
220
310
221
311
fmt .Println ("Wrong executable name" )
222
312
os .Exit (1 )
223
313
}
224
314
225
- func searchBLEversionInDFU (file string , string_to_search string ) bool {
315
+ func searchVersionInDFU (file string , string_to_search string ) bool {
226
316
read , _ := ioutil .ReadFile (file )
227
317
return strings .Contains (string (read ), string_to_search )
228
318
}
0 commit comments