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 /*
elessair 0:f269e3021894 2 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 3 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 4 *
elessair 0:f269e3021894 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 6 * not use this file except in compliance with the License.
elessair 0:f269e3021894 7 * You may obtain a copy of the License at
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 14 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 15 * limitations under the License.
elessair 0:f269e3021894 16 */
elessair 0:f269e3021894 17 #include <stdio.h>
elessair 0:f269e3021894 18 #include <string.h>
elessair 0:f269e3021894 19 #include <utility> // std::pair
elessair 0:f269e3021894 20 #include "mbed.h"
elessair 0:f269e3021894 21 #include "greentea-client/test_env.h"
elessair 0:f269e3021894 22 #include "unity/unity.h"
elessair 0:f269e3021894 23 #include "utest/utest.h"
elessair 0:f269e3021894 24
elessair 0:f269e3021894 25 using namespace utest::v1;
elessair 0:f269e3021894 26
elessair 0:f269e3021894 27 #define PATTERN_CHECK_VALUE 0xF0F0ADAD
elessair 0:f269e3021894 28
elessair 0:f269e3021894 29 class CppTestCaseHelperClass {
elessair 0:f269e3021894 30 private:
elessair 0:f269e3021894 31 const char* name;
elessair 0:f269e3021894 32 const unsigned pattern;
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 public:
elessair 0:f269e3021894 35 CppTestCaseHelperClass(const char* _name) : name(_name), pattern(PATTERN_CHECK_VALUE) {
elessair 0:f269e3021894 36 print("init");
elessair 0:f269e3021894 37 }
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 void print(const char *message) {
elessair 0:f269e3021894 40 printf("%s::%s\n", name, message);
elessair 0:f269e3021894 41 }
elessair 0:f269e3021894 42
elessair 0:f269e3021894 43 bool check_init(void) {
elessair 0:f269e3021894 44 bool result = (pattern == PATTERN_CHECK_VALUE);
elessair 0:f269e3021894 45 print(result ? "check_init: OK" : "check_init: ERROR");
elessair 0:f269e3021894 46 return result;
elessair 0:f269e3021894 47 }
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 void stack_test(void) {
elessair 0:f269e3021894 50 print("stack_test");
elessair 0:f269e3021894 51 CppTestCaseHelperClass t("Stack");
elessair 0:f269e3021894 52 t.hello();
elessair 0:f269e3021894 53 }
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 void hello(void) {
elessair 0:f269e3021894 56 print("hello");
elessair 0:f269e3021894 57 }
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 ~CppTestCaseHelperClass() {
elessair 0:f269e3021894 60 print("destroy");
elessair 0:f269e3021894 61 }
elessair 0:f269e3021894 62 };
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64
elessair 0:f269e3021894 65 void test_case_basic() {
elessair 0:f269e3021894 66 TEST_ASSERT_TRUE(true);
elessair 0:f269e3021894 67 TEST_ASSERT_FALSE(false);
elessair 0:f269e3021894 68 TEST_ASSERT_EQUAL_STRING("The quick brown fox jumps over the lazy dog",
elessair 0:f269e3021894 69 "The quick brown fox jumps over the lazy dog");
elessair 0:f269e3021894 70 }
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 void test_case_blinky() {
elessair 0:f269e3021894 73 static DigitalOut myled(LED1);
elessair 0:f269e3021894 74 const int cnt_max = 1024;
elessair 0:f269e3021894 75 for (int cnt = 0; cnt < cnt_max; ++cnt) {
elessair 0:f269e3021894 76 myled = !myled;
elessair 0:f269e3021894 77 }
elessair 0:f269e3021894 78 }
elessair 0:f269e3021894 79
elessair 0:f269e3021894 80 void test_case_cpp_stack() {
elessair 0:f269e3021894 81 // Check C++ start-up initialisation
elessair 0:f269e3021894 82 CppTestCaseHelperClass s("Static");
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 // Global stack object simple test
elessair 0:f269e3021894 85 s.stack_test();
elessair 0:f269e3021894 86 TEST_ASSERT_TRUE_MESSAGE(s.check_init(), "s.check_init() failed");
elessair 0:f269e3021894 87 }
elessair 0:f269e3021894 88
elessair 0:f269e3021894 89 void test_case_cpp_heap() {
elessair 0:f269e3021894 90 // Heap test object simple test
elessair 0:f269e3021894 91 CppTestCaseHelperClass *m = new CppTestCaseHelperClass("Heap");
elessair 0:f269e3021894 92 m->hello();
elessair 0:f269e3021894 93 TEST_ASSERT_TRUE_MESSAGE(m->check_init(), "m->check_init() failed");
elessair 0:f269e3021894 94 delete m;
elessair 0:f269e3021894 95 }
elessair 0:f269e3021894 96
elessair 0:f269e3021894 97 utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
elessair 0:f269e3021894 98 greentea_case_failure_abort_handler(source, reason);
elessair 0:f269e3021894 99 return STATUS_CONTINUE;
elessair 0:f269e3021894 100 }
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 // Generic test cases
elessair 0:f269e3021894 103 Case cases[] = {
elessair 0:f269e3021894 104 Case("Basic", test_case_basic, greentea_failure_handler),
elessair 0:f269e3021894 105 Case("Blinky", test_case_blinky, greentea_failure_handler),
elessair 0:f269e3021894 106 Case("C++ stack", test_case_cpp_stack, greentea_failure_handler),
elessair 0:f269e3021894 107 Case("C++ heap", test_case_cpp_heap, greentea_failure_handler)
elessair 0:f269e3021894 108 };
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
elessair 0:f269e3021894 111 GREENTEA_SETUP(20, "default_auto");
elessair 0:f269e3021894 112 return greentea_test_setup_handler(number_of_cases);
elessair 0:f269e3021894 113 }
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 int main() {
elessair 0:f269e3021894 118 Harness::run(specification);
elessair 0:f269e3021894 119 }