diff --git a/include/sta/drivers/w25qxx.hpp b/include/sta/drivers/w25qxx.hpp index 01fa958..eff2ca5 100644 --- a/include/sta/drivers/w25qxx.hpp +++ b/include/sta/drivers/w25qxx.hpp @@ -4,6 +4,8 @@ #include #include +#include + namespace sta { @@ -53,8 +55,21 @@ namespace sta */ W25Qxx(SPIDevice * device, DelayUsFunc delay, AddressMode addrMode = AddressMode::_24BIT); + /** + * @brief Initialize the flash chip. + * + * @return uint8_t Returns 1 if successful, 0 otherwise. + */ uint8_t init(); + /** + * @brief Find the last page satisfying the given criterion. Uses a binary search to find this page. + * + * @param criterion A function evaluating the criterion on a page. + * @return uint32_t The last address such that the criterion is satisfied. + */ + uint32_t findLastPage(std::function criterion); + /** * @brief Set the Address Mode object * @@ -173,8 +188,6 @@ namespace sta * @return uint8_t */ uint8_t pageProgram(uint32_t address, uint8_t * buffer, size_t length); - - public: /* * Erase operations diff --git a/src/w25qxx.cpp b/src/w25qxx.cpp index fefc00c..7acfaa1 100644 --- a/src/w25qxx.cpp +++ b/src/w25qxx.cpp @@ -44,6 +44,31 @@ namespace sta return 1; } + uint32_t W25Qxx::findLastPage(std::function criterion) + { + uint32_t left = 0; + uint32_t right = 0xFFFFFFFF & (addrMode_ == AddressMode::_32BIT ? 0xFFFFFFFF : 0x00FFFFFF); + uint32_t middle; + uint8_t * page = new uint8_t[256]; + + while (left < right) + { + middle = (left + right) / 2 + 1; + readData(middle, page, 256); + + if (criterion(page)) + { + left = middle; + } + else + { + right = middle-1; + } + } + + return middle; + } + uint8_t W25Qxx::setAddressMode(AddressMode addrMode) { busWrite(W25QXX_4_BYTE_ADDR_ENABLE);