Skip to content

Commit 3b93350

Browse files
committed
general_block_device test: allocate buffers enough for the largest sector
Previously we get the common erase size of the whole flash, which may or may not exists if there are multiple regions. In this case the size returned is zero and the test fails. Fix this by allocating read and write buffers that are large enough for all sectors. The test itself already supports non-uniform erase sizes, and the erase size at any address can be smaller than our buffers.
1 parent feb1023 commit 3b93350

File tree

1 file changed

+27
-17
lines changed
  • storage/blockdevice/tests/TESTS/blockdevice/general_block_device

1 file changed

+27
-17
lines changed

storage/blockdevice/tests/TESTS/blockdevice/general_block_device/main.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ using namespace utest::v1;
6868

6969
uint8_t num_of_sectors = TEST_NUM_OF_THREADS * TEST_BLOCK_COUNT;
7070
uint32_t sectors_addr[TEST_NUM_OF_THREADS * TEST_BLOCK_COUNT] = {0};
71+
bd_size_t max_sector_size = 0;
7172

7273
const struct {
7374
const char *name;
@@ -261,11 +262,16 @@ void test_init_bd()
261262
TEST_ASSERT_EQUAL(0, err);
262263

263264
bd_addr_t start_address = 0;
265+
bd_size_t curr_sector_size = 0;
264266
uint8_t i = 0;
265267
for (; i < num_of_sectors && start_address < block_device->size(); i++) {
266268
sectors_addr[i] = start_address;
267-
DEBUG_PRINTF("start_address = 0x%llx, sector_size = %d\n", start_address, block_device->get_erase_size(start_address));
268-
start_address += block_device->get_erase_size(start_address);
269+
curr_sector_size = block_device->get_erase_size(start_address);
270+
DEBUG_PRINTF("start_address = 0x%llx, sector_size = %d\n", start_address, curr_sector_size);
271+
if (curr_sector_size > max_sector_size) {
272+
max_sector_size = curr_sector_size;
273+
}
274+
start_address += curr_sector_size;
269275
}
270276
num_of_sectors = i;
271277
}
@@ -288,24 +294,25 @@ void test_random_program_read_erase()
288294
}
289295
}
290296

291-
bd_size_t block_size = block_device->get_erase_size();
292297
unsigned addrwidth = ceil(log(float(block_device->size() - 1)) / log(float(16))) + 1;
293298

294-
uint8_t *write_block = new (std::nothrow) uint8_t[block_size];
295-
uint8_t *read_block = new (std::nothrow) uint8_t[block_size];
299+
uint8_t *write_buffer = new (std::nothrow) uint8_t[max_sector_size];
300+
uint8_t *read_buffer = new (std::nothrow) uint8_t[max_sector_size];
296301

297-
if (!write_block || !read_block) {
302+
if (!write_buffer || !read_buffer) {
298303
utest_printf("Not enough memory for test\n");
299304
goto end;
300305
}
301306

302307
for (int b = 0; b < std::min((uint8_t)TEST_BLOCK_COUNT, num_of_sectors); b++) {
303-
basic_erase_program_read_test(block_device, block_size, write_block, read_block, addrwidth, b);
308+
// basic_erase_program_read_test() can handle non-uniform sector sizes
309+
// and use only part of the buffers if the sector is smaller
310+
basic_erase_program_read_test(block_device, max_sector_size, write_buffer, read_buffer, addrwidth, b);
304311
}
305312

306313
end:
307-
delete[] read_block;
308-
delete[] write_block;
314+
delete[] read_buffer;
315+
delete[] write_buffer;
309316
}
310317

311318
#if defined(MBED_CONF_RTOS_PRESENT)
@@ -318,24 +325,27 @@ static void test_thread_job()
318325

319326
uint8_t sector_per_thread = (num_of_sectors / TEST_NUM_OF_THREADS);
320327

321-
bd_size_t block_size = block_device->get_erase_size();
322328
unsigned addrwidth = ceil(log(float(block_device->size() - 1)) / log(float(16))) + 1;
323329

324-
uint8_t *write_block = new (std::nothrow) uint8_t[block_size];
325-
uint8_t *read_block = new (std::nothrow) uint8_t[block_size];
330+
uint8_t *write_buffer = new (std::nothrow) uint8_t[max_sector_size];
331+
uint8_t *read_buffer = new (std::nothrow) uint8_t[max_sector_size];
326332

327-
if (!write_block || !read_block) {
328-
utest_printf("Not enough memory for test\n");
333+
if (!write_buffer || !read_buffer) {
334+
// Some targets have sectors up to 256KB each and a relatively small RAM.
335+
// This test may not be able to run in this case.
336+
utest_printf("Not enough memory for test, is the sector size (%llu) too big?\n", max_sector_size);
329337
goto end;
330338
}
331339

332340
for (int b = 0; b < sector_per_thread; b++) {
333-
basic_erase_program_read_test(block_device, block_size, write_block, read_block, addrwidth, block_num * sector_per_thread + b);
341+
// basic_erase_program_read_test() can handle non-uniform sector sizes
342+
// and use only part of the buffers if the sector is smaller
343+
basic_erase_program_read_test(block_device, max_sector_size, write_buffer, read_buffer, addrwidth, block_num * sector_per_thread + b);
334344
}
335345

336346
end:
337-
delete[] read_block;
338-
delete[] write_block;
347+
delete[] read_buffer;
348+
delete[] write_buffer;
339349
}
340350

341351
void test_multi_threads()

0 commit comments

Comments
 (0)