mbed os with nrf51 internal bandgap enabled to read battery level
Dependents: BLE_file_test BLE_Blink ExternalEncoder
TESTS/mbed_hal/lp_ticker/main.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2016 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | #if !DEVICE_LOWPOWERTIMER |
elessair | 0:f269e3021894 | 18 | #error [NOT_SUPPORTED] Low power timer not supported for this target |
elessair | 0:f269e3021894 | 19 | #endif |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #include "utest/utest.h" |
elessair | 0:f269e3021894 | 22 | #include "unity/unity.h" |
elessair | 0:f269e3021894 | 23 | #include "greentea-client/test_env.h" |
elessair | 0:f269e3021894 | 24 | |
elessair | 0:f269e3021894 | 25 | #include "mbed.h" |
elessair | 0:f269e3021894 | 26 | #include "us_ticker_api.h" |
elessair | 0:f269e3021894 | 27 | #include "lp_ticker_api.h" |
elessair | 0:f269e3021894 | 28 | |
elessair | 0:f269e3021894 | 29 | using namespace utest::v1; |
elessair | 0:f269e3021894 | 30 | |
elessair | 0:f269e3021894 | 31 | volatile static bool complete; |
elessair | 0:f269e3021894 | 32 | static ticker_event_t delay_event; |
elessair | 0:f269e3021894 | 33 | static const ticker_data_t *lp_ticker_data = get_lp_ticker_data(); |
elessair | 0:f269e3021894 | 34 | |
elessair | 0:f269e3021894 | 35 | |
elessair | 0:f269e3021894 | 36 | /* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */ |
elessair | 0:f269e3021894 | 37 | #define LONG_TIMEOUT (100000) |
elessair | 0:f269e3021894 | 38 | #define SHORT_TIMEOUT (600) |
elessair | 0:f269e3021894 | 39 | |
elessair | 0:f269e3021894 | 40 | void cb_done(uint32_t id) { |
elessair | 0:f269e3021894 | 41 | complete = true; |
elessair | 0:f269e3021894 | 42 | } |
elessair | 0:f269e3021894 | 43 | |
elessair | 0:f269e3021894 | 44 | void lp_ticker_delay_us(uint32_t delay_us, uint32_t tolerance) |
elessair | 0:f269e3021894 | 45 | { |
elessair | 0:f269e3021894 | 46 | complete = false; |
elessair | 0:f269e3021894 | 47 | uint32_t delay_ts; |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | ticker_set_handler(lp_ticker_data, cb_done); |
elessair | 0:f269e3021894 | 50 | ticker_remove_event(lp_ticker_data, &delay_event); |
elessair | 0:f269e3021894 | 51 | delay_ts = lp_ticker_read() + delay_us; |
elessair | 0:f269e3021894 | 52 | |
elessair | 0:f269e3021894 | 53 | timestamp_t start = us_ticker_read(); |
elessair | 0:f269e3021894 | 54 | ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); |
elessair | 0:f269e3021894 | 55 | while (!complete); |
elessair | 0:f269e3021894 | 56 | timestamp_t end = us_ticker_read(); |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start); |
elessair | 0:f269e3021894 | 59 | TEST_ASSERT_TRUE(complete); |
elessair | 0:f269e3021894 | 60 | } |
elessair | 0:f269e3021894 | 61 | |
elessair | 0:f269e3021894 | 62 | #if DEVICE_SLEEP |
elessair | 0:f269e3021894 | 63 | void lp_ticker_1s_deepsleep() |
elessair | 0:f269e3021894 | 64 | { |
elessair | 0:f269e3021894 | 65 | complete = false; |
elessair | 0:f269e3021894 | 66 | uint32_t delay_ts; |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | /* |
elessair | 0:f269e3021894 | 69 | * Since deepsleep() may shut down the UART peripheral, we wait for 10ms |
elessair | 0:f269e3021894 | 70 | * to allow for hardware serial buffers to completely flush. |
elessair | 0:f269e3021894 | 71 | |
elessair | 0:f269e3021894 | 72 | * This should be replaced with a better function that checks if the |
elessair | 0:f269e3021894 | 73 | * hardware buffers are empty. However, such an API does not exist now, |
elessair | 0:f269e3021894 | 74 | * so we'll use the wait_ms() function for now. |
elessair | 0:f269e3021894 | 75 | */ |
elessair | 0:f269e3021894 | 76 | wait_ms(10); |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | ticker_set_handler(lp_ticker_data, cb_done); |
elessair | 0:f269e3021894 | 79 | ticker_remove_event(lp_ticker_data, &delay_event); |
elessair | 0:f269e3021894 | 80 | delay_ts = lp_ticker_read() + 1000000; |
elessair | 0:f269e3021894 | 81 | |
elessair | 0:f269e3021894 | 82 | /* |
elessair | 0:f269e3021894 | 83 | * We use here lp_ticker_read() instead of us_ticker_read() for start and |
elessair | 0:f269e3021894 | 84 | * end because the microseconds timer might be disable during deepsleep. |
elessair | 0:f269e3021894 | 85 | */ |
elessair | 0:f269e3021894 | 86 | timestamp_t start = lp_ticker_read(); |
elessair | 0:f269e3021894 | 87 | ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); |
elessair | 0:f269e3021894 | 88 | deepsleep(); |
elessair | 0:f269e3021894 | 89 | while (!complete); |
elessair | 0:f269e3021894 | 90 | timestamp_t end = lp_ticker_read(); |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start); |
elessair | 0:f269e3021894 | 93 | TEST_ASSERT_TRUE(complete); |
elessair | 0:f269e3021894 | 94 | } |
elessair | 0:f269e3021894 | 95 | |
elessair | 0:f269e3021894 | 96 | void lp_ticker_1s_sleep() |
elessair | 0:f269e3021894 | 97 | { |
elessair | 0:f269e3021894 | 98 | complete = false; |
elessair | 0:f269e3021894 | 99 | uint32_t delay_ts; |
elessair | 0:f269e3021894 | 100 | |
elessair | 0:f269e3021894 | 101 | ticker_set_handler(lp_ticker_data, cb_done); |
elessair | 0:f269e3021894 | 102 | ticker_remove_event(lp_ticker_data, &delay_event); |
elessair | 0:f269e3021894 | 103 | delay_ts = lp_ticker_read() + 1000000; |
elessair | 0:f269e3021894 | 104 | |
elessair | 0:f269e3021894 | 105 | timestamp_t start = us_ticker_read(); |
elessair | 0:f269e3021894 | 106 | ticker_insert_event(lp_ticker_data, &delay_event, delay_ts, (uint32_t)&delay_event); |
elessair | 0:f269e3021894 | 107 | sleep(); |
elessair | 0:f269e3021894 | 108 | while (!complete); |
elessair | 0:f269e3021894 | 109 | timestamp_t end = us_ticker_read(); |
elessair | 0:f269e3021894 | 110 | |
elessair | 0:f269e3021894 | 111 | TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start); |
elessair | 0:f269e3021894 | 112 | TEST_ASSERT_TRUE(complete); |
elessair | 0:f269e3021894 | 113 | } |
elessair | 0:f269e3021894 | 114 | #endif /* DEVICE_SLEEP */ |
elessair | 0:f269e3021894 | 115 | |
elessair | 0:f269e3021894 | 116 | void lp_ticker_500us(void) |
elessair | 0:f269e3021894 | 117 | { |
elessair | 0:f269e3021894 | 118 | lp_ticker_delay_us(500, SHORT_TIMEOUT); |
elessair | 0:f269e3021894 | 119 | } |
elessair | 0:f269e3021894 | 120 | |
elessair | 0:f269e3021894 | 121 | void lp_ticker_1ms(void) |
elessair | 0:f269e3021894 | 122 | { |
elessair | 0:f269e3021894 | 123 | lp_ticker_delay_us(1000, SHORT_TIMEOUT); |
elessair | 0:f269e3021894 | 124 | } |
elessair | 0:f269e3021894 | 125 | |
elessair | 0:f269e3021894 | 126 | void lp_ticker_1s(void) |
elessair | 0:f269e3021894 | 127 | { |
elessair | 0:f269e3021894 | 128 | lp_ticker_delay_us(1000000, LONG_TIMEOUT); |
elessair | 0:f269e3021894 | 129 | } |
elessair | 0:f269e3021894 | 130 | |
elessair | 0:f269e3021894 | 131 | void lp_ticker_5s(void) |
elessair | 0:f269e3021894 | 132 | { |
elessair | 0:f269e3021894 | 133 | lp_ticker_delay_us(5000000, LONG_TIMEOUT); |
elessair | 0:f269e3021894 | 134 | } |
elessair | 0:f269e3021894 | 135 | |
elessair | 0:f269e3021894 | 136 | status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
elessair | 0:f269e3021894 | 137 | greentea_case_failure_abort_handler(source, reason); |
elessair | 0:f269e3021894 | 138 | return STATUS_CONTINUE; |
elessair | 0:f269e3021894 | 139 | } |
elessair | 0:f269e3021894 | 140 | |
elessair | 0:f269e3021894 | 141 | Case cases[] = { |
elessair | 0:f269e3021894 | 142 | Case("500us lp_ticker", lp_ticker_500us, greentea_failure_handler), |
elessair | 0:f269e3021894 | 143 | Case("1ms lp_ticker", lp_ticker_1ms, greentea_failure_handler), |
elessair | 0:f269e3021894 | 144 | Case("1s lp_ticker", lp_ticker_1s, greentea_failure_handler), |
elessair | 0:f269e3021894 | 145 | Case("5s lp_ticker", lp_ticker_5s, greentea_failure_handler), |
elessair | 0:f269e3021894 | 146 | #if DEVICE_SLEEP |
elessair | 0:f269e3021894 | 147 | Case("1s lp_ticker sleep", lp_ticker_1s_sleep, greentea_failure_handler), |
elessair | 0:f269e3021894 | 148 | Case("1s lp_ticker deepsleep", lp_ticker_1s_deepsleep, greentea_failure_handler), |
elessair | 0:f269e3021894 | 149 | #endif /* DEVICE_SLEEP */ |
elessair | 0:f269e3021894 | 150 | }; |
elessair | 0:f269e3021894 | 151 | |
elessair | 0:f269e3021894 | 152 | status_t greentea_test_setup(const size_t number_of_cases) { |
elessair | 0:f269e3021894 | 153 | GREENTEA_SETUP(20, "default_auto"); |
elessair | 0:f269e3021894 | 154 | lp_ticker_data->interface->init(); |
elessair | 0:f269e3021894 | 155 | return greentea_test_setup_handler(number_of_cases); |
elessair | 0:f269e3021894 | 156 | } |
elessair | 0:f269e3021894 | 157 | |
elessair | 0:f269e3021894 | 158 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
elessair | 0:f269e3021894 | 159 | |
elessair | 0:f269e3021894 | 160 | int main() { |
elessair | 0:f269e3021894 | 161 | Harness::run(specification); |
elessair | 0:f269e3021894 | 162 | } |