diff --git a/components/spi_flash/test/test_mmap.c b/components/spi_flash/test/test_mmap.c index 4bf3c5a45f..facc1ffded 100644 --- a/components/spi_flash/test/test_mmap.c +++ b/components/spi_flash/test/test_mmap.c @@ -19,33 +19,59 @@ static uint32_t buffer[1024]; static const uint32_t start = 0x100000; static const uint32_t end = 0x200000; +static spi_flash_mmap_handle_t handle1, handle2, handle3; -TEST_CASE("Prepare data for mmap tests", "[mmap]") +static void setup_mmap_tests() { + /* clean up any mmap handles left over from failed tests */ + if (handle1) { + spi_flash_munmap(handle1); + handle1 = 0; + } + if (handle2) { + spi_flash_munmap(handle2); + handle2 = 0; + } + if (handle3) { + spi_flash_munmap(handle3); + handle3 = 0; + } + + /* prepare flash contents */ srand(0); for (int block = start / 0x10000; block < end / 0x10000; ++block) { - printf("Writing block %d\n", block); for (int sector = 0; sector < 16; ++sector) { + uint32_t abs_sector = (block) * 16 + sector; + uint32_t sector_offs = abs_sector * SPI_FLASH_SEC_SIZE; + bool sector_needs_write = false; + + ESP_ERROR_CHECK( spi_flash_read(sector_offs, buffer, sizeof(buffer)) ); + for (uint32_t word = 0; word < 1024; ++word) { uint32_t val = rand(); if (block == start / 0x10000 && sector == 0 && word == 0) { - printf("first word: %08x\n", val); + printf("setup_mmap_tests(): first prepped word: %08x\n", val); + } + if (buffer[word] != val) { + buffer[word] = val; + sector_needs_write = true; } - buffer[word] = val; } - uint32_t abs_sector = (block) * 16 + sector; - printf("Writing sector %d\n", abs_sector); - ESP_ERROR_CHECK( spi_flash_erase_sector((uint16_t) abs_sector) ); - ESP_ERROR_CHECK( spi_flash_write(abs_sector * SPI_FLASH_SEC_SIZE, (const uint8_t *) buffer, sizeof(buffer)) ); + /* Only rewrite the sector if it has changed */ + if (sector_needs_write) { + printf("setup_mmap_tests(): Prepping sector %d\n", abs_sector); + ESP_ERROR_CHECK( spi_flash_erase_sector((uint16_t) abs_sector) ); + ESP_ERROR_CHECK( spi_flash_write(sector_offs, (const uint8_t *) buffer, sizeof(buffer)) ); + } } } } TEST_CASE("Can mmap into data address space", "[mmap]") { + setup_mmap_tests(); printf("Mapping %x (+%x)\n", start, end - start); - spi_flash_mmap_handle_t handle1; const void *ptr1; ESP_ERROR_CHECK( spi_flash_mmap(start, end - start, SPI_FLASH_MMAP_DATA, &ptr1, &handle1) ); printf("mmap_res: handle=%d ptr=%p\n", handle1, ptr1); @@ -62,19 +88,16 @@ TEST_CASE("Can mmap into data address space", "[mmap]") } } printf("Mapping %x (+%x)\n", start - 0x10000, 0x20000); - spi_flash_mmap_handle_t handle2; const void *ptr2; ESP_ERROR_CHECK( spi_flash_mmap(start - 0x10000, 0x20000, SPI_FLASH_MMAP_DATA, &ptr2, &handle2) ); printf("mmap_res: handle=%d ptr=%p\n", handle2, ptr2); TEST_ASSERT_EQUAL_HEX32(start - 0x10000, spi_flash_cache2phys(ptr2)); TEST_ASSERT_EQUAL_PTR(ptr2, spi_flash_phys2cache(start - 0x10000, SPI_FLASH_MMAP_DATA)); - TEST_ASSERT_EQUAL_PTR((intptr_t)ptr2 + 0x12000, spi_flash_phys2cache(start - 0x10000 + 0x12000, SPI_FLASH_MMAP_DATA)); spi_flash_mmap_dump(); printf("Mapping %x (+%x)\n", start, 0x10000); - spi_flash_mmap_handle_t handle3; const void *ptr3; ESP_ERROR_CHECK( spi_flash_mmap(start, 0x10000, SPI_FLASH_MMAP_DATA, &ptr3, &handle3) ); printf("mmap_res: handle=%d ptr=%p\n", handle3, ptr3); @@ -87,25 +110,29 @@ TEST_CASE("Can mmap into data address space", "[mmap]") printf("Unmapping handle1\n"); spi_flash_munmap(handle1); + handle1 = 0; spi_flash_mmap_dump(); printf("Unmapping handle2\n"); spi_flash_munmap(handle2); + handle2 = 0; spi_flash_mmap_dump(); printf("Unmapping handle3\n"); spi_flash_munmap(handle3); + handle3 = 0; TEST_ASSERT_EQUAL_PTR(NULL, spi_flash_phys2cache(start, SPI_FLASH_MMAP_DATA)); } TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash]") { - spi_flash_mmap_handle_t handle1; const void *ptr1; const size_t test_size = 128; + setup_mmap_tests(); + if (esp_flash_encryption_enabled()) { TEST_IGNORE_MESSAGE("flash encryption enabled, spi_flash_write_encrypted() test won't pass as-is"); } @@ -123,6 +150,7 @@ TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash]") /* unmap the erased region */ spi_flash_munmap(handle1); + handle1 = 0; /* write flash region to 0xEE */ uint8_t buf[test_size]; @@ -143,6 +171,7 @@ TEST_CASE("flash_mmap invalidates just-written data", "[spi_flash]") TEST_ASSERT_EQUAL_HEX8_ARRAY(buf, ptr1, test_size); spi_flash_munmap(handle1); + handle1 = 0; } TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash]") @@ -180,15 +209,16 @@ TEST_CASE("phys2cache/cache2phys basic checks", "[spi_flash]") TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash]") { - spi_flash_mmap_handle_t handle = 0; const void *ptr = NULL; const size_t test_size = 2 * SPI_FLASH_MMU_PAGE_SIZE; + setup_mmap_tests(); + TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr)); - ESP_ERROR_CHECK( spi_flash_mmap(TEST_REGION_START, test_size, SPI_FLASH_MMAP_DATA, &ptr, &handle) ); + ESP_ERROR_CHECK( spi_flash_mmap(TEST_REGION_START, test_size, SPI_FLASH_MMAP_DATA, &ptr, &handle1) ); TEST_ASSERT_NOT_NULL(ptr); - TEST_ASSERT_NOT_EQUAL(0, handle); + TEST_ASSERT_NOT_EQUAL(0, handle1); TEST_ASSERT_EQUAL_HEX(TEST_REGION_START, spi_flash_cache2phys(ptr)); TEST_ASSERT_EQUAL_HEX(TEST_REGION_START + 1024, spi_flash_cache2phys((void *)((intptr_t)ptr + 1024))); @@ -196,7 +226,8 @@ TEST_CASE("mmap consistent with phys2cache/cache2phys", "[spi_flash]") /* this pointer lands in a different MMU table entry */ TEST_ASSERT_EQUAL_HEX(TEST_REGION_START + test_size - 4, spi_flash_cache2phys((void *)((intptr_t)ptr + test_size - 4))); - spi_flash_munmap(handle); + spi_flash_munmap(handle1); + handle1 = 0; TEST_ASSERT_EQUAL_HEX(SPI_FLASH_CACHE2PHYS_FAIL, spi_flash_cache2phys(ptr)); }