diff --git a/firmware/baseband/baseband_thread.cpp b/firmware/baseband/baseband_thread.cpp index cc043162..bf1903b1 100644 --- a/firmware/baseband/baseband_thread.cpp +++ b/firmware/baseband/baseband_thread.cpp @@ -46,24 +46,34 @@ Thread* BasebandThread::thread = nullptr; BasebandThread::BasebandThread( uint32_t sampling_rate, BasebandProcessor* const baseband_processor, - const tprio_t priority, - baseband::Direction direction) - : baseband_processor{baseband_processor}, - _direction{direction}, - sampling_rate{sampling_rate} { - thread = chThdCreateStatic(baseband_thread_wa, sizeof(baseband_thread_wa), - priority, ThreadBase::fn, - this); + baseband::Direction direction, + bool auto_start, + tprio_t priority) + : baseband_processor_{baseband_processor}, + direction_{direction}, + sampling_rate_{sampling_rate}, + priority_{priority} { + if (auto_start) start(); } BasebandThread::~BasebandThread() { - chThdTerminate(thread); - chThdWait(thread); - thread = nullptr; + if (thread) { + chThdTerminate(thread); + chThdWait(thread); + thread = nullptr; + } +} + +void BasebandThread::start() { + if (!thread) { + thread = chThdCreateStatic( + baseband_thread_wa, sizeof(baseband_thread_wa), + priority_, ThreadBase::fn, this); + } } void BasebandThread::set_sampling_rate(uint32_t new_sampling_rate) { - sampling_rate = new_sampling_rate; + sampling_rate_ = new_sampling_rate; } void BasebandThread::run() { @@ -71,9 +81,7 @@ void BasebandThread::run() { baseband::dma::init(); const auto baseband_buffer = std::make_unique>(); - baseband::dma::configure( - baseband_buffer->data(), - direction()); + baseband::dma::configure(baseband_buffer->data(), direction()); // baseband::dma::allocate(4, 2048); baseband_sgpio.configure(direction()); @@ -85,10 +93,10 @@ void BasebandThread::run() { const auto buffer_tmp = baseband::dma::wait_for_buffer(); if (buffer_tmp) { buffer_c8_t buffer{ - buffer_tmp.p, buffer_tmp.count, sampling_rate}; + buffer_tmp.p, buffer_tmp.count, sampling_rate_}; - if (baseband_processor) { - baseband_processor->execute(buffer); + if (baseband_processor_) { + baseband_processor_->execute(buffer); } } } diff --git a/firmware/baseband/baseband_thread.hpp b/firmware/baseband/baseband_thread.hpp index 5f928700..354cb2c5 100644 --- a/firmware/baseband/baseband_thread.hpp +++ b/firmware/baseband/baseband_thread.hpp @@ -28,13 +28,20 @@ #include +/* NB: Because ThreadBase threads start when then are initialized (by default), + * they should be the last members in a Processor class to ensure the rest of the + * members are fully initialized before data handling starts. If the Procressor + * needs to do additional initialization (in its ctor), set 'auto_start' to false + * and manually call 'start()' on the thread. */ + class BasebandThread : public ThreadBase { public: BasebandThread( uint32_t sampling_rate, BasebandProcessor* const baseband_processor, - const tprio_t priority, - const baseband::Direction direction = baseband::Direction::Receive); + baseband::Direction direction, + bool auto_start = true, + tprio_t priority = (NORMALPRIO + 20)); ~BasebandThread(); BasebandThread(const BasebandThread&) = delete; @@ -42,10 +49,12 @@ class BasebandThread : public ThreadBase { BasebandThread& operator=(const BasebandThread&) = delete; BasebandThread& operator=(BasebandThread&&) = delete; + void start() override; + // This getter should die, it's just here to leak information to code that // isn't in the right place to begin with. baseband::Direction direction() const { - return _direction; + return direction_; } void set_sampling_rate(uint32_t new_sampling_rate); @@ -53,9 +62,10 @@ class BasebandThread : public ThreadBase { private: static Thread* thread; - BasebandProcessor* baseband_processor{nullptr}; - baseband::Direction _direction{baseband::Direction::Receive}; - uint32_t sampling_rate{0}; + BasebandProcessor* baseband_processor_; + baseband::Direction direction_; + uint32_t sampling_rate_; + const tprio_t priority_; void run() override; }; diff --git a/firmware/baseband/proc_acars.cpp b/firmware/baseband/proc_acars.cpp index 3c270442..9a47cb1e 100644 --- a/firmware/baseband/proc_acars.cpp +++ b/firmware/baseband/proc_acars.cpp @@ -32,6 +32,7 @@ ACARSProcessor::ACARSProcessor() { decim_0.configure(taps_11k0_decim_0.taps, 33554432); decim_1.configure(taps_11k0_decim_1.taps, 131072); packet.clear(); + baseband_thread.start(); } void ACARSProcessor::execute(const buffer_c8_t& buffer) { diff --git a/firmware/baseband/proc_acars.hpp b/firmware/baseband/proc_acars.hpp index 6e9d0c27..ddd1c0a1 100644 --- a/firmware/baseband/proc_acars.hpp +++ b/firmware/baseband/proc_acars.hpp @@ -110,9 +110,6 @@ class ACARSProcessor : public BasebandProcessor { private: static constexpr size_t baseband_fs = 2457600; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -138,6 +135,11 @@ class ACARSProcessor : public BasebandProcessor { };*/ baseband::Packet packet{}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Receive, /*auto_start*/ false}; + RSSIThread rssi_thread{}; + void consume_symbol(const float symbol); void payload_handler(const baseband::Packet& packet); }; diff --git a/firmware/baseband/proc_adsbrx.hpp b/firmware/baseband/proc_adsbrx.hpp index a9fcd7bf..a401f2b4 100644 --- a/firmware/baseband/proc_adsbrx.hpp +++ b/firmware/baseband/proc_adsbrx.hpp @@ -36,15 +36,11 @@ using namespace adsb; class ADSBRXProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: static constexpr size_t baseband_fs = 2000000; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - ADSBFrame frame{}; bool configured{false}; uint32_t prev_mag{0}; @@ -58,6 +54,10 @@ class ADSBRXProcessor : public BasebandProcessor { uint32_t sample{0}; int32_t re{}, im{}; int32_t amp{0}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; }; #endif diff --git a/firmware/baseband/proc_adsbtx.hpp b/firmware/baseband/proc_adsbtx.hpp index 0884beba..282a1f8c 100644 --- a/firmware/baseband/proc_adsbtx.hpp +++ b/firmware/baseband/proc_adsbtx.hpp @@ -29,14 +29,11 @@ class ADSBTXProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const p) override; private: bool configured = false; - BasebandThread baseband_thread{4000000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - const complex8_t am_lut[4] = { {127, 0}, {0, 127}, @@ -48,6 +45,9 @@ class ADSBTXProcessor : public BasebandProcessor { uint32_t phase{0}; TXProgressMessage txprogress_message{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{4000000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_afsk.hpp b/firmware/baseband/proc_afsk.hpp index 5c065ed6..4bd7f932 100644 --- a/firmware/baseband/proc_afsk.hpp +++ b/firmware/baseband/proc_afsk.hpp @@ -32,14 +32,11 @@ class AFSKProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const msg) override; private: bool configured = false; - BasebandThread baseband_thread{AFSK_SAMPLERATE, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - uint32_t afsk_samples_per_bit{0}; uint32_t afsk_phase_inc_mark{0}; uint32_t afsk_phase_inc_space{0}; @@ -59,6 +56,9 @@ class AFSKProcessor : public BasebandProcessor { int8_t re{0}, im{0}; TXProgressMessage txprogress_message{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{AFSK_SAMPLERATE, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_afskrx.hpp b/firmware/baseband/proc_afskrx.hpp index 9bb8e71d..1d7d8fbf 100644 --- a/firmware/baseband/proc_afskrx.hpp +++ b/firmware/baseband/proc_afskrx.hpp @@ -38,7 +38,6 @@ class AFSKRxProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: @@ -53,9 +52,6 @@ class AFSKRxProcessor : public BasebandProcessor { RECEIVE }; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -93,9 +89,13 @@ class AFSKRxProcessor : public BasebandProcessor { bool trigger_word{}; bool triggered{}; - void configure(const AFSKRxConfigureMessage& message); - AFSKDataMessage data_message{false, 0}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + + void configure(const AFSKRxConfigureMessage& message); }; #endif /*__PROC_TPMS_H__*/ diff --git a/firmware/baseband/proc_ais.cpp b/firmware/baseband/proc_ais.cpp index 278d7ef2..fbefa8a8 100644 --- a/firmware/baseband/proc_ais.cpp +++ b/firmware/baseband/proc_ais.cpp @@ -30,6 +30,7 @@ AISProcessor::AISProcessor() { decim_0.configure(taps_11k0_decim_0.taps, 33554432); decim_1.configure(taps_11k0_decim_1.taps, 131072); + baseband_thread.start(); } void AISProcessor::execute(const buffer_c8_t& buffer) { diff --git a/firmware/baseband/proc_ais.hpp b/firmware/baseband/proc_ais.hpp index a4b0e2fd..e6e2727d 100644 --- a/firmware/baseband/proc_ais.hpp +++ b/firmware/baseband/proc_ais.hpp @@ -51,9 +51,6 @@ class AISProcessor : public BasebandProcessor { private: static constexpr size_t baseband_fs = 2457600; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -77,6 +74,11 @@ class AISProcessor : public BasebandProcessor { this->payload_handler(packet); }}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Receive, /*auto_start*/ false}; + RSSIThread rssi_thread{}; + void consume_symbol(const float symbol); void payload_handler(const baseband::Packet& packet); }; diff --git a/firmware/baseband/proc_am_audio.hpp b/firmware/baseband/proc_am_audio.hpp index c124d1db..d6348118 100644 --- a/firmware/baseband/proc_am_audio.hpp +++ b/firmware/baseband/proc_am_audio.hpp @@ -38,7 +38,6 @@ class NarrowbandAMAudio : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: @@ -46,9 +45,6 @@ class NarrowbandAMAudio : public BasebandProcessor { static constexpr size_t decim_2_decimation_factor = 4; static constexpr size_t channel_filter_decimation_factor = 1; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -65,6 +61,7 @@ class NarrowbandAMAudio : public BasebandProcessor { int32_t channel_filter_low_f = 0; int32_t channel_filter_high_f = 0; int32_t channel_filter_transition = 0; + bool configured{false}; bool modulation_ssb = false; dsp::demodulate::AM demod_am{}; @@ -74,7 +71,10 @@ class NarrowbandAMAudio : public BasebandProcessor { SpectrumCollector channel_spectrum{}; - bool configured{false}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + void configure(const AMConfigureMessage& message); void capture_config(const CaptureConfigMessage& message); diff --git a/firmware/baseband/proc_am_tv.hpp b/firmware/baseband/proc_am_tv.hpp index 7b76c7a2..4414c07b 100644 --- a/firmware/baseband/proc_am_tv.hpp +++ b/firmware/baseband/proc_am_tv.hpp @@ -38,15 +38,11 @@ class WidebandFMAudio : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: static constexpr size_t baseband_fs = 2000000; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -55,8 +51,12 @@ class WidebandFMAudio : public BasebandProcessor { AudioSpectrum audio_spectrum{}; TvCollector channel_spectrum{}; std::array spectrum{}; - bool configured{false}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + void configure(const WFMConfigureMessage& message); }; diff --git a/firmware/baseband/proc_aprsrx.hpp b/firmware/baseband/proc_aprsrx.hpp index f1c38496..6eca9be7 100644 --- a/firmware/baseband/proc_aprsrx.hpp +++ b/firmware/baseband/proc_aprsrx.hpp @@ -75,7 +75,6 @@ static uint16_t crc_ccitt_tab[256] = { class APRSRxProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: @@ -90,9 +89,6 @@ class APRSRxProcessor : public BasebandProcessor { IN_FRAME }; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -135,6 +131,10 @@ class APRSRxProcessor : public BasebandProcessor { aprs::APRSPacket aprs_packet{}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + void configure(const APRSRxConfigureMessage& message); void capture_config(const CaptureConfigMessage& message); void parse_packet(); diff --git a/firmware/baseband/proc_audiotx.hpp b/firmware/baseband/proc_audiotx.hpp index 1d8557fa..db1dea01 100644 --- a/firmware/baseband/proc_audiotx.hpp +++ b/firmware/baseband/proc_audiotx.hpp @@ -37,8 +37,6 @@ class AudioTXProcessor : public BasebandProcessor { private: static constexpr size_t baseband_fs = 1536000; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - std::unique_ptr stream{}; ToneGen tone_gen{}; @@ -61,6 +59,9 @@ class AudioTXProcessor : public BasebandProcessor { TXProgressMessage txprogress_message{}; RequestSignalMessage sig_message{RequestSignalMessage::Signal::FillRequest}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_btlerx.hpp b/firmware/baseband/proc_btlerx.hpp index b7cdaf1f..e137912f 100644 --- a/firmware/baseband/proc_btlerx.hpp +++ b/firmware/baseband/proc_btlerx.hpp @@ -39,16 +39,12 @@ class BTLERxProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: static constexpr size_t baseband_fs = 4000000; static constexpr size_t audio_fs = baseband_fs / 8 / 8 / 2; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -81,10 +77,13 @@ class BTLERxProcessor : public BasebandProcessor { int RB_SIZE{1000}; bool configured{false}; + AFSKDataMessage data_message{false, 0}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; void configure(const BTLERxConfigureMessage& message); - - AFSKDataMessage data_message{false, 0}; }; #endif /*__PROC_BTLERX_H__*/ diff --git a/firmware/baseband/proc_capture.cpp b/firmware/baseband/proc_capture.cpp index feed7f12..5136f2c8 100644 --- a/firmware/baseband/proc_capture.cpp +++ b/firmware/baseband/proc_capture.cpp @@ -31,13 +31,10 @@ CaptureProcessor::CaptureProcessor() { decim_1.configure(taps_200k_decim_1.taps, 131072); channel_spectrum.set_decimation_factor(1); - ready = true; + baseband_thread.start(); } void CaptureProcessor::execute(const buffer_c8_t& buffer) { - if (!ready) - return; - /* 2.4576MHz, 2048 samples */ const auto decim_0_out = decim_0.execute(buffer, dst_buffer); const auto decim_1_out = decim_1.execute(decim_0_out, dst_buffer); diff --git a/firmware/baseband/proc_capture.hpp b/firmware/baseband/proc_capture.hpp index ff71273c..fd1b7767 100644 --- a/firmware/baseband/proc_capture.hpp +++ b/firmware/baseband/proc_capture.hpp @@ -28,9 +28,7 @@ #include "rssi_thread.hpp" #include "dsp_decimate.hpp" - #include "spectrum_collector.hpp" - #include "stream_input.hpp" #include @@ -41,21 +39,12 @@ class CaptureProcessor : public BasebandProcessor { CaptureProcessor(); void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: - // TODO: Repeated value needs to be transmitted from application side. size_t baseband_fs = 3072000; static constexpr auto spectrum_rate_hz = 50.0f; - // HACK: BasebandThread starts immediately and starts sending data to members - // before they are initialized. This is a workaround to prevent uninit data problems. - bool ready = false; - - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -73,6 +62,11 @@ class CaptureProcessor : public BasebandProcessor { size_t spectrum_interval_samples = 0; size_t spectrum_samples = 0; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Receive, /*auto_start*/ false}; + RSSIThread rssi_thread{}; + void samplerate_config(const SamplerateConfigMessage& message); void capture_config(const CaptureConfigMessage& message); }; diff --git a/firmware/baseband/proc_ert.hpp b/firmware/baseband/proc_ert.hpp index 9e2a8655..6b3517d0 100644 --- a/firmware/baseband/proc_ert.hpp +++ b/firmware/baseband/proc_ert.hpp @@ -67,9 +67,6 @@ class ERTProcessor : public BasebandProcessor { const size_t samples_per_symbol = channel_sampling_rate / symbol_rate; const float clock_recovery_rate = symbol_rate * 2; - BasebandThread baseband_thread{baseband_sampling_rate, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - clock_recovery::ClockRecovery clock_recovery{ clock_recovery_rate, symbol_rate, @@ -116,6 +113,10 @@ class ERTProcessor : public BasebandProcessor { float offset_i{0.0f}; float offset_q{0.0f}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_sampling_rate, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + float abs(const complex8_t& v); }; diff --git a/firmware/baseband/proc_fsk.hpp b/firmware/baseband/proc_fsk.hpp index b8f16eb9..55f5d6f0 100644 --- a/firmware/baseband/proc_fsk.hpp +++ b/firmware/baseband/proc_fsk.hpp @@ -29,14 +29,11 @@ class FSKProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const p) override; private: bool configured = false; - BasebandThread baseband_thread{2280000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - uint32_t samples_per_bit{0}; uint32_t length{0}; @@ -48,6 +45,9 @@ class FSKProcessor : public BasebandProcessor { uint32_t phase{0}, sphase{0}; TXProgressMessage txprogress_message{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{2280000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_gps_sim.cpp b/firmware/baseband/proc_gps_sim.cpp index bcb38b91..e1db4988 100644 --- a/firmware/baseband/proc_gps_sim.cpp +++ b/firmware/baseband/proc_gps_sim.cpp @@ -29,7 +29,7 @@ #include "utility.hpp" -ReplayProcessor::ReplayProcessor() { +GPSReplayProcessor::GPSReplayProcessor() { channel_filter_low_f = taps_200k_decim_1.low_frequency_normalized * 1000000; channel_filter_high_f = taps_200k_decim_1.high_frequency_normalized * 1000000; channel_filter_transition = taps_200k_decim_1.transition_normalized * 1000000; @@ -39,9 +39,10 @@ ReplayProcessor::ReplayProcessor() { channel_spectrum.set_decimation_factor(1); configured = false; + baseband_thread.start(); } -void ReplayProcessor::execute(const buffer_c8_t& buffer) { +void GPSReplayProcessor::execute(const buffer_c8_t& buffer) { /* 2.6MHz, 2048 samples */ if (!configured || !stream) return; @@ -75,7 +76,7 @@ void ReplayProcessor::execute(const buffer_c8_t& buffer) { } } -void ReplayProcessor::on_message(const Message* const message) { +void GPSReplayProcessor::on_message(const Message* const message) { switch (message->id) { case Message::ID::UpdateSpectrum: case Message::ID::SpectrumStreamingConfig: @@ -102,13 +103,13 @@ void ReplayProcessor::on_message(const Message* const message) { } } -void ReplayProcessor::samplerate_config(const SamplerateConfigMessage& message) { +void GPSReplayProcessor::samplerate_config(const SamplerateConfigMessage& message) { baseband_fs = message.sample_rate; baseband_thread.set_sampling_rate(baseband_fs); spectrum_interval_samples = baseband_fs / spectrum_rate_hz; } -void ReplayProcessor::replay_config(const ReplayConfigMessage& message) { +void GPSReplayProcessor::replay_config(const ReplayConfigMessage& message) { if (message.config) { stream = std::make_unique(message.config); @@ -120,7 +121,7 @@ void ReplayProcessor::replay_config(const ReplayConfigMessage& message) { } int main() { - EventDispatcher event_dispatcher{std::make_unique()}; + EventDispatcher event_dispatcher{std::make_unique()}; event_dispatcher.run(); return 0; } diff --git a/firmware/baseband/proc_gps_sim.hpp b/firmware/baseband/proc_gps_sim.hpp index ee2a6662..768cfc95 100644 --- a/firmware/baseband/proc_gps_sim.hpp +++ b/firmware/baseband/proc_gps_sim.hpp @@ -34,20 +34,17 @@ #include #include -class ReplayProcessor : public BasebandProcessor { +class GPSReplayProcessor : public BasebandProcessor { public: - ReplayProcessor(); + GPSReplayProcessor(); void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: size_t baseband_fs = 3072000; static constexpr auto spectrum_rate_hz = 50.0f; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - std::array iq{}; const buffer_c8_t iq_buffer{ iq.data(), @@ -72,6 +69,10 @@ class ReplayProcessor : public BasebandProcessor { TXProgressMessage txprogress_message{}; RequestSignalMessage sig_message{RequestSignalMessage::Signal::FillRequest}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Transmit, /*auto_start*/ false}; }; #endif /*__PROC_GPS_SIM_HPP__*/ diff --git a/firmware/baseband/proc_jammer.hpp b/firmware/baseband/proc_jammer.hpp index 850c4ad5..e9fa45c5 100644 --- a/firmware/baseband/proc_jammer.hpp +++ b/firmware/baseband/proc_jammer.hpp @@ -33,14 +33,11 @@ using namespace jammer; class JammerProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const msg) override; private: bool configured{false}; - BasebandThread baseband_thread{3072000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - JammerChannel* jammer_channels{}; JammerType noise_type{}; @@ -54,6 +51,9 @@ class JammerProcessor : public BasebandProcessor { int8_t sample{0}; int8_t re{0}, im{0}; RetuneMessage message{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{3072000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_mictx.hpp b/firmware/baseband/proc_mictx.hpp index 2854a911..f572245d 100644 --- a/firmware/baseband/proc_mictx.hpp +++ b/firmware/baseband/proc_mictx.hpp @@ -32,7 +32,6 @@ class MicTXProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const msg) override; private: @@ -40,8 +39,6 @@ class MicTXProcessor : public BasebandProcessor { bool configured{false}; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - int16_t audio_data[64]; buffer_s16_t audio_buffer{ audio_data, @@ -74,6 +71,9 @@ class MicTXProcessor : public BasebandProcessor { AudioLevelReportMessage level_message{}; TXProgressMessage txprogress_message{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_nfm_audio.hpp b/firmware/baseband/proc_nfm_audio.hpp index 708dfa2b..8ac185ce 100644 --- a/firmware/baseband/proc_nfm_audio.hpp +++ b/firmware/baseband/proc_nfm_audio.hpp @@ -42,15 +42,11 @@ class NarrowbandFMAudio : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: static constexpr size_t baseband_fs = 3072000; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -97,12 +93,16 @@ class NarrowbandFMAudio : public BasebandProcessor { static constexpr float ki = 1.0f / k; bool configured{false}; + // RequestSignalMessage sig_message { RequestSignalMessage::Signal::Squelched }; + CodedSquelchMessage ctcss_message{0}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + void pitch_rssi_config(const PitchRSSIConfigureMessage& message); void configure(const NBFMConfigureMessage& message); void capture_config(const CaptureConfigMessage& message); - - // RequestSignalMessage sig_message { RequestSignalMessage::Signal::Squelched }; - CodedSquelchMessage ctcss_message{0}; }; #endif /*__PROC_NFM_AUDIO_H__*/ diff --git a/firmware/baseband/proc_noop.hpp b/firmware/baseband/proc_noop.hpp index c71232e3..a2532c60 100644 --- a/firmware/baseband/proc_noop.hpp +++ b/firmware/baseband/proc_noop.hpp @@ -31,7 +31,8 @@ class NOOPProcessor : public BasebandProcessor { void execute(const buffer_c8_t& buffer) override; private: - BasebandThread baseband_thread{1536000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{1536000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_nrfrx.hpp b/firmware/baseband/proc_nrfrx.hpp index e909d268..e86d426a 100644 --- a/firmware/baseband/proc_nrfrx.hpp +++ b/firmware/baseband/proc_nrfrx.hpp @@ -39,16 +39,12 @@ class NRFRxProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: static constexpr size_t baseband_fs = 4000000; static constexpr size_t audio_fs = baseband_fs / 8 / 8 / 2; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -82,10 +78,13 @@ class NRFRxProcessor : public BasebandProcessor { int RB_SIZE{1000}; bool configured{false}; + AFSKDataMessage data_message{false, 0}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; void configure(const NRFRxConfigureMessage& message); - - AFSKDataMessage data_message{false, 0}; }; #endif /*__PROC_NRFRX_H__*/ diff --git a/firmware/baseband/proc_ook.hpp b/firmware/baseband/proc_ook.hpp index 24db78c2..9200bf57 100644 --- a/firmware/baseband/proc_ook.hpp +++ b/firmware/baseband/proc_ook.hpp @@ -29,14 +29,11 @@ class OOKProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const p) override; private: bool configured = false; - BasebandThread baseband_thread{2280000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - uint32_t samples_per_bit{0}; uint8_t repeat{0}; uint32_t length{0}; @@ -67,6 +64,9 @@ class OOKProcessor : public BasebandProcessor { size_t scan_progress{0}; uint8_t scan_done{true}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{2280000, this, baseband::Direction::Transmit}; + size_t duval_algo_step(); void scan_process(const buffer_c8_t& buffer); bool scan_init(unsigned int order); diff --git a/firmware/baseband/proc_pocsag.hpp b/firmware/baseband/proc_pocsag.hpp index d4689da4..1f5ec679 100644 --- a/firmware/baseband/proc_pocsag.hpp +++ b/firmware/baseband/proc_pocsag.hpp @@ -129,7 +129,6 @@ class SmoothVals { class POCSAGProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; int OnDataFrame(int len, int baud); @@ -138,9 +137,6 @@ class POCSAGProcessor : public BasebandProcessor { private: static constexpr size_t baseband_fs = 3072000; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -166,7 +162,6 @@ class POCSAGProcessor : public BasebandProcessor { // ---------------------------------------- // Frame extractraction methods and members // ---------------------------------------- - private: void initFrameExtraction(); struct FIFOStruct { unsigned long codeword; @@ -219,6 +214,10 @@ class POCSAGProcessor : public BasebandProcessor { bool m_gotSync{false}; int m_numCode{0}; bool m_inverted{false}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; }; #endif /*__PROC_POCSAG_H__*/ diff --git a/firmware/baseband/proc_rds.hpp b/firmware/baseband/proc_rds.hpp index a39cf45f..7a99395f 100644 --- a/firmware/baseband/proc_rds.hpp +++ b/firmware/baseband/proc_rds.hpp @@ -33,14 +33,11 @@ class RDSProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const msg) override; private: uint32_t* rdsdata{}; - BasebandThread baseband_thread{2280000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - uint16_t message_length{0}; int8_t re{0}, im{0}; uint8_t mphase{0}, s{0}; @@ -132,6 +129,9 @@ class RDSProcessor : public BasebandProcessor { 0, -14, -27, -41, -53, -66, -77, -88, -99, -109, -118, -126, -134, -141, -147, -152, -157, -160, -163, -166, -167, -168, -168, -167}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{2280000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_replay.cpp b/firmware/baseband/proc_replay.cpp index 1f70fe04..962367ee 100644 --- a/firmware/baseband/proc_replay.cpp +++ b/firmware/baseband/proc_replay.cpp @@ -38,6 +38,7 @@ ReplayProcessor::ReplayProcessor() { channel_spectrum.set_decimation_factor(1); configured = false; + baseband_thread.start(); } void ReplayProcessor::execute(const buffer_c8_t& buffer) { diff --git a/firmware/baseband/proc_replay.hpp b/firmware/baseband/proc_replay.hpp index ec6bc99f..e5ba71ab 100644 --- a/firmware/baseband/proc_replay.hpp +++ b/firmware/baseband/proc_replay.hpp @@ -38,15 +38,12 @@ class ReplayProcessor : public BasebandProcessor { ReplayProcessor(); void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: size_t baseband_fs = 3072000; static constexpr auto spectrum_rate_hz = 50.0f; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - std::array iq{}; const buffer_c16_t iq_buffer{ iq.data(), @@ -71,6 +68,10 @@ class ReplayProcessor : public BasebandProcessor { TXProgressMessage txprogress_message{}; RequestSignalMessage sig_message{RequestSignalMessage::Signal::FillRequest}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Transmit, /*auto_start*/ false}; }; #endif /*__PROC_REPLAY_HPP__*/ diff --git a/firmware/baseband/proc_siggen.hpp b/firmware/baseband/proc_siggen.hpp index c5949065..36bc3e04 100644 --- a/firmware/baseband/proc_siggen.hpp +++ b/firmware/baseband/proc_siggen.hpp @@ -30,14 +30,11 @@ class SigGenProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const msg) override; private: bool configured{false}; - BasebandThread baseband_thread{1536000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - uint32_t tone_delta{0}, fm_delta{}, tone_phase{0}; uint8_t tone_shape{}; uint32_t sample_count{0}; @@ -51,6 +48,9 @@ class SigGenProcessor : public BasebandProcessor { // uint8_t lfsr { }, bit { }; // Finally not used lfsr of 8 bits , bit must be 8-bit to allow bit<<7 later in the code */ TXProgressMessage txprogress_message{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{1536000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_sonde.cpp b/firmware/baseband/proc_sonde.cpp index fbf5431d..39105008 100644 --- a/firmware/baseband/proc_sonde.cpp +++ b/firmware/baseband/proc_sonde.cpp @@ -35,6 +35,7 @@ SondeProcessor::SondeProcessor() { audio_output.configure(false); tone_gen.configure(BEEP_BASE_FREQ, 1.0, ToneGen::tone_type::sine, AUDIO_SAMPLE_RATE); + baseband_thread.start(); } void SondeProcessor::execute(const buffer_c8_t& buffer) { diff --git a/firmware/baseband/proc_sonde.hpp b/firmware/baseband/proc_sonde.hpp index 67914d27..93ed22e1 100644 --- a/firmware/baseband/proc_sonde.hpp +++ b/firmware/baseband/proc_sonde.hpp @@ -131,9 +131,6 @@ class SondeProcessor : public BasebandProcessor { ToneGen tone_gen{}; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -179,6 +176,11 @@ class SondeProcessor : public BasebandProcessor { shared_memory.application_queue.push(message); }}; + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Receive, /*auto_start*/ false}; + RSSIThread rssi_thread{}; + void play_beep(); void stop_beep(); diff --git a/firmware/baseband/proc_spectrum_painter.hpp b/firmware/baseband/proc_spectrum_painter.hpp index 2b7bc041..438dd595 100644 --- a/firmware/baseband/proc_spectrum_painter.hpp +++ b/firmware/baseband/proc_spectrum_painter.hpp @@ -33,7 +33,9 @@ class SpectrumPainterProcessor : public BasebandProcessor { private: bool configured{false}; - BasebandThread baseband_thread{3072000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{3072000, this, baseband::Direction::Transmit}; Thread* thread{nullptr}; protected: diff --git a/firmware/baseband/proc_sstvtx.hpp b/firmware/baseband/proc_sstvtx.hpp index 2a14ffd4..496ce064 100644 --- a/firmware/baseband/proc_sstvtx.hpp +++ b/firmware/baseband/proc_sstvtx.hpp @@ -33,7 +33,6 @@ using namespace sstv; class SSTVTXProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const p) override; private: @@ -56,8 +55,6 @@ class SSTVTXProcessor : public BasebandProcessor { bool configured{false}; - BasebandThread baseband_thread{3072000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - uint32_t vis_code_sequence[10]{}; sstv_scanline scanline_buffer[2]{}; uint8_t buffer_flip{0}, substep{0}; @@ -76,6 +73,9 @@ class SSTVTXProcessor : public BasebandProcessor { int8_t re{}, im{}; RequestSignalMessage sig_message{RequestSignalMessage::Signal::FillRequest}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{3072000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_test.cpp b/firmware/baseband/proc_test.cpp index 46a42292..648221a8 100644 --- a/firmware/baseband/proc_test.cpp +++ b/firmware/baseband/proc_test.cpp @@ -29,6 +29,7 @@ TestProcessor::TestProcessor() { decim_0.configure(taps_11k0_decim_0.taps, 33554432); decim_1.configure(taps_11k0_decim_1.taps, 131072); + baseband_thread.start(); } void TestProcessor::execute(const buffer_c8_t& buffer) { diff --git a/firmware/baseband/proc_test.hpp b/firmware/baseband/proc_test.hpp index 4f0889de..78702958 100644 --- a/firmware/baseband/proc_test.hpp +++ b/firmware/baseband/proc_test.hpp @@ -52,9 +52,6 @@ class TestProcessor : public BasebandProcessor { private: static constexpr size_t baseband_fs = 2457600 * 2; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -80,6 +77,11 @@ class TestProcessor : public BasebandProcessor { const TestAppPacketMessage message{packet}; shared_memory.application_queue.push(message); }}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Receive, /*auto_start*/ false}; + RSSIThread rssi_thread{}; }; #endif /*__PROC_TEST_H__*/ diff --git a/firmware/baseband/proc_tones.hpp b/firmware/baseband/proc_tones.hpp index 5c567ae0..e59446f6 100644 --- a/firmware/baseband/proc_tones.hpp +++ b/firmware/baseband/proc_tones.hpp @@ -31,14 +31,11 @@ class TonesProcessor : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const p) override; private: bool configured = false; - BasebandThread baseband_thread{1536000, this, NORMALPRIO + 20, baseband::Direction::Transmit}; - std::array audio{}; // 2048/64 const buffer_s16_t audio_buffer{ (int16_t*)audio.data(), @@ -63,6 +60,9 @@ class TonesProcessor : public BasebandProcessor { TXProgressMessage txprogress_message{}; AudioOutput audio_output{}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{1536000, this, baseband::Direction::Transmit}; }; #endif diff --git a/firmware/baseband/proc_tpms.cpp b/firmware/baseband/proc_tpms.cpp index 0495ae16..ea6a2847 100644 --- a/firmware/baseband/proc_tpms.cpp +++ b/firmware/baseband/proc_tpms.cpp @@ -28,6 +28,7 @@ TPMSProcessor::TPMSProcessor() { decim_0.configure(taps_200k_decim_0.taps, 33554432); decim_1.configure(taps_200k_decim_1.taps, 131072); + baseband_thread.start(); } void TPMSProcessor::execute(const buffer_c8_t& buffer) { diff --git a/firmware/baseband/proc_tpms.hpp b/firmware/baseband/proc_tpms.hpp index 41438be3..9167b3be 100644 --- a/firmware/baseband/proc_tpms.hpp +++ b/firmware/baseband/proc_tpms.hpp @@ -74,9 +74,6 @@ class TPMSProcessor : public BasebandProcessor { private: static constexpr size_t baseband_fs = 2457600; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -141,6 +138,11 @@ class TPMSProcessor : public BasebandProcessor { const TPMSPacketMessage message{tpms::SignalType::OOK_8k4_Schrader, packet}; shared_memory.application_queue.push(message); }}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{ + baseband_fs, this, baseband::Direction::Receive, /*auto_start*/ false}; + RSSIThread rssi_thread{}; }; #endif /*__PROC_TPMS_H__*/ diff --git a/firmware/baseband/proc_wfm_audio.hpp b/firmware/baseband/proc_wfm_audio.hpp index 85e3faff..d003e8fa 100644 --- a/firmware/baseband/proc_wfm_audio.hpp +++ b/firmware/baseband/proc_wfm_audio.hpp @@ -38,16 +38,12 @@ class WidebandFMAudio : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: static constexpr size_t baseband_fs = 3072000; static constexpr auto spectrum_rate_hz = 50.0f; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - std::array dst{}; const buffer_c16_t dst_buffer{ dst.data(), @@ -93,6 +89,11 @@ class WidebandFMAudio : public BasebandProcessor { size_t spectrum_samples = 0; bool configured{false}; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; + void configure(const WFMConfigureMessage& message); void capture_config(const CaptureConfigMessage& message); void post_message(const buffer_c16_t& data); diff --git a/firmware/baseband/proc_wideband_spectrum.hpp b/firmware/baseband/proc_wideband_spectrum.hpp index e4c7d726..ed17a556 100644 --- a/firmware/baseband/proc_wideband_spectrum.hpp +++ b/firmware/baseband/proc_wideband_spectrum.hpp @@ -37,22 +37,20 @@ class WidebandSpectrum : public BasebandProcessor { public: void execute(const buffer_c8_t& buffer) override; - void on_message(const Message* const message) override; private: bool configured = false; - size_t baseband_fs = 20000000; - BasebandThread baseband_thread{baseband_fs, this, NORMALPRIO + 20}; - RSSIThread rssi_thread{NORMALPRIO + 10}; - SpectrumCollector channel_spectrum{}; std::array spectrum{}; - size_t phase = 0, trigger = 127; + + /* NB: Threads should be the last members in the class definition. */ + BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive}; + RSSIThread rssi_thread{}; }; #endif /*__PROC_WIDEBAND_SPECTRUM_H__*/ diff --git a/firmware/baseband/rssi_thread.cpp b/firmware/baseband/rssi_thread.cpp index 6dfcb02a..812f8c1a 100644 --- a/firmware/baseband/rssi_thread.cpp +++ b/firmware/baseband/rssi_thread.cpp @@ -32,16 +32,25 @@ WORKING_AREA(rssi_thread_wa, 128); Thread* RSSIThread::thread = nullptr; -RSSIThread::RSSIThread(const tprio_t priority) { - thread = chThdCreateStatic(rssi_thread_wa, sizeof(rssi_thread_wa), - priority, ThreadBase::fn, - this); +RSSIThread::RSSIThread(bool auto_start, tprio_t priority) + : priority_{priority} { + if (auto_start) start(); } RSSIThread::~RSSIThread() { - chThdTerminate(thread); - chThdWait(thread); - thread = nullptr; + if (thread) { + chThdTerminate(thread); + chThdWait(thread); + thread = nullptr; + } +} + +void RSSIThread::start() { + if (!thread) { + thread = chThdCreateStatic( + rssi_thread_wa, sizeof(rssi_thread_wa), + priority_, ThreadBase::fn, this); + } } void RSSIThread::run() { diff --git a/firmware/baseband/rssi_thread.hpp b/firmware/baseband/rssi_thread.hpp index 5bfe66c3..ea61aee3 100644 --- a/firmware/baseband/rssi_thread.hpp +++ b/firmware/baseband/rssi_thread.hpp @@ -25,20 +25,32 @@ #include "thread_base.hpp" #include - #include +/* NB: Because ThreadBase threads start when then are initialized (by default), + * they should be the last members in a Processor class to ensure the rest of the + * members are fully initialized before data handling starts. If the Procressor + * needs to do additional initialization (in its ctor), set 'auto_start' to false + * and manually call 'start()' on the thread. + * This isn't as relevant for RSSIThread which is entirely self-contained, but + * it's good practice to keep all the thread-init together. */ + class RSSIThread : public ThreadBase { public: - RSSIThread(const tprio_t priority); + RSSIThread( + bool auto_start = true, + tprio_t priority = (NORMALPRIO + 10)); ~RSSIThread(); + void start() override; + private: void run() override; - static Thread* thread; + const tprio_t priority_; - const uint32_t sampling_rate{400000}; + static Thread* thread; + static constexpr uint32_t sampling_rate{400000}; }; #endif /*__RSSI_THREAD_H__*/ diff --git a/firmware/common/thread_base.hpp b/firmware/common/thread_base.hpp index 61ddcade..53701543 100644 --- a/firmware/common/thread_base.hpp +++ b/firmware/common/thread_base.hpp @@ -28,6 +28,8 @@ class ThreadBase { public: virtual ~ThreadBase() = default; + virtual void start() = 0; + protected: static msg_t fn(void* arg) { auto obj = static_cast(arg);