From adf409f7f7a6df42c2dc3239a56d9783ca13185b Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Thu, 21 Nov 2019 09:51:02 +0200 Subject: [PATCH] Do not require Flash device for TDBStore TDBStore used to rely on Flash devices erase value. This logic has been removed, and TDBStore can do the entire erase logic itself, in case the given BlockDevice does not offer erase(). This relies on BlockDevice to properly return -1 in BlockDevice::get_erase_value(). --- .../storage/kvstore/tdbstore/TDBStore.cpp | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/features/storage/kvstore/tdbstore/TDBStore.cpp b/features/storage/kvstore/tdbstore/TDBStore.cpp index e62b6b2301..9846701a14 100644 --- a/features/storage/kvstore/tdbstore/TDBStore.cpp +++ b/features/storage/kvstore/tdbstore/TDBStore.cpp @@ -177,7 +177,22 @@ int TDBStore::erase_erase_unit(uint8_t area, uint32_t offset) uint32_t bd_offset = _area_params[area].address + offset; uint32_t eu_size = _buff_bd->get_erase_size(bd_offset); - return _buff_bd->erase(bd_offset, eu_size); + if (_buff_bd->get_erase_value() != -1) { + return _buff_bd->erase(bd_offset, eu_size); + } else { + // We need to simulate erase, as our block device + // does not do it. We can do this one byte at a time + // because we use BufferedBlockDevice that has page buffers + uint8_t val = 0xff; + int ret; + for (; eu_size; --eu_size) { + ret = _buff_bd->program(&val, bd_offset++, 1); + if (ret) { + return ret; + } + } + } + return MBED_SUCCESS; } void TDBStore::calc_area_params() @@ -1017,11 +1032,6 @@ int TDBStore::init() goto fail; } - // Underlying BD must have flash attributes, i.e. have an erase value - if (_bd->get_erase_value() == -1) { - MBED_ERROR(MBED_ERROR_INVALID_ARGUMENT, "Underlying BD must have flash attributes"); - } - _prog_size = _bd->get_program_size(); _work_buf = new uint8_t[work_buf_size]; _key_buf = new char[MAX_KEY_SIZE];