Пример #1
0
def flash(port, firmware_file, erase_flash):
    # Initialize parameters
    args = Args()
    args.port = port
    args.baud = 460800

    # Initialize device
    initial_baud = min(ESP_ROM_BAUD, args.baud)
    esp = esptool.ESPROM(args.port, initial_baud)

    if erase_flash:
        # Connect to the device
        esp.connect()

        # Erase flash
        esptool.erase_flash(esp, None)
        # Wait for flash erase
        for i in xrange(10):
            print ".",
            sleep(1)
        print "\nDone erasing flash."

    # Reconnect
    esp.connect()

    # Prepare arguments for writing flash
    address = 0
    args.flash_size = "8m"
    args.addr_filename = [(address, open(firmware_file, "rb"))]

    # Write flash
    esptool.write_flash(esp, args)
    print "Done writing new firmware."
Пример #2
0
 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)
Пример #3
0
def flash_nvs_partition(bin_path, addr, esp):
    esp.connect()
    print(bin_path)
    abs_bin_path = os.path.dirname(os.path.abspath(__file__)) + '/' + bin_path
    print(abs_bin_path)
    if (os.path.exists(abs_bin_path) is False):
        print("NVS partition not found")
        sys.exit(-1)

    with open(bin_path, 'rb') as nvs_file:

        flash_file = [(addr, nvs_file)]

        flash_args = DefineArgs({
            'flash_size': '4MB',
            'flash_mode': 'qio',
            'flash_freq': '80m',
            'addr_filename': flash_file,
            'no_stub': False,
            'compress': False,
            'verify': False,
            'encrypt': False,
            'erase_all': False,
        })

        esp.change_baud(baud=921600)
        esptool.write_flash(esp, flash_args)
Пример #4
0
def write_firmware(esp, platform, firmware, flash=None):
    t = time.time()
    click.secho('Flashing firmware (Please wait)...', fg="green")
    # Detect flash size
    if flash is None:
        click.secho('Auto dectect flash size', fg="yellow")
        flash_id = esp.flash_id()
        flid_lowbyte = (flash_id >> 16) & 0xFF
        flash_size = esptool.DETECTED_FLASH_SIZES.get(flid_lowbyte)
        if flash_size is None:
            flash_size = DEFAULT_FLASH_SIZE[platform]
            click.secho('Size detection failed, set to default: %s'%(flash_size), fg="yellow")
        else:
            click.secho('Size detection successfull: %s'%(flash_size), fg="green")  
    else:
        flash_size = flash      
    # Set flash paraneters
    esp.flash_set_parameters(esptool.flash_size_bytes(flash_size))
    # Write
    with open(firmware, 'rb') as file:
        esptool.write_flash(esp, get_flash_params(platform, flash_size, file))
    click.secho('Hard reset...', fg="green")
    # Rest
    esp.hard_reset()
    click.secho('Flash completed successfully in %.1fs' % (time.time() - t), fg="green")
Пример #5
0
def flash(port, firmware_file, erase_flash):
    # Initialize parameters
    args = Args()
    args.port = port
    args.baud = 460800

    # Initialize device
    initial_baud = min(ESP_ROM_BAUD, args.baud)
    esp = esptool.ESPROM(args.port, initial_baud)

    if erase_flash:
        # Connect to the device
        esp.connect()

        # Erase flash
        esptool.erase_flash(esp, args)

    if not firmware_file:
        return

    # Reconnect
    esp.connect()

    # Prepare arguments for writing flash
    address = 0
    args.flash_size = "detect"
    args.addr_filename = [(address, open(firmware_file, "rb"))]

    # Write flash
    esptool.write_flash(esp, args)
    print "Done writing new firmware."
Пример #6
0
def flash(port, firmware_file, erase_flash):
    # Initialize parameters
    args = Args()
    args.port = port
    args.baud = 460800

    # Initialize device
    initial_baud = min(ESP_ROM_BAUD, args.baud)
    esp = esptool.ESPROM(args.port, initial_baud)

    if erase_flash:
        # Connect to the device
        esp.connect()

        # Erase flash
        esptool.erase_flash(esp, args)

    if not firmware_file:
        return

    # Reconnect
    esp.connect()

    # Prepare arguments for writing flash
    address = 0
    args.flash_size = "detect"
    args.addr_filename = [(address, open(firmware_file, "rb"))]

    # Write flash
    esptool.write_flash(esp, args)
    print("Done writing new firmware.")
Пример #7
0
    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
