WORKS

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

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      * We use here lp_ticker_read() instead of us_ticker_read() for start and 
00048      * end because the microseconds timer might be disable during deepsleep.
00049      */
00050     timestamp_t start = lp_ticker_read();
00051     lpt.attach(&cb_done, 1);
00052     deepsleep();
00053     while (!complete);
00054     timestamp_t end = lp_ticker_read();
00055 
00056     /* It takes longer to wake up from deep sleep */
00057     TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
00058     TEST_ASSERT_TRUE(complete);
00059 }
00060 
00061 void lp_timeout_1s_sleep(void)
00062 {
00063     complete = false;
00064 
00065     timestamp_t start = us_ticker_read();
00066     lpt.attach(&cb_done, 1);
00067     sleep();
00068     while (!complete);
00069     timestamp_t end = us_ticker_read();
00070 
00071     TEST_ASSERT_UINT32_WITHIN(LONG_TIMEOUT, 1000000, end - start);
00072     TEST_ASSERT_TRUE(complete);
00073 }
00074 #endif /* DEVICE_SLEEP */
00075 
00076 void lp_timeout_us(uint32_t delay_us, uint32_t tolerance)
00077 {
00078     complete = false;
00079 
00080     timestamp_t start = us_ticker_read();
00081     lpt.attach_us(&cb_done, delay_us);
00082     while (!complete);
00083     timestamp_t end = us_ticker_read();
00084 
00085     /* Using RTC which is less accurate */
00086     TEST_ASSERT_UINT32_WITHIN(tolerance, delay_us, end - start);
00087     TEST_ASSERT_TRUE(complete);
00088 }
00089 
00090 void lp_timeout_5s(void)
00091 {
00092     lp_timeout_us(5000000, LONG_TIMEOUT);
00093 }
00094 
00095 void lp_timeout_1s(void)
00096 {
00097     lp_timeout_us(1000000, LONG_TIMEOUT);
00098 }
00099 
00100 void lp_timeout_1ms(void)
00101 {
00102     lp_timeout_us(1000, SHORT_TIMEOUT);
00103 }
00104 
00105 void lp_timeout_500us(void)
00106 {
00107     lp_timeout_us(500, SHORT_TIMEOUT);
00108 
00109 }
00110 
00111 status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
00112     greentea_case_failure_abort_handler(source, reason);
00113     return STATUS_CONTINUE;
00114 }
00115 
00116 Case cases[] = {
00117     Case("500us LowPowerTimeout", lp_timeout_500us, greentea_failure_handler),
00118     Case("1ms LowPowerTimeout", lp_timeout_1ms, greentea_failure_handler),
00119     Case("1sec LowPowerTimeout", lp_timeout_1s, greentea_failure_handler),
00120     Case("5sec LowPowerTimeout", lp_timeout_5s, greentea_failure_handler),
00121 #if DEVICE_SLEEP
00122     Case("1sec LowPowerTimeout from sleep", lp_timeout_1s_sleep, greentea_failure_handler),
00123     Case("1sec LowPowerTimeout from deepsleep", lp_timeout_1s_deepsleep, greentea_failure_handler),
00124 #endif /* DEVICE_SLEEP */
00125 };
00126 
00127 status_t greentea_test_setup(const size_t number_of_cases) {
00128     GREENTEA_SETUP(20, "default_auto");
00129     return greentea_test_setup_handler(number_of_cases);
00130 }
00131 
00132 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00133 
00134 int main() {
00135     Harness::run(specification);
00136 }