|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "utest/utest.h" |
| 18 | +#include "unity/unity.h" |
| 19 | +#include "greentea-client/test_env.h" |
| 20 | + |
| 21 | +#if !DEVICE_SLEEP |
| 22 | +#error [NOT_SUPPORTED] test not supported |
| 23 | +#endif |
| 24 | + |
| 25 | +using namespace utest::v1; |
| 26 | + |
| 27 | +void sleep_manager_locking_thread_test() |
| 28 | +{ |
| 29 | + for (uint32_t i = 0; i < 100; i++) { |
| 30 | + sleep_manager_lock_deep_sleep(); |
| 31 | + Thread::wait(25); |
| 32 | + sleep_manager_unlock_deep_sleep(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void sleep_manager_multithread_test() |
| 37 | +{ |
| 38 | + { |
| 39 | + Callback<void()> cb(sleep_manager_locking_thread_test); |
| 40 | + Thread t1; |
| 41 | + Thread t2; |
| 42 | + |
| 43 | + t1.start(callback(cb)); |
| 44 | + Thread::wait(25); |
| 45 | + t2.start(callback(cb)); |
| 46 | + |
| 47 | + // Wait for the threads to finish |
| 48 | + t1.join(); |
| 49 | + t2.join(); |
| 50 | + } |
| 51 | + |
| 52 | + bool deep_sleep_allowed = sleep_manager_can_deep_sleep(); |
| 53 | + TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed"); |
| 54 | +} |
| 55 | + |
| 56 | +void sleep_manager_locking_irq_test() |
| 57 | +{ |
| 58 | + sleep_manager_lock_deep_sleep(); |
| 59 | + sleep_manager_unlock_deep_sleep(); |
| 60 | +} |
| 61 | + |
| 62 | +void sleep_manager_irq_test() |
| 63 | +{ |
| 64 | + { |
| 65 | + Ticker ticker1; |
| 66 | + Timer timer; |
| 67 | + |
| 68 | + ticker1.attach_us(&sleep_manager_locking_irq_test, 500); |
| 69 | + |
| 70 | + // run this for 5 seconds |
| 71 | + timer.start(); |
| 72 | + int start = timer.read(); |
| 73 | + int end = start + 5; |
| 74 | + while (timer.read() < end) { |
| 75 | + sleep_manager_locking_irq_test(); |
| 76 | + } |
| 77 | + timer.stop(); |
| 78 | + } |
| 79 | + |
| 80 | + bool deep_sleep_allowed = sleep_manager_can_deep_sleep(); |
| 81 | + TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed"); |
| 82 | +} |
| 83 | + |
| 84 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 85 | +{ |
| 86 | + GREENTEA_SETUP(30, "default_auto"); |
| 87 | + return greentea_test_setup_handler(number_of_cases); |
| 88 | +} |
| 89 | + |
| 90 | +Case cases[] = { |
| 91 | + Case("sleep manager HAL - locking multithreaded", sleep_manager_multithread_test), |
| 92 | + Case("sleep manager HAL - locking IRQ", sleep_manager_irq_test), |
| 93 | +}; |
| 94 | + |
| 95 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 96 | + |
| 97 | +int main() { |
| 98 | + Harness::run(specification); |
| 99 | +} |
0 commit comments