diff --git a/firmware/baseband/baseband_thread.cpp b/firmware/baseband/baseband_thread.cpp index 2182cc9f..6fc8a153 100644 --- a/firmware/baseband/baseband_thread.cpp +++ b/firmware/baseband/baseband_thread.cpp @@ -48,13 +48,23 @@ static baseband::SGPIO baseband_sgpio; WORKING_AREA(baseband_thread_wa, 4096); -Thread* BasebandThread::start(const tprio_t priority) { - return chThdCreateStatic(baseband_thread_wa, sizeof(baseband_thread_wa), +Thread* BasebandThread::thread = nullptr; + +BasebandThread::BasebandThread(const tprio_t priority) { + thread = chThdCreateStatic(baseband_thread_wa, sizeof(baseband_thread_wa), priority, ThreadBase::fn, this ); } +BasebandThread::~BasebandThread() { + if( thread ) { + chThdTerminate(thread); + chThdWait(thread); + thread = nullptr; + } +} + void BasebandThread::set_configuration(const BasebandConfiguration& new_configuration) { if( new_configuration.mode != baseband_configuration.mode ) { disable(); @@ -93,7 +103,7 @@ void BasebandThread::run() { ); //baseband::dma::allocate(4, 2048); - while(true) { + while( !chThdShouldTerminate() ) { // TODO: Place correct sampling rate into buffer returned here: const auto buffer_tmp = baseband::dma::wait_for_rx_buffer(); if( buffer_tmp ) { @@ -127,15 +137,11 @@ void BasebandThread::disable() { i2s::i2s0::tx_mute(); baseband::dma::disable(); baseband_sgpio.streaming_disable(); - rf::rssi::stop(); } } void BasebandThread::enable() { if( baseband_processor ) { - if( direction() == baseband::Direction::Receive ) { - rf::rssi::start(); - } baseband_sgpio.configure(direction()); baseband::dma::enable(direction()); baseband_sgpio.streaming_enable(); diff --git a/firmware/baseband/baseband_thread.hpp b/firmware/baseband/baseband_thread.hpp index 67438709..82862049 100644 --- a/firmware/baseband/baseband_thread.hpp +++ b/firmware/baseband/baseband_thread.hpp @@ -30,7 +30,8 @@ class BasebandThread : public ThreadBase { public: - Thread* start(const tprio_t priority); + BasebandThread(const tprio_t priority); + ~BasebandThread(); void on_message(const Message* const message); @@ -41,6 +42,8 @@ public: } private: + static Thread* thread; + BasebandProcessor* baseband_processor { nullptr }; BasebandConfiguration baseband_configuration; diff --git a/firmware/baseband/event_m4.cpp b/firmware/baseband/event_m4.cpp index 1490ee2a..0ef97bba 100644 --- a/firmware/baseband/event_m4.cpp +++ b/firmware/baseband/event_m4.cpp @@ -54,9 +54,6 @@ Thread* EventDispatcher::thread_event_loop = nullptr; void EventDispatcher::run() { thread_event_loop = chThdSelf(); - rssi_thread.start(NORMALPRIO + 10); - baseband_thread.start(NORMALPRIO + 20); - lpc43xx::creg::m0apptxevent::enable(); while(is_running) { diff --git a/firmware/baseband/event_m4.hpp b/firmware/baseband/event_m4.hpp index 596c253b..f0a7e5b4 100644 --- a/firmware/baseband/event_m4.hpp +++ b/firmware/baseband/event_m4.hpp @@ -50,8 +50,8 @@ public: private: static Thread* thread_event_loop; - BasebandThread baseband_thread; - RSSIThread rssi_thread; + BasebandThread baseband_thread { NORMALPRIO + 20 }; + RSSIThread rssi_thread { NORMALPRIO + 10 }; bool is_running = true; diff --git a/firmware/baseband/rssi_thread.cpp b/firmware/baseband/rssi_thread.cpp index 0bf15334..465e1d1f 100644 --- a/firmware/baseband/rssi_thread.cpp +++ b/firmware/baseband/rssi_thread.cpp @@ -30,20 +30,32 @@ WORKING_AREA(rssi_thread_wa, 128); -Thread* RSSIThread::start(const tprio_t priority) { - return chThdCreateStatic(rssi_thread_wa, sizeof(rssi_thread_wa), +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() { + if( thread ) { + chThdTerminate(thread); + chThdWait(thread); + thread = nullptr; + } +} + void RSSIThread::run() { rf::rssi::init(); rf::rssi::dma::allocate(4, 400); RSSIStatisticsCollector stats; - while(true) { + rf::rssi::start(); + + while( !chThdShouldTerminate() ) { // TODO: Place correct sampling rate into buffer returned here: const auto buffer_tmp = rf::rssi::dma::wait_for_buffer(); const rf::rssi::buffer_t buffer { @@ -59,5 +71,6 @@ void RSSIThread::run() { ); } + rf::rssi::stop(); rf::rssi::dma::free(); } diff --git a/firmware/baseband/rssi_thread.hpp b/firmware/baseband/rssi_thread.hpp index 5233ddd5..46a0031b 100644 --- a/firmware/baseband/rssi_thread.hpp +++ b/firmware/baseband/rssi_thread.hpp @@ -30,11 +30,14 @@ class RSSIThread : public ThreadBase { public: - Thread* start(const tprio_t priority); + RSSIThread(const tprio_t priority); + ~RSSIThread(); private: void run() override; + static Thread* thread; + const uint32_t sampling_rate { 400000 }; };