1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "esp_spi_flash.h"
16 #include "spi_flash_emulation.h"
20 template <typename Tit>
21 bool range_empty_n(Tit it_begin, size_t n)
23 return all_of(it_begin, it_begin + n, bind(equal_to<uint32_t>(), placeholders::_1, 0xffffffff));
26 TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
28 SpiFlashEmulator emu(4);
30 uint8_t sector[SPI_FLASH_SEC_SIZE];
32 for (int i = 0; i < 4; ++i) {
33 CHECK(spi_flash_read(0, sector, sizeof(sector)) == ESP_OK);
34 for (auto v: sector) {
40 TEST_CASE("invalid writes are checked", "[spi_flash_emu]")
42 SpiFlashEmulator emu(1);
45 CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(&val), 4) == ESP_OK);
47 CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(&val), 4) == ESP_ERR_FLASH_OP_FAIL);
51 TEST_CASE("out of bounds writes fail", "[spi_flash_emu]")
53 SpiFlashEmulator emu(4);
55 std::fill_n(vals, 8, 0);
56 CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(vals), sizeof(vals)) == ESP_OK);
58 CHECK(spi_flash_write(4*4096 - sizeof(vals), reinterpret_cast<const uint8_t*>(vals), sizeof(vals)) == ESP_OK);
60 CHECK(spi_flash_write(4*4096 - sizeof(vals) + 4, reinterpret_cast<const uint8_t*>(vals), sizeof(vals)) == ESP_ERR_FLASH_OP_FAIL);
64 TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]")
66 SpiFlashEmulator emu(4);
67 uint32_t val1 = 0xab00cd12;
68 CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(&val1), sizeof(val1)) == ESP_OK);
69 uint32_t val2 = 0x5678efab;
70 CHECK(spi_flash_write(4096 - 4, reinterpret_cast<const uint8_t*>(&val2), sizeof(val2)) == ESP_OK);
72 CHECK(emu.words()[0] == val1);
73 CHECK(range_empty_n(emu.words() + 1, 4096 / 4 - 2));
74 CHECK(emu.words()[4096 / 4 - 1] == val2);
76 CHECK(spi_flash_erase_sector(0) == ESP_OK);
78 CHECK(emu.words()[0] == 0xffffffff);
79 CHECK(range_empty_n(emu.words() + 1, 4096 / 4 - 2));
80 CHECK(emu.words()[4096 / 4 - 1] == 0xffffffff);
83 TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_flash_emu]")
85 SpiFlashEmulator emu(1);
87 spi_flash_read(0, data, 4);
88 CHECK(emu.getTotalTime() == 7);
89 CHECK(emu.getReadOps() == 1);
90 CHECK(emu.getReadBytes() == 4);
92 spi_flash_read(0, data, 8);
93 CHECK(emu.getTotalTime() == 5);
94 CHECK(emu.getReadOps() == 1);
95 CHECK(emu.getReadBytes() == 8);
97 spi_flash_read(0, data, 16);
98 CHECK(emu.getTotalTime() == 6);
99 CHECK(emu.getReadOps() == 1);
100 CHECK(emu.getReadBytes() == 16);
102 spi_flash_read(0, data, 128);
103 CHECK(emu.getTotalTime() == 18);
104 CHECK(emu.getReadOps() == 1);
105 CHECK(emu.getReadBytes() == 128);
107 spi_flash_read(0, data, 256);
108 CHECK(emu.getTotalTime() == 32);
110 spi_flash_read(0, data, (128+256)/2);
111 CHECK(emu.getTotalTime() == (18+32)/2);
114 spi_flash_write(0, data, 4);
115 CHECK(emu.getTotalTime() == 19);
116 CHECK(emu.getWriteOps() == 1);
117 CHECK(emu.getWriteBytes() == 4);
119 CHECK(emu.getWriteOps() == 0);
120 CHECK(emu.getWriteBytes() == 0);
121 spi_flash_write(0, data, 8);
122 CHECK(emu.getTotalTime() == 23);
124 spi_flash_write(0, data, 16);
125 CHECK(emu.getTotalTime() == 35);
126 CHECK(emu.getWriteOps() == 1);
127 CHECK(emu.getWriteBytes() == 16);
129 spi_flash_write(0, data, 128);
130 CHECK(emu.getTotalTime() == 205);
132 spi_flash_write(0, data, 256);
133 CHECK(emu.getTotalTime() == 417);
135 spi_flash_write(0, data, (128+256)/2);
136 CHECK(emu.getTotalTime() == (205+417)/2);
139 spi_flash_erase_sector(0);
140 CHECK(emu.getEraseOps() == 1);
141 CHECK(emu.getTotalTime() == 37142);
144 TEST_CASE("data is randomized predictably", "[spi_flash_emu]")
146 SpiFlashEmulator emu1(3);
147 emu1.randomize(0x12345678);
149 SpiFlashEmulator emu2(3);
150 emu2.randomize(0x12345678);
152 CHECK(std::equal(emu1.bytes(), emu1.bytes() + emu1.size(), emu2.bytes()));