Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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