def _firmware_upload_task(self): firmware = ( '0x1000', os.path.join(gm_firmware_dir(),'bootloader.bin'), '0x10000', os.path.join(gm_firmware_dir(), 'NodeMCU.bin'), '0x8000', os.path.join(gm_firmware_dir(), 'partitions_singleapp.bin') ) args = FirmwareUploadArgs(self.serial_monitor.port,firmware) initial_baud = min(ESPLoader.ESP_ROM_BAUD, args.baud) try: self.serial_monitor.stop(log=False) self.panel_writeln('Firmware Start Update ...') esp_set_log(self.panel_writeln) esp = ESPLoader.detect_chip(args.port, initial_baud, args.before) self.panel_writeln("Chip is %s" % (esp.get_chip_description())) esp = esp.run_stub() if args.baud > initial_baud: try: esp.change_baud(args.baud) except NotImplementedInROMError: self.panel_writeln("WARNING: ROM doesn't support changing baud rate. Keeping initial baud rate %d" % initial_baud) if hasattr(args, "flash_size"): self.panel_writeln("Configuring flash size...") detect_flash_size(esp, args) esp.flash_set_parameters(flash_size_bytes(args.flash_size)) write_flash(esp, args) esp.hard_reset() esp._port.close() self.serial_monitor.start() except Exception as e: self.panel_writeln(str(e)) finally: self.serial_monitor.start(log=False)
def write_flash_data(self, esp, flash_files=None, encrypt_files=None, ignore_flash_encryption_efuse_setting=True, encrypt=False): """ Try flashing at a particular baud rate. Structured this way so @_uses_esptool will reconnect each time :return: None """ last_error = None for baud_rate in [921600, 115200]: try: # fake flasher args object, this is a hack until # esptool Python API is improved class FlashArgs(object): def __init__(self, attributes): for key, value in attributes.items(): self.__setattr__(key, value) # write_flash expects the parameter encrypt_files to be None and not # an empty list, so perform the check here flash_args = FlashArgs({ 'flash_size': self.app.flash_settings['flash_size'], 'flash_mode': self.app.flash_settings['flash_mode'], 'flash_freq': self.app.flash_settings['flash_freq'], 'addr_filename': flash_files or None, 'encrypt_files': encrypt_files or None, 'no_stub': self.secure_boot_en, 'compress': not self.secure_boot_en, 'verify': False, 'encrypt': encrypt, 'ignore_flash_encryption_efuse_setting': ignore_flash_encryption_efuse_setting, 'erase_all': False, 'after': 'no_reset', }) esp.change_baud(baud_rate) esptool.detect_flash_size(esp, flash_args) esptool.write_flash(esp, flash_args) break except RuntimeError as e: last_error = e else: raise last_error
def _try_flash(self, esp, erase_nvs, baud_rate): """ Called by start_app() to try flashing at a particular baud rate. Structured this way so @_uses_esptool will reconnect each time """ flash_files = [] try: # note: opening here prevents us from having to seek back to 0 each time flash_files = [(offs, open(path, "rb")) for (offs, path) in self.app.flash_files] if erase_nvs: address = self.app.partition_table["nvs"]["offset"] size = self.app.partition_table["nvs"]["size"] nvs_file = tempfile.TemporaryFile() nvs_file.write(b'\xff' * size) nvs_file.seek(0) if not isinstance(address, int): address = int(address, 0) flash_files.append((address, nvs_file)) # fake flasher args object, this is a hack until # esptool Python API is improved class FlashArgs(object): def __init__(self, attributes): for key, value in attributes.items(): self.__setattr__(key, value) flash_args = FlashArgs({ 'flash_size': self.app.flash_settings["flash_size"], 'flash_mode': self.app.flash_settings["flash_mode"], 'flash_freq': self.app.flash_settings["flash_freq"], 'addr_filename': flash_files, 'no_stub': False, 'compress': True, 'verify': False, 'encrypt': self.app.flash_settings.get("encrypt", False), 'erase_all': False, }) esp.change_baud(baud_rate) esptool.detect_flash_size(esp, flash_args) esptool.write_flash(esp, flash_args) finally: for (_, f) in flash_files: f.close()
def flash(self, addr_filename=[]): """ Flash firmware to the board using esptool """ self.on_data.emit("Preparing to flash memory. This can take a while.") # Esptool is a command line tool and expects arguments that can be # emulated by creating manually a `Namespace` object args = Namespace() args.flash_freq = "40m" args.flash_mode = "dio" args.flash_size = "detect" args.no_progress = False args.compress = False args.no_stub = False args.trace = False args.verify = False # This is the most important bit: It must be an list of tuples each # tuple containing the memory address and an file object. Generate # this list with `get_addr_filename` args.addr_filename = addr_filename try: # Detects which board is being used. We already know what bard we # are flashing (ESP32) but this will also does a lot of other # setup automatically for us esp32loader = esptool.ESPLoader.detect_chip( self.port, 115200, False) # Loads the program bootloader to ESP32 internal RAM esp = esp32loader.run_stub() # Change baudrate to flash the board faster esp.change_baud(921600) # We already know the flash size but asking esptool to autodetect # it will save us some more setup esptool.detect_flash_size(esp, args) esp.flash_set_parameters(esptool.flash_size_bytes(args.flash_size)) # Erase the current flash memory first self.on_data.emit('Erasing flash memory.') esptool.erase_flash(esp, args) self.on_data.emit('Writing on flash memory.') # Intercept what esptool prints out by replacing the `sys.stdout` # by old_stdout = sys.stdout sys.stdout = WritingProgressStdout(self.on_progress) # Write to flash memory esptool.write_flash(esp, args) # Restore old `sys.stdout` sys.stdout = old_stdout # Reset the board so we don't have to turn the Pixel Kit on and off # again using its terrible power switch that looks like a button esp.hard_reset() except Exception as ex: logging.error(ex) self.on_flash_fail.emit("Could not write to flash memory.")
def run(self): try: print("esptool.py v%s" % esptool.__version__) initial_baud = min(ESPLoader.ESP_ROM_BAUD, self._config.baud) if self._config.port.startswith(__auto_select__): esp = self.connect_to_esp(initial_baud) else: esp = ESPLoader.detect_chip(self._config.port, initial_baud) print("Chip is %s" % (esp.get_chip_description())) print("Features: %s" % ", ".join(esp.get_chip_features())) esptool.read_mac(esp, Namespace()) esp = esp.run_stub() if self._config.baud > initial_baud: try: esp.change_baud(self._config.baud) except NotImplementedInROMError: print( "WARNING: ROM doesn't support changing baud rate. Keeping initial baud rate %d." % initial_baud) args = Namespace() args.flash_size = "detect" args.flash_mode = self._config.mode args.flash_freq = "40m" args.no_progress = False args.no_stub = False args.verify = False # TRUE is deprecated args.compress = True args.addr_filename = [[ int("0x00000", 0), open(self._config.firmware_path, 'rb') ]] print("Configuring flash size...") esptool.detect_flash_size(esp, args) esp.flash_set_parameters(esptool.flash_size_bytes(args.flash_size)) if self._config.erase_before_flash: esptool.erase_flash(esp, args) esptool.write_flash(esp, args) # The last line printed by esptool is "Leaving..." -> some indication that the process is done is needed print("\nDone. Unplug/replug or reset device.") esp._port.close() except SerialException as e: self._parent.report_error(e.strerror) raise e
def write_flash(self, firmware): args = Args() data = firmware['body'] args.set_body(0, data) args.flash_mode = firmware['mode'] args.flash_freq = firmware['speed'] esp = self.esp8266 if hasattr(args, "flash_size"): print("Configuring flash size...") detect_flash_size(esp, args) if args.flash_size != 'keep': esp.flash_set_parameters(flash_size_bytes(args.flash_size)) print('Args: ', args) self._write_flash(esp, args)
def run(self): try: initial_baud = min(ESPLoader.ESP_ROM_BAUD, self._config.baud) esp = ESPLoader.detect_chip(self._config.port, initial_baud) print("Chip is %s" % (esp.get_chip_description())) esp = esp.run_stub() if self._config.baud > initial_baud: try: esp.change_baud(self._config.baud) except NotImplementedInROMError: print( "WARNING: ROM doesn't support changing baud rate. Keeping initial baud rate %d" % initial_baud) args = Namespace() args.flash_size = "detect" args.flash_mode = self._config.mode args.flash_freq = "40m" args.no_progress = False args.no_stub = False args.verify = False # TRUE is deprecated args.compress = True args.addr_filename = [[ int("0x00000", 0), open(self._config.firmware_path, 'rb') ]] print("Configuring flash size...") esptool.detect_flash_size(esp, args) esp.flash_set_parameters(esptool.flash_size_bytes(args.flash_size)) if self._config.erase_before_flash: esptool.erase_flash(esp, args) esptool.write_flash(esp, args) self._parent.log_message( "Hard resetting...") # replicate behavior from esptool.py:2111 esp.hard_reset() except SerialException as e: self._parent.report_error(e.strerror) raise e
def _try_flash(self, esp, erase_nvs, baud_rate): """ Called by start_app() to try flashing at a particular baud rate. Structured this way so @_uses_esptool will reconnect each time """ try: # note: opening here prevents us from having to seek back to 0 each time flash_files = [(offs, open(path, "rb")) for (offs, path) in self.app.flash_files] if erase_nvs: address = self.app.partition_table["nvs"]["offset"] size = self.app.partition_table["nvs"]["size"] nvs_file = tempfile.TemporaryFile() nvs_file.write(b'\xff' * size) nvs_file.seek(0) flash_files.append((int(address, 0), nvs_file)) # fake flasher args object, this is a hack until # esptool Python API is improved class FlashArgs(object): def __init__(self, attributes): for key, value in attributes.items(): self.__setattr__(key, value) flash_args = FlashArgs({ 'flash_size': self.app.flash_settings["flash_size"], 'flash_mode': self.app.flash_settings["flash_mode"], 'flash_freq': self.app.flash_settings["flash_freq"], 'addr_filename': flash_files, 'no_stub': False, 'compress': True, 'verify': False, 'encrypt': False, 'erase_all': False, }) esp.change_baud(baud_rate) esptool.detect_flash_size(esp, flash_args) esptool.write_flash(esp, flash_args) finally: for (_, f) in flash_files: f.close()
def run(self): try: initial_baud = min(ESPLoader.ESP_ROM_BAUD, self._config.baud) esp = ESPLoader.detect_chip(self._config.port, initial_baud) print("芯片类型: %s" % (esp.get_chip_description())) esp = esp.run_stub() if self._config.baud > initial_baud: try: esp.change_baud(self._config.baud) except NotImplementedInROMError: print("警告: ROM 不支持改变波特率. 请保持初始波特率 %d." % initial_baud) args = Namespace() args.flash_size = "detect" args.flash_mode = self._config.mode args.flash_freq = "40m" args.no_progress = False args.no_stub = False args.verify = False # TRUE is deprecated args.compress = True args.addr_filename = [[ int("0x00000", 0), open(self._config.firmware_path, 'rb') ]] print("Configuring flash size...") esptool.detect_flash_size(esp, args) esp.flash_set_parameters(esptool.flash_size_bytes(args.flash_size)) if self._config.erase_before_flash: esptool.erase_flash(esp, args) esptool.write_flash(esp, args) # The last line printed by esptool is "Leaving..." -> some indication that the process is done is needed print("\nDone.") except SerialException as e: self._parent.report_error(e.strerror) raise e
def _try_flash(self, esp, erase_nvs, baud_rate): """ Called by start_app() to try flashing at a particular baud rate. Structured this way so @_uses_esptool will reconnect each time """ flash_files = [] encrypt_files = [] try: # Open the files here to prevents us from having to seek back to 0 # each time. Before opening them, we have to organize the lists the # way esptool.write_flash needs: # If encrypt is provided, flash_files contains all the files to # flash. # Else, flash_files contains the files to be flashed as plain text # and encrypt_files contains the ones to flash encrypted. flash_files = self.app.flash_files encrypt_files = self.app.encrypt_files encrypt = self.app.flash_settings.get("encrypt", False) if encrypt: flash_files = encrypt_files encrypt_files = [] else: flash_files = [ entry for entry in flash_files if entry not in encrypt_files ] flash_files = [(offs, open(path, "rb")) for (offs, path) in flash_files] encrypt_files = [(offs, open(path, "rb")) for (offs, path) in encrypt_files] if erase_nvs: address = self.app.partition_table["nvs"]["offset"] size = self.app.partition_table["nvs"]["size"] nvs_file = tempfile.TemporaryFile() nvs_file.write(b'\xff' * size) nvs_file.seek(0) if not isinstance(address, int): address = int(address, 0) # We have to check whether this file needs to be added to # flash_files list or encrypt_files. # Get the CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT macro # value. If it is set to True, then NVS is always encrypted. sdkconfig_dict = self.app.get_sdkconfig() macro_encryption = "CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT" in sdkconfig_dict # If the macro is not enabled (plain text flash) or all files # must be encrypted, add NVS to flash_files. if not macro_encryption or encrypt: flash_files.append((address, nvs_file)) else: encrypt_files.append((address, nvs_file)) # fake flasher args object, this is a hack until # esptool Python API is improved class FlashArgs(object): def __init__(self, attributes): for key, value in attributes.items(): self.__setattr__(key, value) # write_flash expects the parameter encrypt_files to be None and not # an empty list, so perform the check here flash_args = FlashArgs({ 'flash_size': self.app.flash_settings["flash_size"], 'flash_mode': self.app.flash_settings["flash_mode"], 'flash_freq': self.app.flash_settings["flash_freq"], 'addr_filename': flash_files, 'encrypt_files': encrypt_files or None, 'no_stub': False, 'compress': True, 'verify': False, 'encrypt': encrypt, 'ignore_flash_encryption_efuse_setting': False, 'erase_all': False, }) esp.change_baud(baud_rate) esptool.detect_flash_size(esp, flash_args) esptool.write_flash(esp, flash_args) finally: for (_, f) in flash_files: f.close() for (_, f) in encrypt_files: f.close()
def run(self): esp = None args = None vargs = None ok = False try: # build the args namespace esptool expects args = Namespace() # copy all enties from setup file for a in self.setup: setattr(args, a, self.setup[a]) # We only verify the app portion as the other portions incl # nvm and may/will change during runtime. So we need a special copy # of args for verify. # verify only if we have a single image that starts at 0x1000 if len(self.setup["files"] ) == 1 and self.setup["files"][0]["addr"] == 0x1000: vargs = Namespace() for a in self.setup: setattr(vargs, a, self.setup[a]) # map files to addr_filename tuples args.addr_filename = [] for f in self.setup["files"]: # load file into ram as older python version cannot # seek within zip files but esptool expects to be # able to seek # read "source" if present, otherwise read "filename" if "source" in f: fname = f["source"] else: fname = f["filename"] with self.setup["open"](fname, "rb") as fh: data = fh.read() fh = io.BytesIO(data) setattr(fh, "name", fname) args.addr_filename.append((f["addr"], fh)) # for verify create a ram copy of the firmware which skips to 0x10000 if vargs: vargs.addr_filename = [] f = self.setup["files"][0] if "source" in f: fname = f["source"] else: fname = f["filename"] with self.setup["open"](fname, "rb") as fh: # get access to full image data but skip the first # (0x10000 - addr) = 0xf000 bytes data = args.addr_filename[0][1].getbuffer()[0x10000 - f["addr"]:] dio = io.BytesIO(data) setattr(dio, "name", "app area of {}".format(fname)) vargs.addr_filename.append((0x10000, dio)) esp = esptool.get_default_connected_device( serial_list=[self.port], port=self.port, initial_baud=ESPROM_BAUD, chip=args.chip, connect_attempts=args.connect_attempts) print("Chip is %s" % (esp.get_chip_description())) print("Features: %s" % ", ".join(esp.get_chip_features())) print("Crystal is %dMHz" % esp.get_crystal_freq()) esptool.read_mac(esp, args) esp = esp.run_stub() if args.baud > ESPROM_BAUD: esp.change_baud(args.baud) esptool.detect_flash_size(esp, args) if args.flash_size != 'keep': esp.flash_set_parameters( esptool.flash_size_bytes(args.flash_size)) do_write = True if vargs: try: esptool.verify_flash(esp, vargs) do_write = False # verify successful, no need to write except: pass if do_write: esptool.write_flash(esp, args) else: print("Firmware verified successfully, skipping flash") esp.hard_reset() esp._port.close() ok = True except IOException as e: self.alert.emit({ "title": "esptool", "message": "Esptool error", "info": "Exception when accessing\n" + str(self.port), "detail": str(e) }) if esp: esp._port.close() # since the files are now stored in memory we don't have to # close them here anymore self.done.emit(ok)
def _write_flash(self): #1.Setup widgets #self._status_color = 'blue' self.style.configure('write.TLabel', foreground='blue') self._write['state'] = 'disable' if self.device: self.device.ports['state'] = 'disable' self._update_status('Preprocessing....') #2. Create agrs if not self._create_args(): print("\nFlashFirmware: Failed to create args.\n") self._post_write_flash_sop() return False args = self.args #3. Setup esp #3.1 Use "stub loader" program instead of the UART bootloader in the ESP32 ROM. if self.device.esp: esp = self.device.esp if not esp.IS_STUB: try: esp = esp.run_stub() except esptool.FatalError as err: self._post_write_flash_sop() self._update_status(err.__str__()) print(err) return False else: self._post_write_flash_sop() print("\nFlashFirmware: esp needs to be connected first.\n") print(' -- ', err.__str__()) return False #3.2 Use a different baud to write flash if avaialble if args.baud != esptool.ESPLoader.ESP_ROM_BAUD: self._change_baud(esp, args.baud) #3.3 Use esptool.py "Default SPI flash interface" to write to flash chip. # Commented code is useful if the option of having non-default SPI is preferred. # https://github.com/espressif/esptool/wiki/Serial-Protocol#spi-attach-command '''if hasattr( args, "spi_connection" ) and args.spi_connection is not None: if esp.CHIP_NAME != "ESP32": self._post_write_flash_sop() raise FatalError( "Chip %s does not support --spi-connection option." % esp.CHIP_NAME ) print( "Configuring SPI flash mode..." ) esp.flash_spi_attach( args.spi_connection ) elif args.no_stub: print( "Enabling default SPI flash mode..." ) # ROM loader doesn't enable flash unless we explicitly do it esp.flash_spi_attach( 0 )''' #3.4 Set some parameters of the SPI flash chip if hasattr(args, "flash_size"): print("Configuring flash size...") esptool.detect_flash_size(esp, args) esp.flash_set_parameters(esptool.flash_size_bytes(args.flash_size)) #print('\nargs = '); pprint( args.__dict__ ) #print('\nesp = '); pprint( esp.__dict__ ) #print('\nesp._port = '); pprint( esp._port.__dict__ ) #4. Start writing self._writing = True self._completed = False self._update_status('Writing....') try: #esptool.write_flash( esp, args ) #original self._esptool_write_flash( esp, args ) #allow more detailed display of the write to flash progress. except esptool.FatalError: self._post_write_flash_sop() raise else: print('\nRevert to default Baud...') self._change_baud(esp, esptool.ESPLoader.ESP_ROM_BAUD) finally: try: # Clean up AddrFilenamePairAction files for address, argfile in args.addr_filename: argfile.close() except AttributeError: pass #5. Post writing setups self._update_status('Completed writing Firmware to Flash.') self._post_write_flash_sop() self.device.esp = esp print() return True
def _write_flash(self): #1.Setup widgets self._status_color = 'blue' self.style.configure('write.TLabel', foreground='blue') self._write['state'] = 'disable' self.update_status('Preprocessing....') #2. Create agrs if not self._create_args(): print("\nFlashFirmware: Failed to create args.") #3. Setup esp args = self.args if not self.device.esp: self.device._connect_esp() esp = self.device.esp else: esp = self.device.esp esp.flush_input() #esp.connect() #esp._slip_reader = esptool.slip_reader(esp._port, esp.trace) if not args.no_stub: print('if not args.no_stub:') esp = esp.run_stub() if args.baud != esptool.ESPLoader.ESP_ROM_BAUD: try: esp.change_baud(args.baud) except NotImplementedInROMError: print( "WARNING: ROM doesn't support changing baud rate. Keeping initial baud rate %d" % initial_baud) # override common SPI flash parameter stuff if configured to do so if hasattr(args, "spi_connection") and args.spi_connection is not None: if esp.CHIP_NAME != "ESP32": raise FatalError( "Chip %s does not support --spi-connection option." % esp.CHIP_NAME) print("Configuring SPI flash mode...") esp.flash_spi_attach(args.spi_connection) elif args.no_stub: print("Enabling default SPI flash mode...") # ROM loader doesn't enable flash unless we explicitly do it esp.flash_spi_attach(0) if hasattr(args, "flash_size"): print("Configuring flash size...") esptool.detect_flash_size(esp, args) esp.flash_set_parameters(esptool.flash_size_bytes(args.flash_size)) print('\nargs = ') pprint(args.__dict__) print('\nesp = ') pprint(esp.__dict__) print('\nesp._port = ') pprint(esp._port.__dict__) #4. Start writing self._writing = True self._completed = False self.update_status('Writing....') try: esptool.write_flash(esp, args) except esptool.FatalError: raise else: print('Hard resetting via RTS pin...') esp.hard_reset() finally: try: # Clean up AddrFilenamePairAction files for address, argfile in args.addr_filename: argfile.close() except AttributeError: pass #esp._port.close() #5. Post writing setups self.update_status('Written Firmware to Flash.') self._writing = False self._completed = True self._write['state'] = 'normal'