ON Semiconductor / mbed-os

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #if !DEVICE_LOWPOWERTIMER
00018     #error [NOT_SUPPORTED] Low power timer not supported for this target
00019 #endif
00020 
00021 #include "utest/utest.h"
00022 #include "unity/unity.h"
00023 #include "greentea-client/test_env.h"
00024 
00025 #include "mbed.h"
00026 #include "us_ticker_api.h"
00027 
00028 using namespace utest::v1;
00029 
00030 volatile static bool complete;
00031 static LowPowerTimeout lpt;
00032 
00033 /* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
00034 #define LONG_TIMEOUT (100000)
00035 #define SHORT_TIMEOUT (600)
00036 
00037 void cb_done() {
00038     complete = true;
00039 }
00040 
00041 #if DEVICE_SLEEP
00042 void lp_timeout_1s_deepsleep(void)
00043 {
00044     complete = false;
00045 
00046     /*
00047      * Since deepsleep() may shut down the UART peripheral, we wait for 10ms
00048      * to allow for hardware serial buffers to completely flush.
00049 
00050      * This should be replaced with a better function that checks if the
00051      * hardware buffers are empty. However, such an API does not exist now,
00052      * so we'll use the wait_ms() function for now.
00053      */
00054     wait_ms(10);
00055 
00056     /*
00057      * We use here lp_ticker_read() instead of us_ticker_read() for start and
00058      * end because the microseconds timer might be disable during deepsleep.
00059      */
00060     timestamp_t start = lp_ticker_read();
00061     lpt.attach(&cb_done, 1);
00062     deepsleep();
00063     while (!complete);
00064     timestamp_t end = lp_ticker_read();
00065 
00066     /* It takes longer to wake up from deep sleep */
00067     TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
00068     TEST_ASSERT_TRUE(complete);
00069 }
00070 
00071 void lp_timeout_1s_sleep(void)
00072 {
00073     complete = false;
00074 
00075     timestamp_t start = us_ticker_read();
00076     lpt.attach(&cb_done, 1);
00077     sleep();
00078     while (!complete);
00079     timestamp_t end = us_ticker_read();
00080 
00081     TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
00082     TEST_ASSERT_TRUE(complete);
00083 }
00084 #endif /* DEVICE_SLEEP */
00085 
00086 void lp_timeout_us(uint32_t delay_us, uint32_t tolerance)
00087 {
00088     complete = false;
00089 
00090     timestamp_t start = us_ticker_read();
00091     lpt.attach_us(&cb_done, delay_us);
00092     while (!complete);
00093     timestamp_t end = us_ticker_read();
00094 
00095     /* Using RTC which is less accurate */
00096     TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
00097     TEST_ASSERT_TRUE(complete);
00098 }
00099 
00100 void lp_timeout_5s(void)
00101 {
00102     lp_timeout_us(5000000, LONG_TIMEOUT);
00103 }
00104 
00105 void lp_timeout_1s(void)
00106 {
00107     lp_timeout_us(1000000, LONG_TIMEOUT);
00108 }
00109 
00110 void lp_timeout_1ms(void)
00111 {
00112     lp_timeout_us(1000, SHORT_TIMEOUT);
00113 }
00114 
00115 void lp_timeout_500us(void)
00116 {
00117     lp_timeout_us(500, SHORT_TIMEOUT);
00118 
00119 }
00120 
00121 utest::v1::status_t  greentea_failure_handler(const Case *const source, const failure_t reason) {
00122     greentea_case_failure_abort_handler(source, reason);
00123     return STATUS_CONTINUE;
00124 }
00125 
00126 Case cases[] = {
00127     Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler),
00128     Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler),
00129     Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler),
00130     Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler),
00131 #if DEVICE_SLEEP
00132     Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler),
00133     Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler),
00134 #endif /* DEVICE_SLEEP */
00135 };
00136 
00137 utest::v1::status_t  greentea_test_setup(const size_t number_of_cases) {
00138     GREENTEA_SETUP(20, "default_auto");
00139     return greentea_test_setup_handler(number_of_cases);
00140 }
00141 
00142 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00143 
00144 int main() {
00145     Harness::run(specification);
00146 }