Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2017 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 #include "mbed_events.h" 00017 #include "mbed.h" 00018 #include "rtos.h" 00019 #include "greentea-client/test_env.h" 00020 #include "unity.h" 00021 #include "utest.h" 00022 #include <cstdlib> 00023 #include <cmath> 00024 00025 using namespace utest::v1; 00026 00027 #if !DEVICE_USTICKER 00028 #error [NOT_SUPPORTED] test not supported 00029 #endif 00030 00031 // Test delay 00032 #ifndef TEST_EVENTS_TIMING_TIME 00033 #define TEST_EVENTS_TIMING_TIME 20000 00034 #endif 00035 00036 #ifndef TEST_EVENTS_TIMING_MEAN 00037 #define TEST_EVENTS_TIMING_MEAN 25 00038 #endif 00039 00040 #ifndef M_PI 00041 #define M_PI 3.14159265358979323846264338327950288 00042 #endif 00043 00044 // Random number generation to skew timing values 00045 float gauss(float mu, float sigma) 00046 { 00047 float x = (float)rand() / ((float)RAND_MAX + 1); 00048 float y = (float)rand() / ((float)RAND_MAX + 1); 00049 float x2pi = x * 2.0 * M_PI; 00050 float g2rad = sqrt(-2.0 * log(1.0 - y)); 00051 float z = cos(x2pi) * g2rad; 00052 return mu + z * sigma; 00053 } 00054 00055 float chisq(float sigma) 00056 { 00057 return pow(gauss(0, sqrt(sigma)), 2); 00058 } 00059 00060 00061 Timer timer; 00062 DigitalOut led(LED1); 00063 00064 equeue_sema_t sema; 00065 00066 // Timer timing test 00067 void timer_timing_test() 00068 { 00069 timer.reset(); 00070 timer.start(); 00071 int prev = timer.read_us(); 00072 00073 while (prev < TEST_EVENTS_TIMING_TIME * 1000) { 00074 int next = timer.read_us(); 00075 if (next < prev) { 00076 printf("backwards drift %d -> %d (%08x -> %08x)\r\n", 00077 prev, next, prev, next); 00078 } 00079 TEST_ASSERT(next >= prev); 00080 prev = next; 00081 } 00082 } 00083 00084 // equeue tick timing test 00085 void tick_timing_test() 00086 { 00087 unsigned start = equeue_tick(); 00088 int prev = 0; 00089 00090 while (prev < TEST_EVENTS_TIMING_TIME) { 00091 int next = equeue_tick() - start; 00092 if (next < prev) { 00093 printf("backwards drift %d -> %d (%08x -> %08x)\r\n", 00094 prev, next, prev, next); 00095 } 00096 TEST_ASSERT(next >= prev); 00097 prev = next; 00098 } 00099 } 00100 00101 // equeue semaphore timing test 00102 void semaphore_timing_test() 00103 { 00104 srand(0); 00105 timer.reset(); 00106 timer.start(); 00107 00108 int err = equeue_sema_create(&sema); 00109 TEST_ASSERT_EQUAL(0, err); 00110 00111 while (timer.read_ms() < TEST_EVENTS_TIMING_TIME) { 00112 int delay = chisq(TEST_EVENTS_TIMING_MEAN); 00113 00114 int start = timer.read_us(); 00115 equeue_sema_wait(&sema, delay); 00116 int taken = timer.read_us() - start; 00117 00118 if (taken < (delay * 1000 - 5000) || taken > (delay * 1000 + 5000)) { 00119 printf("delay %dms => error %dus\r\n", delay, abs(1000 * delay - taken)); 00120 } 00121 00122 TEST_ASSERT_INT_WITHIN(5000, taken, delay * 1000); 00123 00124 led = !led; 00125 } 00126 00127 equeue_sema_destroy(&sema); 00128 } 00129 00130 00131 // Test setup 00132 utest::v1::status_t test_setup(const size_t number_of_cases) 00133 { 00134 GREENTEA_SETUP((number_of_cases + 1)*TEST_EVENTS_TIMING_TIME / 1000, "default_auto"); 00135 return verbose_test_setup_handler(number_of_cases); 00136 } 00137 00138 const Case cases[] = { 00139 Case("Testing accuracy of timer", timer_timing_test), 00140 Case("Testing accuracy of equeue tick", tick_timing_test), 00141 Case("Testing accuracy of equeue semaphore", semaphore_timing_test), 00142 }; 00143 00144 Specification specification(test_setup, cases); 00145 00146 int main() 00147 { 00148 return !Harness::run(specification); 00149 } 00150
Generated on Tue Aug 9 2022 00:37:13 by
1.7.2