mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-05-30 04:01:45 -04:00
Extract BufferExchange, simplify threading.
This commit is contained in:
parent
84334ef818
commit
5dfb53263a
6 changed files with 161 additions and 81 deletions
|
@ -174,6 +174,7 @@ set(CPPSRC
|
||||||
file.cpp
|
file.cpp
|
||||||
log_file.cpp
|
log_file.cpp
|
||||||
${COMMON}/png_writer.cpp
|
${COMMON}/png_writer.cpp
|
||||||
|
${COMMON}/buffer_exchange.cpp
|
||||||
capture_thread.cpp
|
capture_thread.cpp
|
||||||
io_file.cpp
|
io_file.cpp
|
||||||
io_wave.cpp
|
io_wave.cpp
|
||||||
|
|
|
@ -22,67 +22,20 @@
|
||||||
#include "capture_thread.hpp"
|
#include "capture_thread.hpp"
|
||||||
|
|
||||||
#include "baseband_api.hpp"
|
#include "baseband_api.hpp"
|
||||||
|
#include "buffer_exchange.hpp"
|
||||||
|
|
||||||
// BufferExchange ///////////////////////////////////////////////////////////
|
struct BasebandCapture {
|
||||||
|
BasebandCapture(CaptureConfig* const config) {
|
||||||
class BufferExchange {
|
baseband::capture_start(config);
|
||||||
public:
|
|
||||||
BufferExchange(CaptureConfig* const config);
|
|
||||||
~BufferExchange();
|
|
||||||
|
|
||||||
#if defined(LPC43XX_M0)
|
|
||||||
StreamBuffer* get() {
|
|
||||||
StreamBuffer* p { nullptr };
|
|
||||||
fifo_buffers_for_application->out(p);
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool put(StreamBuffer* const p) {
|
~BasebandCapture() {
|
||||||
return fifo_buffers_for_baseband->in(p);
|
baseband::capture_stop();
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(LPC43XX_M4)
|
|
||||||
StreamBuffer* get() {
|
|
||||||
StreamBuffer* p { nullptr };
|
|
||||||
fifo_buffers_for_baseband->out(p);
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool put(StreamBuffer* const p) {
|
|
||||||
return fifo_buffers_for_application->in(p);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static FIFO<StreamBuffer*>* fifo_buffers_for_baseband;
|
|
||||||
static FIFO<StreamBuffer*>* fifo_buffers_for_application;
|
|
||||||
|
|
||||||
private:
|
|
||||||
CaptureConfig* const config;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
FIFO<StreamBuffer*>* BufferExchange::fifo_buffers_for_baseband = nullptr;
|
|
||||||
FIFO<StreamBuffer*>* BufferExchange::fifo_buffers_for_application = nullptr;
|
|
||||||
|
|
||||||
BufferExchange::BufferExchange(
|
|
||||||
CaptureConfig* const config
|
|
||||||
) : config { config }
|
|
||||||
{
|
|
||||||
baseband::capture_start(config);
|
|
||||||
fifo_buffers_for_baseband = config->fifo_buffers_empty;
|
|
||||||
fifo_buffers_for_application = config->fifo_buffers_full;
|
|
||||||
}
|
|
||||||
|
|
||||||
BufferExchange::~BufferExchange() {
|
|
||||||
fifo_buffers_for_baseband = nullptr;
|
|
||||||
fifo_buffers_for_application = nullptr;
|
|
||||||
baseband::capture_stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
// CaptureThread //////////////////////////////////////////////////////////
|
// CaptureThread //////////////////////////////////////////////////////////
|
||||||
|
|
||||||
Thread* CaptureThread::thread = nullptr;
|
|
||||||
|
|
||||||
CaptureThread::CaptureThread(
|
CaptureThread::CaptureThread(
|
||||||
std::unique_ptr<stream::Writer> writer,
|
std::unique_ptr<stream::Writer> writer,
|
||||||
size_t write_size,
|
size_t write_size,
|
||||||
|
@ -101,23 +54,11 @@ CaptureThread::CaptureThread(
|
||||||
CaptureThread::~CaptureThread() {
|
CaptureThread::~CaptureThread() {
|
||||||
if( thread ) {
|
if( thread ) {
|
||||||
chThdTerminate(thread);
|
chThdTerminate(thread);
|
||||||
chEvtSignal(thread, event_mask_loop_wake);
|
|
||||||
chThdWait(thread);
|
chThdWait(thread);
|
||||||
thread = nullptr;
|
thread = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CaptureThread::check_fifo_isr() {
|
|
||||||
// TODO: Prevent over-signalling by transmitting a set of
|
|
||||||
// flags from the baseband core.
|
|
||||||
const auto fifo = BufferExchange::fifo_buffers_for_application;
|
|
||||||
if( fifo ) {
|
|
||||||
if( !fifo->is_empty() ) {
|
|
||||||
chEvtSignalI(thread, event_mask_loop_wake);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
msg_t CaptureThread::static_fn(void* arg) {
|
msg_t CaptureThread::static_fn(void* arg) {
|
||||||
auto obj = static_cast<CaptureThread*>(arg);
|
auto obj = static_cast<CaptureThread*>(arg);
|
||||||
const auto error = obj->run();
|
const auto error = obj->run();
|
||||||
|
@ -132,20 +73,17 @@ msg_t CaptureThread::static_fn(void* arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<File::Error> CaptureThread::run() {
|
Optional<File::Error> CaptureThread::run() {
|
||||||
|
BasebandCapture capture { &config };
|
||||||
BufferExchange buffers { &config };
|
BufferExchange buffers { &config };
|
||||||
|
|
||||||
while( !chThdShouldTerminate() ) {
|
while( !chThdShouldTerminate() ) {
|
||||||
auto buffer = buffers.get();
|
auto buffer = buffers.get();
|
||||||
if( buffer ) {
|
auto write_result = writer->write(buffer->data(), buffer->size());
|
||||||
auto write_result = writer->write(buffer->data(), buffer->size());
|
if( write_result.is_error() ) {
|
||||||
if( write_result.is_error() ) {
|
return write_result.error();
|
||||||
return write_result.error();
|
|
||||||
}
|
|
||||||
buffer->empty();
|
|
||||||
buffers.put(buffer);
|
|
||||||
} else {
|
|
||||||
chEvtWaitAny(event_mask_loop_wake);
|
|
||||||
}
|
}
|
||||||
|
buffer->empty();
|
||||||
|
buffers.put(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { };
|
return { };
|
||||||
|
|
|
@ -48,16 +48,12 @@ public:
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check_fifo_isr();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr auto event_mask_loop_wake = EVENT_MASK(0);
|
|
||||||
|
|
||||||
CaptureConfig config;
|
CaptureConfig config;
|
||||||
std::unique_ptr<stream::Writer> writer;
|
std::unique_ptr<stream::Writer> writer;
|
||||||
std::function<void()> success_callback;
|
std::function<void()> success_callback;
|
||||||
std::function<void(File::Error)> error_callback;
|
std::function<void(File::Error)> error_callback;
|
||||||
static Thread* thread;
|
Thread* thread;
|
||||||
|
|
||||||
static msg_t static_fn(void* arg);
|
static msg_t static_fn(void* arg);
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@
|
||||||
|
|
||||||
#include "irq_controls.hpp"
|
#include "irq_controls.hpp"
|
||||||
|
|
||||||
#include "capture_thread.hpp"
|
#include "buffer_exchange.hpp"
|
||||||
|
|
||||||
#include "ch.h"
|
#include "ch.h"
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ CH_IRQ_HANDLER(M4Core_IRQHandler) {
|
||||||
CH_IRQ_PROLOGUE();
|
CH_IRQ_PROLOGUE();
|
||||||
|
|
||||||
chSysLockFromIsr();
|
chSysLockFromIsr();
|
||||||
CaptureThread::check_fifo_isr();
|
BufferExchange::handle_isr();
|
||||||
EventDispatcher::check_fifo_isr();
|
EventDispatcher::check_fifo_isr();
|
||||||
chSysUnlockFromIsr();
|
chSysUnlockFromIsr();
|
||||||
|
|
||||||
|
|
54
firmware/common/buffer_exchange.cpp
Normal file
54
firmware/common/buffer_exchange.cpp
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
|
||||||
|
*
|
||||||
|
* This file is part of PortaPack.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2, or (at your option)
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; see the file COPYING. If not, write to
|
||||||
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||||
|
* Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "buffer_exchange.hpp"
|
||||||
|
|
||||||
|
BufferExchange* BufferExchange::obj { nullptr };
|
||||||
|
|
||||||
|
BufferExchange::BufferExchange(
|
||||||
|
CaptureConfig* const config
|
||||||
|
) : config { config }
|
||||||
|
{
|
||||||
|
obj = this;
|
||||||
|
fifo_buffers_for_baseband = config->fifo_buffers_empty;
|
||||||
|
fifo_buffers_for_application = config->fifo_buffers_full;
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferExchange::~BufferExchange() {
|
||||||
|
obj = nullptr;
|
||||||
|
fifo_buffers_for_baseband = nullptr;
|
||||||
|
fifo_buffers_for_application = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamBuffer* BufferExchange::get(FIFO<StreamBuffer*>* fifo) {
|
||||||
|
while(true) {
|
||||||
|
StreamBuffer* p { nullptr };
|
||||||
|
fifo->out(p);
|
||||||
|
if( p ) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
chSysLock();
|
||||||
|
thread = chThdSelf();
|
||||||
|
chSchGoSleepS(THD_STATE_SUSPENDED);
|
||||||
|
chSysUnlock();
|
||||||
|
}
|
||||||
|
}
|
91
firmware/common/buffer_exchange.hpp
Normal file
91
firmware/common/buffer_exchange.hpp
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
|
||||||
|
*
|
||||||
|
* This file is part of PortaPack.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2, or (at your option)
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; see the file COPYING. If not, write to
|
||||||
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
||||||
|
* Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "ch.h"
|
||||||
|
|
||||||
|
#include "message.hpp"
|
||||||
|
#include "fifo.hpp"
|
||||||
|
#include "io.hpp"
|
||||||
|
|
||||||
|
class BufferExchange {
|
||||||
|
public:
|
||||||
|
BufferExchange(CaptureConfig* const config);
|
||||||
|
~BufferExchange();
|
||||||
|
|
||||||
|
#if defined(LPC43XX_M0)
|
||||||
|
bool empty() const {
|
||||||
|
return fifo_buffers_for_application->is_empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamBuffer* get() {
|
||||||
|
return get(fifo_buffers_for_application);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool put(StreamBuffer* const p) {
|
||||||
|
return fifo_buffers_for_baseband->in(p);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(LPC43XX_M4)
|
||||||
|
bool empty() const {
|
||||||
|
return fifo_buffers_for_baseband->is_empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamBuffer* get() {
|
||||||
|
return get(fifo_buffers_for_baseband);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool put(StreamBuffer* const p) {
|
||||||
|
return fifo_buffers_for_application->in(p);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void handle_isr() {
|
||||||
|
if( obj ) {
|
||||||
|
obj->check_fifo_isr();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
CaptureConfig* const config;
|
||||||
|
FIFO<StreamBuffer*>* fifo_buffers_for_baseband;
|
||||||
|
FIFO<StreamBuffer*>* fifo_buffers_for_application;
|
||||||
|
Thread* thread;
|
||||||
|
static BufferExchange* obj;
|
||||||
|
|
||||||
|
void check_fifo_isr() {
|
||||||
|
if( !empty() ) {
|
||||||
|
wakeup_isr();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void wakeup_isr() {
|
||||||
|
auto thread_tmp = thread;
|
||||||
|
if( thread_tmp ) {
|
||||||
|
thread = nullptr;
|
||||||
|
chSchReadyI(thread_tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
StreamBuffer* get(FIFO<StreamBuffer*>* fifo);
|
||||||
|
};
|
Loading…
Add table
Add a link
Reference in a new issue