leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * mbed Microcontroller Library
00003  * Copyright (c) 2006-2018 ARM Limited
00004  *
00005  * SPDX-License-Identifier: Apache-2.0
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 /*
00021  * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
00022 */
00023 
00024 #include "mbed.h"
00025 #include "FATFileSystem.h"
00026 #include "LittleFileSystem.h"
00027 #include "utest/utest.h"
00028 #include "unity/unity.h"
00029 #include "greentea-client/test_env.h"
00030 #include "common_defines_test.h"
00031 #include "file_test.h"
00032 
00033 #ifdef MBED_CONF_APP_BASICS_TEST_FILENAME
00034   #include MBED_CONF_APP_BASICS_TEST_FILENAME
00035 #else
00036   #include "alice.h"
00037 #endif
00038 
00039 #ifndef MBED_CONF_APP_TESTS_FS_SIZE
00040   #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024)
00041 #endif
00042 
00043 using namespace utest::v1;
00044 
00045 #if !defined(MBED_CONF_APP_NO_LED)
00046 DigitalOut led1(LED1);
00047 DigitalOut led2(LED2);
00048 void led_thread() {
00049     led1 = !led1;
00050     led2 = !led1;
00051 }
00052 #endif
00053 
00054 BlockDevice* bd = BlockDevice::get_default_instance();
00055 SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
00056 #if TEST_USE_FILESYSTEM == FS_FAT
00057 FATFileSystem fs("sd");
00058 #else
00059 LittleFileSystem fs("sd");
00060 #endif
00061 
00062 static control_t test_format(const size_t call_count) {
00063     int mount_err = fs.mount(&sd);
00064     TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
00065 
00066     int format_err = fs.reformat(&sd);
00067     TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
00068 
00069     return CaseNext;
00070 }
00071 
00072 static control_t test_block_size_1(const size_t call_count) {
00073     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 1);
00074     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 1);
00075 
00076     return CaseNext;
00077 }
00078 static control_t test_block_size_4(const size_t call_count) {
00079     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 4);
00080     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 4);
00081 
00082     return CaseNext;
00083 }
00084 static control_t test_block_size_16(const size_t call_count) {
00085     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 16);
00086     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 16);
00087 
00088     return CaseNext;
00089 }
00090 static control_t test_block_size_64(const size_t call_count) {
00091     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 64);
00092     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 64);
00093 
00094     return CaseNext;
00095 }
00096 static control_t test_block_size_256(const size_t call_count) {
00097     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 256);
00098     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 256);
00099 
00100     return CaseNext;
00101 }
00102 static control_t test_block_size_1k(const size_t call_count) {
00103     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 1024);
00104     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 1024);
00105 
00106     return CaseNext;
00107 }
00108 static control_t test_block_size_4k(const size_t call_count) {
00109     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 4*1024);
00110     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 4*1024);
00111 
00112     return CaseNext;
00113 }
00114 static control_t test_block_size_16k(const size_t call_count) {
00115     file_test_write("mbed-file-test-0.txt", 0, story, sizeof(story), 16*1024);
00116     file_test_read("mbed-file-test-0.txt", 0, story, sizeof(story), 16*1024);
00117 
00118     return CaseNext;
00119 }
00120 
00121 utest::v1::status_t greentea_setup(const size_t number_of_cases) {
00122     GREENTEA_SETUP(5*60, "default_auto");
00123     return greentea_test_setup_handler(number_of_cases);
00124 }
00125 
00126 Case cases[] = {
00127     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " format", test_format),
00128     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff     1", test_block_size_1),
00129     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff     4", test_block_size_4),
00130     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff    16", test_block_size_16),
00131     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff    64", test_block_size_64),
00132     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff   256", test_block_size_256),
00133     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff  1024", test_block_size_1k),
00134     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff  4096", test_block_size_4k),
00135     Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " 1 file, buff 16384", test_block_size_16k),
00136 };
00137 
00138 Specification specification(greentea_setup, cases);
00139 
00140 int main() {
00141     //Create a thread to blink an LED and signal that the device is alive
00142 #if !defined(MBED_CONF_APP_NO_LED)
00143     Ticker t;
00144     t.attach(led_thread, 0.5);
00145 #endif
00146 
00147     return !Harness::run(specification);
00148 }