Merge pull request #12919 from VeijoPesonen/fix_blockdevice

ExhaustibleBlockDevice: revert commit 10481f2
pull/12938/head
Martin Kojtal 2020-05-05 13:17:31 +02:00 committed by GitHub
commit 72d1918a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 63 deletions

View File

@ -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);
@ -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());
@ -110,8 +107,6 @@ 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));
@ -119,34 +114,22 @@ TEST_F(ExhaustibleBlockModuleTest, erase_cycle_limit_reached)
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);
}

View File

@ -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()
@ -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;
@ -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;
return 0;
}
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 ret;
return _bd->program(buffer, addr, size);
}
int ExhaustibleBlockDevice::erase(bd_addr_t addr, bd_size_t size)
@ -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;

View File

@ -158,7 +158,6 @@ public:
private:
BlockDevice *_bd;
uint32_t *_erase_array;
bool *_programmable_array;
uint32_t _erase_cycles;
uint32_t _init_ref_count;
bool _is_initialized;