Пример #8
0
    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()
Пример #9
0
 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.")
Пример #10
0
    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
Пример #11
0
    def write(self, offset, contents):
        args = Args()

        args.flash_size = 'detect'
        args.flash_mode = 'dio'
        args.flash_freq = '80m'
        args.compress = True
        args.verify = False
        args.no_stub = False

        fmap = cStringIO.StringIO(contents)
        args.addr_filename = [[offset, fmap]]

        esptool.write_flash(self.esp, args)
        fmap.close()
Пример #12
0
    def flash_bin(self, dest_and_file_pairs):
        args = Args()

        args.flash_size = 'detect'
        args.flash_mode = 'dio'
        args.flash_freq = '80m'
        args.compress = True
        args.verify = True

        dest_and_file = list(dest_and_file_pairs)

        for i, el in enumerate(dest_and_file):
            dest_and_file[i][1] = open(el[1], "rb")

        args.addr_filename = dest_and_file

        esptool.write_flash(self.esp, args)
Пример #13
0
    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
Пример #14
0
    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()
Пример #15
0
 def run(self):
     esp = ESPROM(port=self._config.port)
     args = Namespace()
     args.flash_size = "detect"
     args.flash_mode = self._config.mode
     args.flash_freq = "40m"
     args.no_progress = False
     args.verify = True
     args.baud = self._config.baud
     args.addr_filename = [[
         int("0x00000", 0),
         open(self._config.firmware_path, 'rb')
     ]]
     # needs connect() before each operation, see  https://github.com/espressif/esptool/issues/157
     if self._config.erase_before_flash:
         esp.connect()
         esptool.erase_flash(esp, args)
     esp.connect()
     esptool.write_flash(esp, args)
Пример #16
0
    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
Пример #17
0
    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
            Flash_Args = namedtuple('write_flash_args', [
                'flash_size', 'flash_mode', 'flash_freq', 'addr_filename',
                'no_stub', 'compress', 'verify', 'encrypt'
            ])

            flash_args = Flash_Args(self.app.flash_settings["flash_size"],
                                    self.app.flash_settings["flash_mode"],
                                    self.app.flash_settings["flash_freq"],
                                    flash_files, False, True, False, False)

            esp.change_baud(baud_rate)
            esptool.write_flash(esp, flash_args)
        finally:
            for (_, f) in flash_files:
                f.close()
Пример #18
0
    def writeFirmware(self, address, filename):
        device = self.connect()

        class argsStub():
            def __init__(self, address, filename):
                self.flash_mode = 'qio'
                self.flash_size = '4m'
                self.flash_freq = '40m'
                self.baud = 115200
                self.no_progress = False
                self.operation = 'write_flash'
                self.verify = False
                self.addr_filename = []
                argfile = open(filename, 'rb')
                self.addr_filename.append((address, argfile))

        args = argsStub(address, filename)
        return esptool.write_flash(device, args)
Пример #19
0
	def writeFirmware(self, address, filename):
		device = self.connect()

		class argsStub():
			def __init__(self, address, filename):
				self.flash_mode = 'qio'
				self.flash_size = '4m'
				self.flash_freq='40m'
				self.baud=115200
				self.no_progress=False
				self.operation='write_flash'
				self.verify=False
				self.addr_filename = []
				argfile = open(filename, 'rb')
				self.addr_filename.append((address,argfile))

		args = argsStub(address, filename)
		return esptool.write_flash(device, args)
Пример #20
0
    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()
Пример #21
0
    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)
