Added search functionality using binary search

This commit is contained in:
dario 2024-05-07 20:12:17 +02:00
parent ad76f8e12f
commit d88de338e9
2 changed files with 40 additions and 2 deletions

View File

@ -4,6 +4,8 @@
#include <sta/bus/spi/device.hpp>
#include <sta/drivers/w25qxx_defs.hpp>
#include <functional>
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<bool(uint8_t*)> 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

View File

@ -44,6 +44,31 @@ namespace sta
return 1;
}
uint32_t W25Qxx::findLastPage(std::function<bool(uint8_t*)> 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);