<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def flash_write(self, addr, data, nbits=None, flags=0):
 """Writes data to the flash region of a device. The given number of bits, if provided, must be either ``8``, ``16``, or ``32``. Args: self (JLink):
 the ``JLink`` instance addr (int):
 starting flash address to write to data (list):
 list of data units to write nbits (int):
 number of bits to use for each unit Returns: Number of bytes written to flash. """

        # This indicates that all data written from this point on will go into
        # the buffer of the flashloader of the DLL.
        self._dll.JLINKARM_BeginDownload(flags)

        self.memory_write(addr, data, nbits=nbits)

        # Start downloading the data into the flash memory.
        bytes_flashed = self._dll.JLINKARM_EndDownload()
        if bytes_flashed < 0:
            raise errors.JLinkFlashException(bytes_flashed)

        return bytes_flashed