mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-06-12 12:28:35 +00:00
Finish object-izing Baseband and RSSI threads.
This commit is contained in:
parent
4f82524e77
commit
1770dff522
@ -79,75 +79,118 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
|
||||||
constexpr auto baseband_thread_priority = NORMALPRIO + 20;
|
class ThreadBase {
|
||||||
constexpr auto rssi_thread_priority = NORMALPRIO + 10;
|
public:
|
||||||
|
constexpr ThreadBase(
|
||||||
|
const char* const name
|
||||||
|
) : name { name }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static msg_t fn(void* arg) {
|
||||||
|
auto obj = static_cast<ThreadBase*>(arg);
|
||||||
|
chRegSetThreadName(obj->name);
|
||||||
|
obj->run();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void run() = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const char* const name;
|
||||||
|
};
|
||||||
|
|
||||||
|
class BasebandThread : public ThreadBase {
|
||||||
|
public:
|
||||||
|
BasebandThread(
|
||||||
|
) : ThreadBase { "baseband" }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread* start(const tprio_t priority) {
|
||||||
|
return chThdCreateStatic(wa, sizeof(wa),
|
||||||
|
priority, ThreadBase::fn,
|
||||||
|
this
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
struct baseband_thread_data_t {
|
|
||||||
Thread* thread_main { nullptr };
|
Thread* thread_main { nullptr };
|
||||||
Thread* thread_rssi { nullptr };
|
Thread* thread_rssi { nullptr };
|
||||||
BasebandProcessor* baseband_processor { nullptr };
|
BasebandProcessor* baseband_processor { nullptr };
|
||||||
BasebandConfiguration baseband_configuration;
|
BasebandConfiguration baseband_configuration;
|
||||||
};
|
|
||||||
|
|
||||||
static WORKING_AREA(baseband_thread_wa, 8192);
|
private:
|
||||||
static __attribute__((noreturn)) msg_t baseband_fn(void *arg) {
|
WORKING_AREA(wa, 2048);
|
||||||
auto thread_data = static_cast<const baseband_thread_data_t*>(arg);
|
|
||||||
chRegSetThreadName("baseband");
|
|
||||||
|
|
||||||
BasebandStatsCollector stats {
|
void run() override {
|
||||||
chSysGetIdleThread(),
|
BasebandStatsCollector stats {
|
||||||
thread_data->thread_main,
|
chSysGetIdleThread(),
|
||||||
thread_data->thread_rssi,
|
thread_main,
|
||||||
chThdSelf()
|
thread_rssi,
|
||||||
};
|
chThdSelf()
|
||||||
|
|
||||||
while(true) {
|
|
||||||
// TODO: Place correct sampling rate into buffer returned here:
|
|
||||||
const auto buffer_tmp = baseband::dma::wait_for_rx_buffer();
|
|
||||||
const buffer_c8_t buffer {
|
|
||||||
buffer_tmp.p, buffer_tmp.count, thread_data->baseband_configuration.sampling_rate
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if( thread_data->baseband_processor ) {
|
while(true) {
|
||||||
thread_data->baseband_processor->execute(buffer);
|
// TODO: Place correct sampling rate into buffer returned here:
|
||||||
|
const auto buffer_tmp = baseband::dma::wait_for_rx_buffer();
|
||||||
|
const buffer_c8_t buffer {
|
||||||
|
buffer_tmp.p, buffer_tmp.count, baseband_configuration.sampling_rate
|
||||||
|
};
|
||||||
|
|
||||||
|
if( baseband_processor ) {
|
||||||
|
baseband_processor->execute(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
stats.process(buffer,
|
||||||
|
[](const BasebandStatistics statistics) {
|
||||||
|
const BasebandStatisticsMessage message { statistics };
|
||||||
|
shared_memory.application_queue.push(message);
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.process(buffer,
|
|
||||||
[](const BasebandStatistics statistics) {
|
|
||||||
const BasebandStatisticsMessage message { statistics };
|
|
||||||
shared_memory.application_queue.push(message);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
struct rssi_thread_data_t {
|
|
||||||
uint32_t sampling_rate;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static WORKING_AREA(rssi_thread_wa, 128);
|
class RSSIThread : public ThreadBase {
|
||||||
static __attribute__((noreturn)) msg_t rssi_fn(void *arg) {
|
public:
|
||||||
auto thread_data = static_cast<const rssi_thread_data_t*>(arg);
|
RSSIThread(
|
||||||
chRegSetThreadName("rssi");
|
) : ThreadBase { "rssi" }
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
RSSIStatisticsCollector stats;
|
Thread* start(const tprio_t priority) {
|
||||||
|
return chThdCreateStatic(wa, sizeof(wa),
|
||||||
while(true) {
|
priority, ThreadBase::fn,
|
||||||
// TODO: Place correct sampling rate into buffer returned here:
|
this
|
||||||
const auto buffer_tmp = rf::rssi::dma::wait_for_buffer();
|
|
||||||
const rf::rssi::buffer_t buffer {
|
|
||||||
buffer_tmp.p, buffer_tmp.count, thread_data->sampling_rate
|
|
||||||
};
|
|
||||||
|
|
||||||
stats.process(
|
|
||||||
buffer,
|
|
||||||
[](const RSSIStatistics statistics) {
|
|
||||||
const RSSIStatisticsMessage message { statistics };
|
|
||||||
shared_memory.application_queue.push(message);
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
uint32_t sampling_rate { 400000 };
|
||||||
|
|
||||||
|
private:
|
||||||
|
WORKING_AREA(wa, 128);
|
||||||
|
|
||||||
|
void run() override {
|
||||||
|
RSSIStatisticsCollector stats;
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
// 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 {
|
||||||
|
buffer_tmp.p, buffer_tmp.count, sampling_rate
|
||||||
|
};
|
||||||
|
|
||||||
|
stats.process(
|
||||||
|
buffer,
|
||||||
|
[](const RSSIStatistics statistics) {
|
||||||
|
const RSSIStatisticsMessage message { statistics };
|
||||||
|
shared_memory.application_queue.push(message);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
@ -170,8 +213,8 @@ void __late_init(void) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static baseband_thread_data_t baseband_thread_data;
|
static BasebandThread baseband_thread;
|
||||||
static rssi_thread_data_t rssi_thread_data { 400000 };
|
static RSSIThread rssi_thread;
|
||||||
|
|
||||||
static void init() {
|
static void init() {
|
||||||
i2s::i2s0::configure(
|
i2s::i2s0::configure(
|
||||||
@ -198,18 +241,12 @@ static void init() {
|
|||||||
|
|
||||||
const auto thread_main = chThdSelf();
|
const auto thread_main = chThdSelf();
|
||||||
|
|
||||||
const auto thread_rssi = chThdCreateStatic(rssi_thread_wa, sizeof(rssi_thread_wa),
|
const auto thread_rssi = rssi_thread.start(NORMALPRIO + 10);
|
||||||
rssi_thread_priority, rssi_fn,
|
|
||||||
&rssi_thread_data
|
|
||||||
);
|
|
||||||
|
|
||||||
baseband_thread_data.thread_main = thread_main;
|
baseband_thread.thread_main = thread_main;
|
||||||
baseband_thread_data.thread_rssi = thread_rssi;
|
baseband_thread.thread_rssi = thread_rssi;
|
||||||
|
|
||||||
chThdCreateStatic(baseband_thread_wa, sizeof(baseband_thread_wa),
|
baseband_thread.start(NORMALPRIO + 20);
|
||||||
baseband_thread_priority, baseband_fn,
|
|
||||||
&baseband_thread_data
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void shutdown() {
|
static void shutdown() {
|
||||||
@ -275,8 +312,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handle_spectrum() {
|
void handle_spectrum() {
|
||||||
if( baseband_thread_data.baseband_processor ) {
|
if( baseband_thread.baseband_processor ) {
|
||||||
baseband_thread_data.baseband_processor->update_spectrum();
|
baseband_thread.baseband_processor->update_spectrum();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -295,49 +332,49 @@ int main(void) {
|
|||||||
message_handlers.register_handler(Message::ID::BasebandConfiguration,
|
message_handlers.register_handler(Message::ID::BasebandConfiguration,
|
||||||
[&message_handlers](const Message* const p) {
|
[&message_handlers](const Message* const p) {
|
||||||
auto message = reinterpret_cast<const BasebandConfigurationMessage*>(p);
|
auto message = reinterpret_cast<const BasebandConfigurationMessage*>(p);
|
||||||
if( message->configuration.mode != baseband_thread_data.baseband_configuration.mode ) {
|
if( message->configuration.mode != baseband_thread.baseband_configuration.mode ) {
|
||||||
|
|
||||||
if( baseband_thread_data.baseband_processor ) {
|
if( baseband_thread.baseband_processor ) {
|
||||||
i2s::i2s0::tx_mute();
|
i2s::i2s0::tx_mute();
|
||||||
baseband::dma::disable();
|
baseband::dma::disable();
|
||||||
rf::rssi::stop();
|
rf::rssi::stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Timing problem around disabling DMA and nulling and deleting old processor
|
// TODO: Timing problem around disabling DMA and nulling and deleting old processor
|
||||||
auto old_p = baseband_thread_data.baseband_processor;
|
auto old_p = baseband_thread.baseband_processor;
|
||||||
baseband_thread_data.baseband_processor = nullptr;
|
baseband_thread.baseband_processor = nullptr;
|
||||||
delete old_p;
|
delete old_p;
|
||||||
|
|
||||||
switch(message->configuration.mode) {
|
switch(message->configuration.mode) {
|
||||||
case 0:
|
case 0:
|
||||||
baseband_thread_data.baseband_processor = new NarrowbandAMAudio();
|
baseband_thread.baseband_processor = new NarrowbandAMAudio();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
baseband_thread_data.baseband_processor = new NarrowbandFMAudio();
|
baseband_thread.baseband_processor = new NarrowbandFMAudio();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
baseband_thread_data.baseband_processor = new WidebandFMAudio();
|
baseband_thread.baseband_processor = new WidebandFMAudio();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
baseband_thread_data.baseband_processor = new AISProcessor();
|
baseband_thread.baseband_processor = new AISProcessor();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 4:
|
case 4:
|
||||||
baseband_thread_data.baseband_processor = new WidebandSpectrum();
|
baseband_thread.baseband_processor = new WidebandSpectrum();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 5:
|
case 5:
|
||||||
baseband_thread_data.baseband_processor = new TPMSProcessor();
|
baseband_thread.baseband_processor = new TPMSProcessor();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( baseband_thread_data.baseband_processor ) {
|
if( baseband_thread.baseband_processor ) {
|
||||||
if( direction == baseband::Direction::Receive ) {
|
if( direction == baseband::Direction::Receive ) {
|
||||||
rf::rssi::start();
|
rf::rssi::start();
|
||||||
}
|
}
|
||||||
@ -345,7 +382,7 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
baseband_thread_data.baseband_configuration = message->configuration;
|
baseband_thread.baseband_configuration = message->configuration;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user