Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 00002 /* mbed Microcontroller Library 00003 * Copyright (c) 2018 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 #include "greentea-client/test_env.h" 00019 #include "unity/unity.h" 00020 #include "utest/utest.h" 00021 00022 #include "mbed.h" 00023 00024 #if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP) 00025 #error [NOT_SUPPORTED] test not supported 00026 #endif 00027 00028 using namespace utest::v1; 00029 00030 DigitalOut led1(LED1); 00031 00032 #define MAX_THREAD_STACK 384 00033 #define SAMPLE_TIME 1000 // msec 00034 #define LOOP_TIME 2000 // msec 00035 00036 static int32_t wait_time = 5000; 00037 00038 static void busy_thread() 00039 { 00040 volatile uint64_t i = ~0; 00041 00042 while (i--) { 00043 led1 = !led1; 00044 wait_us(wait_time); 00045 } 00046 } 00047 00048 void get_cpu_usage() 00049 { 00050 static uint64_t prev_idle_time = 0; 00051 mbed_stats_cpu_t stats; 00052 00053 while (1) { 00054 mbed_stats_cpu_get(&stats); 00055 uint64_t diff = (stats.idle_time - prev_idle_time); 00056 uint8_t usage = 100 - ((diff * 100) / (SAMPLE_TIME * 1000)); 00057 prev_idle_time = stats.idle_time; 00058 TEST_ASSERT_NOT_EQUAL(0, usage); 00059 Thread::wait(SAMPLE_TIME); 00060 } 00061 } 00062 00063 void test_cpu_info(void) 00064 { 00065 mbed_stats_cpu_t stats; 00066 // Additional read to make sure timer is initialized 00067 mbed_stats_cpu_get(&stats); 00068 Thread::wait(3); 00069 mbed_stats_cpu_get(&stats); 00070 TEST_ASSERT_NOT_EQUAL(0, stats.uptime); 00071 TEST_ASSERT_NOT_EQUAL(0, stats.idle_time); 00072 return; 00073 } 00074 00075 void test_cpu_load(void) 00076 { 00077 00078 Thread thread(osPriorityNormal, MAX_THREAD_STACK); 00079 Thread thread_stats(osPriorityNormal, MAX_THREAD_STACK); 00080 00081 thread.start(busy_thread); 00082 thread_stats.start(get_cpu_usage); 00083 00084 // Steadily increase the system load 00085 for (int count = 1; ; count++) { 00086 Thread::wait(LOOP_TIME); 00087 if (wait_time <= 0) { 00088 break; 00089 } 00090 wait_time -= 1000; // usec 00091 } 00092 thread.terminate(); 00093 thread_stats.terminate(); 00094 } 00095 00096 Case cases[] = { 00097 Case("Test CPU Info", test_cpu_info), 00098 Case("Test CPU load", test_cpu_load) 00099 }; 00100 00101 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) 00102 { 00103 GREENTEA_SETUP(20, "default_auto"); 00104 return greentea_test_setup_handler(number_of_cases); 00105 } 00106 00107 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00108 00109 int main() 00110 { 00111 Harness::run(specification); 00112 }
Generated on Tue Jul 12 2022 12:45:19 by
1.7.2