From 29b7a5ee563e04d54e10da393851b70ce88407be Mon Sep 17 00:00:00 2001 From: Bernd Herzog Date: Sun, 23 Apr 2023 23:48:20 +0200 Subject: [PATCH] improved m4 m0 communication --- firmware/application/ui_navigation.cpp | 3 ++ firmware/baseband/chconf.h | 4 +++ firmware/baseband/debug.cpp | 30 +++++++++++++++++ firmware/baseband/event_m4.cpp | 33 +------------------ firmware/baseband/event_m4.hpp | 1 - .../baseband/sd_over_usb/proc_sd_over_usb.cpp | 2 ++ firmware/common/portapack_shared_memory.hpp | 8 ++--- 7 files changed, 44 insertions(+), 37 deletions(-) diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 70e22e40..1870b126 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -759,6 +759,9 @@ void SystemView::toggle_overlay() { this->add_child(&this->overlay); this->set_dirty(); shared_memory.request_m4_performance_counter = 1; + shared_memory.m4_cpu_usage = 0; + shared_memory.m4_heap_usage = 0; + shared_memory.m4_stack_usage = 0; } overlay_active = !overlay_active; diff --git a/firmware/baseband/chconf.h b/firmware/baseband/chconf.h index 0ad24529..f5b45196 100755 --- a/firmware/baseband/chconf.h +++ b/firmware/baseband/chconf.h @@ -508,6 +508,8 @@ } #endif + + /** * @brief System tick event hook. * @details This hook is invoked in the system tick handler immediately @@ -516,6 +518,8 @@ #if !defined(SYSTEM_TICK_EVENT_HOOK) || defined(__DOXYGEN__) #define SYSTEM_TICK_EVENT_HOOK() { \ /* System tick event code here.*/ \ + extern void update_performance_counters(); \ + update_performance_counters(); \ } #endif diff --git a/firmware/baseband/debug.cpp b/firmware/baseband/debug.cpp index f69f4053..450c9bce 100644 --- a/firmware/baseband/debug.cpp +++ b/firmware/baseband/debug.cpp @@ -116,5 +116,35 @@ CH_IRQ_HANDLER(HardFaultVector) { #endif } +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; + + 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 free_stack = (uint32_t)get_free_stack_space(); + auto free_heap = chCoreStatus(); + + shared_memory.m4_cpu_usage = cpu_usage; + shared_memory.m4_stack_usage = free_stack; + shared_memory.m4_heap_usage = free_heap; } +} /* extern "C" */ diff --git a/firmware/baseband/event_m4.cpp b/firmware/baseband/event_m4.cpp index dbf3a187..ee0079eb 100644 --- a/firmware/baseband/event_m4.cpp +++ b/firmware/baseband/event_m4.cpp @@ -87,10 +87,6 @@ void EventDispatcher::dispatch(const eventmask_t events) { if( events & EVT_MASK_SPECTRUM ) { handle_spectrum(); } - - if (shared_memory.request_m4_performance_counter == 0x01) { - update_performance_counters(); - } } void EventDispatcher::handle_baseband_queue() { @@ -100,34 +96,6 @@ void EventDispatcher::handle_baseband_queue() { } } -void EventDispatcher::update_performance_counters() { - static bool last_paint_state = false; - if ((((chTimeNow()>>10) & 0x01) == 0x01) == last_paint_state) - return; - - 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 free_stack = (uint32_t)get_free_stack_space(); - auto free_heap = chCoreStatus(); - - shared_memory.m4_cpu_usage = cpu_usage; - shared_memory.m4_stack_usage = free_stack; - shared_memory.m4_heap_usage = free_heap; -} - void EventDispatcher::on_message(const Message* const message) { switch(message->id) { case Message::ID::Shutdown: @@ -153,3 +121,4 @@ void EventDispatcher::handle_spectrum() { const UpdateSpectrumMessage message; baseband_processor->on_message(&message); } + diff --git a/firmware/baseband/event_m4.hpp b/firmware/baseband/event_m4.hpp index 5f3097f9..42b5d3b9 100644 --- a/firmware/baseband/event_m4.hpp +++ b/firmware/baseband/event_m4.hpp @@ -61,7 +61,6 @@ private: void dispatch(const eventmask_t events); void handle_baseband_queue(); - void update_performance_counters(); void on_message(const Message* const message); void on_message_shutdown(const ShutdownMessage&); diff --git a/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp b/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp index 3645804e..01bcef05 100644 --- a/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp +++ b/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp @@ -45,3 +45,5 @@ int main() { return 0; } + +void update_performance_counters() {} diff --git a/firmware/common/portapack_shared_memory.hpp b/firmware/common/portapack_shared_memory.hpp index 3d289a85..014c92db 100644 --- a/firmware/common/portapack_shared_memory.hpp +++ b/firmware/common/portapack_shared_memory.hpp @@ -65,10 +65,10 @@ struct SharedMemory { uint8_t data[512]; } bb_data { { { { 0, 0 } }, 0, { 0 } } }; - uint8_t request_m4_performance_counter{ 0 }; - uint8_t m4_cpu_usage{ 0 }; - uint16_t m4_stack_usage{ 0 }; - uint16_t m4_heap_usage{ 0 }; + uint8_t volatile request_m4_performance_counter{ 0 }; + uint8_t volatile m4_cpu_usage{ 0 }; + uint16_t volatile m4_stack_usage{ 0 }; + uint16_t volatile m4_heap_usage{ 0 }; }; extern SharedMemory& shared_memory;