diff --git a/firmware/application/CMakeLists.txt b/firmware/application/CMakeLists.txt index a2288e00..fd41cc63 100644 --- a/firmware/application/CMakeLists.txt +++ b/firmware/application/CMakeLists.txt @@ -155,6 +155,7 @@ set(CPPSRC ${COMMON}/ui_widget.cpp ${COMMON}/utility.cpp ${COMMON}/wm8731.cpp + ${COMMON}/performance_counter.cpp app_settings.cpp audio.cpp baseband_api.cpp diff --git a/firmware/application/apps/ui_dfu_menu.cpp b/firmware/application/apps/ui_dfu_menu.cpp index 5d127b07..08fe1f3e 100644 --- a/firmware/application/apps/ui_dfu_menu.cpp +++ b/firmware/application/apps/ui_dfu_menu.cpp @@ -21,6 +21,7 @@ #include "ui_dfu_menu.hpp" #include "portapack_shared_memory.hpp" +#include "performance_counter.hpp" namespace ui { @@ -39,21 +40,11 @@ DfuMenu::DfuMenu(NavigationView& nav) : nav_ (nav) { } void DfuMenu::paint(Painter& painter) { - auto now = chTimeNow(); - auto idle_ticks = chThdGetTicks(chSysGetIdleThread()); - - static systime_t last_time; - static systime_t last_last_time; - - auto time_elapsed = now - last_time; - auto idle_elapsed = idle_ticks - last_last_time; - - last_time = now; - last_last_time = idle_ticks; + auto utilisation = get_cpu_utilisation_in_percent(); text_info_line_1.set(to_string_dec_uint(chCoreStatus(), 6)); text_info_line_2.set(to_string_dec_uint((uint32_t)get_free_stack_space(), 6)); - text_info_line_3.set(to_string_dec_uint((time_elapsed - idle_elapsed) / 10, 6)); + text_info_line_3.set(to_string_dec_uint(utilisation, 6)); text_info_line_4.set(to_string_dec_uint(shared_memory.m4_heap_usage, 6)); text_info_line_5.set(to_string_dec_uint(shared_memory.m4_stack_usage, 6)); text_info_line_6.set(to_string_dec_uint(shared_memory.m4_cpu_usage, 6)); diff --git a/firmware/baseband/CMakeLists.txt b/firmware/baseband/CMakeLists.txt index 0d6e6947..b19059d4 100644 --- a/firmware/baseband/CMakeLists.txt +++ b/firmware/baseband/CMakeLists.txt @@ -139,6 +139,7 @@ set(CPPSRC ${COMMON}/chibios_cpp.cpp debug.cpp ${COMMON}/gcc.cpp + ${COMMON}/performance_counter.cpp tone_gen.cpp ) diff --git a/firmware/baseband/debug.cpp b/firmware/baseband/debug.cpp index 450c9bce..032d0042 100644 --- a/firmware/baseband/debug.cpp +++ b/firmware/baseband/debug.cpp @@ -26,6 +26,7 @@ #include #include "portapack_shared_memory.hpp" +#include "performance_counter.hpp" void write_m4_panic_msg(const char *panic_message, struct extctx *ctxp) { if (ctxp == nullptr) { @@ -120,29 +121,25 @@ void update_performance_counters() { auto performance_counter_active = shared_memory.request_m4_performance_counter; if (performance_counter_active == 0x00) return; + static bool last_paint_state = false; if ((((chTimeNow()>>10) & 0x01) == 0x01) == last_paint_state) return; + volatile auto idle_thread = chSysGetIdleThread(); + if (chThdGetTicks(idle_thread) > 0x10000000) { + return; + } + + idle_thread = nullptr; + last_paint_state = !last_paint_state; - auto now = chTimeNow(); - auto idle_ticks = chThdGetTicks(chSysGetIdleThread()); - - static systime_t last_time; - static systime_t last_last_time; - - auto time_elapsed = now - last_time; - auto idle_elapsed = idle_ticks - last_last_time; - - last_time = now; - last_last_time = idle_ticks; - - auto cpu_usage = (time_elapsed - idle_elapsed) / 10; + auto utilisation = get_cpu_utilisation_in_percent(); auto free_stack = (uint32_t)get_free_stack_space(); auto free_heap = chCoreStatus(); - shared_memory.m4_cpu_usage = cpu_usage; + shared_memory.m4_cpu_usage = utilisation; shared_memory.m4_stack_usage = free_stack; shared_memory.m4_heap_usage = free_heap; } diff --git a/firmware/common/performance_counter.cpp b/firmware/common/performance_counter.cpp new file mode 100644 index 00000000..d5de2ebc --- /dev/null +++ b/firmware/common/performance_counter.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023 Bernd Herzog + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "performance_counter.hpp" +#include "ch.h" + +uint8_t get_cpu_utilisation_in_percent() { + static systime_t last_time = 0; + static systime_t last_idle_ticks = 0; + + auto now = chTimeNow(); + auto idle_ticks = chThdGetTicks(chSysGetIdleThread()); + + if (last_time == 0) { + last_time = now; + last_idle_ticks = idle_ticks; + + return 0; + } + + int32_t time_elapsed = now - last_time; + int32_t idle_elapsed = idle_ticks - last_idle_ticks; + + int32_t working_ticks = time_elapsed - idle_elapsed; + + if (working_ticks < 0) + working_ticks = 0; + + auto utilisation = working_ticks * 100 / time_elapsed; + + last_time = now; + last_idle_ticks = idle_ticks; + + if (utilisation > 100) { + return 100; + } + + return (uint8_t) utilisation; +} diff --git a/firmware/common/performance_counter.hpp b/firmware/common/performance_counter.hpp new file mode 100644 index 00000000..4b9b43cb --- /dev/null +++ b/firmware/common/performance_counter.hpp @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2023 Bernd Herzog + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __PERFORMANCE_COUNTER_H__ +#define __PERFORMANCE_COUNTER_H__ + +#include + +uint8_t get_cpu_utilisation_in_percent(); + +#endif /* __PERFORMANCE_COUNTER_H__ */ \ No newline at end of file