Skip to content

Commit

Permalink
spiFlash: added get_bp_mask to return default bp mask (unknown device…
Browse files Browse the repository at this point in the history
…) or compute mask based on bp_offset. Replace all manual mask compute. (#468)
  • Loading branch information
trabucayre committed Jul 4, 2024
1 parent 1b00940 commit f6b6f6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/spiFlash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ int SPIFlash::erase_and_prog(int base_addr, const uint8_t *data, int len)
}
} else { // unknown chip: basic test
printWarn("flash chip unknown: use basic protection detection");
if ((status & 0x1c) != 0)
if (get_bp() != 0)
must_relock = true;
}

Expand Down Expand Up @@ -701,9 +701,7 @@ int SPIFlash::disable_protection()
uint8_t data = read_status_reg();

/* only set to 0 bp bits */
uint8_t mask = 0;
for (int i = 0; i < _flash_model->bp_len; i++)
mask |= _flash_model->bp_offset[i];
uint8_t mask = get_bp_mask();
data &= ~mask;

if (write_enable() == -1)
Expand Down Expand Up @@ -774,9 +772,7 @@ int SPIFlash::enable_protection(uint32_t length)
/* keep existing STATR by reading register
* and applying mask
*/
uint8_t mask = 0;
for (int i = 0; i < _flash_model->bp_len; i++)
mask |= _flash_model->bp_offset[i];
uint8_t mask = get_bp_mask();
uint8_t tmp = read_status_reg();
tmp &= ~mask;

Expand Down Expand Up @@ -900,14 +896,7 @@ uint8_t SPIFlash::get_bp()
{
uint8_t mask = 0;
uint8_t status = read_status_reg();
if (!_flash_model) {
mask = 0x1C;
} else {
for (int i = 0; i < _flash_model->bp_len; i++)
mask |= _flash_model->bp_offset[i];
}

return (status & mask);
return (status & get_bp_mask());
}

/* convert bp area (status register) to len in byte */
Expand Down Expand Up @@ -964,6 +953,20 @@ uint8_t SPIFlash::len_to_bp(uint32_t len)
return tmp;
}

/* return bitmask (default for unknown device)
* or based on bp_offset (see spiFlashdb)
*/
uint8_t SPIFlash::get_bp_mask()
{
if (!_flash_model)
return 0x1C;

uint8_t mask = 0;
for (int i = 0; i < _flash_model->bp_len; i++)
mask |= _flash_model->bp_offset[i];
return mask;
}

/* microchip SST26VF032B has a dedicated register
* to read sectors (un)lock status and another one to unlock
* sectors
Expand Down
6 changes: 6 additions & 0 deletions src/spiFlash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ class SPIFlash {
*/
uint8_t get_bp();

/* \brief convert bp_offset (see spiFlashdb) to a mask
* \return bitmask (0x1c (default) for unknown device)
* or based on bp_offset (see spiFlashdb)
*/
uint8_t get_bp_mask();

public:
/*!
* \brief convert block protect to len in byte
Expand Down

0 comments on commit f6b6f6f

Please sign in to comment.