'Write up to 24 bits (num_bytes) of new status register
num_bytes can be 1, 2 or 3.
Not all flash supports the additional commands to write the
second and third byte of the status register. When writing 2
bytes, esptool also sends a 16-byte WRSR command (as some
flash types use this instead of WRSR2.)
If the set_non_volatile flag is set, non-volatile bits will
be set as well as volatile ones (WREN used instead of WEVSR).'

def write_status(self, new_status, num_bytes=2, set_non_volatile=False):

    SPIFLASH_WRSR = 1
    SPIFLASH_WRSR2 = 49
    SPIFLASH_WRSR3 = 17
    SPIFLASH_WEVSR = 80
    SPIFLASH_WREN = 6
    SPIFLASH_WRDI = 4
    enable_cmd = (SPIFLASH_WREN if set_non_volatile else SPIFLASH_WEVSR)
    if (num_bytes == 2):
        self.run_spiflash_command(enable_cmd)
        self.run_spiflash_command(SPIFLASH_WRSR, struct.pack('<H', new_status))
    for cmd in [SPIFLASH_WRSR, SPIFLASH_WRSR2, SPIFLASH_WRSR3][0:num_bytes]:
        self.run_spiflash_command(enable_cmd)
        self.run_spiflash_command(cmd, struct.pack('B', (new_status & 255)))
        new_status >>= 8
    self.run_spiflash_command(SPIFLASH_WRDI)
