Preliminary main mbed library for nexpaq development
TESTS/mbed_drivers/lp_timeout/main.cpp@1:d96dbedaebdb, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:54:50 2016 +0000
- Revision:
- 1:d96dbedaebdb
- Parent:
- 0:6c56fb4bc5f0
Removed extra directories for other platforms
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | /* mbed Microcontroller Library |
nexpaq | 0:6c56fb4bc5f0 | 2 | * Copyright (c) 2016 ARM Limited |
nexpaq | 0:6c56fb4bc5f0 | 3 | * |
nexpaq | 0:6c56fb4bc5f0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 0:6c56fb4bc5f0 | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 0:6c56fb4bc5f0 | 6 | * You may obtain a copy of the License at |
nexpaq | 0:6c56fb4bc5f0 | 7 | * |
nexpaq | 0:6c56fb4bc5f0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 0:6c56fb4bc5f0 | 9 | * |
nexpaq | 0:6c56fb4bc5f0 | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 0:6c56fb4bc5f0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 0:6c56fb4bc5f0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 0:6c56fb4bc5f0 | 13 | * See the License for the specific language governing permissions and |
nexpaq | 0:6c56fb4bc5f0 | 14 | * limitations under the License. |
nexpaq | 0:6c56fb4bc5f0 | 15 | */ |
nexpaq | 0:6c56fb4bc5f0 | 16 | |
nexpaq | 0:6c56fb4bc5f0 | 17 | #if !DEVICE_LOWPOWERTIMER |
nexpaq | 0:6c56fb4bc5f0 | 18 | #error [NOT_SUPPORTED] Low power timer not supported for this target |
nexpaq | 0:6c56fb4bc5f0 | 19 | #endif |
nexpaq | 0:6c56fb4bc5f0 | 20 | |
nexpaq | 0:6c56fb4bc5f0 | 21 | #include "utest/utest.h" |
nexpaq | 0:6c56fb4bc5f0 | 22 | #include "unity/unity.h" |
nexpaq | 0:6c56fb4bc5f0 | 23 | #include "greentea-client/test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 24 | |
nexpaq | 0:6c56fb4bc5f0 | 25 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 26 | #include "us_ticker_api.h" |
nexpaq | 0:6c56fb4bc5f0 | 27 | |
nexpaq | 0:6c56fb4bc5f0 | 28 | using namespace utest::v1; |
nexpaq | 0:6c56fb4bc5f0 | 29 | |
nexpaq | 0:6c56fb4bc5f0 | 30 | volatile static bool complete; |
nexpaq | 0:6c56fb4bc5f0 | 31 | static LowPowerTimeout lpt; |
nexpaq | 0:6c56fb4bc5f0 | 32 | |
nexpaq | 0:6c56fb4bc5f0 | 33 | /* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */ |
nexpaq | 0:6c56fb4bc5f0 | 34 | #define LONG_TIMEOUT (100000) |
nexpaq | 0:6c56fb4bc5f0 | 35 | #define SHORT_TIMEOUT (600) |
nexpaq | 0:6c56fb4bc5f0 | 36 | |
nexpaq | 0:6c56fb4bc5f0 | 37 | void cb_done() { |
nexpaq | 0:6c56fb4bc5f0 | 38 | complete = true; |
nexpaq | 0:6c56fb4bc5f0 | 39 | } |
nexpaq | 0:6c56fb4bc5f0 | 40 | |
nexpaq | 0:6c56fb4bc5f0 | 41 | #if DEVICE_SLEEP |
nexpaq | 0:6c56fb4bc5f0 | 42 | void lp_timeout_1s_deepsleep(void) |
nexpaq | 0:6c56fb4bc5f0 | 43 | { |
nexpaq | 0:6c56fb4bc5f0 | 44 | complete = false; |
nexpaq | 0:6c56fb4bc5f0 | 45 | |
nexpaq | 0:6c56fb4bc5f0 | 46 | /* |
nexpaq | 0:6c56fb4bc5f0 | 47 | * We use here lp_ticker_read() instead of us_ticker_read() for start and |
nexpaq | 0:6c56fb4bc5f0 | 48 | * end because the microseconds timer might be disable during deepsleep. |
nexpaq | 0:6c56fb4bc5f0 | 49 | */ |
nexpaq | 0:6c56fb4bc5f0 | 50 | timestamp_t start = lp_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 51 | lpt.attach(&cb_done, 1); |
nexpaq | 0:6c56fb4bc5f0 | 52 | deepsleep(); |
nexpaq | 0:6c56fb4bc5f0 | 53 | while (!complete); |
nexpaq | 0:6c56fb4bc5f0 | 54 | timestamp_t end = lp_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 55 | |
nexpaq | 0:6c56fb4bc5f0 | 56 | /* It takes longer to wake up from deep sleep */ |
nexpaq | 0:6c56fb4bc5f0 | 57 | TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start); |
nexpaq | 0:6c56fb4bc5f0 | 58 | TEST_ASSERT_TRUE(complete); |
nexpaq | 0:6c56fb4bc5f0 | 59 | } |
nexpaq | 0:6c56fb4bc5f0 | 60 | |
nexpaq | 0:6c56fb4bc5f0 | 61 | void lp_timeout_1s_sleep(void) |
nexpaq | 0:6c56fb4bc5f0 | 62 | { |
nexpaq | 0:6c56fb4bc5f0 | 63 | complete = false; |
nexpaq | 0:6c56fb4bc5f0 | 64 | |
nexpaq | 0:6c56fb4bc5f0 | 65 | timestamp_t start = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 66 | lpt.attach(&cb_done, 1); |
nexpaq | 0:6c56fb4bc5f0 | 67 | sleep(); |
nexpaq | 0:6c56fb4bc5f0 | 68 | while (!complete); |
nexpaq | 0:6c56fb4bc5f0 | 69 | timestamp_t end = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 70 | |
nexpaq | 0:6c56fb4bc5f0 | 71 | TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start); |
nexpaq | 0:6c56fb4bc5f0 | 72 | TEST_ASSERT_TRUE(complete); |
nexpaq | 0:6c56fb4bc5f0 | 73 | } |
nexpaq | 0:6c56fb4bc5f0 | 74 | #endif /* DEVICE_SLEEP */ |
nexpaq | 0:6c56fb4bc5f0 | 75 | |
nexpaq | 0:6c56fb4bc5f0 | 76 | void lp_timeout_us(uint32_t delay_us, uint32_t tolerance) |
nexpaq | 0:6c56fb4bc5f0 | 77 | { |
nexpaq | 0:6c56fb4bc5f0 | 78 | complete = false; |
nexpaq | 0:6c56fb4bc5f0 | 79 | |
nexpaq | 0:6c56fb4bc5f0 | 80 | timestamp_t start = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 81 | lpt.attach_us(&cb_done, delay_us); |
nexpaq | 0:6c56fb4bc5f0 | 82 | while (!complete); |
nexpaq | 0:6c56fb4bc5f0 | 83 | timestamp_t end = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 84 | |
nexpaq | 0:6c56fb4bc5f0 | 85 | /* Using RTC which is less accurate */ |
nexpaq | 0:6c56fb4bc5f0 | 86 | TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start); |
nexpaq | 0:6c56fb4bc5f0 | 87 | TEST_ASSERT_TRUE(complete); |
nexpaq | 0:6c56fb4bc5f0 | 88 | } |
nexpaq | 0:6c56fb4bc5f0 | 89 | |
nexpaq | 0:6c56fb4bc5f0 | 90 | void lp_timeout_5s(void) |
nexpaq | 0:6c56fb4bc5f0 | 91 | { |
nexpaq | 0:6c56fb4bc5f0 | 92 | lp_timeout_us(5000000, LONG_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 93 | } |
nexpaq | 0:6c56fb4bc5f0 | 94 | |
nexpaq | 0:6c56fb4bc5f0 | 95 | void lp_timeout_1s(void) |
nexpaq | 0:6c56fb4bc5f0 | 96 | { |
nexpaq | 0:6c56fb4bc5f0 | 97 | lp_timeout_us(1000000, LONG_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 98 | } |
nexpaq | 0:6c56fb4bc5f0 | 99 | |
nexpaq | 0:6c56fb4bc5f0 | 100 | void lp_timeout_1ms(void) |
nexpaq | 0:6c56fb4bc5f0 | 101 | { |
nexpaq | 0:6c56fb4bc5f0 | 102 | lp_timeout_us(1000, SHORT_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 103 | } |
nexpaq | 0:6c56fb4bc5f0 | 104 | |
nexpaq | 0:6c56fb4bc5f0 | 105 | void lp_timeout_500us(void) |
nexpaq | 0:6c56fb4bc5f0 | 106 | { |
nexpaq | 0:6c56fb4bc5f0 | 107 | lp_timeout_us(500, SHORT_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 108 | |
nexpaq | 0:6c56fb4bc5f0 | 109 | } |
nexpaq | 0:6c56fb4bc5f0 | 110 | |
nexpaq | 0:6c56fb4bc5f0 | 111 | status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
nexpaq | 0:6c56fb4bc5f0 | 112 | greentea_case_failure_abort_handler(source, reason); |
nexpaq | 0:6c56fb4bc5f0 | 113 | return STATUS_CONTINUE; |
nexpaq | 0:6c56fb4bc5f0 | 114 | } |
nexpaq | 0:6c56fb4bc5f0 | 115 | |
nexpaq | 0:6c56fb4bc5f0 | 116 | Case cases[] = { |
nexpaq | 0:6c56fb4bc5f0 | 117 | Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 118 | Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 119 | Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 120 | Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 121 | #if DEVICE_SLEEP |
nexpaq | 0:6c56fb4bc5f0 | 122 | Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 123 | Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 124 | #endif /* DEVICE_SLEEP */ |
nexpaq | 0:6c56fb4bc5f0 | 125 | }; |
nexpaq | 0:6c56fb4bc5f0 | 126 | |
nexpaq | 0:6c56fb4bc5f0 | 127 | status_t greentea_test_setup(const size_t number_of_cases) { |
nexpaq | 0:6c56fb4bc5f0 | 128 | GREENTEA_SETUP(20, "default_auto"); |
nexpaq | 0:6c56fb4bc5f0 | 129 | return greentea_test_setup_handler(number_of_cases); |
nexpaq | 0:6c56fb4bc5f0 | 130 | } |
nexpaq | 0:6c56fb4bc5f0 | 131 | |
nexpaq | 0:6c56fb4bc5f0 | 132 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
nexpaq | 0:6c56fb4bc5f0 | 133 | |
nexpaq | 0:6c56fb4bc5f0 | 134 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 135 | Harness::run(specification); |
nexpaq | 0:6c56fb4bc5f0 | 136 | } |