Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* mbed Microcontroller Library
00003  * Copyright (c) 2013-2016 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 // define this to get rid of the minar dependency.
00019 #define YOTTA_CFG_UTEST_USE_CUSTOM_SCHEDULER 1
00020 
00021 #include "mbed.h"
00022 #include "greentea-client/test_env.h"
00023 #include "utest/utest.h"
00024 #include "unity/unity.h"
00025 #include "utest/utest_stack_trace.h"
00026 
00027 #include "ticker_api.h"
00028 #include "us_ticker_api.h"
00029 
00030 using namespace utest::v1;
00031 
00032 // only one callback is active at any given time
00033 volatile utest_v1_harness_callback_t minimal_callback;
00034 volatile utest_v1_harness_callback_t ticker_callback;
00035 static Timeout utest_minimal_object;
00036 
00037 // Scheduler ----------------------------------------------------------------------------------------------------------
00038 static void ticker_handler()
00039 {
00040     UTEST_LOG_FUNCTION();
00041     minimal_callback = ticker_callback;
00042 }
00043 
00044 static int32_t utest_minimal_init()
00045 {
00046     UTEST_LOG_FUNCTION();
00047     minimal_callback = NULL;
00048     ticker_callback = NULL;
00049     return 0;
00050 }
00051 static void *utest_minimal_post(const utest_v1_harness_callback_t callback,  timestamp_t delay_ms)
00052 {
00053     UTEST_LOG_FUNCTION();
00054     timestamp_t delay_us = delay_ms *1000;
00055 
00056     if (delay_ms) {
00057         ticker_callback = callback;
00058         // fire the interrupt in 1000us * delay_ms
00059         utest_minimal_object.attach_us(ticker_handler, delay_us);
00060     } else {
00061         minimal_callback = callback;
00062     }
00063 
00064     // return a bogus handle
00065     return (void*)1;
00066 }
00067 static int32_t utest_minimal_cancel(void *handle)
00068 {
00069     UTEST_LOG_FUNCTION();
00070     (void) handle;
00071     utest_minimal_object.detach();
00072     return 0;
00073 }
00074 static int32_t utest_minimal_run()
00075 {
00076     UTEST_LOG_FUNCTION();
00077     /* This is the amazing minimal scheduler.
00078      * This is just a busy loop that calls the callbacks in this context.
00079      * THIS LOOP IS BLOCKING.
00080      */
00081     while(1)
00082     {
00083         // check if a new callback has been set
00084         if (minimal_callback)
00085         {
00086             // copy the callback
00087             utest_v1_harness_callback_t callback = minimal_callback;
00088             // reset the shared callback
00089             minimal_callback = NULL;
00090             // execute the copied callback
00091             callback();
00092         }
00093     }
00094     return 0;
00095 }
00096 static const utest_v1_scheduler_t utest_minimal_scheduler =
00097 {
00098     utest_minimal_init,
00099     utest_minimal_post,
00100     utest_minimal_cancel,
00101     utest_minimal_run
00102 };
00103 
00104 // Tests --------------------------------------------------------------------------------------------------------------
00105 int call_counter(0);
00106 
00107 // Basic Test Case ----------------------------------------------------------------------------------------------------
00108 control_t test_case()
00109 {
00110     UTEST_LOG_FUNCTION();
00111     static int counter(0);
00112     TEST_ASSERT_EQUAL(counter++, call_counter++);
00113     return CaseNext;
00114 }
00115 
00116 // Async Test Case Failure --------------------------------------------------------------------------------------------
00117 control_t test_case_async()
00118 {
00119     UTEST_LOG_FUNCTION();
00120     static int counter(3);
00121     TEST_ASSERT_EQUAL(counter++, call_counter++);
00122     return CaseTimeout(200);
00123 }
00124 utest::v1::status_t test_case_async_failure(const Case *const source, const failure_t reason)
00125 {
00126     UTEST_LOG_FUNCTION();
00127     // ignore the timeout, since this is a test
00128     return greentea_case_failure_continue_handler(source, reason.ignored ());
00129 }
00130 
00131 // Cases --------------------------------------------------------------------------------------------------------------
00132 Case cases[] = {
00133     Case("Minimal Scheduler: Case 1", test_case),
00134     Case("Minimal Scheduler: Case 2", test_case),
00135     Case("Minimal Scheduler: Case 3", test_case),
00136     Case("Minimal Scheduler: Async Case 4 (Failure)", test_case_async, test_case_async_failure)
00137 };
00138 
00139 // Specification: Setup & Teardown ------------------------------------------------------------------------------------
00140 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00141 {
00142     GREENTEA_SETUP(15, "default_auto");
00143     return greentea_test_setup_handler(number_of_cases);
00144 }
00145 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
00146 {
00147     UTEST_LOG_FUNCTION();
00148     TEST_ASSERT_EQUAL(4, call_counter++);
00149     TEST_ASSERT_EQUAL(4, passed);
00150     TEST_ASSERT_EQUAL(0, failed);
00151     TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
00152     TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
00153     UTEST_DUMP_TRACE
00154     greentea_test_teardown_handler(passed, failed, failure);
00155 }
00156 
00157 Specification specification(greentea_setup, cases, greentea_teardown, selftest_handlers);
00158 
00159 int main()
00160 {
00161     // You MUST set the custom scheduler before running the specification.
00162     Harness::set_scheduler(utest_minimal_scheduler);
00163     // Run the specification only AFTER setting the custom scheduler.
00164     Harness::run(specification);
00165 }