test_spi_flash_emulation.cpp 4.6 KB

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