Vekatech / SDHI_driver
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2016 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 
00017 #include "mbed.h"
00018 #include "greentea-client/test_env.h"
00019 #include "unity.h"
00020 #include "utest.h"
00021 #include <stdlib.h>
00022 #include <errno.h>
00023 
00024 using namespace utest::v1;
00025 
00026 // test configuration
00027 #ifndef MBED_TEST_FILESYSTEM
00028 #define MBED_TEST_FILESYSTEM FATFileSystem
00029 #endif
00030 
00031 #ifndef MBED_TEST_FILESYSTEM_DECL
00032 #define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
00033 #endif
00034 
00035 #ifndef MBED_TEST_BLOCKDEVICE
00036 #define MBED_TEST_BLOCKDEVICE RZ_SDHIBlockDevice
00037 #define MBED_TEST_BLOCKDEVICE_DECL SDHIBlockDevice bd(MBED_CONF_RZ_SDHI_CH);
00038 #endif
00039 
00040 #ifndef MBED_TEST_BLOCKDEVICE_DECL
00041 #define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
00042 #endif
00043 
00044 #ifndef MBED_TEST_FILES
00045 #define MBED_TEST_FILES 4
00046 #endif
00047 
00048 #ifndef MBED_TEST_DIRS
00049 #define MBED_TEST_DIRS 4
00050 #endif
00051 
00052 #ifndef MBED_TEST_BUFFER
00053 #define MBED_TEST_BUFFER 512
00054 #endif
00055 
00056 #ifndef MBED_TEST_TIMEOUT
00057 #define MBED_TEST_TIMEOUT 120
00058 #endif
00059 
00060 #ifndef MBED_THREAD_COUNT
00061 #define MBED_THREAD_COUNT    MBED_TEST_FILES
00062 #endif
00063 
00064 // declarations
00065 #define STRINGIZE(x) STRINGIZE2(x)
00066 #define STRINGIZE2(x) #x
00067 #define INCLUDE(x, ext) STRINGIZE(x.ext)
00068 
00069 #include INCLUDE(MBED_TEST_FILESYSTEM, h)
00070 #include INCLUDE(MBED_TEST_BLOCKDEVICE, hpp)
00071 
00072 MBED_TEST_FILESYSTEM_DECL;
00073 MBED_TEST_BLOCKDEVICE_DECL;
00074 
00075 Dir dir[MBED_TEST_DIRS];
00076 File file[MBED_TEST_FILES];
00077 DIR *dd[MBED_TEST_DIRS];
00078 FILE *fd[MBED_TEST_FILES];
00079 struct dirent ent;
00080 struct dirent *ed;
00081 
00082 volatile bool count_done = 0;
00083 
00084 // tests
00085 
00086 void test_file_tests()
00087 {
00088     int res = bd.init();
00089     TEST_ASSERT_EQUAL(0, res);
00090 
00091     {
00092         res = MBED_TEST_FILESYSTEM::format(&bd);
00093         TEST_ASSERT_EQUAL(0, res);
00094     }
00095 
00096     res = bd.deinit();
00097     TEST_ASSERT_EQUAL(0, res);
00098 }
00099 
00100 void write_file_data(char count)
00101 {
00102 
00103     char filename[10];
00104     uint8_t wbuffer[MBED_TEST_BUFFER];
00105     int res;
00106 
00107     sprintf(filename, "%s%d", "data", count);
00108     res = file[count].open(&fs, filename, O_WRONLY | O_CREAT);
00109     TEST_ASSERT_EQUAL(0, res);
00110 
00111     char letter = 'A' + count;
00112     for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) {
00113         wbuffer[i] = letter++;
00114         if ('z' == letter) {
00115             letter = 'A' + count;
00116         }
00117     }
00118 
00119     for (uint32_t i = 0; i < 5; i++) {
00120         res = file[count].write(wbuffer, MBED_TEST_BUFFER);
00121         TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res);
00122     }
00123 
00124     res = file[count].close();
00125     TEST_ASSERT_EQUAL(0, res);
00126 }
00127 
00128 void read_file_data(char count)
00129 {
00130     char filename[10];
00131     uint8_t rbuffer[MBED_TEST_BUFFER];
00132     int res;
00133 
00134     sprintf(filename, "%s%d", "data", count);
00135     res = file[count].open(&fs, filename, O_RDONLY);
00136     TEST_ASSERT_EQUAL(0, res);
00137 
00138     for (uint32_t i = 0; i < 5; i++) {
00139         res = file[count].read(rbuffer, MBED_TEST_BUFFER);
00140         TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res);
00141         char letter = 'A' + count;
00142         for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) {
00143             res = rbuffer[i];
00144             TEST_ASSERT_EQUAL(letter++, res);
00145             if ('z' == letter) {
00146                 letter = 'A' + count;
00147             }
00148         }
00149     }
00150 
00151     res = file[count].close();
00152     TEST_ASSERT_EQUAL(0, res);
00153 }
00154 
00155 void test_thread_access_test()
00156 {
00157     char *dummy = new (std::nothrow) char[OS_STACK_SIZE * MBED_THREAD_COUNT];
00158     delete[] dummy;
00159     TEST_SKIP_UNLESS_MESSAGE(dummy, "Not enough memory to run test");
00160 
00161     Thread *data[MBED_THREAD_COUNT];
00162     int res = bd.init();
00163     TEST_ASSERT_EQUAL(0, res);
00164     res = fs.mount(&bd);
00165     TEST_ASSERT_EQUAL(0, res);
00166 
00167     // Write threads in parallel
00168     for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
00169         data[thread_count] = new Thread(osPriorityNormal);
00170         data[thread_count]->start(callback((void(*)(void *))write_file_data, (void *)thread_count));
00171     }
00172 
00173     // Wait for write thread to join before creating read thread
00174     for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
00175         data[thread_count]->join();
00176         delete data[thread_count];
00177         data[thread_count] = new Thread(osPriorityNormal);
00178         data[thread_count]->start(callback((void(*)(void *))read_file_data, (void *)thread_count));
00179     }
00180 
00181     // Wait for read threads to join
00182     for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
00183         data[thread_count]->join();
00184         delete data[thread_count];
00185     }
00186     res = fs.unmount();
00187     TEST_ASSERT_EQUAL(0, res);
00188     res = bd.deinit();
00189     TEST_ASSERT_EQUAL(0, res);
00190 }
00191 
00192 // test setup
00193 utest::v1::status_t test_setup(const size_t number_of_cases)
00194 {
00195     GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
00196     return verbose_test_setup_handler(number_of_cases);
00197 }
00198 
00199 Case cases[] = {
00200     Case("File tests", test_file_tests),
00201     Case("Filesystem access from multiple threads", test_thread_access_test),
00202 };
00203 
00204 Specification specification(test_setup, cases);
00205 
00206 int main()
00207 {
00208     return !Harness::run(specification);
00209 }