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 
00026 using namespace utest::v1;
00027 
00028 // only one callback is active at any given time
00029 volatile utest_v1_harness_callback_t minimal_callback;
00030 
00031 // Scheduler ----------------------------------------------------------------------------------------------------------
00032 static int32_t utest_minimal_init()
00033 {
00034     minimal_callback = NULL;
00035     return 0;
00036 }
00037 static void *utest_minimal_post(const utest_v1_harness_callback_t callback, const uint32_t delay_ms)
00038 {
00039     minimal_callback = callback;
00040     // this scheduler does not support scheduling of asynchronous callbacks
00041     return (delay_ms ? NULL : (void*)1);
00042 }
00043 static int32_t utest_minimal_cancel(void *handle)
00044 {
00045     (void) handle;
00046     // this scheduler does not support canceling of asynchronous callbacks
00047     return -1;
00048 }
00049 static int32_t utest_minimal_run()
00050 {
00051     /* This is the amazing minimal scheduler.
00052      * This is just a busy loop that calls the callbacks in this context.
00053      * THIS LOOP IS BLOCKING.
00054      */
00055     while(1)
00056     {
00057         // check if a new callback has been set
00058         if (minimal_callback) {
00059             // copy the callback
00060             utest_v1_harness_callback_t callback = minimal_callback;
00061             // reset the shared callback
00062             minimal_callback = NULL;
00063             // execute the copied callback
00064             callback();
00065         }
00066     }
00067     return 0;
00068 }
00069 static const utest_v1_scheduler_t utest_minimal_scheduler =
00070 {
00071     utest_minimal_init,
00072     utest_minimal_post,
00073     utest_minimal_cancel,
00074     utest_minimal_run
00075 };
00076 
00077 // Tests --------------------------------------------------------------------------------------------------------------
00078 int call_counter(0);
00079 
00080 // Basic Test Case ----------------------------------------------------------------------------------------------------
00081 control_t test_case()
00082 {
00083     static int counter(0);
00084     TEST_ASSERT_EQUAL(counter++, call_counter++);
00085     return CaseNext;
00086 }
00087 
00088 // Cases --------------------------------------------------------------------------------------------------------------
00089 Case cases[] = {
00090     Case("Minimal Scheduler: Case 1", test_case),
00091     Case("Minimal Scheduler: Case 2", test_case),
00092     Case("Minimal Scheduler: Case 3", test_case),
00093     Case("Minimal Scheduler: Case 4", test_case),
00094     Case("Minimal Scheduler: Case 5", test_case),
00095     Case("Minimal Scheduler: Case 6", test_case),
00096     Case("Minimal Scheduler: Case 7", test_case),
00097     Case("Minimal Scheduler: Case 8", test_case)
00098 };
00099 
00100 // Specification: Setup & Teardown ------------------------------------------------------------------------------------
00101 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00102 {
00103     GREENTEA_SETUP(15, "default_auto");
00104     return greentea_test_setup_handler(number_of_cases);
00105 }
00106 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
00107 {
00108     TEST_ASSERT_EQUAL(8, call_counter++);
00109     TEST_ASSERT_EQUAL(8, passed);
00110     TEST_ASSERT_EQUAL(0, failed);
00111     TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
00112     TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
00113     greentea_test_teardown_handler(passed, failed, failure);
00114 }
00115 
00116 Specification specification(greentea_setup, cases, greentea_teardown, selftest_handlers);
00117 
00118 int main()
00119 {
00120     // You MUST set the custom scheduler before running the specification.
00121     Harness::set_scheduler(utest_minimal_scheduler);
00122     // Run the specification only AFTER setting the custom scheduler.
00123     Harness::run(specification);
00124 }