From f6b6f6f9d8e3de198b5258829fffc7e844ef75c6 Mon Sep 17 00:00:00 2001 From: Gwenhael Goavec-Merou Date: Thu, 4 Jul 2024 08:17:49 +0200 Subject: [PATCH] spiFlash: added get_bp_mask to return default bp mask (unknown device) or compute mask based on bp_offset. Replace all manual mask compute. (#468) --- src/spiFlash.cpp | 33 ++++++++++++++++++--------------- src/spiFlash.hpp | 6 ++++++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/spiFlash.cpp b/src/spiFlash.cpp index 8c09bed02d..528e1d5628 100644 --- a/src/spiFlash.cpp +++ b/src/spiFlash.cpp @@ -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; } @@ -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) @@ -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; @@ -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 */ @@ -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 diff --git a/src/spiFlash.hpp b/src/spiFlash.hpp index 2ef040b311..9f620196ba 100644 --- a/src/spiFlash.hpp +++ b/src/spiFlash.hpp @@ -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