Skip to content

ExhaustibleBlockDevice: revert commit 10481f2 #12919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ TEST_F(ExhaustibleBlockModuleTest, init)
EXPECT_EQ(b.get_read_size(), 0);
EXPECT_EQ(b.get_program_size(), 0);
EXPECT_EQ(b.size(), 0);
b.set_erase_cycles(0, 100); // This should not take effect.
EXPECT_EQ(b.get_erase_cycles(0), 0);
EXPECT_EQ(b.erase(0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
EXPECT_EQ(b.program(magic, 0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
EXPECT_EQ(b.read(buf, 0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
Expand All @@ -95,7 +93,6 @@ TEST_F(ExhaustibleBlockModuleTest, init)
EXPECT_EQ(b.get_erase_size(), bd_mock.get_erase_size());
EXPECT_EQ(b.get_erase_size(0), bd_mock.get_erase_size(0));
EXPECT_EQ(b.get_erase_value(), bd_mock.get_erase_value());
EXPECT_NE(b.get_erase_cycles(0), 100);
EXPECT_EQ(b.get_program_size(), 512);
EXPECT_EQ(b.get_read_size(), 512);
EXPECT_EQ(b.size(), bd_mock.size());
Expand All @@ -110,43 +107,29 @@ TEST_F(ExhaustibleBlockModuleTest, program_unaligned)

TEST_F(ExhaustibleBlockModuleTest, erase_cycle_limit_reached)
{
EXPECT_NE(bd.get_erase_cycles(0), 0);

EXPECT_CALL(bd_mock, program(_, 0, BLOCK_SIZE))
.Times(1)
.WillRepeatedly(Return(BD_ERROR_OK));

EXPECT_EQ(bd.program(magic, 0, BLOCK_SIZE), 0);

EXPECT_CALL(bd_mock, erase(0, BLOCK_SIZE))
.Times(ERASE_CYCLES)
.Times(ERASE_CYCLES - 1) // Will fall silently after erase cycles are worn out.
.WillRepeatedly(Return(BD_ERROR_OK));

for (int i = 0; i < ERASE_CYCLES; i++) {
EXPECT_EQ(bd.erase(0, BLOCK_SIZE), 0);
}
EXPECT_EQ(bd.get_erase_cycles(0), 0);

// This calls are expect not to happen.
EXPECT_EQ(bd.erase(0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
EXPECT_EQ(bd.program(magic2, 0, BLOCK_SIZE), BD_ERROR_DEVICE_ERROR);
// Erase silently fails, no error report.
EXPECT_EQ(bd.erase(0, BLOCK_SIZE), BD_ERROR_OK);
EXPECT_EQ(bd.program(magic2, 0, BLOCK_SIZE), BD_ERROR_OK);
}

TEST_F(ExhaustibleBlockModuleTest, erase_invalid)
{
ASSERT_GT(ERASE_CYCLES, 1);

EXPECT_EQ(bd.get_erase_cycles(0), ERASE_CYCLES);

// Unaligned erase should fail
EXPECT_EQ(bd.erase(0, BLOCK_SIZE-1), BD_ERROR_DEVICE_ERROR);
// Number of erase cycles should not change
EXPECT_EQ(bd.get_erase_cycles(0), ERASE_CYCLES);
}

TEST_F(ExhaustibleBlockModuleTest, set_erase_cycles)
{
EXPECT_EQ(bd.get_erase_cycles(0), ERASE_CYCLES);
bd.set_erase_cycles(0, ERASE_CYCLES+1);
EXPECT_EQ(bd.get_erase_cycles(0), ERASE_CYCLES+1);
}
47 changes: 6 additions & 41 deletions features/storage/blockdevice/ExhaustibleBlockDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,13 @@
namespace mbed {

ExhaustibleBlockDevice::ExhaustibleBlockDevice(BlockDevice *bd, uint32_t erase_cycles)
: _bd(bd), _erase_array(NULL), _programmable_array(NULL), _erase_cycles(erase_cycles),
_init_ref_count(0), _is_initialized(false)
: _bd(bd), _erase_array(NULL), _erase_cycles(erase_cycles), _init_ref_count(0), _is_initialized(false)
{
}

ExhaustibleBlockDevice::~ExhaustibleBlockDevice()
{
delete[] _erase_array;
delete[] _programmable_array;
}

uint32_t ExhaustibleBlockDevice::get_erase_cycles(bd_addr_t addr) const
{
if (!_is_initialized) {
return 0;
}
return _erase_array[addr / get_erase_size()];
}

void ExhaustibleBlockDevice::set_erase_cycles(bd_addr_t addr, uint32_t cycles)
{
if (!_is_initialized) {
return;
}
_erase_array[addr / get_erase_size()] = cycles;
}

int ExhaustibleBlockDevice::init()
Expand All @@ -70,13 +52,6 @@ int ExhaustibleBlockDevice::init()
}
}

if (!_programmable_array) {
_programmable_array = new bool[_bd->size() / _bd->get_erase_size()];
for (size_t i = 0; i < _bd->size() / _bd->get_erase_size(); i++) {
_programmable_array[i] = true;
}
}

_is_initialized = true;
return BD_ERROR_OK;

Expand Down Expand Up @@ -133,20 +108,10 @@ int ExhaustibleBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_
}

if (_erase_array[addr / get_erase_size()] == 0) {
return BD_ERROR_DEVICE_ERROR;
}

if (!_programmable_array[addr / get_erase_size()]) {
return BD_ERROR_DEVICE_ERROR;
}

int ret = _bd->program(buffer, addr, size);

if (ret == BD_ERROR_OK) {
_programmable_array[addr / get_erase_size()] = false;
return 0;
}

return ret;
return _bd->program(buffer, addr, size);
}

int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size)
Expand All @@ -164,13 +129,13 @@ int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size)
// use an erase cycle
if (_erase_array[addr / eu_size] > 0) {
_erase_array[addr / eu_size] -= 1;
}

if (_erase_array[addr / eu_size] > 0) {
int err = _bd->erase(addr, eu_size);
if (err) {
return err;
}
_programmable_array[addr / get_erase_size()] = true;
} else {
return BD_ERROR_DEVICE_ERROR;
}

addr += eu_size;
Expand Down
1 change: 0 additions & 1 deletion features/storage/blockdevice/ExhaustibleBlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class ExhaustibleBlockDevice : public BlockDevice {
private:
BlockDevice *_bd;
uint32_t *_erase_array;
bool *_programmable_array;
uint32_t _erase_cycles;
uint32_t _init_ref_count;
bool _is_initialized;
Expand Down