test_spi_flash_emulation.cpp 4.6 KB

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