Rtos API example

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