How to read and write flash write protection bit (CMP)

SpaeniDario
Posts: 8
Joined: Wed Feb 10, 2021 1:42 pm

How to read and write flash write protection bit (CMP)

Postby SpaeniDario » Thu Mar 21, 2024 4:23 pm

Hi,

This is a follow-up question to thread: https://esp32.com/viewtopic.php?f=12&t=37286.

I'm trying to read and write the write-protect bit "CMP" from the flash status-register in my own program. The equivalent functions for the esptool are read_flash_status/write_flash_status.
The status I want to achieve is either 0x0200 (write protection disabled) or 0x4200 (write protection enabled) when read with read_flash_status.

Are there any functions to do this properly? I'd prefer not having to modify the idf-components for this...

So far, I've found out the following:
  • The flash chip in my ESP is a BYTe BY25Q64AS (ID 0x684017), which (mostly?) uses the generic flash-functions.
  • The write-protect bit I want is bit 14 (CMP), with value 0 = write-protection disabled, 1 = write-protection enabled.
  • The function esp_flash_get_chip_write_protect() returns the state of the Write Enable Latch bit (WEL, bit 1), by requesting the bits 0-7 of the flash status register (Command CMD_RDSR, 0x05). This seems to always return true after a restart (logic is inverted here, true means the bit is not set). If I'm reading the datasheet correctly then this bit needs to be set before any other write to the status-registers.
  • I've modified the function memspi_host_read_status_hs() in memspi_host_driver.c (gets called in esp_flash_get_chip_write_protect()) to read the bits 8-15 (Command CMD_RDSR2, 0x35), which correctly returns the byte containing bit CMP. I've verified that the value is read properly by changing the status-register using esptool write_flash_status and then checking using this modified function.
  • The function esp_rom_spiflash_read_statushigh() in spi_flash.h would read the correct register, but it says "Please do not call this function in SDK."
  • Likewise, the functions esp_rom_spiflash_unlock() and esp_rom_spiflash_lock() also have that comment, and they are setting different bits anyway.
I'm using esp-idf 4.4.3, but the relevant code seems to be the same up to master.

Who is online

Users browsing this forum: MicroController and 218 guests