Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2013-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 #include "mbed.h"
00018 #include "greentea-client/test_env.h"
00019 #include "utest/utest.h"
00020 #include "unity/unity.h"
00021 
00022 #if defined(MBED_RTOS_SINGLE_THREAD)
00023 #error [NOT_SUPPORTED] test not supported
00024 #endif
00025 
00026 #if !DEVICE_USTICKER
00027   #error [NOT_SUPPORTED] test not supported
00028 #endif
00029 
00030 using utest::v1::Case;
00031 
00032 #define TEST_STACK_SIZE 256
00033 #define ONE_MILLI_SEC 1000
00034 
00035 volatile uint32_t elapsed_time_ms = 0;
00036 static const int test_timeout = 40;
00037 
00038 
00039 void update_tick_thread(Mutex *mutex)
00040 {
00041     while (true) {
00042         Thread::wait(1);
00043         mutex->lock();
00044         ++elapsed_time_ms;
00045         mutex->unlock();
00046     }
00047 }
00048 
00049 
00050 /** Tests is to measure the accuracy of Thread::wait() over a period of time
00051 
00052     Given
00053         a thread updating elapsed_time_ms every milli sec
00054         and host script for time measurement accuracy check (More details on tests can be found in timing_drift_auto.py)
00055     When host query what is current count base_time
00056     Then Device responds by the elapsed_time_ms
00057     When host query what is current count final_time
00058     Then Device responds by the elapsed_time_ms
00059     When host computes the drift considering base_time, final_time, transport delay and measurement stretch
00060     Then host send the results back to device pass/fail based on tolerance
00061  */
00062 void test(void)
00063 {
00064     char _key[11] = { };
00065     char _value[128] = { };
00066     int expected_key = 1;
00067     Mutex mutex;
00068     uint32_t elapsed_time;
00069 
00070     Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
00071     tick_thread.start(callback(update_tick_thread, &mutex));
00072 
00073     greentea_send_kv("timing_drift_check_start", 0);
00074 
00075     // wait for 1st signal from host
00076     do {
00077         greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
00078         expected_key = strcmp(_key, "base_time");
00079     } while (expected_key);
00080 
00081     mutex.lock();
00082     elapsed_time = elapsed_time_ms;
00083     mutex.unlock();
00084     // send base_time
00085     greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
00086 
00087     // wait for 2nd signal from host
00088     greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
00089 
00090     mutex.lock();
00091     elapsed_time = elapsed_time_ms;
00092     mutex.unlock();
00093     // send final_time
00094     greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
00095 
00096     //get the results from host
00097     greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
00098 
00099     TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key,"Host side script reported a fail...");
00100 }
00101 
00102 Case cases[] = {
00103     Case("Test Thread::wait accuracy", test)
00104 };
00105 
00106 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
00107 {
00108     GREENTEA_SETUP(test_timeout, "timing_drift_auto");
00109     return utest::v1::greentea_test_setup_handler(number_of_cases);
00110 }
00111 
00112 utest::v1::Specification specification(greentea_test_setup, cases);
00113 
00114 int main()
00115 {
00116     utest::v1::Harness::run(specification);
00117 }