test_spi_flash_emulation.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "catch.hpp"
  14. #include "esp_spi_flash.h"
  15. #include "esp_partition.h"
  16. #include "spi_flash_emulation.h"
  17. #include <functional>
  18. using namespace std;
  19. template <typename Tit>
  20. bool range_empty_n(Tit it_begin, size_t n)
  21. {
  22. return all_of(it_begin, it_begin + n, bind(equal_to<uint32_t>(), placeholders::_1, 0xffffffff));
  23. }
  24. struct FlashEmuFixture {
  25. FlashEmuFixture(size_t sectors) : esp_part(), emu(sectors) { }
  26. esp_partition_t esp_part;
  27. SpiFlashEmulator emu;
  28. };
  29. TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
  30. {
  31. FlashEmuFixture f(4);
  32. uint8_t sector[SPI_FLASH_SEC_SIZE];
  33. for (int i = 0; i < 4; ++i) {
  34. CHECK(esp_partition_read(&f.esp_part, 0, sector, sizeof(sector)) == ESP_OK);
  35. for (auto v: sector) {
  36. CHECK(v == 0xff);
  37. }
  38. }
  39. }
  40. TEST_CASE("invalid writes are checked", "[spi_flash_emu]")
  41. {
  42. FlashEmuFixture f(1);
  43. uint32_t val = 0;
  44. CHECK(esp_partition_write(&f.esp_part, 0, &val, 4) == ESP_OK);
  45. val = 1;
  46. CHECK(esp_partition_write(&f.esp_part, 0, &val, 4) == ESP_ERR_FLASH_OP_FAIL);
  47. }
  48. TEST_CASE("out of bounds writes fail", "[spi_flash_emu]")
  49. {
  50. FlashEmuFixture f(4);
  51. uint32_t vals[8];
  52. std::fill_n(vals, 8, 0);
  53. CHECK(esp_partition_write(&f.esp_part, 0, &vals, sizeof(vals)) == ESP_OK);
  54. CHECK(esp_partition_write(&f.esp_part, 4*4096 - sizeof(vals), &vals, sizeof(vals)) == ESP_OK);
  55. CHECK(esp_partition_write(&f.esp_part, 4*4096 - sizeof(vals) + 4, &vals, sizeof(vals)) == ESP_ERR_FLASH_OP_FAIL);
  56. }
  57. TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]")
  58. {
  59. FlashEmuFixture f(4);
  60. uint32_t val1 = 0xab00cd12;
  61. CHECK(esp_partition_write(&f.esp_part, 0, &val1, sizeof(val1)) == ESP_OK);
  62. uint32_t val2 = 0x5678efab;
  63. CHECK(esp_partition_write(&f.esp_part, 4096 - 4, &val2, sizeof(val2)) == ESP_OK);
  64. CHECK(f.emu.words()[0] == val1);
  65. CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2));
  66. CHECK(f.emu.words()[4096 / 4 - 1] == val2);
  67. CHECK(esp_partition_erase_range(&f.esp_part, 0, SPI_FLASH_SEC_SIZE) == ESP_OK);
  68. CHECK(f.emu.words()[0] == 0xffffffff);
  69. CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2));
  70. CHECK(f.emu.words()[4096 / 4 - 1] == 0xffffffff);
  71. }
  72. TEST_CASE("EMU raw read function works", "[spi_flash_emu]")
  73. {
  74. FlashEmuFixture f(4);
  75. uint32_t value = 0xdeadbeef;
  76. uint32_t read_value = 0;
  77. CHECK(esp_partition_write(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
  78. CHECK(esp_partition_read_raw(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);
  79. CHECK(read_value == 0xdeadbeef);
  80. }
  81. TEST_CASE("EMU raw write function works", "[spi_flash_emu]")
  82. {
  83. FlashEmuFixture f(4);
  84. uint32_t value = 0xdeadbeef;
  85. uint32_t read_value = 0;
  86. CHECK(esp_partition_write_raw(&f.esp_part, 0, &value, sizeof(value)) == ESP_OK);
  87. CHECK(esp_partition_read(&f.esp_part, 0, &read_value, sizeof(&read_value)) == ESP_OK);
  88. CHECK(read_value == 0xdeadbeef);
  89. }
  90. TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_flash_emu]")
  91. {
  92. FlashEmuFixture f(1);
  93. uint8_t data[512];
  94. esp_partition_read(&f.esp_part, 0, data, 4);
  95. CHECK(f.emu.getTotalTime() == 7);
  96. CHECK(f.emu.getReadOps() == 1);
  97. CHECK(f.emu.getReadBytes() == 4);
  98. f.emu.clearStats();
  99. esp_partition_read(&f.esp_part, 0, data, 8);
  100. CHECK(f.emu.getTotalTime() == 5);
  101. CHECK(f.emu.getReadOps() == 1);
  102. CHECK(f.emu.getReadBytes() == 8);
  103. f.emu.clearStats();
  104. esp_partition_read(&f.esp_part, 0, data, 16);
  105. CHECK(f.emu.getTotalTime() == 6);
  106. CHECK(f.emu.getReadOps() == 1);
  107. CHECK(f.emu.getReadBytes() == 16);
  108. f.emu.clearStats();
  109. esp_partition_read(&f.esp_part, 0, data, 128);
  110. CHECK(f.emu.getTotalTime() == 18);
  111. CHECK(f.emu.getReadOps() == 1);
  112. CHECK(f.emu.getReadBytes() == 128);
  113. f.emu.clearStats();
  114. esp_partition_read(&f.esp_part, 0, data, 256);
  115. CHECK(f.emu.getTotalTime() == 32);
  116. f.emu.clearStats();
  117. esp_partition_read(&f.esp_part, 0, data, (128+256)/2);
  118. CHECK(f.emu.getTotalTime() == (18+32)/2);
  119. f.emu.clearStats();
  120. esp_partition_write(&f.esp_part, 0, data, 4);
  121. CHECK(f.emu.getTotalTime() == 19);
  122. CHECK(f.emu.getWriteOps() == 1);
  123. CHECK(f.emu.getWriteBytes() == 4);
  124. f.emu.clearStats();
  125. CHECK(f.emu.getWriteOps() == 0);
  126. CHECK(f.emu.getWriteBytes() == 0);
  127. esp_partition_write(&f.esp_part, 0, data, 8);
  128. CHECK(f.emu.getTotalTime() == 23);
  129. f.emu.clearStats();
  130. esp_partition_write(&f.esp_part, 0, data, 16);
  131. CHECK(f.emu.getTotalTime() == 35);
  132. CHECK(f.emu.getWriteOps() == 1);
  133. CHECK(f.emu.getWriteBytes() == 16);
  134. f.emu.clearStats();
  135. esp_partition_write(&f.esp_part, 0, data, 128);
  136. CHECK(f.emu.getTotalTime() == 205);
  137. f.emu.clearStats();
  138. esp_partition_write(&f.esp_part, 0, data, 256);
  139. CHECK(f.emu.getTotalTime() == 417);
  140. f.emu.clearStats();
  141. esp_partition_write(&f.esp_part, 0, data, (128+256)/2);
  142. CHECK(f.emu.getTotalTime() == (205+417)/2);
  143. f.emu.clearStats();
  144. esp_partition_erase_range(&f.esp_part, 0, SPI_FLASH_SEC_SIZE);
  145. CHECK(f.emu.getEraseOps() == 1);
  146. CHECK(f.emu.getTotalTime() == 37142);
  147. }
  148. TEST_CASE("data is randomized predictably", "[spi_flash_emu]")
  149. {
  150. SpiFlashEmulator emu1(3);
  151. emu1.randomize(0x12345678);
  152. SpiFlashEmulator emu2(3);
  153. emu2.randomize(0x12345678);
  154. CHECK(std::equal(emu1.bytes(), emu1.bytes() + emu1.size(), emu2.bytes()));
  155. }