@@ -68,6 +68,7 @@ using namespace utest::v1;
6868
6969uint8_t num_of_sectors = TEST_NUM_OF_THREADS * TEST_BLOCK_COUNT;
7070uint32_t sectors_addr[TEST_NUM_OF_THREADS * TEST_BLOCK_COUNT] = {0 };
71+ bd_size_t max_sector_size = 0 ;
7172
7273const 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
306313end:
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
336346end:
337- delete[] read_block ;
338- delete[] write_block ;
347+ delete[] read_buffer ;
348+ delete[] write_buffer ;
339349}
340350
341351void test_multi_threads ()
@@ -584,7 +594,6 @@ void test_program_read_small_data_sizes()
584594
585595 TEST_SKIP_UNLESS_MESSAGE (block_device != NULL , " no block device found." );
586596
587- bd_size_t erase_size = block_device->get_erase_size ();
588597 bd_size_t program_size = block_device->get_program_size ();
589598 bd_size_t read_size = block_device->get_read_size ();
590599 TEST_ASSERT (program_size > 0 );
@@ -606,6 +615,7 @@ void test_program_read_small_data_sizes()
606615
607616 // Determine starting address
608617 bd_addr_t start_address = 0 ;
618+ bd_size_t erase_size = block_device->get_erase_size (start_address);
609619
610620 for (int i = 1 ; i <= 7 ; i++) {
611621 err = buff_block_device->erase (start_address, erase_size);
0 commit comments