|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "utest/utest.h" |
| 17 | +#include "unity/unity.h" |
| 18 | +#include "greentea-client/test_env.h" |
| 19 | + |
| 20 | +#include "mbed.h" |
| 21 | +#include "us_ticker_api.h" |
| 22 | +#include "ticker_api.h" |
| 23 | + |
| 24 | +using namespace utest::v1; |
| 25 | + |
| 26 | +namespace { |
| 27 | + volatile bool complete; |
| 28 | + const ticker_interface_t* intf; |
| 29 | +} |
| 30 | + |
| 31 | +/* Ticker init should be run just once, thus read should always return a value that |
| 32 | + * increases (no reset). |
| 33 | + */ |
| 34 | +void test_ticker_init(void) |
| 35 | +{ |
| 36 | + intf->init(); |
| 37 | + uint32_t previous = intf->read(); |
| 38 | + |
| 39 | + intf->init(); |
| 40 | + uint32_t current = intf->read(); |
| 41 | + TEST_ASSERT_TRUE_MESSAGE(previous <= current, "init() changed the counter"); |
| 42 | + |
| 43 | + previous = intf->read(); |
| 44 | + intf->init(); |
| 45 | + current = intf->read(); |
| 46 | + TEST_ASSERT_TRUE_MESSAGE(previous <= current, "init() changed the counter"); |
| 47 | +} |
| 48 | + |
| 49 | +/* Read multiple times, each returned time should be bigger than the previous one |
| 50 | + * Counter up. |
| 51 | + */ |
| 52 | +void test_ticker_read(void) |
| 53 | +{ |
| 54 | + // this test assumes that to wrap around we would need to run >60 minutes |
| 55 | + // therefore wrapping should not happen - previous <= current |
| 56 | + const uint32_t test_loop = 15000; |
| 57 | + uint32_t previous = intf->read(); |
| 58 | + uint32_t current; |
| 59 | + for (uint32_t i = 0UL; i < test_loop; i++) { |
| 60 | + current = intf->read(); |
| 61 | + TEST_ASSERT_TRUE_MESSAGE(previous <= current, "us ticker counter wrapped around"); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/* Implement simple read while loop to check if time is increasing (counter up) |
| 66 | + */ |
| 67 | +void test_ticker_read_loop() |
| 68 | +{ |
| 69 | + uint32_t future_time = intf->read() + 10000UL; |
| 70 | + while (intf->read() < future_time); |
| 71 | + TEST_ASSERT_TRUE_MESSAGE(future_time <= intf->read(), "Future time is in the past"); |
| 72 | +} |
| 73 | + |
| 74 | +utest::v1::status_t us_ticker_setup(const Case *const source, const size_t index_of_case) |
| 75 | +{ |
| 76 | + intf = get_us_ticker_data()->interface; |
| 77 | + return greentea_case_setup_handler(source, index_of_case); |
| 78 | +} |
| 79 | + |
| 80 | +#if DEVICE_LOWPOWERTIMER |
| 81 | +utest::v1::status_t lp_ticker_setup(const Case *const source, const size_t index_of_case) |
| 82 | +{ |
| 83 | + intf = get_lp_ticker_data()->interface; |
| 84 | + return greentea_case_setup_handler(source, index_of_case); |
| 85 | +} |
| 86 | +#endif |
| 87 | + |
| 88 | +Case cases[] = { |
| 89 | + Case("us ticker HAL - Init", us_ticker_setup, test_ticker_init), |
| 90 | + Case("us ticker HAL - Read", us_ticker_setup, test_ticker_read), |
| 91 | + Case("us ticker HAL - Read in the loop", us_ticker_setup, test_ticker_read_loop), |
| 92 | +#if DEVICE_LOWPOWERTIMER |
| 93 | + Case("lp ticker HAL - Init", lp_ticker_setup, test_ticker_init), |
| 94 | + Case("lp ticker HAL - Read", lp_ticker_setup, test_ticker_read), |
| 95 | + Case("lp ticker HAL - Read in the loop", lp_ticker_setup, test_ticker_read_loop), |
| 96 | +#endif |
| 97 | +}; |
| 98 | + |
| 99 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 100 | +{ |
| 101 | + GREENTEA_SETUP(20, "default_auto"); |
| 102 | + return greentea_test_setup_handler(number_of_cases); |
| 103 | +} |
| 104 | + |
| 105 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 106 | + |
| 107 | +int main() |
| 108 | +{ |
| 109 | + Harness::run(specification); |
| 110 | +} |
0 commit comments