]> granicus.if.org Git - esp-idf/blob - components/nvs_flash/test/test_spi_flash_emulation.cpp
329e721ce79346380d6500cdcfad6a6da2864a90
[esp-idf] / components / nvs_flash / test / test_spi_flash_emulation.cpp
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
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 #include "catch.hpp"
15 #include "esp_spi_flash.h"
16 #include "spi_flash_emulation.h"
17
18 using namespace std;
19
20 template <typename Tit>
21 bool range_empty_n(Tit it_begin, size_t n)
22 {
23     return all_of(it_begin, it_begin + n, bind(equal_to<uint32_t>(), placeholders::_1, 0xffffffff));
24 }
25
26 TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]")
27 {
28     SpiFlashEmulator emu(4);
29
30     uint8_t sector[SPI_FLASH_SEC_SIZE];
31
32     for (int i = 0; i < 4; ++i) {
33         CHECK(spi_flash_read(0, sector, sizeof(sector)) == ESP_OK);
34         for (auto v: sector) {
35             CHECK(v == 0xff);
36         }
37     }
38 }
39
40 TEST_CASE("invalid writes are checked", "[spi_flash_emu]")
41 {
42     SpiFlashEmulator emu(1);
43
44     uint32_t val = 0;
45     CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(&val), 4) == ESP_OK);
46     val = 1;
47     CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(&val), 4) == ESP_ERR_FLASH_OP_FAIL);
48 }
49
50
51 TEST_CASE("out of bounds writes fail", "[spi_flash_emu]")
52 {
53     SpiFlashEmulator emu(4);
54     uint32_t vals[8];
55     std::fill_n(vals, 8, 0);
56     CHECK(spi_flash_write(0, reinterpret_cast<const uint8_t*>(vals), sizeof(vals)) == ESP_OK);
57
58     CHECK(spi_flash_write(4*4096 - sizeof(vals), reinterpret_cast<const uint8_t*>(vals), sizeof(vals)) == ESP_OK);
59
60     CHECK(spi_flash_write(4*4096 - sizeof(vals) + 4, reinterpret_cast<const uint8_t*>(vals), sizeof(vals)) == ESP_ERR_FLASH_OP_FAIL);
61 }
62
63
64 TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]")
65 {
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);
71
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);
75
76     CHECK(spi_flash_erase_sector(0) == ESP_OK);
77
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);
81 }
82
83 TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_flash_emu]")
84 {
85     SpiFlashEmulator emu(1);
86     uint8_t data[512];
87     spi_flash_read(0, data, 4);
88     CHECK(emu.getTotalTime() == 7);
89     CHECK(emu.getReadOps() == 1);
90     CHECK(emu.getReadBytes() == 4);
91     emu.clearStats();
92     spi_flash_read(0, data, 8);
93     CHECK(emu.getTotalTime() == 5);
94     CHECK(emu.getReadOps() == 1);
95     CHECK(emu.getReadBytes() == 8);
96     emu.clearStats();
97     spi_flash_read(0, data, 16);
98     CHECK(emu.getTotalTime() == 6);
99     CHECK(emu.getReadOps() == 1);
100     CHECK(emu.getReadBytes() == 16);
101     emu.clearStats();
102     spi_flash_read(0, data, 128);
103     CHECK(emu.getTotalTime() == 18);
104     CHECK(emu.getReadOps() == 1);
105     CHECK(emu.getReadBytes() == 128);
106     emu.clearStats();
107     spi_flash_read(0, data, 256);
108     CHECK(emu.getTotalTime() == 32);
109     emu.clearStats();
110     spi_flash_read(0, data, (128+256)/2);
111     CHECK(emu.getTotalTime() == (18+32)/2);
112     emu.clearStats();
113
114     spi_flash_write(0, data, 4);
115     CHECK(emu.getTotalTime() == 19);
116     CHECK(emu.getWriteOps() == 1);
117     CHECK(emu.getWriteBytes() == 4);
118     emu.clearStats();
119     CHECK(emu.getWriteOps() == 0);
120     CHECK(emu.getWriteBytes() == 0);
121     spi_flash_write(0, data, 8);
122     CHECK(emu.getTotalTime() == 23);
123     emu.clearStats();
124     spi_flash_write(0, data, 16);
125     CHECK(emu.getTotalTime() == 35);
126     CHECK(emu.getWriteOps() == 1);
127     CHECK(emu.getWriteBytes() == 16);
128     emu.clearStats();
129     spi_flash_write(0, data, 128);
130     CHECK(emu.getTotalTime() == 205);
131     emu.clearStats();
132     spi_flash_write(0, data, 256);
133     CHECK(emu.getTotalTime() == 417);
134     emu.clearStats();
135     spi_flash_write(0, data, (128+256)/2);
136     CHECK(emu.getTotalTime() == (205+417)/2);
137     emu.clearStats();
138
139     spi_flash_erase_sector(0);
140     CHECK(emu.getEraseOps() == 1);
141     CHECK(emu.getTotalTime() == 37142);
142 }
143
144 TEST_CASE("data is randomized predictably", "[spi_flash_emu]")
145 {
146     SpiFlashEmulator emu1(3);
147     emu1.randomize(0x12345678);
148     
149     SpiFlashEmulator emu2(3);
150     emu2.randomize(0x12345678);
151     
152     CHECK(std::equal(emu1.bytes(), emu1.bytes() + emu1.size(), emu2.bytes()));
153 }
154