code: def flash(self, data, addr, on_progress=None, power_on=False, flags=0):
          
          if flags != 0:
              raise errors.JLinkException('Flags are reserved for future use.')
  
          if on_progress is not None:
              # Set the function to be called on flash programming progress.
              func = enums.JLinkFunctions.FLASH_PROGRESS_PROTOTYPE(on_progress)
              self._dll.JLINK_SetFlashProgProgressCallback(func)
          else:
              self._dll.JLINK_SetFlashProgProgressCallback(0)
  
          # First power on the device.
          if power_on:
              self.power_on()
  
          try:
              # Stop the target before flashing.  This is required to be in a
              # try-catch as the 'halted()' check may fail with an exception.
              if not self.halted():
                  self.halt()
          except errors.JLinkException:
              pass
  
          res = self.flash_write(addr, data, flags=flags)
  
          return res
docs: Flashes the target device.
  
          The given ``on_progress`` callback will be called as
          ``on_progress(action, progress_string, percentage)`` periodically as the
          data is written to flash.  The action is one of ``Compare``, ``Erase``,
          ``Verify``, ``Flash``.
  
          Args:
            self (JLink): the ``JLink`` instance
            data (list): list of bytes to write to flash
            addr (int): start address on flash which to write the data
            on_progress (function): callback to be triggered on flash progress
            power_on (boolean): whether to power the target before flashing
            flags (int): reserved, do not use
  
          Returns:
            Number of bytes flashed.  This number may not necessarily be equal to
            ``len(data)``, but that does not indicate an error.
  
          Raises:
            JLinkException: on hardware errors.