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.
Dependents: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
main.cpp
00001 /* 00002 * Copyright (c) 2017, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #include "mbed.h" 00019 #include "greentea-client/test_env.h" 00020 #include "unity.h" 00021 #include "utest.h" 00022 #include "rtos.h" 00023 #include "hal/us_ticker_api.h" 00024 00025 #if !DEVICE_LOWPOWERTIMER 00026 #error [NOT_SUPPORTED] test not supported 00027 #endif 00028 00029 using namespace utest::v1; 00030 00031 extern uint32_t SystemCoreClock; 00032 00033 /* This test is created based on the test for Timer class. 00034 * Since low power timer is less accurate than regular 00035 * timer we need to adjust delta. 00036 */ 00037 00038 /* Macro to define delta based on CPU clock frequency. 00039 * 00040 * Note that some extra time is counted by the timer. 00041 * Additional time is caused by the function calls and 00042 * additional operations performed by wait and 00043 * stop functions before in fact timer is stopped. This may 00044 * add additional time to the counted result. 00045 * 00046 * To take in to account this extra time we introduce DELTA 00047 * value based on CPU clock (speed): 00048 * DELTA = TOLERANCE_FACTOR / SystemCoreClock * US_FACTOR 00049 * 00050 * e.g. 00051 * For K64F DELTA = (80000 / 120000000) * 1000000 = 666[us] 00052 * For NUCLEO_F070RB DELTA = (80000 / 48000000) * 1000000 = 1666[us] 00053 * For NRF51_DK DELTA = (80000 / 16000000) * 1000000 = 5000[us] 00054 */ 00055 #define US_PER_SEC 1000000 00056 #define US_PER_MSEC 1000 00057 #define TOLERANCE_FACTOR 80000.0f 00058 #define US_FACTOR 1000000.0f 00059 00060 static const int delta_sys_clk_us = ((int) (TOLERANCE_FACTOR / (float) SystemCoreClock * US_FACTOR)); 00061 00062 /* When test performs time measurement using Timer in sequence, then measurement error accumulates 00063 * in the successive attempts. */ 00064 #define DELTA_US(i) (delta_sys_clk_us * i) 00065 #define DELTA_S(i) ((float)delta_sys_clk_us * i / US_PER_SEC) 00066 #define DELTA_MS(i) (1 + ( (i * delta_sys_clk_us) / US_PER_MSEC)) 00067 00068 /* This test verifies if low power timer is stopped after 00069 * creation. 00070 * 00071 * Given Timer has been successfully created. 00072 * When read of timer elapsed time is requested. 00073 * Then result is always 0. 00074 */ 00075 void test_lptimer_creation() 00076 { 00077 LowPowerTimer lp_timer; 00078 00079 /* Check results. */ 00080 TEST_ASSERT_EQUAL_FLOAT(0, lp_timer.read()); 00081 TEST_ASSERT_EQUAL_INT32(0, lp_timer.read_ms()); 00082 TEST_ASSERT_EQUAL_INT32(0, lp_timer.read_us()); 00083 TEST_ASSERT_EQUAL_UINT64(0, lp_timer.read_high_resolution_us()); 00084 00085 /* Wait 10 ms. 00086 * After that operation timer read routines should still return 0. */ 00087 wait_ms(10); 00088 00089 /* Check results. */ 00090 TEST_ASSERT_EQUAL_FLOAT(0, lp_timer.read()); 00091 TEST_ASSERT_EQUAL_INT32(0, lp_timer.read_ms()); 00092 TEST_ASSERT_EQUAL_INT32(0, lp_timer.read_us()); 00093 TEST_ASSERT_EQUAL_UINT64(0, lp_timer.read_high_resolution_us()); 00094 } 00095 00096 /* This test verifies if read(), read_us(), read_ms(), 00097 * read_high_resolution_us() 00098 * functions return time accumulated between 00099 * low power timer starts and stops. 00100 * 00101 * Given Timer has been successfully created and 00102 * few times started and stopped after a specified period of time. 00103 * When timer read request is performed. 00104 * Then read functions return accumulated time elapsed between starts 00105 * and stops. 00106 */ 00107 void test_lptimer_time_accumulation() 00108 { 00109 LowPowerTimer lp_timer; 00110 00111 /* Start the timer. */ 00112 lp_timer.start(); 00113 00114 /* Wait 10 ms. */ 00115 wait_ms(10); 00116 00117 /* Stop the timer. */ 00118 lp_timer.stop(); 00119 00120 /* Check results - totally 10 ms have elapsed. */ 00121 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, lp_timer.read()); 00122 TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 10, lp_timer.read_ms()); 00123 TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 10000, lp_timer.read_us()); 00124 TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 10000, lp_timer.read_high_resolution_us()); 00125 00126 /* Wait 50 ms - this is done to show that time elapsed when 00127 * the timer is stopped does not have influence on the 00128 * timer counted time. */ 00129 wait_ms(50); 00130 00131 /* ------ */ 00132 00133 /* Start the timer. */ 00134 lp_timer.start(); 00135 00136 /* Wait 20 ms. */ 00137 wait_ms(20); 00138 00139 /* Stop the timer. */ 00140 lp_timer.stop(); 00141 00142 /* Check results - totally 30 ms have elapsed. */ 00143 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(2), 0.030f, lp_timer.read()); 00144 TEST_ASSERT_INT32_WITHIN(DELTA_MS(2), 30, lp_timer.read_ms()); 00145 TEST_ASSERT_INT32_WITHIN(DELTA_US(2), 30000, lp_timer.read_us()); 00146 TEST_ASSERT_UINT64_WITHIN(DELTA_US(2), 30000, lp_timer.read_high_resolution_us()); 00147 00148 /* Wait 50 ms - this is done to show that time elapsed when 00149 * the timer is stopped does not have influence on the 00150 * timer counted time. */ 00151 00152 /* ------ */ 00153 00154 /* Start the timer. */ 00155 lp_timer.start(); 00156 00157 /* Wait 30 ms. */ 00158 wait_ms(30); 00159 00160 /* Stop the timer. */ 00161 lp_timer.stop(); 00162 00163 /* Check results - totally 60 ms have elapsed. */ 00164 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(3), 0.060f, lp_timer.read()); 00165 TEST_ASSERT_INT32_WITHIN(DELTA_MS(3), 60, lp_timer.read_ms()); 00166 TEST_ASSERT_INT32_WITHIN(DELTA_US(3), 60000, lp_timer.read_us()); 00167 TEST_ASSERT_UINT64_WITHIN(DELTA_US(3), 60000, lp_timer.read_high_resolution_us()); 00168 00169 /* Wait 50 ms - this is done to show that time elapsed when 00170 * the timer is stopped does not have influence on the 00171 * timer time. */ 00172 wait_ms(50); 00173 00174 /* ------ */ 00175 00176 /* Start the timer. */ 00177 lp_timer.start(); 00178 00179 /* Wait 1 sec. */ 00180 wait_ms(1000); 00181 00182 /* Stop the timer. */ 00183 lp_timer.stop(); 00184 00185 /* Check results - totally 1060 ms have elapsed. */ 00186 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(4), 1.060f, lp_timer.read()); 00187 TEST_ASSERT_INT32_WITHIN(DELTA_MS(4), 1060, lp_timer.read_ms()); 00188 TEST_ASSERT_INT32_WITHIN(DELTA_US(4), 1060000, lp_timer.read_us()); 00189 TEST_ASSERT_UINT64_WITHIN(DELTA_US(4), 1060000, lp_timer.read_high_resolution_us()); 00190 } 00191 00192 /* This test verifies if reset() function resets the 00193 * low power timer counted time. 00194 * 00195 * Given timer has been started and stopped once, then reset 00196 * operation was performed. 00197 * When timer is started and stopped next time. 00198 * Then timer read functions returns only the the second 00199 * measured time. 00200 */ 00201 void test_lptimer_reset() 00202 { 00203 LowPowerTimer lp_timer; 00204 00205 /* First measure 10 ms delay. */ 00206 lp_timer.start(); 00207 00208 /* Wait 10 ms. */ 00209 wait_ms(10); 00210 00211 /* Stop the timer. */ 00212 lp_timer.stop(); 00213 00214 /* Check results - totally 10 ms elapsed. */ 00215 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, lp_timer.read()); 00216 TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 10, lp_timer.read_ms()); 00217 TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 10000, lp_timer.read_us()); 00218 TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 10000, lp_timer.read_high_resolution_us()); 00219 00220 /* Reset the timer - previous measured time should be lost now. */ 00221 lp_timer.reset(); 00222 00223 /* Now measure 20 ms delay. */ 00224 lp_timer.start(); 00225 00226 /* Wait 20 ms. */ 00227 wait_ms(20); 00228 00229 /* Stop the timer. */ 00230 lp_timer.stop(); 00231 00232 /* Check results - 20 ms elapsed since the reset. */ 00233 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.020f, lp_timer.read()); 00234 TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), 20, lp_timer.read_ms()); 00235 TEST_ASSERT_INT32_WITHIN(DELTA_US(1), 20000, lp_timer.read_us()); 00236 TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), 20000, lp_timer.read_high_resolution_us()); 00237 } 00238 00239 /* This test verifies if calling start() for already 00240 * started low power timer does nothing. 00241 * 00242 * Given timer is already started. 00243 * When timer is started again. 00244 * Then second start operation is ignored. 00245 */ 00246 void test_lptimer_start_started_timer() 00247 { 00248 LowPowerTimer lp_timer; 00249 00250 /* Start the timer. */ 00251 lp_timer.start(); 00252 00253 /* Wait 10 ms. */ 00254 wait_ms(10); 00255 00256 /* Now start timer again. */ 00257 lp_timer.start(); 00258 00259 /* Wait 20 ms. */ 00260 wait_ms(20); 00261 00262 /* Stop the timer. */ 00263 lp_timer.stop(); 00264 00265 /* Check results - 30 ms have elapsed since the first start. */ 00266 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(2), 0.030f, lp_timer.read()); 00267 TEST_ASSERT_INT32_WITHIN(DELTA_MS(2), 30, lp_timer.read_ms()); 00268 TEST_ASSERT_INT32_WITHIN(DELTA_US(2), 30000, lp_timer.read_us()); 00269 TEST_ASSERT_UINT64_WITHIN(DELTA_US(2), 30000, lp_timer.read_high_resolution_us()); 00270 } 00271 00272 /* This test verifies low power timer float operator. 00273 * 00274 * Given timer is created and a time period time is counted. 00275 * When timer object is casted on float type. 00276 * Then counted type in seconds is returned by means of 00277 * read() function. 00278 */ 00279 void test_lptimer_float_operator() 00280 { 00281 LowPowerTimer lp_timer; 00282 00283 /* Start the timer. */ 00284 lp_timer.start(); 00285 00286 /* Wait 10 ms. */ 00287 wait_ms(10); 00288 00289 /* Stop the timer. */ 00290 lp_timer.stop(); 00291 00292 /* Check result - 10 ms elapsed. */ 00293 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), 0.010f, (float )(lp_timer)); 00294 } 00295 00296 /* This test verifies if time counted by the low power timer is 00297 * valid. 00298 * 00299 * Given timer is created. 00300 * When timer is used to measure 1ms/10ms/100ms/1s 00301 * delays. 00302 * Then the results are valid (within acceptable range). 00303 */ 00304 template<int wait_val_us> 00305 void test_lptimer_time_measurement() 00306 { 00307 LowPowerTimer lp_timer; 00308 00309 /* Start the timer. */ 00310 lp_timer.start(); 00311 00312 /* Wait <wait_val_us> us. */ 00313 wait_us(wait_val_us); 00314 00315 /* Stop the timer. */ 00316 lp_timer.stop(); 00317 00318 /* Check results - wait_val_us us have elapsed. */ 00319 TEST_ASSERT_FLOAT_WITHIN(DELTA_S(1), (float )wait_val_us / 1000000, lp_timer.read()); 00320 TEST_ASSERT_INT32_WITHIN(DELTA_MS(1), wait_val_us / 1000, lp_timer.read_ms()); 00321 TEST_ASSERT_INT32_WITHIN(DELTA_US(1), wait_val_us, lp_timer.read_us()); 00322 TEST_ASSERT_UINT64_WITHIN(DELTA_US(1), wait_val_us, lp_timer.read_high_resolution_us()); 00323 } 00324 00325 utest::v1::status_t test_setup(const size_t number_of_cases) 00326 { 00327 GREENTEA_SETUP(15, "default_auto"); 00328 return verbose_test_setup_handler(number_of_cases); 00329 } 00330 00331 Case cases[] = { 00332 Case("Test: LowPowerTimer - stopped after creation.", test_lptimer_creation), 00333 Case("Test: LowPowerTimer - measure time accumulation.", test_lptimer_time_accumulation), 00334 Case("Test: LowPowerTimer - reset.", test_lptimer_reset), 00335 Case("Test: LowPowerTimer - start started timer.", test_lptimer_start_started_timer), 00336 Case("Test: LowPowerTimer - float operator.", test_lptimer_float_operator), 00337 Case("Test: LowPowerTimer - time measurement 1 ms.", test_lptimer_time_measurement<1000>), 00338 Case("Test: LowPowerTimer - time measurement 10 ms.", test_lptimer_time_measurement<10000>), 00339 Case("Test: LowPowerTimer - time measurement 100 ms.", test_lptimer_time_measurement<100000>), 00340 Case("Test: LowPowerTimer - time measurement 1 s.", test_lptimer_time_measurement<1000000>) 00341 }; 00342 00343 Specification specification(test_setup, cases); 00344 00345 int main() 00346 { 00347 return !Harness::run(specification); 00348 }
Generated on Tue Jul 12 2022 13:03:02 by
