Preliminary main mbed library for nexpaq development
TESTS/mbed_hal/lp_ticker/main.cpp@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
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 | #include "lp_ticker_api.h" |
nexpaq | 0:6c56fb4bc5f0 | 28 | |
nexpaq | 0:6c56fb4bc5f0 | 29 | using namespace utest::v1; |
nexpaq | 0:6c56fb4bc5f0 | 30 | |
nexpaq | 0:6c56fb4bc5f0 | 31 | volatile static bool complete; |
nexpaq | 0:6c56fb4bc5f0 | 32 | static ticker_event_t delay_event; |
nexpaq | 0:6c56fb4bc5f0 | 33 | static const ticker_data_t *lp_ticker_data = get_lp_ticker_data(); |
nexpaq | 0:6c56fb4bc5f0 | 34 | |
nexpaq | 0:6c56fb4bc5f0 | 35 | |
nexpaq | 0:6c56fb4bc5f0 | 36 | /* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */ |
nexpaq | 0:6c56fb4bc5f0 | 37 | #define LONG_TIMEOUT (100000) |
nexpaq | 0:6c56fb4bc5f0 | 38 | #define SHORT_TIMEOUT (600) |
nexpaq | 0:6c56fb4bc5f0 | 39 | |
nexpaq | 0:6c56fb4bc5f0 | 40 | void cb_done(uint32_t id) { |
nexpaq | 0:6c56fb4bc5f0 | 41 | complete = true; |
nexpaq | 0:6c56fb4bc5f0 | 42 | } |
nexpaq | 0:6c56fb4bc5f0 | 43 | |
nexpaq | 0:6c56fb4bc5f0 | 44 | void lp_ticker_delay_us(uint32_t delay_us, uint32_t tolerance) |
nexpaq | 0:6c56fb4bc5f0 | 45 | { |
nexpaq | 0:6c56fb4bc5f0 | 46 | complete = false; |
nexpaq | 0:6c56fb4bc5f0 | 47 | uint32_t delay_ts; |
nexpaq | 0:6c56fb4bc5f0 | 48 | |
nexpaq | 0:6c56fb4bc5f0 | 49 | ticker_set_handler(lp_ticker_data, cb_done); |
nexpaq | 0:6c56fb4bc5f0 | 50 | ticker_remove_event(lp_ticker_data, &delay_event); |
nexpaq | 0:6c56fb4bc5f0 | 51 | delay_ts = lp_ticker_read() + delay_us; |
nexpaq | 0:6c56fb4bc5f0 | 52 | |
nexpaq | 0:6c56fb4bc5f0 | 53 | timestamp_t start = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 54 | ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); |
nexpaq | 0:6c56fb4bc5f0 | 55 | while (!complete); |
nexpaq | 0:6c56fb4bc5f0 | 56 | timestamp_t end = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 57 | |
nexpaq | 0:6c56fb4bc5f0 | 58 | TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start); |
nexpaq | 0:6c56fb4bc5f0 | 59 | TEST_ASSERT_TRUE(complete); |
nexpaq | 0:6c56fb4bc5f0 | 60 | } |
nexpaq | 0:6c56fb4bc5f0 | 61 | |
nexpaq | 0:6c56fb4bc5f0 | 62 | #if DEVICE_SLEEP |
nexpaq | 0:6c56fb4bc5f0 | 63 | void lp_ticker_1s_deepsleep() |
nexpaq | 0:6c56fb4bc5f0 | 64 | { |
nexpaq | 0:6c56fb4bc5f0 | 65 | complete = false; |
nexpaq | 0:6c56fb4bc5f0 | 66 | uint32_t delay_ts; |
nexpaq | 0:6c56fb4bc5f0 | 67 | |
nexpaq | 0:6c56fb4bc5f0 | 68 | ticker_set_handler(lp_ticker_data, cb_done); |
nexpaq | 0:6c56fb4bc5f0 | 69 | ticker_remove_event(lp_ticker_data, &delay_event); |
nexpaq | 0:6c56fb4bc5f0 | 70 | delay_ts = lp_ticker_read() + 1000000; |
nexpaq | 0:6c56fb4bc5f0 | 71 | |
nexpaq | 0:6c56fb4bc5f0 | 72 | /* |
nexpaq | 0:6c56fb4bc5f0 | 73 | * We use here lp_ticker_read() instead of us_ticker_read() for start and |
nexpaq | 0:6c56fb4bc5f0 | 74 | * end because the microseconds timer might be disable during deepsleep. |
nexpaq | 0:6c56fb4bc5f0 | 75 | */ |
nexpaq | 0:6c56fb4bc5f0 | 76 | timestamp_t start = lp_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 77 | ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); |
nexpaq | 0:6c56fb4bc5f0 | 78 | deepsleep(); |
nexpaq | 0:6c56fb4bc5f0 | 79 | while (!complete); |
nexpaq | 0:6c56fb4bc5f0 | 80 | timestamp_t end = lp_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 81 | |
nexpaq | 0:6c56fb4bc5f0 | 82 | TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start); |
nexpaq | 0:6c56fb4bc5f0 | 83 | TEST_ASSERT_TRUE(complete); |
nexpaq | 0:6c56fb4bc5f0 | 84 | } |
nexpaq | 0:6c56fb4bc5f0 | 85 | |
nexpaq | 0:6c56fb4bc5f0 | 86 | void lp_ticker_1s_sleep() |
nexpaq | 0:6c56fb4bc5f0 | 87 | { |
nexpaq | 0:6c56fb4bc5f0 | 88 | complete = false; |
nexpaq | 0:6c56fb4bc5f0 | 89 | uint32_t delay_ts; |
nexpaq | 0:6c56fb4bc5f0 | 90 | |
nexpaq | 0:6c56fb4bc5f0 | 91 | ticker_set_handler(lp_ticker_data, cb_done); |
nexpaq | 0:6c56fb4bc5f0 | 92 | ticker_remove_event(lp_ticker_data, &delay_event); |
nexpaq | 0:6c56fb4bc5f0 | 93 | delay_ts = lp_ticker_read() + 1000000; |
nexpaq | 0:6c56fb4bc5f0 | 94 | |
nexpaq | 0:6c56fb4bc5f0 | 95 | timestamp_t start = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 96 | ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); |
nexpaq | 0:6c56fb4bc5f0 | 97 | sleep(); |
nexpaq | 0:6c56fb4bc5f0 | 98 | while (!complete); |
nexpaq | 0:6c56fb4bc5f0 | 99 | timestamp_t end = us_ticker_read(); |
nexpaq | 0:6c56fb4bc5f0 | 100 | |
nexpaq | 0:6c56fb4bc5f0 | 101 | TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start); |
nexpaq | 0:6c56fb4bc5f0 | 102 | TEST_ASSERT_TRUE(complete); |
nexpaq | 0:6c56fb4bc5f0 | 103 | } |
nexpaq | 0:6c56fb4bc5f0 | 104 | #endif /* DEVICE_SLEEP */ |
nexpaq | 0:6c56fb4bc5f0 | 105 | |
nexpaq | 0:6c56fb4bc5f0 | 106 | void lp_ticker_500us(void) |
nexpaq | 0:6c56fb4bc5f0 | 107 | { |
nexpaq | 0:6c56fb4bc5f0 | 108 | lp_ticker_delay_us(500, SHORT_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 109 | } |
nexpaq | 0:6c56fb4bc5f0 | 110 | |
nexpaq | 0:6c56fb4bc5f0 | 111 | void lp_ticker_1ms(void) |
nexpaq | 0:6c56fb4bc5f0 | 112 | { |
nexpaq | 0:6c56fb4bc5f0 | 113 | lp_ticker_delay_us(1000, SHORT_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 114 | } |
nexpaq | 0:6c56fb4bc5f0 | 115 | |
nexpaq | 0:6c56fb4bc5f0 | 116 | void lp_ticker_1s(void) |
nexpaq | 0:6c56fb4bc5f0 | 117 | { |
nexpaq | 0:6c56fb4bc5f0 | 118 | lp_ticker_delay_us(1000000, LONG_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 119 | } |
nexpaq | 0:6c56fb4bc5f0 | 120 | |
nexpaq | 0:6c56fb4bc5f0 | 121 | void lp_ticker_5s(void) |
nexpaq | 0:6c56fb4bc5f0 | 122 | { |
nexpaq | 0:6c56fb4bc5f0 | 123 | lp_ticker_delay_us(5000000, LONG_TIMEOUT); |
nexpaq | 0:6c56fb4bc5f0 | 124 | } |
nexpaq | 0:6c56fb4bc5f0 | 125 | |
nexpaq | 0:6c56fb4bc5f0 | 126 | status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
nexpaq | 0:6c56fb4bc5f0 | 127 | greentea_case_failure_abort_handler(source, reason); |
nexpaq | 0:6c56fb4bc5f0 | 128 | return STATUS_CONTINUE; |
nexpaq | 0:6c56fb4bc5f0 | 129 | } |
nexpaq | 0:6c56fb4bc5f0 | 130 | |
nexpaq | 0:6c56fb4bc5f0 | 131 | Case cases[] = { |
nexpaq | 0:6c56fb4bc5f0 | 132 | Case("500us lp_ticker", lp_ticker_500us, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 133 | Case("1ms lp_ticker", lp_ticker_1ms, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 134 | Case("1s lp_ticker", lp_ticker_1s, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 135 | Case("5s lp_ticker", lp_ticker_5s, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 136 | #if DEVICE_SLEEP |
nexpaq | 0:6c56fb4bc5f0 | 137 | Case("1s lp_ticker sleep", lp_ticker_1s_sleep, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 138 | Case("1s lp_ticker deepsleep", lp_ticker_1s_deepsleep, greentea_failure_handler), |
nexpaq | 0:6c56fb4bc5f0 | 139 | #endif /* DEVICE_SLEEP */ |
nexpaq | 0:6c56fb4bc5f0 | 140 | }; |
nexpaq | 0:6c56fb4bc5f0 | 141 | |
nexpaq | 0:6c56fb4bc5f0 | 142 | status_t greentea_test_setup(const size_t number_of_cases) { |
nexpaq | 0:6c56fb4bc5f0 | 143 | GREENTEA_SETUP(20, "default_auto"); |
nexpaq | 0:6c56fb4bc5f0 | 144 | lp_ticker_data->interface->init(); |
nexpaq | 0:6c56fb4bc5f0 | 145 | return greentea_test_setup_handler(number_of_cases); |
nexpaq | 0:6c56fb4bc5f0 | 146 | } |
nexpaq | 0:6c56fb4bc5f0 | 147 | |
nexpaq | 0:6c56fb4bc5f0 | 148 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
nexpaq | 0:6c56fb4bc5f0 | 149 | |
nexpaq | 0:6c56fb4bc5f0 | 150 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 151 | Harness::run(specification); |
nexpaq | 0:6c56fb4bc5f0 | 152 | } |