diff --git a/include/sta/sensors/w25qxx.hpp b/include/sta/sensors/w25qxx.hpp index be7467e..35162b6 100644 --- a/include/sta/sensors/w25qxx.hpp +++ b/include/sta/sensors/w25qxx.hpp @@ -58,12 +58,32 @@ namespace sta */ uint8_t setAddressMode(AddressMode addrMode); + /** + * @brief Get the Address Mode object + * + * @return AddressMode + */ AddressMode getAddressMode(); + /** + * @brief Get the Chip I D object + * + * @return uint8_t + */ uint8_t getChipID(); + /** + * @brief Get the Manufacturer I D object + * + * @return uint8_t + */ uint8_t getManufacturerID(); + /** + * @brief Get the Unique I D object + * + * @return uint64_t + */ uint64_t getUniqueID(); // TODO: SFDP register? @@ -210,8 +230,10 @@ namespace sta // Read security registers // Indiv Block / Sector lock + uint8_t lockBlock(uint32_t address); // Indiv Block / Sector unlock + uint8_t unlockBlock(uint32_t address); // Indiv Block / Sector lock read diff --git a/src/w25qxx.cpp b/src/w25qxx.cpp index bded378..b2eec16 100644 --- a/src/w25qxx.cpp +++ b/src/w25qxx.cpp @@ -146,6 +146,8 @@ namespace sta return 0; } + while (!isWriteEnabled()) {} + if (addrMode_ == AddressMode::_32BIT) { uint8_t addrBuffer[4] = { @@ -256,6 +258,8 @@ namespace sta return 0; } + while (!isWriteEnabled()) {} + if (!nonvolatile) { uint8_t rslt = writeVolatileEnable(); @@ -286,7 +290,10 @@ namespace sta bool W25Qxx::isWriteEnabled() { - return true; // TODO + uint8_t status = 0; + readStatusRegister(1, &status); + + return (0x02 && status) == 0x02; } uint8_t W25Qxx::sectorErase(uint32_t address) @@ -296,6 +303,8 @@ namespace sta return 0; } + while (!isWriteEnabled()) {} + if (addrMode_ == AddressMode::_32BIT) { uint8_t addrBuffer[4] = { @@ -328,6 +337,8 @@ namespace sta return 0; } + while (!isWriteEnabled()) {} + uint8_t instruction = blockSize == BlockSize::_32KB ? W25QXX_BLOCK_ERASE_32_KB : W25QXX_BLOCK_ERASE_64_KB; if (addrMode_ == AddressMode::_32BIT) @@ -389,4 +400,40 @@ namespace sta return 0; } + + uint8_t W25Qxx::lockBlock(uint32_t address) + { + if (!writeEnable()) + { + return 0; + } + + while (!isWriteEnabled()) {} + + uint8_t addrBuffer[3] = { + (uint8_t) (address << 16), + (uint8_t) (address << 8), + (uint8_t) (address) + }; + + return busWrite(W25QXX_INDIV_BLOCK_LOCK, addrBuffer, 3); + } + + uint8_t W25Qxx::unlockBlock(uint32_t address) + { + if (!writeEnable()) + { + return 0; + } + + while (!isWriteEnabled()) {} + + uint8_t addrBuffer[3] = { + (uint8_t) (address << 16), + (uint8_t) (address << 8), + (uint8_t) (address) + }; + + return busWrite(W25QXX_INDIV_BLOCK_UNLOCK, addrBuffer, 3); + } } // namespace sta