mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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
elessair 0:f269e3021894 28 using namespace utest::v1;
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 volatile static bool complete;
elessair 0:f269e3021894 31 static LowPowerTimeout lpt;
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 /* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
elessair 0:f269e3021894 34 #define LONG_TIMEOUT (100000)
elessair 0:f269e3021894 35 #define SHORT_TIMEOUT (600)
elessair 0:f269e3021894 36
elessair 0:f269e3021894 37 void cb_done() {
elessair 0:f269e3021894 38 complete = true;
elessair 0:f269e3021894 39 }
elessair 0:f269e3021894 40
elessair 0:f269e3021894 41 #if DEVICE_SLEEP
elessair 0:f269e3021894 42 void lp_timeout_1s_deepsleep(void)
elessair 0:f269e3021894 43 {
elessair 0:f269e3021894 44 complete = false;
elessair 0:f269e3021894 45
elessair 0:f269e3021894 46 /*
elessair 0:f269e3021894 47 * Since deepsleep() may shut down the UART peripheral, we wait for 10ms
elessair 0:f269e3021894 48 * to allow for hardware serial buffers to completely flush.
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 * This should be replaced with a better function that checks if the
elessair 0:f269e3021894 51 * hardware buffers are empty. However, such an API does not exist now,
elessair 0:f269e3021894 52 * so we'll use the wait_ms() function for now.
elessair 0:f269e3021894 53 */
elessair 0:f269e3021894 54 wait_ms(10);
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 /*
elessair 0:f269e3021894 57 * We use here lp_ticker_read() instead of us_ticker_read() for start and
elessair 0:f269e3021894 58 * end because the microseconds timer might be disable during deepsleep.
elessair 0:f269e3021894 59 */
elessair 0:f269e3021894 60 timestamp_t start = lp_ticker_read();
elessair 0:f269e3021894 61 lpt.attach(&cb_done, 1);
elessair 0:f269e3021894 62 deepsleep();
elessair 0:f269e3021894 63 while (!complete);
elessair 0:f269e3021894 64 timestamp_t end = lp_ticker_read();
elessair 0:f269e3021894 65
elessair 0:f269e3021894 66 /* It takes longer to wake up from deep sleep */
elessair 0:f269e3021894 67 TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
elessair 0:f269e3021894 68 TEST_ASSERT_TRUE(complete);
elessair 0:f269e3021894 69 }
elessair 0:f269e3021894 70
elessair 0:f269e3021894 71 void lp_timeout_1s_sleep(void)
elessair 0:f269e3021894 72 {
elessair 0:f269e3021894 73 complete = false;
elessair 0:f269e3021894 74
elessair 0:f269e3021894 75 timestamp_t start = us_ticker_read();
elessair 0:f269e3021894 76 lpt.attach(&cb_done, 1);
elessair 0:f269e3021894 77 sleep();
elessair 0:f269e3021894 78 while (!complete);
elessair 0:f269e3021894 79 timestamp_t end = us_ticker_read();
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
elessair 0:f269e3021894 82 TEST_ASSERT_TRUE(complete);
elessair 0:f269e3021894 83 }
elessair 0:f269e3021894 84 #endif /* DEVICE_SLEEP */
elessair 0:f269e3021894 85
elessair 0:f269e3021894 86 void lp_timeout_us(uint32_t delay_us, uint32_t tolerance)
elessair 0:f269e3021894 87 {
elessair 0:f269e3021894 88 complete = false;
elessair 0:f269e3021894 89
elessair 0:f269e3021894 90 timestamp_t start = us_ticker_read();
elessair 0:f269e3021894 91 lpt.attach_us(&cb_done, delay_us);
elessair 0:f269e3021894 92 while (!complete);
elessair 0:f269e3021894 93 timestamp_t end = us_ticker_read();
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 /* Using RTC which is less accurate */
elessair 0:f269e3021894 96 TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
elessair 0:f269e3021894 97 TEST_ASSERT_TRUE(complete);
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 void lp_timeout_5s(void)
elessair 0:f269e3021894 101 {
elessair 0:f269e3021894 102 lp_timeout_us(5000000, LONG_TIMEOUT);
elessair 0:f269e3021894 103 }
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 void lp_timeout_1s(void)
elessair 0:f269e3021894 106 {
elessair 0:f269e3021894 107 lp_timeout_us(1000000, LONG_TIMEOUT);
elessair 0:f269e3021894 108 }
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 void lp_timeout_1ms(void)
elessair 0:f269e3021894 111 {
elessair 0:f269e3021894 112 lp_timeout_us(1000, SHORT_TIMEOUT);
elessair 0:f269e3021894 113 }
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 void lp_timeout_500us(void)
elessair 0:f269e3021894 116 {
elessair 0:f269e3021894 117 lp_timeout_us(500, SHORT_TIMEOUT);
elessair 0:f269e3021894 118
elessair 0:f269e3021894 119 }
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
elessair 0:f269e3021894 122 greentea_case_failure_abort_handler(source, reason);
elessair 0:f269e3021894 123 return STATUS_CONTINUE;
elessair 0:f269e3021894 124 }
elessair 0:f269e3021894 125
elessair 0:f269e3021894 126 Case cases[] = {
elessair 0:f269e3021894 127 Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler),
elessair 0:f269e3021894 128 Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler),
elessair 0:f269e3021894 129 Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler),
elessair 0:f269e3021894 130 Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler),
elessair 0:f269e3021894 131 #if DEVICE_SLEEP
elessair 0:f269e3021894 132 Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler),
elessair 0:f269e3021894 133 Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler),
elessair 0:f269e3021894 134 #endif /* DEVICE_SLEEP */
elessair 0:f269e3021894 135 };
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 status_t greentea_test_setup(const size_t number_of_cases) {
elessair 0:f269e3021894 138 GREENTEA_SETUP(20, "default_auto");
elessair 0:f269e3021894 139 return greentea_test_setup_handler(number_of_cases);
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
elessair 0:f269e3021894 143
elessair 0:f269e3021894 144 int main() {
elessair 0:f269e3021894 145 Harness::run(specification);
elessair 0:f269e3021894 146 }