@@ -218,6 +218,53 @@ void flash_program_page_test()
218218 delete[] data_flashed;
219219}
220220
221+ // Tests that one flash page can be copied to another
222+ void flash_copy_flash_to_flash ()
223+ {
224+ flash_t test_flash;
225+ int32_t ret = flash_init (&test_flash);
226+ TEST_ASSERT_EQUAL_INT32 (0 , ret);
227+
228+ uint32_t last_page_address = flash_get_start_address (&test_flash) + flash_get_size (&test_flash) - flash_get_page_size (&test_flash);
229+ uint32_t *last_page_pointer = reinterpret_cast <uint32_t *>(last_page_address);
230+ uint32_t second_to_last_page_address = last_page_address - flash_get_page_size (&test_flash);
231+ uint32_t *second_to_last_page_pointer = reinterpret_cast <uint32_t *>(second_to_last_page_address);
232+
233+ // Erase the sector(s) which contain the last two pages
234+ uint32_t last_page_sector = ALIGN_DOWN (last_page_address, flash_get_sector_size (&test_flash, last_page_address));
235+ uint32_t second_to_last_page_sector = ALIGN_DOWN (second_to_last_page_address, flash_get_sector_size (&test_flash, second_to_last_page_address));
236+
237+ ret = flash_erase_sector (&test_flash, last_page_sector);
238+ TEST_ASSERT_EQUAL_INT32 (0 , ret);
239+
240+ if (last_page_sector != second_to_last_page_sector) {
241+ ret = flash_erase_sector (&test_flash, second_to_last_page_sector);
242+ TEST_ASSERT_EQUAL_INT32 (0 , ret);
243+ }
244+
245+ // Fill second to last page with test data
246+ size_t const numDataWords = flash_get_page_size (&test_flash) / sizeof (uint32_t );
247+ uint32_t *data = new uint32_t [numDataWords];
248+ for (size_t wordIdx = 0 ; wordIdx < numDataWords; ++wordIdx) {
249+ data[wordIdx] = wordIdx;
250+ }
251+
252+ ret = flash_program_page (&test_flash, second_to_last_page_address, reinterpret_cast <const uint8_t *>(data), flash_get_page_size (&test_flash));
253+ TEST_ASSERT_EQUAL_INT32 (0 , ret);
254+
255+ // Make sure data was written
256+ TEST_ASSERT_EQUAL_UINT32_ARRAY (data, second_to_last_page_pointer, numDataWords);
257+
258+ // Now, program last page from the second to last page
259+ ret = flash_program_page (&test_flash, last_page_address, reinterpret_cast <const uint8_t *>(second_to_last_page_pointer), flash_get_page_size (&test_flash));
260+ TEST_ASSERT_EQUAL_INT32 (0 , ret);
261+
262+ // Make sure data was written
263+ TEST_ASSERT_EQUAL_UINT32_ARRAY (data, last_page_pointer, numDataWords);
264+
265+ delete[] data;
266+ }
267+
221268// check the execution speed at the start and end of the test to make sure
222269// cache settings weren't changed
223270void flash_clock_and_cache_test ()
@@ -232,6 +279,7 @@ Case cases[] = {
232279 Case (" Flash - mapping alignment" , flash_mapping_alignment_test),
233280 Case (" Flash - erase sector" , flash_erase_sector_test),
234281 Case (" Flash - program page" , flash_program_page_test),
282+ Case (" Flash - copy flash to flash" , flash_copy_flash_to_flash),
235283 Case (" Flash - clock and cache test" , flash_clock_and_cache_test),
236284};
237285
0 commit comments