Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 /*
maygup01 0:11cc2b7889af 2 * mbed Microcontroller Library
maygup01 0:11cc2b7889af 3 * Copyright (c) 2006-2018 ARM Limited
maygup01 0:11cc2b7889af 4 *
maygup01 0:11cc2b7889af 5 * SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 6 *
maygup01 0:11cc2b7889af 7 * Licensed under the Apache License, Version 2.0 (the "License");
maygup01 0:11cc2b7889af 8 * you may not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 9 * You may obtain a copy of the License at
maygup01 0:11cc2b7889af 10 *
maygup01 0:11cc2b7889af 11 * http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 12 *
maygup01 0:11cc2b7889af 13 * Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 14 * distributed under the License is distributed on an "AS IS" BASIS,
maygup01 0:11cc2b7889af 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 16 * See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 17 * limitations under the License.
maygup01 0:11cc2b7889af 18 */
maygup01 0:11cc2b7889af 19
maygup01 0:11cc2b7889af 20 /*
maygup01 0:11cc2b7889af 21 * Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
maygup01 0:11cc2b7889af 22 */
maygup01 0:11cc2b7889af 23
maygup01 0:11cc2b7889af 24 #include "mbed.h"
maygup01 0:11cc2b7889af 25 #include "FATFileSystem.h"
maygup01 0:11cc2b7889af 26 #include "LittleFileSystem.h"
maygup01 0:11cc2b7889af 27 #include "utest/utest.h"
maygup01 0:11cc2b7889af 28 #include "unity/unity.h"
maygup01 0:11cc2b7889af 29 #include "greentea-client/test_env.h"
maygup01 0:11cc2b7889af 30 #include "common_defines_test.h"
maygup01 0:11cc2b7889af 31 #include "download_test.h"
maygup01 0:11cc2b7889af 32 #include "file_test.h"
maygup01 0:11cc2b7889af 33 #include <string>
maygup01 0:11cc2b7889af 34
maygup01 0:11cc2b7889af 35 #ifdef MBED_CONF_APP_BASICS_TEST_FILENAME
maygup01 0:11cc2b7889af 36 #include MBED_CONF_APP_BASICS_TEST_FILENAME
maygup01 0:11cc2b7889af 37 #else
maygup01 0:11cc2b7889af 38 #include "alice.h"
maygup01 0:11cc2b7889af 39 #endif
maygup01 0:11cc2b7889af 40
maygup01 0:11cc2b7889af 41 #ifndef MBED_CONF_APP_TESTS_FS_SIZE
maygup01 0:11cc2b7889af 42 #define MBED_CONF_APP_TESTS_FS_SIZE (2*1024*1024)
maygup01 0:11cc2b7889af 43 #endif
maygup01 0:11cc2b7889af 44
maygup01 0:11cc2b7889af 45 using namespace utest::v1;
maygup01 0:11cc2b7889af 46
maygup01 0:11cc2b7889af 47 #if !defined(MBED_CONF_APP_NO_LED)
maygup01 0:11cc2b7889af 48 DigitalOut led1(LED1);
maygup01 0:11cc2b7889af 49 DigitalOut led2(LED2);
maygup01 0:11cc2b7889af 50 void led_thread() {
maygup01 0:11cc2b7889af 51 led1 = !led1;
maygup01 0:11cc2b7889af 52 led2 = !led1;
maygup01 0:11cc2b7889af 53 }
maygup01 0:11cc2b7889af 54 #endif
maygup01 0:11cc2b7889af 55
maygup01 0:11cc2b7889af 56 #define MAX_RETRIES 3
maygup01 0:11cc2b7889af 57 NetworkInterface* interface = NULL;
maygup01 0:11cc2b7889af 58
maygup01 0:11cc2b7889af 59 static control_t setup_network(const size_t call_count) {
maygup01 0:11cc2b7889af 60 interface = NetworkInterface::get_default_instance();
maygup01 0:11cc2b7889af 61 TEST_ASSERT_NOT_NULL_MESSAGE(interface, "failed to initialize network");
maygup01 0:11cc2b7889af 62
maygup01 0:11cc2b7889af 63 nsapi_error_t err = -1;
maygup01 0:11cc2b7889af 64 for (int tries = 0; tries < MAX_RETRIES; tries++) {
maygup01 0:11cc2b7889af 65 err = interface->connect();
maygup01 0:11cc2b7889af 66 if (err == NSAPI_ERROR_OK) {
maygup01 0:11cc2b7889af 67 break;
maygup01 0:11cc2b7889af 68 } else {
maygup01 0:11cc2b7889af 69 printf("[ERROR] Connecting to network. Retrying %d of %d...\r\n", tries, MAX_RETRIES);
maygup01 0:11cc2b7889af 70 }
maygup01 0:11cc2b7889af 71 }
maygup01 0:11cc2b7889af 72 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
maygup01 0:11cc2b7889af 73 printf("[NET] IP address is '%s'\n", interface->get_ip_address());
maygup01 0:11cc2b7889af 74 printf("[NET] MAC address is '%s'\n", interface->get_mac_address());
maygup01 0:11cc2b7889af 75 return CaseNext;
maygup01 0:11cc2b7889af 76 }
maygup01 0:11cc2b7889af 77
maygup01 0:11cc2b7889af 78 BlockDevice* bd = BlockDevice::get_default_instance();
maygup01 0:11cc2b7889af 79 SlicingBlockDevice sd(bd, 0, MBED_CONF_APP_TESTS_FS_SIZE);
maygup01 0:11cc2b7889af 80 #if TEST_USE_FILESYSTEM == FS_FAT
maygup01 0:11cc2b7889af 81 FATFileSystem fs("sd");
maygup01 0:11cc2b7889af 82 #else
maygup01 0:11cc2b7889af 83 LittleFileSystem fs("sd");
maygup01 0:11cc2b7889af 84 #endif
maygup01 0:11cc2b7889af 85
maygup01 0:11cc2b7889af 86 static control_t test_format(const size_t call_count) {
maygup01 0:11cc2b7889af 87 int format_err = fs.format(&sd);
maygup01 0:11cc2b7889af 88 TEST_ASSERT_EQUAL_INT_MESSAGE(0, format_err, "could not format block device");
maygup01 0:11cc2b7889af 89
maygup01 0:11cc2b7889af 90 int mount_err = fs.mount(&sd);
maygup01 0:11cc2b7889af 91 TEST_ASSERT_EQUAL_INT_MESSAGE(0, mount_err, "could not mount block device");
maygup01 0:11cc2b7889af 92
maygup01 0:11cc2b7889af 93 return CaseNext;
maygup01 0:11cc2b7889af 94 }
maygup01 0:11cc2b7889af 95
maygup01 0:11cc2b7889af 96 static uint32_t thread_counter = 0;
maygup01 0:11cc2b7889af 97
maygup01 0:11cc2b7889af 98 void download_fn() {
maygup01 0:11cc2b7889af 99 uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
maygup01 0:11cc2b7889af 100 download_test(interface, story, sizeof(story), 256, thread_id);
maygup01 0:11cc2b7889af 101 }
maygup01 0:11cc2b7889af 102 void file_fn(size_t buffer) {
maygup01 0:11cc2b7889af 103 uint32_t thread_id = core_util_atomic_incr_u32(&thread_counter, 1);
maygup01 0:11cc2b7889af 104 char filename[255] = { 0 };
maygup01 0:11cc2b7889af 105 snprintf(filename, 255, "mbed-file-test-%d.txt", thread_id);
maygup01 0:11cc2b7889af 106 file_test_write(filename, 0, story, sizeof(story), buffer);
maygup01 0:11cc2b7889af 107 file_test_read(filename, 0, story, sizeof(story), buffer);
maygup01 0:11cc2b7889af 108 }
maygup01 0:11cc2b7889af 109 void file_1b_fn() { return file_fn(1); }
maygup01 0:11cc2b7889af 110 void file_4b_fn() { return file_fn(4); }
maygup01 0:11cc2b7889af 111 void file_256b_fn() { return file_fn(256); }
maygup01 0:11cc2b7889af 112 void file_1kb_fn() { return file_fn(1024); }
maygup01 0:11cc2b7889af 113
maygup01 0:11cc2b7889af 114 static control_t stress_1_thread(const size_t call_count) {
maygup01 0:11cc2b7889af 115 thread_counter = 0;
maygup01 0:11cc2b7889af 116
maygup01 0:11cc2b7889af 117 Thread t1;
maygup01 0:11cc2b7889af 118 t1.start(download_fn);
maygup01 0:11cc2b7889af 119 t1.join();
maygup01 0:11cc2b7889af 120 t1.start(file_1kb_fn);
maygup01 0:11cc2b7889af 121 t1.join();
maygup01 0:11cc2b7889af 122
maygup01 0:11cc2b7889af 123 return CaseNext;
maygup01 0:11cc2b7889af 124 }
maygup01 0:11cc2b7889af 125
maygup01 0:11cc2b7889af 126 static control_t stress_2_threads(const size_t call_count) {
maygup01 0:11cc2b7889af 127 thread_counter = 0;
maygup01 0:11cc2b7889af 128
maygup01 0:11cc2b7889af 129 Thread t1;
maygup01 0:11cc2b7889af 130 Thread t2;
maygup01 0:11cc2b7889af 131 t1.start(file_1kb_fn);
maygup01 0:11cc2b7889af 132 wait(1);
maygup01 0:11cc2b7889af 133 t2.start(download_fn);
maygup01 0:11cc2b7889af 134 t2.join();
maygup01 0:11cc2b7889af 135 t1.join();
maygup01 0:11cc2b7889af 136
maygup01 0:11cc2b7889af 137 return CaseNext;
maygup01 0:11cc2b7889af 138 }
maygup01 0:11cc2b7889af 139
maygup01 0:11cc2b7889af 140 static control_t stress_3_threads(const size_t call_count) {
maygup01 0:11cc2b7889af 141 thread_counter = 0;
maygup01 0:11cc2b7889af 142
maygup01 0:11cc2b7889af 143 Thread t1;
maygup01 0:11cc2b7889af 144 Thread t2;
maygup01 0:11cc2b7889af 145 Thread t3;
maygup01 0:11cc2b7889af 146 t1.start(file_256b_fn);
maygup01 0:11cc2b7889af 147 t2.start(file_1kb_fn);
maygup01 0:11cc2b7889af 148 wait(1);
maygup01 0:11cc2b7889af 149 t3.start(download_fn);
maygup01 0:11cc2b7889af 150 t3.join();
maygup01 0:11cc2b7889af 151 t2.join();
maygup01 0:11cc2b7889af 152 t1.join();
maygup01 0:11cc2b7889af 153
maygup01 0:11cc2b7889af 154 return CaseNext;
maygup01 0:11cc2b7889af 155 }
maygup01 0:11cc2b7889af 156
maygup01 0:11cc2b7889af 157 static control_t stress_4_threads(const size_t call_count) {
maygup01 0:11cc2b7889af 158 thread_counter = 0;
maygup01 0:11cc2b7889af 159
maygup01 0:11cc2b7889af 160 Thread t1;
maygup01 0:11cc2b7889af 161 Thread t2;
maygup01 0:11cc2b7889af 162 Thread t3;
maygup01 0:11cc2b7889af 163 Thread t4;
maygup01 0:11cc2b7889af 164 t1.start(file_256b_fn);
maygup01 0:11cc2b7889af 165 t2.start(file_256b_fn);
maygup01 0:11cc2b7889af 166 t3.start(file_256b_fn);
maygup01 0:11cc2b7889af 167 wait(1);
maygup01 0:11cc2b7889af 168 t4.start(download_fn);
maygup01 0:11cc2b7889af 169 t4.join();
maygup01 0:11cc2b7889af 170 t3.join();
maygup01 0:11cc2b7889af 171 t2.join();
maygup01 0:11cc2b7889af 172 t1.join();
maygup01 0:11cc2b7889af 173
maygup01 0:11cc2b7889af 174 return CaseNext;
maygup01 0:11cc2b7889af 175 }
maygup01 0:11cc2b7889af 176
maygup01 0:11cc2b7889af 177 template <uint32_t size>
maygup01 0:11cc2b7889af 178 void test_malloc(){
maygup01 0:11cc2b7889af 179
maygup01 0:11cc2b7889af 180 void *bufferTest = NULL;
maygup01 0:11cc2b7889af 181 TEST_ASSERT_MESSAGE(size > 0, "Size must not be zero for test");
maygup01 0:11cc2b7889af 182 printf("Allocating %d bytes",(int)size);
maygup01 0:11cc2b7889af 183 bufferTest = malloc(size);
maygup01 0:11cc2b7889af 184 TEST_ASSERT(bufferTest !=NULL);
maygup01 0:11cc2b7889af 185 free(bufferTest);
maygup01 0:11cc2b7889af 186 }
maygup01 0:11cc2b7889af 187
maygup01 0:11cc2b7889af 188 utest::v1::status_t greentea_setup(const size_t number_of_cases) {
maygup01 0:11cc2b7889af 189 GREENTEA_SETUP(10*60, "default_auto");
maygup01 0:11cc2b7889af 190 return greentea_test_setup_handler(number_of_cases);
maygup01 0:11cc2b7889af 191 }
maygup01 0:11cc2b7889af 192
maygup01 0:11cc2b7889af 193 Case cases[] = {
maygup01 0:11cc2b7889af 194 Case(TEST_NETWORK_TYPE " network setup", setup_network),
maygup01 0:11cc2b7889af 195 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE " format", test_format),
maygup01 0:11cc2b7889af 196 Case("Test memory allocation of 10 K bytes", test_malloc<TEST_MEMORY_SIZE_10K>),
maygup01 0:11cc2b7889af 197 Case("Test memory allocation of 20 K bytes", test_malloc<TEST_MEMORY_SIZE_20K>),
maygup01 0:11cc2b7889af 198 Case("Test memory allocation of 40 K bytes", test_malloc<TEST_MEMORY_SIZE_40K>),
maygup01 0:11cc2b7889af 199 Case("Test memory allocation of 60 K bytes", test_malloc<TEST_MEMORY_SIZE_60K>),
maygup01 0:11cc2b7889af 200 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE != CELLULAR
maygup01 0:11cc2b7889af 201 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 1 thread, dl, file seq.", stress_1_thread),
maygup01 0:11cc2b7889af 202 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 2 threads, dl, 1kb", stress_2_threads),
maygup01 0:11cc2b7889af 203 #endif
maygup01 0:11cc2b7889af 204 Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 3 threads, dl, 256b, 1kb", stress_3_threads),
maygup01 0:11cc2b7889af 205 //Case(TEST_BLOCK_DEVICE_TYPE "+" TEST_FILESYSTEM_TYPE "+" TEST_NETWORK_TYPE " 4 threads, dl, 256b, 256b, 256b", stress_4_threads),
maygup01 0:11cc2b7889af 206 };
maygup01 0:11cc2b7889af 207
maygup01 0:11cc2b7889af 208 Specification specification(greentea_setup, cases);
maygup01 0:11cc2b7889af 209
maygup01 0:11cc2b7889af 210 int main() {
maygup01 0:11cc2b7889af 211 //Create a thread to blink an LED and signal that the device is alive
maygup01 0:11cc2b7889af 212 #if !defined(MBED_CONF_APP_NO_LED)
maygup01 0:11cc2b7889af 213 Ticker t;
maygup01 0:11cc2b7889af 214 t.attach(led_thread, 0.5);
maygup01 0:11cc2b7889af 215 #endif
maygup01 0:11cc2b7889af 216
maygup01 0:11cc2b7889af 217 return !Harness::run(specification);
maygup01 0:11cc2b7889af 218 }