mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2026-08-10 01:43:39 +00:00
66f1835232
* Implement radio transmission drain mechanism and update shared memory structure * don't wait for timeout if baseband already in shut down state
138 lines
4.2 KiB
C++
138 lines
4.2 KiB
C++
/*
|
|
* Copyright (C) 2014 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 "baseband_thread.hpp"
|
|
|
|
#include "dsp_types.hpp"
|
|
|
|
#include "baseband.hpp"
|
|
#include "baseband_sgpio.hpp"
|
|
#include "baseband_dma.hpp"
|
|
|
|
#include "rssi.hpp"
|
|
#include "i2s.hpp"
|
|
using namespace lpc43xx;
|
|
|
|
#include "portapack_shared_memory.hpp"
|
|
|
|
#include "utility.hpp"
|
|
|
|
#include <array>
|
|
|
|
static baseband::SGPIO baseband_sgpio;
|
|
|
|
WORKING_AREA(baseband_thread_wa, 4096);
|
|
|
|
Thread* BasebandThread::thread = nullptr;
|
|
|
|
BasebandThread::BasebandThread(
|
|
uint32_t sampling_rate,
|
|
BasebandProcessor* const baseband_processor,
|
|
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() {
|
|
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;
|
|
}
|
|
|
|
void BasebandThread::run() {
|
|
baseband_sgpio.init();
|
|
baseband::dma::init();
|
|
|
|
const auto baseband_buffer = std::make_unique<std::array<baseband::sample_t, 8192>>();
|
|
baseband::dma::configure(baseband_buffer->data(), direction());
|
|
// baseband::dma::allocate(4, 2048);
|
|
|
|
baseband_sgpio.configure(direction());
|
|
baseband::dma::enable(direction());
|
|
baseband_sgpio.streaming_enable();
|
|
#ifdef PRALINE
|
|
shared_memory.m4_streaming_marker = 0xAA; // Phase 0 instrumentation
|
|
#endif
|
|
uint8_t buffer_drained = 0; // to count how many dma buffers we already emptied
|
|
while (!chThdShouldTerminate()) {
|
|
#ifdef PRALINE
|
|
shared_memory.m4_baseband_loops++; // Phase 0 instrumentation
|
|
#endif
|
|
|
|
// TODO: Place correct sampling rate into buffer returned here:
|
|
const auto buffer_tmp = baseband::dma::wait_for_buffer();
|
|
if (buffer_tmp) {
|
|
buffer_c8_t buffer{
|
|
buffer_tmp.p, buffer_tmp.count, sampling_rate_};
|
|
|
|
if (shared_memory.request_m4_performance_counter == 0x02) {
|
|
uint8_t max = shared_memory.m4_performance_counter;
|
|
for (size_t i = 0; i < buffer_tmp.count; i++) {
|
|
int8_t a = buffer_tmp.p[i].real();
|
|
if (a < 0)
|
|
a = -a;
|
|
|
|
if (a > max)
|
|
max = a;
|
|
}
|
|
|
|
shared_memory.m4_performance_counter = max;
|
|
}
|
|
|
|
if (baseband_processor_) {
|
|
if (shared_memory.radio_tx_drain == 0) { // only generate if not draining.
|
|
baseband_processor_->execute(buffer);
|
|
}
|
|
}
|
|
if (shared_memory.radio_tx_drain == 1) {
|
|
buffer_drained++;
|
|
if (buffer_drained >= 4) { // We have 4 buffers, so after draining 4 we should be safe to disable.
|
|
shared_memory.radio_tx_drain = 0; // Clear the drain request to allow normal operation to resume.
|
|
buffer_drained = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
shared_memory.radio_tx_drain = 0;
|
|
i2s::i2s0::tx_mute();
|
|
baseband::dma::disable();
|
|
baseband_sgpio.streaming_disable();
|
|
}
|