EL4121 Embedded System / mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2013-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.h"
00017 #include "greentea-client/test_env.h"
00018 #include "utest/utest.h"
00019 #include "unity/unity.h"
00020 
00021 
00022 #if !DEVICE_LOWPOWERTIMER
00023     #error [NOT_SUPPORTED] Low power ticker not supported for this target
00024 #endif
00025 
00026 using utest::v1::Case;
00027 
00028 static const int test_timeout = 10;
00029 
00030 #define TICKER_COUNT 16
00031 #define MULTI_TICKER_TIME_MS 100
00032 
00033 /* Due to poor accuracy of LowPowerTicker on many platforms
00034    there is no sense to tune tolerance value as it was in Ticker tests.
00035 
00036    Tolerance value is set to 2000us to cover this diversity */
00037 #define TOLERANCE_US 2000
00038 
00039 
00040 volatile uint32_t ticker_callback_flag;
00041 volatile uint32_t multi_counter;
00042 Timer gtimer;
00043 
00044 
00045 
00046 void sem_release(Semaphore *sem)
00047 {
00048     sem->release();
00049 }
00050 
00051 
00052 void stop_gtimer_set_flag(void)
00053 {
00054     gtimer.stop();
00055     core_util_atomic_incr_u32((uint32_t*)&ticker_callback_flag, 1);
00056 }
00057 
00058 void increment_multi_counter(void)
00059 {
00060     core_util_atomic_incr_u32((uint32_t*)&multi_counter, 1);;
00061 }
00062 
00063 /** Test many tickers run one after the other
00064 
00065     Given many Tickers
00066     When schedule them one after the other with the same time intervals
00067     Then tickers properly execute callbacks
00068     When schedule them one after the other with the different time intervals
00069     Then tickers properly execute callbacks
00070  */
00071 void test_multi_ticker(void)
00072 {
00073     LowPowerTicker ticker[TICKER_COUNT];
00074     const uint32_t extra_wait = 10; // extra 10ms wait time
00075 
00076     multi_counter = 0;
00077     for (int i = 0; i < TICKER_COUNT; i++) {
00078         ticker[i].attach_us(callback(increment_multi_counter), MULTI_TICKER_TIME_MS * 1000);
00079     }
00080 
00081     Thread::wait(MULTI_TICKER_TIME_MS + extra_wait);
00082     for (int i = 0; i < TICKER_COUNT; i++) {
00083             ticker[i].detach();
00084     }
00085     TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
00086 
00087     multi_counter = 0;
00088     for (int i = 0; i < TICKER_COUNT; i++) {
00089         ticker[i].attach_us(callback(increment_multi_counter), (MULTI_TICKER_TIME_MS + i) * 1000);
00090     }
00091 
00092     Thread::wait(MULTI_TICKER_TIME_MS + TICKER_COUNT + extra_wait);
00093     for (int i = 0; i < TICKER_COUNT; i++) {
00094         ticker[i].detach();
00095     }
00096     TEST_ASSERT_EQUAL(TICKER_COUNT, multi_counter);
00097 }
00098 
00099 /** Test multi callback time
00100 
00101     Given a Ticker
00102     When the callback is attached multiple times
00103     Then ticker properly execute callback multiple times
00104  */
00105 void test_multi_call_time(void)
00106 {
00107     LowPowerTicker ticker;
00108     int time_diff;
00109     const int attach_count = 10;
00110 
00111     for (int i = 0; i < attach_count; i++) {
00112         ticker_callback_flag = 0;
00113         gtimer.reset();
00114 
00115         gtimer.start();
00116         ticker.attach_us(callback(stop_gtimer_set_flag), MULTI_TICKER_TIME_MS * 1000);
00117         while(!ticker_callback_flag);
00118         time_diff = gtimer.read_us();
00119 
00120         TEST_ASSERT_UINT32_WITHIN(TOLERANCE_US, MULTI_TICKER_TIME_MS * 1000, time_diff);
00121     }
00122 }
00123 
00124 /** Test if detach cancel scheduled callback event
00125 
00126     Given a Ticker with callback attached
00127     When the callback is detached
00128     Then the callback is not being called
00129  */
00130 void test_detach(void)
00131 {
00132     LowPowerTicker ticker;
00133     int32_t ret;
00134     const float ticker_time_s = 0.1f;
00135     const uint32_t wait_time_ms = 500;
00136     Semaphore sem(0, 1);
00137 
00138     ticker.attach(callback(sem_release, &sem), ticker_time_s);
00139 
00140     ret = sem.wait();
00141     TEST_ASSERT_TRUE(ret > 0);
00142 
00143     ret = sem.wait();
00144     ticker.detach(); /* cancel */
00145     TEST_ASSERT_TRUE(ret > 0);
00146 
00147     ret = sem.wait(wait_time_ms);
00148     TEST_ASSERT_EQUAL(0, ret);
00149 }
00150 
00151 /** Test single callback time via attach
00152 
00153     Given a Ticker
00154     When callback attached with time interval specified
00155     Then ticker properly executes callback within a specified time interval
00156  */
00157 template<us_timestamp_t DELAY_US>
00158 void test_attach_time(void)
00159 {
00160     LowPowerTicker ticker;
00161     ticker_callback_flag = 0;
00162 
00163     gtimer.reset();
00164     gtimer.start();
00165     ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
00166     while(!ticker_callback_flag);
00167     ticker.detach();
00168     const int time_diff = gtimer.read_us();
00169 
00170     TEST_ASSERT_UINT64_WITHIN(TOLERANCE_US, DELAY_US, time_diff);
00171 }
00172 
00173 /** Test single callback time via attach_us
00174 
00175     Given a Ticker
00176     When callback attached with time interval specified
00177     Then ticker properly executes callback within a specified time interval
00178  */
00179 template<us_timestamp_t DELAY_US>
00180 void test_attach_us_time(void)
00181 {
00182     LowPowerTicker ticker;
00183     ticker_callback_flag = 0;
00184 
00185     gtimer.reset();
00186     gtimer.start();
00187     ticker.attach_us(callback(stop_gtimer_set_flag), DELAY_US);
00188     while(!ticker_callback_flag);
00189     ticker.detach();
00190     const int time_diff = gtimer.read_us();
00191 
00192     TEST_ASSERT_UINT64_WITHIN(TOLERANCE_US, DELAY_US, time_diff);
00193 }
00194 
00195 // Test cases
00196 Case cases[] = {
00197     Case("Test attach for 0.001s and time measure", test_attach_time<1000>),
00198     Case("Test attach_us for 1ms and time measure", test_attach_us_time<1000>),
00199     Case("Test attach for 0.01s and time measure", test_attach_time<10000>),
00200     Case("Test attach_us for 10ms and time measure", test_attach_us_time<10000>),
00201     Case("Test attach for 0.1s and time measure", test_attach_time<100000>),
00202     Case("Test attach_us for 100ms and time measure", test_attach_us_time<100000>),
00203     Case("Test attach for 0.5s and time measure", test_attach_time<500000>),
00204     Case("Test attach_us for 500ms and time measure", test_attach_us_time<500000>),
00205     Case("Test detach", test_detach),
00206     Case("Test multi call and time measure", test_multi_call_time),
00207     Case("Test multi ticker", test_multi_ticker),
00208 };
00209 
00210 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
00211 {
00212     GREENTEA_SETUP(test_timeout, "timing_drift_auto");
00213     return utest::v1::greentea_test_setup_handler(number_of_cases);
00214 }
00215 
00216 utest::v1::Specification specification(greentea_test_setup, cases, utest::v1::greentea_test_teardown_handler);
00217 
00218 int main()
00219 {
00220     utest::v1::Harness::run(specification);
00221 }