Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 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 #include "utest/utest.h"
00018 #include "unity/unity.h"
00019 #include "greentea-client/test_env.h"
00020 
00021 #if !DEVICE_SLEEP
00022 #error [NOT_SUPPORTED] test not supported
00023 #endif
00024 
00025 using namespace utest::v1;
00026 
00027 #define TEST_STACK_SIZE 256
00028 
00029 void sleep_manager_locking_thread_test()
00030 {
00031     for (uint32_t i = 0; i < 100; i++) {
00032         sleep_manager_lock_deep_sleep();
00033         Thread::wait(25);
00034         sleep_manager_unlock_deep_sleep();
00035     }
00036 }
00037 
00038 void sleep_manager_multithread_test()
00039 {
00040     {
00041         Callback<void()> cb(sleep_manager_locking_thread_test);
00042         Thread t1(osPriorityNormal, TEST_STACK_SIZE);
00043         Thread t2(osPriorityNormal, TEST_STACK_SIZE);
00044 
00045         t1.start(callback(cb));
00046         Thread::wait(25);
00047         t2.start(callback(cb));
00048 
00049         // Wait for the threads to finish
00050         t1.join();
00051         t2.join();
00052     }
00053 
00054     bool deep_sleep_allowed = sleep_manager_can_deep_sleep();
00055     TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
00056 }
00057 
00058 void sleep_manager_locking_irq_test()
00059 {
00060     sleep_manager_lock_deep_sleep();
00061     sleep_manager_unlock_deep_sleep();
00062 }
00063 
00064 void sleep_manager_irq_test()
00065 {
00066     {
00067         Ticker ticker1;
00068         Timer timer;
00069 
00070         ticker1.attach_us(&sleep_manager_locking_irq_test, 500);
00071 
00072         // run this for 5 seconds
00073         timer.start();
00074         int start = timer.read();
00075         int end = start + 5;
00076         while (timer.read() < end) {
00077             sleep_manager_locking_irq_test();
00078         }
00079         timer.stop();
00080     }
00081 
00082     bool deep_sleep_allowed = sleep_manager_can_deep_sleep();
00083     TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
00084 }
00085 
00086 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) 
00087 {
00088     GREENTEA_SETUP(30, "default_auto");
00089     return greentea_test_setup_handler(number_of_cases);
00090 }
00091 
00092 Case cases[] = {
00093     Case("sleep manager HAL - locking multithreaded", sleep_manager_multithread_test),
00094     Case("sleep manager HAL - locking IRQ", sleep_manager_irq_test),
00095 };
00096 
00097 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00098 
00099 int main() {
00100     Harness::run(specification);
00101 }