mirror of https://github.com/ARMmbed/mbed-os.git
Merge pull request #8524 from korjaa/more_blockdevice_tests
Add more generic BlockDevice tests.pull/8692/head
commit
73cc54e5f7
|
@ -164,6 +164,7 @@ DataFlashBlockDevice::DataFlashBlockDevice(PinName mosi,
|
|||
|
||||
int DataFlashBlockDevice::init()
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("init\r\n");
|
||||
|
||||
if (!_is_initialized) {
|
||||
|
@ -173,6 +174,7 @@ int DataFlashBlockDevice::init()
|
|||
uint32_t val = core_util_atomic_incr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val != 1) {
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
|
@ -281,33 +283,40 @@ int DataFlashBlockDevice::init()
|
|||
_is_initialized = true;
|
||||
}
|
||||
|
||||
_mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
int DataFlashBlockDevice::deinit()
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("deinit\r\n");
|
||||
|
||||
if (!_is_initialized) {
|
||||
_init_ref_count = 0;
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
uint32_t val = core_util_atomic_decr_u32(&_init_ref_count, 1);
|
||||
|
||||
if (val) {
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
_is_initialized = false;
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_OK;
|
||||
}
|
||||
|
||||
int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("read: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size);
|
||||
|
||||
if (!_is_initialized) {
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
|
@ -345,14 +354,17 @@ int DataFlashBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
|||
result = BD_ERROR_OK;
|
||||
}
|
||||
|
||||
_mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("program: %p %" PRIX64 " %" PRIX64 "\r\n", buffer, addr, size);
|
||||
|
||||
if (!_is_initialized) {
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
|
@ -411,14 +423,17 @@ int DataFlashBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t
|
|||
_write_enable(false);
|
||||
}
|
||||
|
||||
_mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
int DataFlashBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("erase: %" PRIX64 " %" PRIX64 "\r\n", addr, size);
|
||||
|
||||
if (!_is_initialized) {
|
||||
_mutex.unlock();
|
||||
return BD_ERROR_DEVICE_ERROR;
|
||||
}
|
||||
|
||||
|
@ -465,6 +480,7 @@ int DataFlashBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
|||
_write_enable(false);
|
||||
}
|
||||
|
||||
_mutex.unlock();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -484,23 +500,29 @@ bd_size_t DataFlashBlockDevice::get_program_size() const
|
|||
|
||||
bd_size_t DataFlashBlockDevice::get_erase_size() const
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("erase size: %" PRIX16 "\r\n", _block_size);
|
||||
|
||||
return _block_size;
|
||||
bd_size_t block_size = _block_size;
|
||||
_mutex.unlock();
|
||||
return block_size;
|
||||
}
|
||||
|
||||
bd_size_t DataFlashBlockDevice::get_erase_size(bd_addr_t addr) const
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("erase size: %" PRIX16 "\r\n", _block_size);
|
||||
|
||||
return _block_size;
|
||||
bd_size_t block_size = _block_size;
|
||||
_mutex.unlock();
|
||||
return block_size;
|
||||
}
|
||||
|
||||
bd_size_t DataFlashBlockDevice::size() const
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("device size: %" PRIX32 "\r\n", _device_size);
|
||||
|
||||
return _device_size;
|
||||
bd_size_t device_size = _device_size;
|
||||
_mutex.unlock();
|
||||
return device_size;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -512,6 +534,7 @@ bd_size_t DataFlashBlockDevice::size() const
|
|||
*/
|
||||
uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
|
||||
{
|
||||
_mutex.lock();
|
||||
DEBUG_PRINTF("_get_register: %" PRIX8 "\r\n", opcode);
|
||||
|
||||
/* activate device */
|
||||
|
@ -527,6 +550,7 @@ uint16_t DataFlashBlockDevice::_get_register(uint8_t opcode)
|
|||
/* deactivate device */
|
||||
_cs = 1;
|
||||
|
||||
_mutex.unlock();
|
||||
return status;
|
||||
}
|
||||
|
||||
|
|
|
@ -173,6 +173,9 @@ private:
|
|||
int _sync(void);
|
||||
int _write_page(const uint8_t *buffer, uint32_t addr, uint32_t offset, uint32_t size);
|
||||
uint32_t _translate_address(bd_addr_t addr);
|
||||
|
||||
// Mutex for thread safety
|
||||
mutable PlatformMutex _mutex;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -13,12 +13,16 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef __STDC_FORMAT_MACROS
|
||||
#define __STDC_FORMAT_MACROS //Required for PRIu64
|
||||
#endif
|
||||
|
||||
#include "mbed.h"
|
||||
#include "greentea-client/test_env.h"
|
||||
#include "unity.h"
|
||||
#include "utest.h"
|
||||
#include "mbed_trace.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
using namespace utest::v1;
|
||||
|
@ -209,6 +213,212 @@ void test_multi_threads()
|
|||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
void test_get_erase_value()
|
||||
{
|
||||
utest_printf("\nTest BlockDevice::get_erase_value()..\n");
|
||||
|
||||
// Test flow:
|
||||
// 1. Write data to selected region
|
||||
// - Known starting point
|
||||
// 2. Erase selected region
|
||||
// 3. Read erased region and compare with get_erase_value()
|
||||
|
||||
BlockDevice *block_device = BlockDevice::get_default_instance();
|
||||
TEST_SKIP_UNLESS_MESSAGE(block_device != NULL, "\nno block device found.\n");
|
||||
|
||||
int err = block_device->init();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Check erase value
|
||||
int erase_value_int = block_device->get_erase_value();
|
||||
utest_printf("\nblock_device->get_erase_value()=%d", erase_value_int);
|
||||
TEST_SKIP_UNLESS_MESSAGE(erase_value_int >= 0, "\nerase value is negative which means the erase value is unknown\n");
|
||||
|
||||
// Assuming that get_erase_value() returns byte value as documentation mentions
|
||||
// "If get_erase_value() returns a non-negative byte value" for unknown case.
|
||||
TEST_ASSERT(erase_value_int <= 255);
|
||||
uint8_t erase_value = (uint8_t)erase_value_int;
|
||||
|
||||
// Determine data_buf_size
|
||||
bd_size_t erase_size = block_device->get_erase_size();
|
||||
TEST_ASSERT(erase_size > 0);
|
||||
bd_size_t data_buf_size = erase_size;
|
||||
|
||||
// Determine start_address
|
||||
bd_addr_t start_address = rand(); // low 32 bytes
|
||||
start_address += (uint64_t)rand() << 32; // high 32 bytes
|
||||
start_address %= block_device->size() - data_buf_size - erase_size; // fit all data + alignment reserve
|
||||
start_address += erase_size; // add alignment reserve
|
||||
start_address -= start_address % erase_size; // align with erase_block
|
||||
utest_printf("\nstart_address=0x%016" PRIx64, start_address);
|
||||
|
||||
// Allocate buffer for read test data
|
||||
uint8_t *data_buf = (uint8_t*)malloc(data_buf_size);
|
||||
TEST_ASSERT_NOT_NULL(data_buf);
|
||||
|
||||
// Write random data to selected region to make sure data is not accidentally set to "erased" value.
|
||||
// With this pre-write, the test case will fail even if block_device->erase() is broken.
|
||||
for (bd_size_t i=0; i<data_buf_size; i++) {
|
||||
data_buf[i] = (uint8_t) rand();
|
||||
}
|
||||
utest_printf("\nwriting given memory region");
|
||||
err = block_device->program((const void*)data_buf, start_address, data_buf_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Erase given memory region
|
||||
utest_printf("\nerasing given memory region");
|
||||
err = block_device->erase(start_address, data_buf_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Read erased memory region
|
||||
utest_printf("\nreading erased memory region");
|
||||
err = block_device->read((void*)data_buf, start_address, data_buf_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Verify erased memory region
|
||||
utest_printf("\nverifying erased memory region");
|
||||
for (bd_size_t i=0; i<data_buf_size; i++) {
|
||||
TEST_ASSERT_EQUAL(erase_value, data_buf[i]);
|
||||
}
|
||||
|
||||
free(data_buf);
|
||||
|
||||
// BlockDevice deinitialization
|
||||
err = block_device->deinit();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
void test_contiguous_erase_write_read()
|
||||
{
|
||||
utest_printf("\nTest Contiguous Erase/Program/Read Starts..\n");
|
||||
|
||||
// Test flow:
|
||||
// 1. Erase whole test area
|
||||
// - Tests contiguous erase
|
||||
// 2. Write smaller memory area
|
||||
// - Tests contiguous sector writes
|
||||
// 3. Rerun step 2 for whole erase region
|
||||
|
||||
BlockDevice *block_device = BlockDevice::get_default_instance();
|
||||
TEST_SKIP_UNLESS_MESSAGE(block_device != NULL, "\nno block device found.\n");
|
||||
|
||||
// Initialize BlockDevice
|
||||
int err = block_device->init();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Test parameters
|
||||
bd_size_t erase_size = block_device->get_erase_size();
|
||||
TEST_ASSERT(erase_size > 0);
|
||||
bd_size_t program_size = block_device->get_program_size();
|
||||
TEST_ASSERT(program_size > 0);
|
||||
utest_printf("\nerase_size=%d", erase_size);
|
||||
utest_printf("\nprogram_size=%d", program_size);
|
||||
utest_printf("\nblock_device->size()=%" PRId64, block_device->size());
|
||||
|
||||
// Determine write/read buffer size
|
||||
// start write_read_buf_size from 1% block_device->size()
|
||||
bd_size_t write_read_buf_size = block_device->size() / 100; // 1%, 10k=100, 100k=1k, 1MB=10k, 32MB=32k
|
||||
// try to limit write_read_buf_size to 10k. If program_size*2 is larger than 10k, that will be used instead.
|
||||
if (write_read_buf_size > 10000) {
|
||||
write_read_buf_size = 10000;
|
||||
}
|
||||
// 2 program_size blocks is minimum for contiguous write/read test
|
||||
if (write_read_buf_size < program_size*2) {
|
||||
write_read_buf_size = program_size*2; // going over 10k
|
||||
}
|
||||
bd_size_t contiguous_write_read_blocks_per_region = write_read_buf_size / program_size; // 2 is minimum to test contiguous write
|
||||
write_read_buf_size = contiguous_write_read_blocks_per_region * program_size;
|
||||
utest_printf("\ncontiguous_write_read_blocks_per_region=%" PRIu64, contiguous_write_read_blocks_per_region);
|
||||
utest_printf("\nwrite_read_buf_size=%" PRIu64, write_read_buf_size);
|
||||
|
||||
// Determine test region count
|
||||
int contiguous_write_read_regions = TEST_BLOCK_COUNT;
|
||||
utest_printf("\ncontiguous_write_read_regions=%d", contiguous_write_read_regions);
|
||||
|
||||
// Determine whole erase size
|
||||
bd_size_t contiguous_erase_size = write_read_buf_size * contiguous_write_read_regions;
|
||||
contiguous_erase_size -= contiguous_erase_size % erase_size; // aligned to erase_size
|
||||
contiguous_erase_size += erase_size; // but larger than write/read size * regions
|
||||
utest_printf("\ncontiguous_erase_size=%" PRIu64, contiguous_erase_size);
|
||||
|
||||
// Determine starting address
|
||||
bd_addr_t start_address = rand(); // low 32 bytes
|
||||
start_address += (uint64_t)rand() << 32; // high 32 bytes
|
||||
start_address %= block_device->size() - contiguous_erase_size - erase_size; // fit all data + alignment reserve
|
||||
start_address += erase_size; // add alignment reserve
|
||||
start_address -= start_address % erase_size; // align with erase_block
|
||||
bd_addr_t stop_address = start_address + write_read_buf_size * contiguous_write_read_regions;
|
||||
utest_printf("\nstart_address=0x%016" PRIx64, start_address);
|
||||
utest_printf("\nstop_address=0x%016" PRIx64, stop_address);
|
||||
|
||||
// Allocate write/read buffer
|
||||
uint8_t *write_read_buf = (uint8_t*)malloc(write_read_buf_size);
|
||||
if (write_read_buf == NULL) {
|
||||
block_device->deinit();
|
||||
TEST_SKIP_MESSAGE("\nnot enough memory for test");
|
||||
}
|
||||
utest_printf("\nwrite_read_buf_size=%" PRIu64 "", (uint64_t)write_read_buf_size);
|
||||
|
||||
// Pre-fill the to-be-erased region. By pre-filling the region,
|
||||
// we can be sure the test will not pass if the erase doesn't work.
|
||||
for (bd_size_t offset=0; start_address+offset < stop_address; offset+=write_read_buf_size) {
|
||||
for (size_t i=0; i<write_read_buf_size; i++) {
|
||||
write_read_buf[i] = (uint8_t)rand();
|
||||
}
|
||||
utest_printf("\npre-filling memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address+offset, write_read_buf_size);
|
||||
err = block_device->program((const void*)write_read_buf, start_address+offset, write_read_buf_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
// Erase the whole region first
|
||||
utest_printf("\nerasing memory, from 0x%" PRIx64 " of size 0x%" PRIx64, start_address, contiguous_erase_size);
|
||||
err = block_device->erase(start_address, contiguous_erase_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Loop through all write/read regions
|
||||
int region = 0;
|
||||
for (; start_address < stop_address; start_address+=write_read_buf_size) {
|
||||
utest_printf("\n\nregion #%d start_address=0x%016" PRIx64, region++, start_address);
|
||||
|
||||
// Generate test data
|
||||
unsigned int seed = rand();
|
||||
utest_printf("\ngenerating test data, seed=%u", seed);
|
||||
srand(seed);
|
||||
for (size_t i=0; i<write_read_buf_size; i++) {
|
||||
write_read_buf[i] = (uint8_t)rand();
|
||||
}
|
||||
|
||||
// Write test data
|
||||
utest_printf("\nwriting test data");
|
||||
err = block_device->program((const void*)write_read_buf, start_address, write_read_buf_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Read test data
|
||||
memset(write_read_buf, 0, (size_t)write_read_buf_size);
|
||||
utest_printf("\nreading test data");
|
||||
err = block_device->read(write_read_buf, start_address, write_read_buf_size);
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
|
||||
// Verify read data
|
||||
utest_printf("\nverifying test data");
|
||||
srand(seed);
|
||||
for (size_t i=0; i<write_read_buf_size; i++) {
|
||||
uint8_t expected_value = (uint8_t)rand();
|
||||
if (write_read_buf[i] != expected_value) {
|
||||
utest_printf("\ndata verify failed, write_read_buf[%d]=%" PRIu8 " and not %" PRIu8 "\n",
|
||||
i, write_read_buf[i], expected_value);
|
||||
}
|
||||
TEST_ASSERT_EQUAL(write_read_buf[i], expected_value);
|
||||
}
|
||||
utest_printf("\nverify OK");
|
||||
}
|
||||
|
||||
free(write_read_buf);
|
||||
|
||||
// BlockDevice deinitialization
|
||||
err = block_device->deinit();
|
||||
TEST_ASSERT_EQUAL(0, err);
|
||||
}
|
||||
|
||||
// Test setup
|
||||
utest::v1::status_t test_setup(const size_t number_of_cases)
|
||||
|
@ -219,7 +429,9 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
|
|||
|
||||
Case cases[] = {
|
||||
Case("Testing read write random blocks", test_random_program_read_erase),
|
||||
Case("Testing Multi Threads Erase Program Read", test_multi_threads)
|
||||
Case("Testing Multi Threads Erase Program Read", test_multi_threads),
|
||||
Case("Testing contiguous erase, write and read", test_contiguous_erase_write_read),
|
||||
Case("Test BlockDevice::get_erase_value()", test_get_erase_value)
|
||||
};
|
||||
|
||||
Specification specification(test_setup, cases);
|
||||
|
|
Loading…
Reference in New Issue