Daniel Levine / sd-driver_compatible_with_MAX32630FTHR

Dependents:   CircularBufferSDCardLib time_between_inerupt

Committer:
DVLevine
Date:
Tue Mar 20 17:35:00 2018 +0000
Revision:
0:69bfc1595ae5
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DVLevine 0:69bfc1595ae5 1 #include "mbed.h"
DVLevine 0:69bfc1595ae5 2 #include "greentea-client/test_env.h"
DVLevine 0:69bfc1595ae5 3 #include "unity.h"
DVLevine 0:69bfc1595ae5 4 #include "utest.h"
DVLevine 0:69bfc1595ae5 5 #include <stdlib.h>
DVLevine 0:69bfc1595ae5 6 #include <errno.h>
DVLevine 0:69bfc1595ae5 7
DVLevine 0:69bfc1595ae5 8 using namespace utest::v1;
DVLevine 0:69bfc1595ae5 9
DVLevine 0:69bfc1595ae5 10 // test configuration
DVLevine 0:69bfc1595ae5 11 #ifndef MBED_TEST_FILESYSTEM
DVLevine 0:69bfc1595ae5 12 #define MBED_TEST_FILESYSTEM FATFileSystem
DVLevine 0:69bfc1595ae5 13 #endif
DVLevine 0:69bfc1595ae5 14
DVLevine 0:69bfc1595ae5 15 #ifndef MBED_TEST_FILESYSTEM_DECL
DVLevine 0:69bfc1595ae5 16 #define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs")
DVLevine 0:69bfc1595ae5 17 #endif
DVLevine 0:69bfc1595ae5 18
DVLevine 0:69bfc1595ae5 19 #ifndef MBED_TEST_BLOCKDEVICE
DVLevine 0:69bfc1595ae5 20 #define MBED_TEST_BLOCKDEVICE SDBlockDevice
DVLevine 0:69bfc1595ae5 21 #define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS);
DVLevine 0:69bfc1595ae5 22 #endif
DVLevine 0:69bfc1595ae5 23
DVLevine 0:69bfc1595ae5 24 #ifndef MBED_TEST_BLOCKDEVICE_DECL
DVLevine 0:69bfc1595ae5 25 #define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd
DVLevine 0:69bfc1595ae5 26 #endif
DVLevine 0:69bfc1595ae5 27
DVLevine 0:69bfc1595ae5 28 #ifndef MBED_TEST_FILES
DVLevine 0:69bfc1595ae5 29 #define MBED_TEST_FILES 4
DVLevine 0:69bfc1595ae5 30 #endif
DVLevine 0:69bfc1595ae5 31
DVLevine 0:69bfc1595ae5 32 #ifndef MBED_TEST_DIRS
DVLevine 0:69bfc1595ae5 33 #define MBED_TEST_DIRS 4
DVLevine 0:69bfc1595ae5 34 #endif
DVLevine 0:69bfc1595ae5 35
DVLevine 0:69bfc1595ae5 36 #ifndef MBED_TEST_BUFFER
DVLevine 0:69bfc1595ae5 37 #define MBED_TEST_BUFFER 512
DVLevine 0:69bfc1595ae5 38 #endif
DVLevine 0:69bfc1595ae5 39
DVLevine 0:69bfc1595ae5 40 #ifndef MBED_TEST_TIMEOUT
DVLevine 0:69bfc1595ae5 41 #define MBED_TEST_TIMEOUT 120
DVLevine 0:69bfc1595ae5 42 #endif
DVLevine 0:69bfc1595ae5 43
DVLevine 0:69bfc1595ae5 44 #ifndef MBED_THREAD_COUNT
DVLevine 0:69bfc1595ae5 45 #define MBED_THREAD_COUNT MBED_TEST_FILES
DVLevine 0:69bfc1595ae5 46 #endif
DVLevine 0:69bfc1595ae5 47
DVLevine 0:69bfc1595ae5 48 // declarations
DVLevine 0:69bfc1595ae5 49 #define STRINGIZE(x) STRINGIZE2(x)
DVLevine 0:69bfc1595ae5 50 #define STRINGIZE2(x) #x
DVLevine 0:69bfc1595ae5 51 #define INCLUDE(x) STRINGIZE(x.h)
DVLevine 0:69bfc1595ae5 52
DVLevine 0:69bfc1595ae5 53 #include INCLUDE(MBED_TEST_FILESYSTEM)
DVLevine 0:69bfc1595ae5 54 #include INCLUDE(MBED_TEST_BLOCKDEVICE)
DVLevine 0:69bfc1595ae5 55
DVLevine 0:69bfc1595ae5 56 MBED_TEST_FILESYSTEM_DECL;
DVLevine 0:69bfc1595ae5 57 MBED_TEST_BLOCKDEVICE_DECL;
DVLevine 0:69bfc1595ae5 58
DVLevine 0:69bfc1595ae5 59 Dir dir[MBED_TEST_DIRS];
DVLevine 0:69bfc1595ae5 60 File file[MBED_TEST_FILES];
DVLevine 0:69bfc1595ae5 61 DIR *dd[MBED_TEST_DIRS];
DVLevine 0:69bfc1595ae5 62 FILE *fd[MBED_TEST_FILES];
DVLevine 0:69bfc1595ae5 63 struct dirent ent;
DVLevine 0:69bfc1595ae5 64 struct dirent *ed;
DVLevine 0:69bfc1595ae5 65
DVLevine 0:69bfc1595ae5 66 volatile bool count_done = 0;
DVLevine 0:69bfc1595ae5 67
DVLevine 0:69bfc1595ae5 68 // tests
DVLevine 0:69bfc1595ae5 69
DVLevine 0:69bfc1595ae5 70 void test_file_tests() {
DVLevine 0:69bfc1595ae5 71 int res = bd.init();
DVLevine 0:69bfc1595ae5 72 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 73
DVLevine 0:69bfc1595ae5 74 {
DVLevine 0:69bfc1595ae5 75 res = MBED_TEST_FILESYSTEM::format(&bd);
DVLevine 0:69bfc1595ae5 76 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 77 }
DVLevine 0:69bfc1595ae5 78
DVLevine 0:69bfc1595ae5 79 res = bd.deinit();
DVLevine 0:69bfc1595ae5 80 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 81 }
DVLevine 0:69bfc1595ae5 82
DVLevine 0:69bfc1595ae5 83 void write_file_data (char count) {
DVLevine 0:69bfc1595ae5 84
DVLevine 0:69bfc1595ae5 85 char filename[10];
DVLevine 0:69bfc1595ae5 86 uint8_t wbuffer[MBED_TEST_BUFFER];
DVLevine 0:69bfc1595ae5 87 int res;
DVLevine 0:69bfc1595ae5 88
DVLevine 0:69bfc1595ae5 89 sprintf(filename, "%s%d", "data", count);
DVLevine 0:69bfc1595ae5 90 res = file[count].open(&fs, filename, O_WRONLY | O_CREAT);
DVLevine 0:69bfc1595ae5 91 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 92
DVLevine 0:69bfc1595ae5 93 char letter = 'A'+ count;
DVLevine 0:69bfc1595ae5 94 for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) {
DVLevine 0:69bfc1595ae5 95 wbuffer[i] = letter++;
DVLevine 0:69bfc1595ae5 96 if ('z' == letter) {
DVLevine 0:69bfc1595ae5 97 letter = 'A' + count;
DVLevine 0:69bfc1595ae5 98 }
DVLevine 0:69bfc1595ae5 99 }
DVLevine 0:69bfc1595ae5 100
DVLevine 0:69bfc1595ae5 101 for (uint32_t i = 0; i < 5; i++) {
DVLevine 0:69bfc1595ae5 102 res = file[count].write(wbuffer, MBED_TEST_BUFFER);
DVLevine 0:69bfc1595ae5 103 TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res);
DVLevine 0:69bfc1595ae5 104 }
DVLevine 0:69bfc1595ae5 105
DVLevine 0:69bfc1595ae5 106 res = file[count].close();
DVLevine 0:69bfc1595ae5 107 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 108 }
DVLevine 0:69bfc1595ae5 109
DVLevine 0:69bfc1595ae5 110 void read_file_data (char count) {
DVLevine 0:69bfc1595ae5 111 char filename[10];
DVLevine 0:69bfc1595ae5 112 uint8_t rbuffer[MBED_TEST_BUFFER];
DVLevine 0:69bfc1595ae5 113 int res;
DVLevine 0:69bfc1595ae5 114
DVLevine 0:69bfc1595ae5 115 sprintf(filename, "%s%d", "data", count);
DVLevine 0:69bfc1595ae5 116 res = file[count].open(&fs, filename, O_RDONLY);
DVLevine 0:69bfc1595ae5 117 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 118
DVLevine 0:69bfc1595ae5 119 for (uint32_t i = 0; i < 5; i++) {
DVLevine 0:69bfc1595ae5 120 res = file[count].read(rbuffer, MBED_TEST_BUFFER);
DVLevine 0:69bfc1595ae5 121 TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res);
DVLevine 0:69bfc1595ae5 122 char letter = 'A' + count;
DVLevine 0:69bfc1595ae5 123 for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) {
DVLevine 0:69bfc1595ae5 124 res = rbuffer[i];
DVLevine 0:69bfc1595ae5 125 TEST_ASSERT_EQUAL(letter++, res);
DVLevine 0:69bfc1595ae5 126 if ('z' == letter) {
DVLevine 0:69bfc1595ae5 127 letter = 'A' + count;
DVLevine 0:69bfc1595ae5 128 }
DVLevine 0:69bfc1595ae5 129 }
DVLevine 0:69bfc1595ae5 130 }
DVLevine 0:69bfc1595ae5 131
DVLevine 0:69bfc1595ae5 132 res = file[count].close();
DVLevine 0:69bfc1595ae5 133 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 134 }
DVLevine 0:69bfc1595ae5 135
DVLevine 0:69bfc1595ae5 136 void test_thread_access_test() {
DVLevine 0:69bfc1595ae5 137
DVLevine 0:69bfc1595ae5 138 Thread *data[MBED_THREAD_COUNT];
DVLevine 0:69bfc1595ae5 139 int res = bd.init();
DVLevine 0:69bfc1595ae5 140 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 141 res = fs.mount(&bd);
DVLevine 0:69bfc1595ae5 142 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 143
DVLevine 0:69bfc1595ae5 144 // Write threads in parallel
DVLevine 0:69bfc1595ae5 145 for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
DVLevine 0:69bfc1595ae5 146 data[thread_count] = new Thread(osPriorityNormal);
DVLevine 0:69bfc1595ae5 147 data[thread_count]->start(callback((void(*)(void*))write_file_data, (void*)thread_count));
DVLevine 0:69bfc1595ae5 148 }
DVLevine 0:69bfc1595ae5 149
DVLevine 0:69bfc1595ae5 150 // Wait for write thread to join before creating read thread
DVLevine 0:69bfc1595ae5 151 for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
DVLevine 0:69bfc1595ae5 152 data[thread_count]->join();
DVLevine 0:69bfc1595ae5 153 delete data[thread_count];
DVLevine 0:69bfc1595ae5 154 data[thread_count] = new Thread(osPriorityNormal);
DVLevine 0:69bfc1595ae5 155 data[thread_count]->start(callback((void(*)(void*))read_file_data, (void*)thread_count));
DVLevine 0:69bfc1595ae5 156 }
DVLevine 0:69bfc1595ae5 157
DVLevine 0:69bfc1595ae5 158 // Wait for read threads to join
DVLevine 0:69bfc1595ae5 159 for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) {
DVLevine 0:69bfc1595ae5 160 data[thread_count]->join();
DVLevine 0:69bfc1595ae5 161 delete data[thread_count];
DVLevine 0:69bfc1595ae5 162 }
DVLevine 0:69bfc1595ae5 163 res = fs.unmount();
DVLevine 0:69bfc1595ae5 164 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 165 res = bd.deinit();
DVLevine 0:69bfc1595ae5 166 TEST_ASSERT_EQUAL(0, res);
DVLevine 0:69bfc1595ae5 167 }
DVLevine 0:69bfc1595ae5 168
DVLevine 0:69bfc1595ae5 169 // test setup
DVLevine 0:69bfc1595ae5 170 utest::v1::status_t test_setup(const size_t number_of_cases) {
DVLevine 0:69bfc1595ae5 171 GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto");
DVLevine 0:69bfc1595ae5 172 return verbose_test_setup_handler(number_of_cases);
DVLevine 0:69bfc1595ae5 173 }
DVLevine 0:69bfc1595ae5 174
DVLevine 0:69bfc1595ae5 175 Case cases[] = {
DVLevine 0:69bfc1595ae5 176 Case("File tests", test_file_tests),
DVLevine 0:69bfc1595ae5 177 Case("Filesystem access from multiple threads", test_thread_access_test),
DVLevine 0:69bfc1595ae5 178 };
DVLevine 0:69bfc1595ae5 179
DVLevine 0:69bfc1595ae5 180 Specification specification(test_setup, cases);
DVLevine 0:69bfc1595ae5 181
DVLevine 0:69bfc1595ae5 182 int main() {
DVLevine 0:69bfc1595ae5 183 return !Harness::run(specification);
DVLevine 0:69bfc1595ae5 184 }