init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "unity.h"
00018 #include "utest.h"
00019 #include "test_env.h"
00020 
00021 #include "atomic_usage.h"
00022 #include "ExhaustibleBlockDevice.h"
00023 #include "SlicingBlockDevice.h"
00024 
00025 using namespace utest::v1;
00026 
00027 // test configuration
00028 #ifndef MBED_TEST_SIM_BLOCKDEVICE
00029 #error [NOT_SUPPORTED] Simulation block device required for wear leveling tests
00030 #endif
00031 
00032 #ifndef MBED_TEST_SIM_BLOCKDEVICE_DECL
00033 #define MBED_TEST_SIM_BLOCKDEVICE_DECL MBED_TEST_SIM_BLOCKDEVICE bd(MBED_TEST_BLOCK_COUNT*512, 1, 1, 512)
00034 #endif
00035 
00036 #ifndef MBED_TEST_BLOCK_COUNT
00037 #define MBED_TEST_BLOCK_COUNT 64
00038 #endif
00039 
00040 #ifndef MBED_TEST_ERASE_CYCLES
00041 #define MBED_TEST_ERASE_CYCLES 100
00042 #endif
00043 
00044 #ifndef MBED_TEST_TIMEOUT
00045 #define MBED_TEST_TIMEOUT 480
00046 #endif
00047 
00048 // declarations
00049 #define STRINGIZE(x) STRINGIZE2(x)
00050 #define STRINGIZE2(x) #x
00051 #define INCLUDE(x) STRINGIZE(x.h)
00052 
00053 #include INCLUDE(MBED_TEST_SIM_BLOCKDEVICE)
00054 
00055 
00056 static uint32_t test_wear_leveling_size(uint32_t block_count)
00057 {
00058     MBED_TEST_SIM_BLOCKDEVICE_DECL;
00059 
00060     // bring up to get block size
00061     bd.init();
00062     bd_size_t block_size = bd.get_erase_size();
00063     bd.deinit();
00064 
00065     SlicingBlockDevice slice(&bd, 0, block_count*block_size);
00066     ExhaustibleBlockDevice ebd(&slice, MBED_TEST_ERASE_CYCLES);
00067 
00068     printf("Testing size %llu bytes (%lux%llu) blocks\n",
00069         block_count*block_size, block_count, block_size);
00070     setup_atomic_operations(&ebd, true);
00071 
00072     int64_t cycles = 0;
00073     while (true) {
00074         int64_t ret = perform_atomic_operations(&ebd);
00075         check_atomic_operations(&ebd);
00076         if (-1 == ret) {
00077             break;
00078         }
00079         cycles++;
00080         TEST_ASSERT_EQUAL(cycles, ret);
00081 
00082     }
00083 
00084     printf("  Simulated flash lasted %lli cylces\n", cycles);
00085     return cycles;
00086 }
00087 
00088 /**
00089  * Check that storage life is proportional to storage size
00090  *
00091  * This test is to ensure that littlefs is properly handling wear
00092  * leveling. It does this by creating a simulated flash block device
00093  * which can be worn out and then using it until it is exhausted.
00094  * It then doubles the size of the block device and runs the same
00095  * test. If the block device with twice the size lasts at least
00096  * twice as long then the test passes.
00097  */
00098 void test_wear_leveling()
00099 {
00100     uint32_t cycles_1 = test_wear_leveling_size(MBED_TEST_BLOCK_COUNT / 2);
00101     uint32_t cycles_2 = test_wear_leveling_size(MBED_TEST_BLOCK_COUNT / 1);
00102     TEST_ASSERT(cycles_2 > cycles_1 * 2);
00103 }
00104 
00105 Case cases[] = {
00106     Case("test wear leveling", test_wear_leveling),
00107 };
00108 
00109 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
00110 {
00111     GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
00112     return greentea_test_setup_handler(number_of_cases);
00113 }
00114 
00115 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00116 
00117 int main()
00118 {
00119     Harness::run(specification);
00120 }