From eeb464524295ffb384df1db5e9e775eb4132f141 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Mon, 2 May 2016 12:50:49 -0700 Subject: [PATCH] Move lots of capture_thread code to .cpp. --- firmware/application/capture_thread.cpp | 104 +++++++++++++++++++++++ firmware/application/capture_thread.hpp | 107 +----------------------- 2 files changed, 108 insertions(+), 103 deletions(-) diff --git a/firmware/application/capture_thread.cpp b/firmware/application/capture_thread.cpp index e39d6312..867faeea 100644 --- a/firmware/application/capture_thread.cpp +++ b/firmware/application/capture_thread.cpp @@ -21,5 +21,109 @@ #include "capture_thread.hpp" +#include "portapack_shared_memory.hpp" + +#include "hackrf_gpio.hpp" +using namespace hackrf::one; + +// StreamOutput /////////////////////////////////////////////////////////// + +class StreamOutput { +public: + StreamOutput(CaptureConfig* const config); + ~StreamOutput(); + + size_t available() { + return fifo->len(); + } + + size_t read(void* const data, const size_t length) { + return fifo->out(reinterpret_cast(data), length); + } + + static FIFO* fifo; + +private: + CaptureConfig* const config; +}; + FIFO* StreamOutput::fifo = nullptr; + +StreamOutput::StreamOutput( + CaptureConfig* const config +) : config { config } +{ + shared_memory.baseband_queue.push_and_wait( + CaptureConfigMessage { config } + ); + fifo = config->fifo; +} + +StreamOutput::~StreamOutput() { + fifo = nullptr; + shared_memory.baseband_queue.push_and_wait( + CaptureConfigMessage { nullptr } + ); +} + +// CaptureThread ////////////////////////////////////////////////////////// + Thread* CaptureThread::thread = nullptr; + +CaptureThread::CaptureThread( + std::unique_ptr writer, + size_t write_size_log2, + size_t buffer_count_log2 +) : config { write_size_log2, buffer_count_log2 }, + writer { std::move(writer) } +{ + // Need significant stack for FATFS + thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this); +} + +CaptureThread::~CaptureThread() { + if( thread ) { + chThdTerminate(thread); + chEvtSignal(thread, EVT_MASK_CAPTURE_THREAD); + const auto success = chThdWait(thread); + thread = nullptr; + + if( !success ) { + led_tx.on(); + } + } +} + +void CaptureThread::check_fifo_isr() { + // TODO: Prevent over-signalling by transmitting a set of + // flags from the baseband core. + const auto fifo = StreamOutput::fifo; + if( fifo ) { + chEvtSignalI(thread, EVT_MASK_CAPTURE_THREAD); + } +} + +msg_t CaptureThread::run() { + const size_t write_size = 1U << config.write_size_log2; + const auto write_buffer = std::make_unique(write_size); + if( !write_buffer ) { + return false; + } + + StreamOutput stream { &config }; + + while( !chThdShouldTerminate() ) { + if( stream.available() >= write_size ) { + if( stream.read(write_buffer.get(), write_size) != write_size ) { + return false; + } + if( !writer->write(write_buffer.get(), write_size) ) { + return false; + } + } else { + chEvtWaitAny(EVT_MASK_CAPTURE_THREAD); + } + } + + return true; +} diff --git a/firmware/application/capture_thread.hpp b/firmware/application/capture_thread.hpp index 792af195..c4bdbb1c 100644 --- a/firmware/application/capture_thread.hpp +++ b/firmware/application/capture_thread.hpp @@ -26,11 +26,6 @@ #include "event_m0.hpp" -#include "portapack_shared_memory.hpp" - -#include "hackrf_gpio.hpp" -using namespace hackrf::one; - #include #include #include @@ -41,77 +36,20 @@ public: virtual ~Writer() = default; }; -class StreamOutput { -public: - StreamOutput( - CaptureConfig* const config - ) : config { config } - { - shared_memory.baseband_queue.push_and_wait( - CaptureConfigMessage { config } - ); - fifo = config->fifo; - } - - ~StreamOutput() { - fifo = nullptr; - shared_memory.baseband_queue.push_and_wait( - CaptureConfigMessage { nullptr } - ); - } - - size_t available() { - return fifo->len(); - } - - size_t read(void* const data, const size_t length) { - return fifo->out(reinterpret_cast(data), length); - } - - static FIFO* fifo; - -private: - CaptureConfig* const config; -}; - class CaptureThread { public: CaptureThread( std::unique_ptr writer, size_t write_size_log2, size_t buffer_count_log2 - ) : config { write_size_log2, buffer_count_log2 }, - writer { std::move(writer) } - { - // Need significant stack for FATFS - thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this); - } - - ~CaptureThread() { - if( thread ) { - chThdTerminate(thread); - chEvtSignal(thread, EVT_MASK_CAPTURE_THREAD); - const auto success = chThdWait(thread); - thread = nullptr; - - if( !success ) { - led_tx.on(); - } - } - } + ); + ~CaptureThread(); const CaptureConfig& state() const { return config; } - static void check_fifo_isr() { - // TODO: Prevent over-signalling by transmitting a set of - // flags from the baseband core. - const auto fifo = StreamOutput::fifo; - if( fifo ) { - chEvtSignalI(thread, EVT_MASK_CAPTURE_THREAD); - } - } + static void check_fifo_isr(); private: CaptureConfig config; @@ -123,44 +61,7 @@ private: return obj->run(); } - msg_t run() { - const size_t write_size = 1U << config.write_size_log2; - const auto write_buffer = std::make_unique(write_size); - if( !write_buffer ) { - return false; - } - - StreamOutput stream { &config }; - - while( !chThdShouldTerminate() ) { - if( stream.available() >= write_size ) { - if( !transfer(stream, write_buffer.get(), write_size) ) { - return false; - } - } else { - chEvtWaitAny(EVT_MASK_CAPTURE_THREAD); - } - } - - return true; - } - - bool transfer(StreamOutput& stream, uint8_t* const write_buffer, const size_t write_size) { - bool success = false; - - led_usb.on(); - - const auto bytes_to_write = stream.read(write_buffer, write_size); - if( bytes_to_write == write_size ) { - if( writer->write(write_buffer, write_size) ) { - success = true; - } - } - - led_usb.off(); - - return success; - } + msg_t run(); }; #endif/*__CAPTURE_THREAD_H__*/