Skip to content

Commit

Permalink
spiFlash: added ask before writting TB when OTP, added missing write_…
Browse files Browse the repository at this point in the history
…enable and fixed mask
  • Loading branch information
Gwenhael Goavec-Merou committed Jun 3, 2024
1 parent be188c0 commit 2b80ce1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/spiFlash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ int SPIFlash::sectors_erase(int base_addr, int size)
if (!sector_rdy)
step = 0x1000;

printf("start addr: %08x, end_addr: %08x\n", base_addr, (base_addr + size + 0xffff) & ~0xffff);

for (int addr = start_addr; addr < end_addr; addr += step) {
if (write_enable() == -1) {
ret = -1;
Expand Down Expand Up @@ -770,8 +772,13 @@ int SPIFlash::enable_protection(uint32_t length)
uint8_t tb = get_tb();
/* check if TB is set */
if (tb == 0) {
printError("TOP/BOTTOM bit is OTP: can't write this bit");
return -1;
std::string confirm{};
printError("TOP/BOTTOM bit is OTP: changing this bit is irreversible");
printError("Please confirm modification y/n");
std::cin >> confirm;

if (confirm != "y")
return -1;
}
}

Expand Down Expand Up @@ -823,14 +830,18 @@ int SPIFlash::enable_protection(uint32_t length)
}

/* write status register and wait until Flash idle */
if (write_enable() != 0) {
printError("Error: failed to enable write");
return -1;
}
_spi->spi_put(reg_wr, &val, NULL, 1);
if (_spi->spi_wait(FLASH_RDSR, 0x03, 0, 1000) < 0) {
printError("Error: enable protection failed\n");
return -1;
}
uint8_t rd_val;
_spi->spi_put(reg_rd, NULL, &rd_val, 1);
if (rd_val != val) {
if ((rd_val & val) == 0) {
printError("failed to update TB bit");
return -1;
}
Expand Down

0 comments on commit 2b80ce1

Please sign in to comment.