test_spi_flash_emulation.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "spi_flash_emulation.h"
  16. #include <functional>
  17. using namespace std;
  18. template <typename Tit>
  19. bool range_empty_n(Tit it_begin, size_t n)
  20. {
  21. return all_of(it_begin, it_begin + n, bind(equal_to<uint32_t>(), placeholders::_1, 0xffffffff));
  22. }
  23. TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
  24. {
  25. SpiFlashEmulator emu(4);
  26. uint8_t sector[SPI_FLASH_SEC_SIZE];
  27. for (int i = 0; i < 4; ++i) {
  28. CHECK(spi_flash_read(0, sector, sizeof(sector)) == ESP_OK);
  29. for (auto v: sector) {
  30. CHECK(v == 0xff);
  31. }
  32. }
  33. }
  34. TEST_CASE("invalid writes are checked", "[spi_flash_emu]")
  35. {
  36. SpiFlashEmulator emu(1);
  37. uint32_t val = 0;
  38. CHECK(spi_flash_write(0, &val, 4) == ESP_OK);
  39. val = 1;
  40. CHECK(spi_flash_write(0, &val, 4) == ESP_ERR_FLASH_OP_FAIL);
  41. }
  42. TEST_CASE("out of bounds writes fail", "[spi_flash_emu]")
  43. {
  44. SpiFlashEmulator emu(4);
  45. uint32_t vals[8];
  46. std::fill_n(vals, 8, 0);
  47. CHECK(spi_flash_write(0, vals, sizeof(vals)) == ESP_OK);
  48. CHECK(spi_flash_write(4*4096 - sizeof(vals), vals, sizeof(vals)) == ESP_OK);
  49. CHECK(spi_flash_write(4*4096 - sizeof(vals) + 4, vals, sizeof(vals)) == ESP_ERR_FLASH_OP_FAIL);
  50. }
  51. TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]")
  52. {
  53. SpiFlashEmulator emu(4);
  54. uint32_t val1 = 0xab00cd12;
  55. CHECK(spi_flash_write(0, &val1, sizeof(val1)) == ESP_OK);
  56. uint32_t val2 = 0x5678efab;
  57. CHECK(spi_flash_write(4096 - 4, &val2, sizeof(val2)) == ESP_OK);
  58. CHECK(emu.words()[0] == val1);
  59. CHECK(range_empty_n(emu.words() + 1, 4096 / 4 - 2));
  60. CHECK(emu.words()[4096 / 4 - 1] == val2);
  61. CHECK(spi_flash_erase_sector(0) == ESP_OK);
  62. CHECK(emu.words()[0] == 0xffffffff);
  63. CHECK(range_empty_n(emu.words() + 1, 4096 / 4 - 2));
  64. CHECK(emu.words()[4096 / 4 - 1] == 0xffffffff);
  65. }
  66. TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_flash_emu]")
  67. {
  68. SpiFlashEmulator emu(1);
  69. uint8_t data[512];
  70. spi_flash_read(0, data, 4);
  71. CHECK(emu.getTotalTime() == 7);
  72. CHECK(emu.getReadOps() == 1);
  73. CHECK(emu.getReadBytes() == 4);
  74. emu.clearStats();
  75. spi_flash_read(0, data, 8);
  76. CHECK(emu.getTotalTime() == 5);
  77. CHECK(emu.getReadOps() == 1);
  78. CHECK(emu.getReadBytes() == 8);
  79. emu.clearStats();
  80. spi_flash_read(0, data, 16);
  81. CHECK(emu.getTotalTime() == 6);
  82. CHECK(emu.getReadOps() == 1);
  83. CHECK(emu.getReadBytes() == 16);
  84. emu.clearStats();
  85. spi_flash_read(0, data, 128);
  86. CHECK(emu.getTotalTime() == 18);
  87. CHECK(emu.getReadOps() == 1);
  88. CHECK(emu.getReadBytes() == 128);
  89. emu.clearStats();
  90. spi_flash_read(0, data, 256);
  91. CHECK(emu.getTotalTime() == 32);
  92. emu.clearStats();
  93. spi_flash_read(0, data, (128+256)/2);
  94. CHECK(emu.getTotalTime() == (18+32)/2);
  95. emu.clearStats();
  96. spi_flash_write(0, data, 4);
  97. CHECK(emu.getTotalTime() == 19);
  98. CHECK(emu.getWriteOps() == 1);
  99. CHECK(emu.getWriteBytes() == 4);
  100. emu.clearStats();
  101. CHECK(emu.getWriteOps() == 0);
  102. CHECK(emu.getWriteBytes() == 0);
  103. spi_flash_write(0, data, 8);
  104. CHECK(emu.getTotalTime() == 23);
  105. emu.clearStats();
  106. spi_flash_write(0, data, 16);
  107. CHECK(emu.getTotalTime() == 35);
  108. CHECK(emu.getWriteOps() == 1);
  109. CHECK(emu.getWriteBytes() == 16);
  110. emu.clearStats();
  111. spi_flash_write(0, data, 128);
  112. CHECK(emu.getTotalTime() == 205);
  113. emu.clearStats();
  114. spi_flash_write(0, data, 256);
  115. CHECK(emu.getTotalTime() == 417);
  116. emu.clearStats();
  117. spi_flash_write(0, data, (128+256)/2);
  118. CHECK(emu.getTotalTime() == (205+417)/2);
  119. emu.clearStats();
  120. spi_flash_erase_sector(0);
  121. CHECK(emu.getEraseOps() == 1);
  122. CHECK(emu.getTotalTime() == 37142);
  123. }
  124. TEST_CASE("data is randomized predictably", "[spi_flash_emu]")
  125. {
  126. SpiFlashEmulator emu1(3);
  127. emu1.randomize(0x12345678);
  128. SpiFlashEmulator emu2(3);
  129. emu2.randomize(0x12345678);
  130. CHECK(std::equal(emu1.bytes(), emu1.bytes() + emu1.size(), emu2.bytes()));
  131. }