From 2b80ce158b15890ed62fdc49785512a84cbd0235 Mon Sep 17 00:00:00 2001 From: Gwenhael Goavec-Merou Date: Mon, 3 Jun 2024 15:23:12 +0200 Subject: [PATCH] spiFlash: added ask before writting TB when OTP, added missing write_enable and fixed mask --- src/spiFlash.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/spiFlash.cpp b/src/spiFlash.cpp index d7bbbba0b1..7e027673d3 100644 --- a/src/spiFlash.cpp +++ b/src/spiFlash.cpp @@ -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; @@ -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; } } @@ -823,6 +830,10 @@ 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"); @@ -830,7 +841,7 @@ int SPIFlash::enable_protection(uint32_t length) } 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; }