Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 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 "utest/utest.h"
00017 #include "unity/unity.h"
00018 #include "greentea-client/test_env.h"
00019 
00020 #include "mbed.h"
00021 #include "us_ticker_api.h"
00022 #include "ticker_api.h"
00023 
00024 using namespace utest::v1;
00025 
00026 namespace {
00027     volatile bool complete;
00028     const ticker_interface_t* intf;
00029 }
00030 
00031 /* Ticker init should be run just once, thus read should always return a value that 
00032  * increases (no reset).
00033  */
00034 void test_ticker_init(void)
00035 {
00036     intf->init();
00037     uint32_t previous = intf->read();
00038 
00039     intf->init();
00040     uint32_t current = intf->read();
00041     TEST_ASSERT_TRUE_MESSAGE(previous <= current, "init() changed the counter");
00042 
00043     previous = intf->read();
00044     intf->init();
00045     current = intf->read();
00046     TEST_ASSERT_TRUE_MESSAGE(previous <= current, "init() changed the counter");
00047 }
00048 
00049 /* Read multiple times, each returned time should be bigger than the previous one
00050  * Counter up.
00051  */
00052 void test_ticker_read(void)
00053 {
00054     // this test assumes that to wrap around we would need to run >60 minutes
00055     // therefore wrapping should not happen - previous <= current
00056     const uint32_t test_loop = 15000;
00057     uint32_t previous = intf->read();
00058     uint32_t current;
00059     for (uint32_t i = 0UL; i < test_loop; i++) {
00060         current = intf->read();
00061         TEST_ASSERT_TRUE_MESSAGE(previous <= current, "us ticker counter wrapped around");
00062     }
00063 }
00064 
00065 /* Implement simple read while loop to check if time is increasing (counter up)
00066  */
00067 void test_ticker_read_loop()
00068 {
00069     uint32_t future_time = intf->read() + 10000UL;
00070     while (intf->read() < future_time);
00071     TEST_ASSERT_TRUE_MESSAGE(future_time <= intf->read(), "Future time is in the past");
00072 }
00073 
00074 utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index_of_case)
00075 {
00076     intf = get_us_ticker_data()->interface;
00077     return greentea_case_setup_handler(source, index_of_case);
00078 }
00079 
00080 #if DEVICE_LOWPOWERTIMER
00081 utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index_of_case)
00082 {
00083     intf = get_lp_ticker_data()->interface;
00084     return greentea_case_setup_handler(source, index_of_case);
00085 }
00086 #endif
00087 
00088 Case cases[] = {
00089     Case("us ticker HAL - Init", us_ticker_setup, test_ticker_init),
00090     Case("us ticker HAL - Read", us_ticker_setup, test_ticker_read),
00091     Case("us ticker HAL - Read in the loop", us_ticker_setup, test_ticker_read_loop),
00092 #if DEVICE_LOWPOWERTIMER
00093     Case("lp ticker HAL - Init", lp_ticker_setup, test_ticker_init),
00094     Case("lp ticker HAL - Read", lp_ticker_setup, test_ticker_read),
00095     Case("lp ticker HAL - Read in the loop", lp_ticker_setup, test_ticker_read_loop),
00096 #endif
00097 };
00098 
00099 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) 
00100 {
00101     GREENTEA_SETUP(20, "default_auto");
00102     return greentea_test_setup_handler(number_of_cases);
00103 }
00104 
00105 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00106 
00107 int main() 
00108 {
00109     Harness::run(specification);
00110 }