Пример #22
0
def run_esphomeflasher(argv):
    args = parse_args(argv)
    port = select_port(args)

    if args.show_logs:
        serial_port = serial.Serial(port, baudrate=921600)
        show_logs(serial_port)
        return

    print("Starting firmware upgrade...")
    print("Getting latest firmware from fujinet.online..")
    print(fujinet_version_info(), end='')
    firmware = open_downloadable_binary(ESP32_DEFAULT_FIRMWARE)

    chip = detect_chip(port, args.esp8266, args.esp32)
    info = read_chip_info(chip)

    print()
    print("Chip Info:")
    print(" - Chip Family: {}".format(info.family))
    print(" - Chip Model: {}".format(info.model))
    if isinstance(info, ESP32ChipInfo):
        print(" - Number of Cores: {}".format(info.num_cores))
        print(" - Max CPU Frequency: {}".format(info.cpu_frequency))
        print(" - Has Bluetooth: {}".format(
            'YES' if info.has_bluetooth else 'NO'))
        print(" - Has Embedded Flash: {}".format(
            'YES' if info.has_embedded_flash else 'NO'))
        print(" - Has Factory-Calibrated ADC: {}".format(
            'YES' if info.has_factory_calibrated_adc else 'NO'))
    else:
        print(" - Chip ID: {:08X}".format(info.chip_id))

    print(" - MAC Address: {}".format(info.mac))

    stub_chip = chip_run_stub(chip)

    if args.upload_baud_rate != 115200:
        try:
            stub_chip.change_baud(args.upload_baud_rate)
        except esptool.FatalError as err:
            raise EsphomeflasherError(
                "Error changing ESP upload baud rate: {}".format(err))

    flash_size = detect_flash_size(stub_chip)
    print(" - Flash Size: {}".format(flash_size))

    mock_args = configure_write_flash_args(info, firmware, flash_size,
                                           args.bootloader, args.partitions,
                                           args.otadata, args.spiffs)

    print(" - Flash Mode: {}".format(mock_args.flash_mode))
    print(" - Flash Frequency: {}Hz".format(mock_args.flash_freq.upper()))

    try:
        stub_chip.flash_set_parameters(esptool.flash_size_bytes(flash_size))
    except esptool.FatalError as err:
        raise EsphomeflasherError(
            "Error setting flash parameters: {}".format(err))

    if not args.no_erase:
        try:
            esptool.erase_flash(stub_chip, mock_args)
        except esptool.FatalError as err:
            raise EsphomeflasherError(
                "Error while erasing flash: {}".format(err))

    try:
        esptool.write_flash(stub_chip, mock_args)
    except esptool.FatalError as err:
        raise EsphomeflasherError("Error while writing flash: {}".format(err))

    print("Hard Resetting...")
    stub_chip.hard_reset()

    print("Done! Flashing is complete!")
    print()

    if args.upload_baud_rate != 921600:
        stub_chip._port.baudrate = 921600
        time.sleep(0.05)  # get rid of crap sent during baud rate change
        stub_chip._port.flushInput()

    show_logs(stub_chip._port)
Пример #23
0
def run_esphomeflasher(argv):
    args = parse_args(argv)
    try:
        firmware = open(args.binary, 'rb')
    except IOError as err:
        raise EsphomeflasherError("Error opening binary: {}".format(err))
    port = select_port(args)
    chip = detect_chip(port, args.esp8266, args.esp32)
    info = read_chip_info(chip)

    print()
    print("Chip Info:")
    print(" - Chip Family: {}".format(info.family))
    print(" - Chip Model: {}".format(info.model))
    if isinstance(info, ESP32ChipInfo):
        print(" - Number of Cores: {}".format(info.num_cores))
        print(" - Max CPU Frequency: {}".format(info.cpu_frequency))
        print(" - Has Bluetooth: {}".format('YES' if info.has_bluetooth else 'NO'))
        print(" - Has Embedded Flash: {}".format('YES' if info.has_embedded_flash else 'NO'))
        print(" - Has Factory-Calibrated ADC: {}".format(
            'YES' if info.has_factory_calibrated_adc else 'NO'))
    else:
        print(" - Chip ID: {:08X}".format(info.chip_id))

    print(" - MAC Address: {}".format(info.mac))

    stub_chip = chip_run_stub(chip)

    flash_size = detect_flash_size(stub_chip)
    print(" - Flash Size: {}".format(flash_size))

    mock_args = configure_write_flash_args(info, firmware, flash_size,
                                           args.bootloader, args.partitions,
                                           args.otadata)

    print(" - Flash Mode: {}".format(mock_args.flash_mode))
    print(" - Flash Frequency: {}Hz".format(mock_args.flash_freq.upper()))

    try:
        stub_chip.flash_set_parameters(esptool.flash_size_bytes(flash_size))
    except esptool.FatalError as err:
        raise EsphomeflasherError("Error setting flash parameters: {}".format(err))

    if not args.no_erase:
        try:
            esptool.erase_flash(stub_chip, mock_args)
        except esptool.FatalError as err:
            raise EsphomeflasherError("Error while erasing flash: {}".format(err))

    try:
        esptool.write_flash(stub_chip, mock_args)
    except esptool.FatalError as err:
        raise EsphomeflasherError("Error while writing flash: {}".format(err))

    print("Hard Resetting...")
    stub_chip.hard_reset()

    print("Done! Flashing is complete!")
    print()

    print("Showing logs:")
    with stub_chip._port as ser:
        while True:
            try:
                raw = ser.readline()
            except serial.SerialException:
                print("Serial port closed!")
                return
            text = raw.decode(errors='ignore')
            text = ANSI_REGEX.sub('', text)
            line = text.replace('\r', '').replace('\n', '')
            time = datetime.now().time().strftime('[%H:%M:%S]')
            message = time + line
            try:
                print(message)
            except UnicodeEncodeError:
                print(message.encode('ascii', 'backslashreplace'))
