test_spi_flash_emulation.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "catch.hpp"
  7. #include "spi_flash_mmap.h"
  8. #include "esp_partition.h"
  9. #include "spi_flash_emulation.h"
  10. #include <functional>
  11. template <typename Tit>
  12. bool range_empty_n(Tit it_begin, size_t n)
  13. {
  14. return std::all_of(it_begin, it_begin + n, bind(std::equal_to<uint32_t>(), std::placeholders::_1, 0xffffffff));
  15. }
  16. struct FlashEmuFixture {
  17. FlashEmuFixture(size_t sectors) : esp_part(), emu(sectors) { }
  18. esp_partition_t esp_part;
  19. SpiFlashEmulator emu;
  20. };
  21. TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
  22. {
  23. FlashEmuFixture f(4);
  24. uint8_t sector[SPI_FLASH_SEC_SIZE];
  25. for (int i = 0; i < 4; ++i) {
  26. CHECK(esp_partition_read(&f.esp_part, 0, sector, sizeof(sector)) == ESP_OK);
  27. for (auto v: sector) {
  28. CHECK(v == 0xff);
  29. }
  30. }
  31. }
  32. TEST_CASE("invalid writes are checked", "[spi_flash_emu]")
  33. {
  34. FlashEmuFixture f(1);
  35. uint32_t val = 0;
  36. CHECK(esp_partition_write(&f.esp_part, 0, &val, 4) == ESP_OK);
  37. val = 1;
  38. CHECK(esp_partition_write(&f.esp_part, 0, &val, 4) == ESP_ERR_FLASH_OP_FAIL);
  39. }
  40. TEST_CASE("out of bounds writes fail", "[spi_flash_emu]")
  41. {
  42. FlashEmuFixture f(4);
  43. uint32_t vals[8];
  44. std::fill_n(vals, 8, 0);
  45. CHECK(esp_partition_write(&f.esp_part, 0, &vals, sizeof(vals)) == ESP_OK);
  46. CHECK(esp_partition_write(&f.esp_part, 4*4096 - sizeof(vals), &vals, sizeof(vals)) == ESP_OK);
  47. CHECK(esp_partition_write(&f.esp_part, 4*4096 - sizeof(vals) + 4, &vals, sizeof(vals)) == ESP_ERR_FLASH_OP_FAIL);
  48. }
  49. TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]")
  50. {
  51. FlashEmuFixture f(4);
  52. uint32_t val1 = 0xab00cd12;
  53. CHECK(esp_partition_write(&f.esp_part, 0, &val1, sizeof(val1)) == ESP_OK);
  54. uint32_t val2 = 0x5678efab;
  55. CHECK(esp_partition_write(&f.esp_part, 4096 - 4, &val2, sizeof(val2)) == ESP_OK);
  56. CHECK(f.emu.words()[0] == val1);
  57. CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2));
  58. CHECK(f.emu.words()[4096 / 4 - 1] == val2);
  59. CHECK(esp_partition_erase_range(&f.esp_part, 0, SPI_FLASH_SEC_SIZE) == ESP_OK);
  60. CHECK(f.emu.words()[0] == 0xffffffff);
  61. CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2));
  62. CHECK(f.emu.words()[4096 / 4 - 1] == 0xffffffff);
  63. }
  64. TEST_CASE("EMU raw read function works", "[spi_flash_emu]")
  65. {
  66. FlashEmuFixture f(4);
  67. uint32_t value = 0xdeadbeef;
  68. uint32_t read_value = 0;
  69. CHECK(esp_partition_write(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
  70. CHECK(esp_partition_read_raw(&f.esp_part, 0, &read_value, sizeof(read_value)) == ESP_OK);
  71. CHECK(read_value == 0xdeadbeef);
  72. }
  73. TEST_CASE("EMU raw write function works", "[spi_flash_emu]")
  74. {
  75. FlashEmuFixture f(4);
  76. uint32_t value = 0xdeadbeef;
  77. uint32_t read_value = 0;
  78. CHECK(esp_partition_write_raw(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
  79. CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(read_value)) == ESP_OK);
  80. CHECK(read_value == 0xdeadbeef);
  81. }
  82. TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_flash_emu]")
  83. {
  84. FlashEmuFixture f(1);
  85. uint8_t data[512];
  86. esp_partition_read(&f.esp_part, 0, data, 4);
  87. CHECK(f.emu.getTotalTime() == 7);
  88. CHECK(f.emu.getReadOps() == 1);
  89. CHECK(f.emu.getReadBytes() == 4);
  90. f.emu.clearStats();
  91. esp_partition_read(&f.esp_part, 0, data, 8);
  92. CHECK(f.emu.getTotalTime() == 5);
  93. CHECK(f.emu.getReadOps() == 1);
  94. CHECK(f.emu.getReadBytes() == 8);
  95. f.emu.clearStats();
  96. esp_partition_read(&f.esp_part, 0, data, 16);
  97. CHECK(f.emu.getTotalTime() == 6);
  98. CHECK(f.emu.getReadOps() == 1);
  99. CHECK(f.emu.getReadBytes() == 16);
  100. f.emu.clearStats();
  101. esp_partition_read(&f.esp_part, 0, data, 128);
  102. CHECK(f.emu.getTotalTime() == 18);
  103. CHECK(f.emu.getReadOps() == 1);
  104. CHECK(f.emu.getReadBytes() == 128);
  105. f.emu.clearStats();
  106. esp_partition_read(&f.esp_part, 0, data, 256);
  107. CHECK(f.emu.getTotalTime() == 32);
  108. f.emu.clearStats();
  109. esp_partition_read(&f.esp_part, 0, data, (128+256)/2);
  110. CHECK(f.emu.getTotalTime() == (18+32)/2);
  111. f.emu.clearStats();
  112. esp_partition_write(&f.esp_part, 0, data, 4);
  113. CHECK(f.emu.getTotalTime() == 19);
  114. CHECK(f.emu.getWriteOps() == 1);
  115. CHECK(f.emu.getWriteBytes() == 4);
  116. f.emu.clearStats();
  117. CHECK(f.emu.getWriteOps() == 0);
  118. CHECK(f.emu.getWriteBytes() == 0);
  119. esp_partition_write(&f.esp_part, 0, data, 8);
  120. CHECK(f.emu.getTotalTime() == 23);
  121. f.emu.clearStats();
  122. esp_partition_write(&f.esp_part, 0, data, 16);
  123. CHECK(f.emu.getTotalTime() == 35);
  124. CHECK(f.emu.getWriteOps() == 1);
  125. CHECK(f.emu.getWriteBytes() == 16);
  126. f.emu.clearStats();
  127. esp_partition_write(&f.esp_part, 0, data, 128);
  128. CHECK(f.emu.getTotalTime() == 205);
  129. f.emu.clearStats();
  130. esp_partition_write(&f.esp_part, 0, data, 256);
  131. CHECK(f.emu.getTotalTime() == 417);
  132. f.emu.clearStats();
  133. esp_partition_write(&f.esp_part, 0, data, (128+256)/2);
  134. CHECK(f.emu.getTotalTime() == (205+417)/2);
  135. f.emu.clearStats();
  136. esp_partition_erase_range(&f.esp_part, 0, SPI_FLASH_SEC_SIZE);
  137. CHECK(f.emu.getEraseOps() == 1);
  138. CHECK(f.emu.getTotalTime() == 37142);
  139. }
  140. TEST_CASE("data is randomized predictably", "[spi_flash_emu]")
  141. {
  142. SpiFlashEmulator emu1(3);
  143. emu1.randomize(0x12345678);
  144. SpiFlashEmulator emu2(3);
  145. emu2.randomize(0x12345678);
  146. CHECK(std::equal(emu1.bytes(), emu1.bytes() + emu1.size(), emu2.bytes()));
  147. }