|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#if !DEVICE_SPI // check if SPI is supported on this device |
| 19 | + #error [NOT_SUPPORTED] SPI is not supported on this platform, add 'DEVICE_SPI' definition to your platform. |
| 20 | + |
| 21 | +#elif !DEVICE_I2C // check if I2C is supported on this device |
| 22 | + #error [NOT_SUPPORTED] I2C not supported on this platform, add 'DEVICE_I2C' definition to your platform. |
| 23 | + |
| 24 | +#endif // !DEVICE_SPI or !DEVICE_I2C |
| 25 | + |
| 26 | + |
| 27 | +#include "mbed.h" |
| 28 | +#include "greentea-client/test_env.h" |
| 29 | +#include "unity.h" |
| 30 | +#include "utest.h" |
| 31 | +#include "ci_test_config.h" |
| 32 | +#include "FATFileSystem.h" |
| 33 | +#include "SDBlockDevice.h" |
| 34 | +#include <I2CEeprom.h> |
| 35 | + |
| 36 | +using namespace utest::v1; |
| 37 | + |
| 38 | +#define TEST_STRING_MAX 128 |
| 39 | + |
| 40 | +Thread Thread_I2C; // thread used in multithread tests |
| 41 | +Thread Thread_SPI; // thread used in multithread tests |
| 42 | +osThreadId Multi_Thread_ID; // thread id for main function controlling multithread tests |
| 43 | +char Test_String[TEST_STRING_MAX] = {0}; // reference string used in testing |
| 44 | + |
| 45 | +// Fill array with random characters. TODO: make this string a randomly generated thing |
| 46 | +void init_string() |
| 47 | +{ |
| 48 | + for(int x = 0; x < TEST_STRING_MAX-1; x++){ |
| 49 | + Test_String[x] = 'A' + (rand() % 26); |
| 50 | + } |
| 51 | + Test_String[TEST_STRING_MAX-1] = 0; |
| 52 | + |
| 53 | + DEBUG_PRINTF("\r\n****\r\n Test_String Size(Bytes) = %d\r\n Test_String = %s\r\n****\r\n",TEST_STRING_MAX,Test_String); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +// test I2C API by writting and reading from EEPROM |
| 58 | +void test_I2C() |
| 59 | +{ |
| 60 | + // initialize variables and API |
| 61 | + int address = 0; // starting address to write to |
| 62 | + int num_read = 0; // will report number of bytes read |
| 63 | + int num_written = 0; // will report number of bytes written |
| 64 | + volatile char read_string[TEST_STRING_MAX] = {0}; // string that will be updated from reading EEPROM |
| 65 | + I2CEeprom memory(MBED_CONF_APP_I2C_SDA,MBED_CONF_APP_I2C_SCL,MBED_CONF_APP_I2C_EEPROM_ADDR,32,0); |
| 66 | + |
| 67 | + // write to EEPROM |
| 68 | + num_written = memory.write(address,Test_String,TEST_STRING_MAX); |
| 69 | + |
| 70 | + // read back from EEPROM |
| 71 | + num_read = memory.read(address,(char *)read_string,TEST_STRING_MAX); |
| 72 | + |
| 73 | + // test for equality |
| 74 | + TEST_ASSERT_EQUAL_STRING_MESSAGE(Test_String,(char *)read_string,"String read does not match the string written"); // test that strings match |
| 75 | + TEST_ASSERT_EQUAL_MESSAGE(num_written,num_read,"Number of bytes written does not match the number of bytes read"); |
| 76 | + DEBUG_PRINTF("\r\n****\r\n I2C TEST:\r\n Address = `%d`\r\n Num Bytes Written = `%d` \r\n Num Bytes Read = `%d` \r\n String written = `%s` \r\n String read = `%s` \r\n****\r\n",address,num_written,num_read,Test_String,read_string); |
| 77 | + osSignalSet(Multi_Thread_ID, 0x1); // signal completion of thread |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +// test SPI API by writing and reading from SD card |
| 82 | +void test_SPI() |
| 83 | +{ |
| 84 | + // initialze SD hardware, filesystem, and variables |
| 85 | + SDBlockDevice sd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS); |
| 86 | + FATFileSystem fs("sd", &sd); |
| 87 | + sd.init(); |
| 88 | + fs.mount(&sd); |
| 89 | + FILE *File = fopen("/sd/test_sd_w.txt", "w"); |
| 90 | + TEST_ASSERT_MESSAGE(File != NULL,"SD Card is not present. Please insert an SD Card."); |
| 91 | + volatile char read_string[TEST_STRING_MAX] = {0}; |
| 92 | + |
| 93 | + // write data to SD card |
| 94 | + int error = fprintf(File, Test_String); // write data |
| 95 | + TEST_ASSERT_MESSAGE(error > 0,"Writing file to sd card failed"); // error checking |
| 96 | + fclose(File); // close file on SD |
| 97 | + |
| 98 | + // read data from SD card |
| 99 | + File = fopen("/sd/test_sd_w.txt", "r"); |
| 100 | + fgets((char *)read_string,TEST_STRING_MAX,File); // read string from the file |
| 101 | + |
| 102 | + // test for equality |
| 103 | + TEST_ASSERT_EQUAL_STRING_MESSAGE(Test_String,(char *)read_string,"String read does not match the string written"); // test that strings match |
| 104 | + DEBUG_PRINTF("\r\n****\r\n SPI TEST:\r\n String written = `%s` \r\n String read = `%s` \r\n****\r\n",Test_String,read_string); |
| 105 | + |
| 106 | + // close, unmount, and deinitialize |
| 107 | + fclose(File); // close file on SD |
| 108 | + fs.unmount(); |
| 109 | + sd.deinit(); |
| 110 | + osSignalSet(Multi_Thread_ID, 0x2); // signal completion of thread |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +// test I2C and SPI APIs concurrently in multiple threads |
| 115 | +void test_multiple_threads() |
| 116 | +{ |
| 117 | + Multi_Thread_ID = Thread::gettid(); // update thread id for this thread |
| 118 | + Thread_I2C.start(callback(test_I2C)); // kick off threads |
| 119 | + Thread_SPI.start(callback(test_SPI)); // kick off threads |
| 120 | + wait(0.1); // allow time for debug print statements to complete. |
| 121 | + |
| 122 | + // Use this to wait for both signaling events to occur |
| 123 | + // Internaly the code is doing something like this: |
| 124 | + // if ((signal1 | signal2) & 0x3 == 0x3) then exit, else wait |
| 125 | + osSignalWait(0x3, osWaitForever); |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +// test I2C and SPI APIs concurrently in a single thread |
| 130 | +void test_single_thread() |
| 131 | +{ |
| 132 | + // ******************************* |
| 133 | + // Initialize variables and APIs |
| 134 | + // ******************************* |
| 135 | + |
| 136 | + // I2C test initializations |
| 137 | + int address = 0; // starting address to write to in EEPROM |
| 138 | + int num_read = 0; // will report number of bytes read |
| 139 | + int num_written = 0; // will report number of bytes written |
| 140 | + volatile char read_string_i2c[TEST_STRING_MAX] = {0}; // string that will be updated from reading EEPROM |
| 141 | + I2CEeprom memory(MBED_CONF_APP_I2C_SDA,MBED_CONF_APP_I2C_SCL,MBED_CONF_APP_I2C_EEPROM_ADDR,32,0); |
| 142 | + |
| 143 | + // SPI test initializations |
| 144 | + volatile char read_string_spi[TEST_STRING_MAX] = {0}; // string that will be updated from reading SD card |
| 145 | + SDBlockDevice sd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS); |
| 146 | + FATFileSystem fs("sd", &sd); |
| 147 | + sd.init(); |
| 148 | + fs.mount(&sd); |
| 149 | + FILE *File = fopen("/sd/test_sd_w.txt", "w"); |
| 150 | + TEST_ASSERT_MESSAGE(File != NULL,"SD Card is not present. Please insert an SD Card."); |
| 151 | + |
| 152 | + // ****************************** |
| 153 | + // Begin concurrent API testing |
| 154 | + // ****************************** |
| 155 | + // write to EEPROM using I2C |
| 156 | + num_written = memory.write(address,Test_String,TEST_STRING_MAX); |
| 157 | + |
| 158 | + // write to SD card using SPI |
| 159 | + int error = fprintf(File, Test_String); // write data |
| 160 | + TEST_ASSERT_MESSAGE(error > 0,"Writing file to sd card failed"); // error checking |
| 161 | + fclose(File); // close file on SD |
| 162 | + |
| 163 | + // read back from EEPROM |
| 164 | + num_read = memory.read(address,(char *)read_string_i2c,TEST_STRING_MAX); |
| 165 | + |
| 166 | + // read data from SD card |
| 167 | + File = fopen("/sd/test_sd_w.txt", "r"); |
| 168 | + fgets((char *)read_string_spi,TEST_STRING_MAX,File); // read string from the file |
| 169 | + |
| 170 | + // ****************************** |
| 171 | + // Check results |
| 172 | + // ****************************** |
| 173 | + |
| 174 | + // test results for I2C |
| 175 | + TEST_ASSERT_EQUAL_STRING_MESSAGE(Test_String,(char *)read_string_i2c,"String read does not match the string written"); // test that strings match |
| 176 | + TEST_ASSERT_EQUAL_MESSAGE(num_written,num_read,"Number of bytes written does not match the number of bytes read"); |
| 177 | + DEBUG_PRINTF("\r\n****\r\n I2C TEST:\r\n Address = `%d`\r\n Num Bytes Written = `%d` \r\n Num Bytes Read = `%d` \r\n String written = `%s` \r\n String read = `%s` \r\n****\r\n",address,num_written,num_read,Test_String,read_string_i2c); |
| 178 | + |
| 179 | + // test results for SPI |
| 180 | + TEST_ASSERT_EQUAL_STRING_MESSAGE(Test_String,(char *)read_string_spi,"String read does not match the string written"); // test that strings match |
| 181 | + DEBUG_PRINTF("\r\n****\r\n SPI TEST:\r\n String written = `%s` \r\n String read = `%s` \r\n****\r\n",Test_String,read_string_spi); |
| 182 | + fclose(File); // close file on SD |
| 183 | + fs.unmount(); // unmount |
| 184 | + sd.deinit(); // deinitialize SD |
| 185 | +} |
| 186 | + |
| 187 | + |
| 188 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 189 | +{ |
| 190 | + // Setup Greentea using a reasonable timeout in seconds |
| 191 | + GREENTEA_SETUP(40, "default_auto"); |
| 192 | + return verbose_test_setup_handler(number_of_cases); |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +// Handle test failures, keep testing, dont stop |
| 197 | +utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) |
| 198 | +{ |
| 199 | + greentea_case_failure_abort_handler(source, reason); |
| 200 | + return STATUS_CONTINUE; |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | +// Test cases |
| 205 | +Case cases[] = { |
| 206 | + Case("Concurrent testing of I2C and SPI in a single thread",test_single_thread,greentea_failure_handler), |
| 207 | + Case("Concurrent testing of I2C and SPI in multiple threads",test_multiple_threads,greentea_failure_handler), |
| 208 | +}; |
| 209 | + |
| 210 | + |
| 211 | +Specification specification(test_setup, cases); |
| 212 | + |
| 213 | +// // Entry point into the tests |
| 214 | +int main() { |
| 215 | + init_string(); |
| 216 | + return !Harness::run(specification); |
| 217 | +} |
0 commit comments