Пример #24
0
def run_esphomeflasher(argv):
    args = parse_args(argv)
    port = select_port(args)

    if args.show_logs:
        serial_port = serial.Serial(port, baudrate=115200)
        show_logs(serial_port)
        return

    try:
        firmware = open(args.binary, 'rb')
    except IOError as err:
        raise EsphomeflasherError("Error opening binary: {}".format(err))
    chip = detect_chip(port, args.esp8266, args.esp32)
    info = read_chip_info(chip)

    print()
    print("Chip Info:")
    print(" - Chip Family: {}".format(info.family))
    print(" - Chip Model: {}".format(info.model))
    if isinstance(info, ESP32ChipInfo):
        print(" - Number of Cores: {}".format(info.num_cores))
        print(" - Max CPU Frequency: {}".format(info.cpu_frequency))
        print(" - Has Bluetooth: {}".format(
            'YES' if info.has_bluetooth else 'NO'))
        print(" - Has Embedded Flash: {}".format(
            'YES' if info.has_embedded_flash else 'NO'))
        print(" - Has Factory-Calibrated ADC: {}".format(
            'YES' if info.has_factory_calibrated_adc else 'NO'))
    else:
        print(" - Chip ID: {:08X}".format(info.chip_id))

    print(" - MAC Address: {}".format(info.mac))

    stub_chip = chip_run_stub(chip)

    flash_size = detect_flash_size(stub_chip)
    print(" - Flash Size: {}".format(flash_size))

    mock_args = configure_write_flash_args(info, firmware, flash_size,
                                           args.bootloader, args.partitions,
                                           args.otadata)

    print(" - Flash Mode: {}".format(mock_args.flash_mode))
    print(" - Flash Frequency: {}Hz".format(mock_args.flash_freq.upper()))

    try:
        stub_chip.flash_set_parameters(esptool.flash_size_bytes(flash_size))
    except esptool.FatalError as err:
        raise EsphomeflasherError(
            "Error setting flash parameters: {}".format(err))

    if not args.no_erase:
        try:
            esptool.erase_flash(stub_chip, mock_args)
        except esptool.FatalError as err:
            raise EsphomeflasherError(
                "Error while erasing flash: {}".format(err))

    try:
        esptool.write_flash(stub_chip, mock_args)
    except esptool.FatalError as err:
        raise EsphomeflasherError("Error while writing flash: {}".format(err))

    print("Hard Resetting...")
    stub_chip.hard_reset()

    print("Done! Flashing is complete!")
    print()

    show_logs(stub_chip._port)
def uart_upload(port, filename, baudrate, key=None, target=""):
    print_header("=================== FIRMWARE UPLOAD ===================")
    print_log("  Bin file '%s'" % filename)
    print_log("  Port %s @ %s" % (port, baudrate))

    cmd_reboot_to_bootloader = bootloader.get_init_seq('CRSF', key)

    if not os.path.exists(filename):
        msg = "[FAILED] file '%s' does not exist" % filename
        raise SystemExit(msg)

    detected_baud = baudrate

    chip_type, esp = esp_read_chip_id(port, detected_baud)
    if not chip_type:
        # Init Betaflight passthrough
        try:
            detected_baud = BFinitPassthrough.bf_passthrough_init(port, None)
            if detected_baud is None:
                detected_baud = baudrate
        except BFinitPassthrough.PassthroughEnabled as info:
            print_warning("  Warning: '%s'" % info)
        except BFinitPassthrough.PassthroughFailed as failed:
            raise SystemExit(failed)
        # Reset into bootloader
        bootloader.reset_to_bootloader(port, detected_baud,
                                       cmd_reboot_to_bootloader, target)
        # send_training_to_esp_rom(port, detected_baud)
        chip_type, esp = esp_read_chip_id(port, detected_baud)
        if not esp:
            raise SystemExit("Cannot connect to ESP")

    # Prepare to upload
    print_header("======== UPLOADING ========")
    if esp:
        _binary = open(os.path.abspath(filename), 'rb')

        class TempArgs():
            compress = None
            no_compress = True
            no_stub = False
            encrypt = False
            flash_size = "keep"
            flash_mode = "keep"
            flash_freq = "keep"
            addr_filename = []
            erase_all = False
            verify = False

        args = TempArgs()
        args.addr_filename.append((0, _binary))
        try:
            esptool.write_flash(esp, args)
            # flash_finish will trigger a soft reset
            esp.soft_reset(False)
        finally:
            _binary.close()
            esp._port.close()
        return
    # --------------------
    chip_type = ["auto", "esp8266"][chip_type.startswith("ESP82")]
    args = [
        "-b",
        str(detected_baud), "-p", port, "-c", chip_type, "--before",
        "no_reset", "--after", "soft_reset", "write_flash", "0x0",
        os.path.abspath(filename)
    ]
    cmd = " ".join(args)
    print_log("  Using command: '%s'\n" % cmd)
    esptool.main(args)
