From d41c6ee36a1ffa144f950167c5b9676ed08a9c1e Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 24 Jun 2016 14:16:45 -0700 Subject: [PATCH] Simplify app->baseband message handling. No need for a FIFO when all messages are intended to be synchronous. --- firmware/application/baseband_api.cpp | 57 +++++++++++---------- firmware/application/event_m0.cpp | 4 +- firmware/baseband/event_m4.cpp | 8 +-- firmware/common/portapack_shared_memory.hpp | 4 +- 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/firmware/application/baseband_api.cpp b/firmware/application/baseband_api.cpp index 71c41fe9b..93dd0180f 100644 --- a/firmware/application/baseband_api.cpp +++ b/firmware/application/baseband_api.cpp @@ -30,6 +30,14 @@ namespace baseband { +static void send_message(const Message* const message) { + // If message is only sent by this function via one thread, no need to check if + // another message is present before setting new message. + shared_memory.baseband_message = message; + creg::m0apptxevent::assert(); + while(shared_memory.baseband_message); +} + void AMConfig::apply() const { const AMConfigureMessage message { taps_6k0_decim_0, @@ -39,7 +47,7 @@ void AMConfig::apply() const { modulation, audio_12k_hpf_300hz_config }; - shared_memory.baseband_queue.push_and_wait(message); + send_message(&message); audio::set_rate(audio::Rate::Hz_12000); } @@ -53,7 +61,7 @@ void NBFMConfig::apply() const { audio_24k_hpf_300hz_config, audio_24k_deemph_300_6_config }; - shared_memory.baseband_queue.push_and_wait(message); + send_message(&message); audio::set_rate(audio::Rate::Hz_24000); } @@ -66,21 +74,20 @@ void WFMConfig::apply() const { audio_48k_hpf_30hz_config, audio_48k_deemph_2122_6_config }; - shared_memory.baseband_queue.push_and_wait(message); + send_message(&message); audio::set_rate(audio::Rate::Hz_48000); } void start(BasebandConfiguration configuration) { BasebandConfigurationMessage message { configuration }; - shared_memory.baseband_queue.push_and_wait(message); + send_message(&message); } void stop() { - shared_memory.baseband_queue.push_and_wait( - BasebandConfigurationMessage { - .configuration = { }, - } - ); + BasebandConfigurationMessage message { + .configuration = { }, + }; + send_message(&message); } void run_image(const portapack::spi_flash::region_t image_region) { @@ -92,36 +99,32 @@ void run_image(const portapack::spi_flash::region_t image_region) { void shutdown() { creg::m4txevent::disable(); - ShutdownMessage shutdown_message; - shared_memory.baseband_queue.push_and_wait(shutdown_message); + ShutdownMessage message; + send_message(&message); } void spectrum_streaming_start() { - shared_memory.baseband_queue.push_and_wait( - SpectrumStreamingConfigMessage { - SpectrumStreamingConfigMessage::Mode::Running - } - ); + SpectrumStreamingConfigMessage message { + SpectrumStreamingConfigMessage::Mode::Running + }; + send_message(&message); } void spectrum_streaming_stop() { - shared_memory.baseband_queue.push_and_wait( - SpectrumStreamingConfigMessage { - SpectrumStreamingConfigMessage::Mode::Stopped - } - ); + SpectrumStreamingConfigMessage message { + SpectrumStreamingConfigMessage::Mode::Stopped + }; + send_message(&message); } void capture_start(CaptureConfig* const config) { - shared_memory.baseband_queue.push_and_wait( - CaptureConfigMessage { config } - ); + CaptureConfigMessage message { config }; + send_message(&message); } void capture_stop() { - shared_memory.baseband_queue.push_and_wait( - CaptureConfigMessage { nullptr } - ); + CaptureConfigMessage message { nullptr }; + send_message(&message); } } /* namespace baseband */ diff --git a/firmware/application/event_m0.cpp b/firmware/application/event_m0.cpp index 7966152aa..96df09a42 100644 --- a/firmware/application/event_m0.cpp +++ b/firmware/application/event_m0.cpp @@ -284,9 +284,7 @@ void EventDispatcher::event_bubble_encoder(const ui::EncoderEvent event) { } void EventDispatcher::init_message_queues() { - new (&shared_memory.baseband_queue) MessageQueue( - shared_memory.baseband_queue_data, SharedMemory::baseband_queue_k - ); + shared_memory.baseband_message = nullptr; new (&shared_memory.application_queue) MessageQueue( shared_memory.application_queue_data, SharedMemory::application_queue_k ); diff --git a/firmware/baseband/event_m4.cpp b/firmware/baseband/event_m4.cpp index 8140cd262..d956f2c00 100644 --- a/firmware/baseband/event_m4.cpp +++ b/firmware/baseband/event_m4.cpp @@ -87,9 +87,11 @@ void EventDispatcher::dispatch(const eventmask_t events) { } void EventDispatcher::handle_baseband_queue() { - shared_memory.baseband_queue.handle([this](Message* const message) { - this->on_message(message); - }); + const auto message = shared_memory.baseband_message; + if( message ) { + on_message(message); + shared_memory.baseband_message = nullptr; + } } void EventDispatcher::on_message(const Message* const message) { diff --git a/firmware/common/portapack_shared_memory.hpp b/firmware/common/portapack_shared_memory.hpp index a2ffe8415..ce58f9fa9 100644 --- a/firmware/common/portapack_shared_memory.hpp +++ b/firmware/common/portapack_shared_memory.hpp @@ -33,14 +33,12 @@ struct TouchADCFrame { /* NOTE: These structures must be located in the same location in both M4 and M0 binaries */ struct SharedMemory { - static constexpr size_t baseband_queue_k = 11; static constexpr size_t application_queue_k = 11; static constexpr size_t app_local_queue_k = 11; - uint8_t baseband_queue_data[1 << baseband_queue_k]; uint8_t application_queue_data[1 << application_queue_k]; uint8_t app_local_queue_data[1 << app_local_queue_k]; - MessageQueue baseband_queue; + const Message* volatile baseband_message; MessageQueue application_queue; MessageQueue app_local_queue;