Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:4beb2ea291ec 1 /*
boro 0:4beb2ea291ec 2 * Copyright (c) 2013-2017, ARM Limited, All Rights Reserved
boro 0:4beb2ea291ec 3 * SPDX-License-Identifier: Apache-2.0
boro 0:4beb2ea291ec 4 *
boro 0:4beb2ea291ec 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
boro 0:4beb2ea291ec 6 * not use this file except in compliance with the License.
boro 0:4beb2ea291ec 7 * You may obtain a copy of the License at
boro 0:4beb2ea291ec 8 *
boro 0:4beb2ea291ec 9 * http://www.apache.org/licenses/LICENSE-2.0
boro 0:4beb2ea291ec 10 *
boro 0:4beb2ea291ec 11 * Unless required by applicable law or agreed to in writing, software
boro 0:4beb2ea291ec 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
boro 0:4beb2ea291ec 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
boro 0:4beb2ea291ec 14 * See the License for the specific language governing permissions and
boro 0:4beb2ea291ec 15 * limitations under the License.
boro 0:4beb2ea291ec 16 */
boro 0:4beb2ea291ec 17 #include "mbed.h"
boro 0:4beb2ea291ec 18 #include "greentea-client/test_env.h"
boro 0:4beb2ea291ec 19 #include "utest/utest.h"
boro 0:4beb2ea291ec 20 #include "unity/unity.h"
boro 0:4beb2ea291ec 21
boro 0:4beb2ea291ec 22 #if defined(MBED_RTOS_SINGLE_THREAD)
boro 0:4beb2ea291ec 23 #error [NOT_SUPPORTED] test not supported
boro 0:4beb2ea291ec 24 #endif
boro 0:4beb2ea291ec 25
boro 0:4beb2ea291ec 26 using utest::v1::Case;
boro 0:4beb2ea291ec 27
boro 0:4beb2ea291ec 28 #define TEST_STACK_SIZE 256
boro 0:4beb2ea291ec 29 #define ONE_MILLI_SEC 1000
boro 0:4beb2ea291ec 30
boro 0:4beb2ea291ec 31 volatile uint32_t elapsed_time_ms = 0;
boro 0:4beb2ea291ec 32 static const int test_timeout = 40;
boro 0:4beb2ea291ec 33
boro 0:4beb2ea291ec 34
boro 0:4beb2ea291ec 35 void update_tick_thread(Mutex *mutex)
boro 0:4beb2ea291ec 36 {
boro 0:4beb2ea291ec 37 while (true) {
boro 0:4beb2ea291ec 38 Thread::wait(1);
boro 0:4beb2ea291ec 39 mutex->lock();
boro 0:4beb2ea291ec 40 ++elapsed_time_ms;
boro 0:4beb2ea291ec 41 mutex->unlock();
boro 0:4beb2ea291ec 42 }
boro 0:4beb2ea291ec 43 }
boro 0:4beb2ea291ec 44
boro 0:4beb2ea291ec 45
boro 0:4beb2ea291ec 46 /** Tests is to measure the accuracy of Thread::wait() over a period of time
boro 0:4beb2ea291ec 47
boro 0:4beb2ea291ec 48 Given
boro 0:4beb2ea291ec 49 a thread updating elapsed_time_ms every milli sec
boro 0:4beb2ea291ec 50 and host script for time measurement accuracy check (More details on tests can be found in timing_drift_auto.py)
boro 0:4beb2ea291ec 51 When host query what is current count base_time
boro 0:4beb2ea291ec 52 Then Device responds by the elapsed_time_ms
boro 0:4beb2ea291ec 53 When host query what is current count final_time
boro 0:4beb2ea291ec 54 Then Device responds by the elapsed_time_ms
boro 0:4beb2ea291ec 55 When host computes the drift considering base_time, final_time, transport delay and measurement stretch
boro 0:4beb2ea291ec 56 Then host send the results back to device pass/fail based on tolerance
boro 0:4beb2ea291ec 57 */
boro 0:4beb2ea291ec 58 void test(void)
boro 0:4beb2ea291ec 59 {
boro 0:4beb2ea291ec 60 char _key[11] = { };
boro 0:4beb2ea291ec 61 char _value[128] = { };
boro 0:4beb2ea291ec 62 int expected_key = 1;
boro 0:4beb2ea291ec 63 Mutex mutex;
boro 0:4beb2ea291ec 64 uint32_t elapsed_time;
boro 0:4beb2ea291ec 65
boro 0:4beb2ea291ec 66 Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
boro 0:4beb2ea291ec 67 tick_thread.start(callback(update_tick_thread, &mutex));
boro 0:4beb2ea291ec 68
boro 0:4beb2ea291ec 69 greentea_send_kv("timing_drift_check_start", 0);
boro 0:4beb2ea291ec 70
boro 0:4beb2ea291ec 71 // wait for 1st signal from host
boro 0:4beb2ea291ec 72 do {
boro 0:4beb2ea291ec 73 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
boro 0:4beb2ea291ec 74 expected_key = strcmp(_key, "base_time");
boro 0:4beb2ea291ec 75 } while (expected_key);
boro 0:4beb2ea291ec 76
boro 0:4beb2ea291ec 77 mutex.lock();
boro 0:4beb2ea291ec 78 elapsed_time = elapsed_time_ms;
boro 0:4beb2ea291ec 79 mutex.unlock();
boro 0:4beb2ea291ec 80 // send base_time
boro 0:4beb2ea291ec 81 greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
boro 0:4beb2ea291ec 82
boro 0:4beb2ea291ec 83 // wait for 2nd signal from host
boro 0:4beb2ea291ec 84 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
boro 0:4beb2ea291ec 85
boro 0:4beb2ea291ec 86 mutex.lock();
boro 0:4beb2ea291ec 87 elapsed_time = elapsed_time_ms;
boro 0:4beb2ea291ec 88 mutex.unlock();
boro 0:4beb2ea291ec 89 // send final_time
boro 0:4beb2ea291ec 90 greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
boro 0:4beb2ea291ec 91
boro 0:4beb2ea291ec 92 //get the results from host
boro 0:4beb2ea291ec 93 greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
boro 0:4beb2ea291ec 94
boro 0:4beb2ea291ec 95 TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key,"Host side script reported a fail...");
boro 0:4beb2ea291ec 96 }
boro 0:4beb2ea291ec 97
boro 0:4beb2ea291ec 98 Case cases[] = {
boro 0:4beb2ea291ec 99 Case("Test Thread::wait accuracy", test)
boro 0:4beb2ea291ec 100 };
boro 0:4beb2ea291ec 101
boro 0:4beb2ea291ec 102 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
boro 0:4beb2ea291ec 103 {
boro 0:4beb2ea291ec 104 GREENTEA_SETUP(test_timeout, "timing_drift_auto");
boro 0:4beb2ea291ec 105 return utest::v1::greentea_test_setup_handler(number_of_cases);
boro 0:4beb2ea291ec 106 }
boro 0:4beb2ea291ec 107
boro 0:4beb2ea291ec 108 utest::v1::Specification specification(greentea_test_setup, cases);
boro 0:4beb2ea291ec 109
boro 0:4beb2ea291ec 110 int main()
boro 0:4beb2ea291ec 111 {
boro 0:4beb2ea291ec 112 utest::v1::Harness::run(specification);
boro 0:4beb2ea291ec 113 }