Пример #26
0
def run_esphomeflasher(argv):
    args = parse_args(argv)
    port = select_port(args)

    if args.show_logs:
        serial_port = serial.Serial(port, baudrate=115200)
        show_logs(serial_port)
        return

    try:
        firmware = open(args.binary, 'rb')
    except IOError as err:
        raise EsphomeflasherError("Error opening binary: {}".format(err))
    chip = detect_chip(port, args.esp8266, args.esp32)
    info = read_chip_info(chip)

    print()
    print("Chip Info:")
    print(" - Chip Family: {}".format(info.family))
    print(" - Chip Model: {}".format(info.model))
    if isinstance(info, ESP32ChipInfo):
        print(" - Number of Cores: {}".format(info.num_cores))
        print(" - Max CPU Frequency: {}".format(info.cpu_frequency))
        print(" - Has Bluetooth: {}".format(
            'YES' if info.has_bluetooth else 'NO'))
        print(" - Has Embedded Flash: {}".format(
            'YES' if info.has_embedded_flash else 'NO'))
        print(" - Has Factory-Calibrated ADC: {}".format(
            'YES' if info.has_factory_calibrated_adc else 'NO'))
    else:
        print(" - Chip ID: {:08X}".format(info.chip_id))

    print(" - MAC Address: {}".format(info.mac))

    stub_chip = chip_run_stub(chip)
    flash_size = None

    if args.upload_baud_rate != 115200:
        try:
            stub_chip.change_baud(args.upload_baud_rate)
        except esptool.FatalError as err:
            raise EsphomeflasherError(
                "Error changing ESP upload baud rate: {}".format(err))

        # Check if the higher baud rate works
        try:
            flash_size = detect_flash_size(stub_chip)
        except esptool.FatalError as err:
            # Go back to old baud rate by recreating chip instance
            print("Chip does not support baud rate {}, changing to 115200".
                  format(args.upload_baud_Rate))
            stub_chip._port.close()
            chip = detect_chip(port, args.esp8266, args.esp32)
            stub_chip = chip_run_stub(chip)

    if flash_size is None:
        flash_size = detect_flash_size(stub_chip)

    print(" - Flash Size: {}".format(flash_size))

    mock_args = configure_write_flash_args(info, firmware, flash_size,
                                           args.bootloader, args.partitions,
                                           args.otadata)

    print(" - Flash Mode: {}".format(mock_args.flash_mode))
    print(" - Flash Frequency: {}Hz".format(mock_args.flash_freq.upper()))

    try:
        stub_chip.flash_set_parameters(esptool.flash_size_bytes(flash_size))
    except esptool.FatalError as err:
        raise EsphomeflasherError(
            "Error setting flash parameters: {}".format(err))

    if not args.no_erase:
        try:
            esptool.erase_flash(stub_chip, mock_args)
        except esptool.FatalError as err:
            raise EsphomeflasherError(
                "Error while erasing flash: {}".format(err))

    try:
        esptool.write_flash(stub_chip, mock_args)
    except esptool.FatalError as err:
        raise EsphomeflasherError("Error while writing flash: {}".format(err))

    print("Hard Resetting...")
    stub_chip.hard_reset()

    print("Done! Flashing is complete!")
    print()

    if args.upload_baud_rate != 115200:
        stub_chip._port.baudrate = 115200
        time.sleep(0.05)  # get rid of crap sent during baud rate change
        stub_chip._port.flushInput()

    show_logs(stub_chip._port)
    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'