|
42 | 42 | # Read in command line arguments |
43 | 43 | try: |
44 | 44 | opts, args = getopt.getopt(sys.argv[1:], "hf:t:ma", |
45 | | - ['help', 'file=', 'multi', 'tag=', 'testmode', 'autodfu', 'testmode']) |
| 45 | + ['help', 'file=', 'system=', 'multi', 'tag=', 'testmode', 'autodfu', 'testmode', 'noreset']) |
46 | 46 | except getopt.GetoptError: |
47 | | - print "Unknown parameter, available Options: --file, --multi, --tag --autodfu --testmode" |
| 47 | + print "Unknown parameter, available Options: --file, --system, --multi, --tag --autodfu --testmode --noreset" |
48 | 48 |
|
49 | 49 | sys.exit() |
50 | 50 |
|
51 | 51 | multi = False |
52 | 52 | testMode = False |
53 | 53 | autoDfu = False |
| 54 | +noReset = False |
54 | 55 | tag = None |
55 | 56 | # binaries for system update |
56 | 57 | system1 = None |
|
64 | 65 | if o in ('-h', '--help'): |
65 | 66 | print "\n Available command line options: " |
66 | 67 | print "--help: print this help message" |
67 | | - print "--file: path to .bin file to flash instead of the latest release on GitHub" |
68 | 68 | print "--tag: specify which tag to download from github" |
| 69 | + print "--file: path to .bin file to flash instead of the latest release on GitHub.\n" \ |
| 70 | + "If this is a directory, search for binary and system update files." |
| 71 | + print "--system: path to system binaries to update the system firmware on the photon." |
69 | 72 | print "--multi: keep the script alive to flash multiple devices" |
70 | 73 | print "--autodfu: automatically reboot photon in DFU mode by opening serial port at 14400 baud" |
71 | 74 | print "--testmode: set controller o test mode after flashing" |
| 75 | + print "--noreset: do not reset EEPROM after flashing" |
72 | 76 |
|
73 | 77 | exit() |
74 | | - # supply a config file |
| 78 | + # supply a binary file |
75 | 79 | if o in ('-f', '--file'): |
76 | 80 | print("Using local files instead of downloading a release. \n") |
77 | 81 | if os.path.isdir(a): |
78 | 82 | for file in os.listdir(a): |
79 | 83 | if all(x in file for x in ['brewpi', '.bin']): |
80 | 84 | binFile = os.path.join(os.path.abspath(a), file) |
| 85 | + else: |
| 86 | + binFile = os.path.abspath(a) |
| 87 | + if not os.path.exists(binFile): |
| 88 | + print('ERROR: Binary file "%s" was not found!' % binFile) |
| 89 | + exit(1) |
| 90 | + # supply a system update directory |
| 91 | + if o in ('-s', '--system'): |
| 92 | + print("Using local files for system update instead of downloading from GitHub.\n") |
| 93 | + if os.path.isdir(a): |
| 94 | + for file in os.listdir(a): |
81 | 95 | if all(x in file for x in ['system', 'part1', '.bin']): |
82 | 96 | system1 = os.path.join(os.path.abspath(a), file) |
83 | 97 | if all(x in file for x in ['system', 'part2', '.bin']): |
84 | 98 | system2 = os.path.join(os.path.abspath(a), file) |
85 | 99 | else: |
86 | | - binFile = os.path.abspath(a) |
87 | | - if not os.path.exists(binFile): |
88 | | - print('ERROR: Binary file(s) "%s" was not found!' % binFile) |
| 100 | + print('ERROR: System binaries location {0} is not a directory!' % a) |
| 101 | + if not os.path.exists(system1): |
| 102 | + print('ERROR: System binary 1 "%s" was not found!' % binFile) |
89 | 103 | exit(1) |
90 | | - if os.path.exists(system1) and os.path.exists(system2): |
91 | | - print('System update files found, will update system part before flashing user binary. \n') |
| 104 | + if not os.path.exists(system2): |
| 105 | + print('ERROR: System binary 2 "%s" was not found!' % binFile) |
| 106 | + exit(1) |
| 107 | + |
92 | 108 | # send quit instruction to all running instances of BrewPi |
93 | 109 | if o in ('-m', '--multi'): |
94 | 110 | multi = True |
|
102 | 118 | if o in ('-a', '--autodfu'): |
103 | 119 | autoDfu = True |
104 | 120 | print "Will automatically reboot newly detected photons into DFU mode" |
| 121 | + if o in ('--noreset'): |
| 122 | + noReset = True |
105 | 123 |
|
106 | 124 | dfuPath = "dfu-util" |
107 | 125 | # check whether dfu-util can be found |
|
179 | 197 |
|
180 | 198 | # download latest binary from GitHub if file not specified |
181 | 199 | if not binFile: |
182 | | - print "Downloading latest firmware..." |
183 | 200 | if tag is None: |
| 201 | + print "Downloading latest firmware..." |
184 | 202 | tag = releases.getLatestTag() |
185 | 203 | print "Latest stable version on GitHub: " + tag |
| 204 | + else: |
| 205 | + print "Downloading release " + tag |
186 | 206 |
|
187 | 207 | binFile = releases.getBin(tag, [type, 'brewpi', '.bin']) |
188 | 208 | if binFile: |
|
192 | 212 | exit(1) |
193 | 213 |
|
194 | 214 | if type == 'photon': |
195 | | - system1 = releases.getBin(tag, ['photon', 'system-part1', '.bin']) |
196 | | - system2 = releases.getBin(tag, ['photon', 'system-part2', '.bin']) |
197 | | - |
| 215 | + if LooseVersion(tag) > LooseVersion('0.2.11'): # 0.2.11 was compiled against non-forward compatible system |
| 216 | + latestSystemTag = releases.getLatestTagForSystem() |
| 217 | + else: |
| 218 | + latestSystemTag = tag |
| 219 | + print ("Updated system firmware for the photon found in release {0}".format(latestSystemTag)) |
| 220 | + system1 = releases.getBin(latestSystemTag, ['photon', 'system-part1', '.bin']) |
| 221 | + system2 = releases.getBin(latestSystemTag, ['photon', 'system-part2', '.bin']) |
198 | 222 | if system1: |
199 | | - print "Release contains updated system firmware for the photon" |
| 223 | + print "Downloaded new system firmware to:\n" |
| 224 | + print "{0} and\n".format(system1) |
200 | 225 | if not system2: |
201 | 226 | print "Error: system firmware part2 not found in release" |
202 | | - exit() |
| 227 | + exit(1) |
| 228 | + else: |
| 229 | + print "{0} and\n".format(system2) |
| 230 | + |
203 | 231 |
|
204 | 232 | if binFile: |
205 | 233 | if type == 'core': |
|
218 | 246 | p = subprocess.Popen(dfuPath + " -d 0x2B04:0xD006 -a 0 -s 0x80A0000:leave -D {0}".format(binFile), shell=True) |
219 | 247 | p.wait() |
220 | 248 |
|
221 | | - print "Programming done, now resetting EEPROM to defaults" |
222 | | - # reset EEPROM to defaults |
223 | | - configFile = util.scriptPath() + '/settings/config.cfg' |
224 | | - config = util.readCfgWithDefaults(configFile) |
225 | | - programmer = SerialProgrammer.create(config, "core") |
| 249 | + print "Programming done" |
226 | 250 |
|
227 | | - # open serial port |
228 | | - print "Opening serial port" |
229 | | - retries = 10 |
230 | | - while retries > 0: |
231 | | - if programmer.open_serial(config, 57600, 0.2): |
232 | | - break |
233 | | - retries -= 1 |
234 | | - time.sleep(1) |
235 | | - if retries > 0: |
236 | | - programmer.fetch_version("Success! ") |
237 | | - programmer.reset_settings(testMode) |
238 | | - serialPorts = autoSerial.detect_all_ports() # update serial ports here so device will not be seen as new |
239 | | - else: |
240 | | - print "Could not open serial port after programming" |
| 251 | + if not noReset: |
| 252 | + print "Now resetting EEPROM to defaults" |
| 253 | + # reset EEPROM to defaults |
| 254 | + configFile = util.scriptPath() + '/settings/config.cfg' |
| 255 | + config = util.readCfgWithDefaults(configFile) |
| 256 | + programmer = SerialProgrammer.create(config, type) |
| 257 | + |
| 258 | + # open serial port |
| 259 | + print "Opening serial port" |
| 260 | + retries = 10 |
| 261 | + while retries > 0: |
| 262 | + if programmer.open_serial(config, 57600, 0.2): |
| 263 | + break |
| 264 | + retries -= 1 |
| 265 | + time.sleep(1) |
| 266 | + if retries > 0: |
| 267 | + programmer.fetch_version("Success! ") |
| 268 | + programmer.reset_settings(testMode) |
| 269 | + serialPorts = autoSerial.detect_all_ports() # update serial ports here so device will not be seen as new |
| 270 | + else: |
| 271 | + print "Could not open serial port after programming" |
241 | 272 | else: |
242 | 273 | print "found DFU device, but no binary specified for flashing" |
243 | 274 | if not multi: |
|
261 | 292 | ser = serial.Serial(port) |
262 | 293 | try: |
263 | 294 | ser.setBaudrate(14400) # this triggers a reboot in DFU mode |
| 295 | + time.sleep(1) |
| 296 | + ser.close() |
264 | 297 | except ValueError: |
265 | 298 | pass # because device reboots while reconfiguring an exception is thrown, ignore |
266 | | - ser.close() |
| 299 | + if ser.isOpen(): |
| 300 | + ser.close() |
| 301 | + ser.setBaudrate(57600) # don't leave serial port at 14400, or a reboot will be triggered later |
| 302 | + |
267 | 303 |
|
268 | 304 | time.sleep(1) |
0 commit comments