mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-14 11:47:43 +00:00
Nrf24l01 demodulation (#338)
* NRF demodulation * Update ui_navigation.cpp
This commit is contained in:
@@ -311,6 +311,12 @@ set(MODE_CPPSRC
|
||||
proc_afskrx.cpp
|
||||
)
|
||||
DeclareTargets(PAFR afskrx)
|
||||
### NRF RX
|
||||
|
||||
set(MODE_CPPSRC
|
||||
proc_nrfrx.cpp
|
||||
)
|
||||
DeclareTargets(PNRR nrfrx)
|
||||
|
||||
### AIS
|
||||
|
||||
|
286
firmware/baseband/proc_nrfrx.cpp
Normal file
286
firmware/baseband/proc_nrfrx.cpp
Normal file
@@ -0,0 +1,286 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
||||
* Copyright (C) 2016 Furrtek
|
||||
* Copyright (C) 2020 Shao
|
||||
*
|
||||
* 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 "proc_nrfrx.hpp"
|
||||
#include "portapack_shared_memory.hpp"
|
||||
|
||||
#include "event_m4.hpp"
|
||||
|
||||
void NRFRxProcessor::execute(const buffer_c8_t& buffer) {
|
||||
if (!configured) return;
|
||||
|
||||
// FM demodulation
|
||||
|
||||
const auto decim_0_out = decim_0.execute(buffer, dst_buffer);
|
||||
feed_channel_stats(decim_0_out);
|
||||
|
||||
auto audio_oversampled = demod.execute(decim_0_out, work_audio_buffer);
|
||||
// Audio signal processing
|
||||
for (size_t c = 0; c < audio_oversampled.count; c++) {
|
||||
int result;
|
||||
int g_srate = 4; //4 for 250KPS
|
||||
//int g_srate = 1; //1 for 1MPS, not working yet
|
||||
int32_t current_sample = audio_oversampled.p[c]; //if I directly use this, some results can pass crc but not correct.
|
||||
rb_head++;
|
||||
rb_head=(rb_head)%RB_SIZE;
|
||||
|
||||
rb_buf[rb_head] = current_sample;
|
||||
|
||||
skipSamples = skipSamples - 1;
|
||||
|
||||
|
||||
if (skipSamples<1)
|
||||
{
|
||||
|
||||
|
||||
int32_t threshold_tmp=0;
|
||||
for (int c=0;c<8*g_srate;c++)
|
||||
{
|
||||
threshold_tmp = threshold_tmp + (int32_t)rb_buf[(rb_head+c)%RB_SIZE];
|
||||
}
|
||||
|
||||
g_threshold = (int32_t)threshold_tmp/(8*g_srate);
|
||||
|
||||
int transitions=0;
|
||||
if (rb_buf[(rb_head + 9*g_srate)%RB_SIZE] > g_threshold)
|
||||
{
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head + c*g_srate)%RB_SIZE] > rb_buf[(rb_head + (c+1)*g_srate)%RB_SIZE])
|
||||
transitions = transitions + 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head + c*g_srate)%RB_SIZE] < rb_buf[(rb_head + (c+1)*g_srate)%RB_SIZE])
|
||||
transitions = transitions + 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool packet_detected=false;
|
||||
//if ( transitions==4 && abs(g_threshold)<15500)
|
||||
if ( transitions==4 && abs(g_threshold)<15500)
|
||||
{
|
||||
int packet_length = 0;
|
||||
uint8_t tmp_buf[10];
|
||||
uint8_t packet_data[500];
|
||||
uint8_t packet_packed[50];
|
||||
uint16_t pcf;
|
||||
uint32_t packet_crc;
|
||||
uint32_t calced_crc;
|
||||
uint64_t packet_addr_l;
|
||||
|
||||
/* extract address */
|
||||
packet_addr_l=0;
|
||||
|
||||
for (int t=0;t<5;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+(1*8+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
tmp_buf[t]=byte;
|
||||
}
|
||||
|
||||
for (int t=0;t<5;t++) packet_addr_l|=((uint64_t)tmp_buf[t])<<(4-t)*8;
|
||||
|
||||
//channel_number = 26;
|
||||
|
||||
|
||||
/* extract pcf */
|
||||
for (int t=0;t<2;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+(6*8+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
tmp_buf[t]=byte;
|
||||
}
|
||||
|
||||
pcf = tmp_buf[0]<<8 | tmp_buf[1];
|
||||
pcf >>=7;
|
||||
|
||||
/* extract packet length, avoid excessive length packets */
|
||||
if(packet_length == 0)
|
||||
packet_length=(int)pcf>>3;
|
||||
if (packet_length>32)
|
||||
packet_detected = false;
|
||||
|
||||
/* extract data */
|
||||
for (int t=0;t<packet_length;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+(6*8+9+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
packet_data[t]=byte;
|
||||
}
|
||||
|
||||
/* Prepare packed bit stream for CRC calculation */
|
||||
uint64_t packet_header=packet_addr_l;
|
||||
packet_header<<=9;
|
||||
packet_header|=pcf;
|
||||
for (int c=0;c<7;c++){
|
||||
packet_packed[c]=(packet_header>>((6-c)*8))&0xFF;
|
||||
}
|
||||
|
||||
for (int c=0;c<packet_length;c++){
|
||||
packet_packed[c+7]=packet_data[c];
|
||||
}
|
||||
|
||||
/* calculate packet crc */
|
||||
const uint8_t* data = packet_packed;
|
||||
size_t data_len = 7+packet_length;
|
||||
bool bit;
|
||||
uint8_t cc;
|
||||
uint_fast16_t crc=0x3C18;
|
||||
while (data_len--) {
|
||||
cc = *data++;
|
||||
for (uint8_t i = 0x80; i > 0; i >>= 1)
|
||||
{
|
||||
bit = crc & 0x8000;
|
||||
if (cc & i)
|
||||
{
|
||||
bit = !bit;
|
||||
}
|
||||
crc <<= 1;
|
||||
if (bit)
|
||||
{
|
||||
crc ^= 0x1021;
|
||||
}
|
||||
}
|
||||
crc &= 0xffff;
|
||||
}
|
||||
calced_crc = (uint16_t)(crc & 0xffff);
|
||||
|
||||
/* extract crc */
|
||||
for (int t=0;t<2;t++)
|
||||
{
|
||||
bool current_bit;
|
||||
uint8_t byte=0;
|
||||
for (int c=0;c<8;c++)
|
||||
{
|
||||
if (rb_buf[(rb_head+((6+packet_length)*8+9+t*8+c)*g_srate)%RB_SIZE] > g_threshold)
|
||||
current_bit = true;
|
||||
else
|
||||
current_bit = false;
|
||||
byte |= current_bit << (7-c);
|
||||
}
|
||||
tmp_buf[t]=byte;
|
||||
}
|
||||
packet_crc = tmp_buf[0]<<8 | tmp_buf[1];
|
||||
|
||||
/* NRF24L01+ packet found, dump information */
|
||||
//if (packet_addr_l==0xE7E7E7E7)
|
||||
if (packet_crc==calced_crc)
|
||||
{
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'A';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
for (int c=0;c<7;c++)
|
||||
{
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l >> 8;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
}
|
||||
/*data_message.is_data = true;
|
||||
data_message.value = packet_addr_l;
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_addr_l >> 8;
|
||||
shared_memory.application_queue.push(data_message);*/
|
||||
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'B';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
for (int c=0;c<packet_length;c++)
|
||||
{
|
||||
data_message.is_data = true;
|
||||
data_message.value = packet_data[c];
|
||||
shared_memory.application_queue.push(data_message);
|
||||
}
|
||||
|
||||
data_message.is_data = false;
|
||||
data_message.value = 'C';
|
||||
shared_memory.application_queue.push(data_message);
|
||||
|
||||
|
||||
packet_detected = true;
|
||||
}
|
||||
else
|
||||
packet_detected = false;
|
||||
}
|
||||
|
||||
if (packet_detected)
|
||||
{
|
||||
skipSamples=20;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NRFRxProcessor::on_message(const Message* const message) {
|
||||
if (message->id == Message::ID::NRFRxConfigure)
|
||||
configure(*reinterpret_cast<const NRFRxConfigureMessage*>(message));
|
||||
}
|
||||
|
||||
void NRFRxProcessor::configure(const NRFRxConfigureMessage& message) {
|
||||
decim_0.configure(taps_200k_wfm_decim_0.taps, 33554432);
|
||||
decim_1.configure(taps_200k_wfm_decim_1.taps, 131072);
|
||||
demod.configure(audio_fs, 5000);
|
||||
|
||||
configured = true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
EventDispatcher event_dispatcher { std::make_unique<NRFRxProcessor>() };
|
||||
event_dispatcher.run();
|
||||
return 0;
|
||||
}
|
96
firmware/baseband/proc_nrfrx.hpp
Normal file
96
firmware/baseband/proc_nrfrx.hpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
||||
* Copyright (C) 2016 Furrtek
|
||||
* Copyright (C) 2020 Shao
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef __PROC_NRFRX_H__
|
||||
#define __PROC_NRFRX_H__
|
||||
|
||||
#include "baseband_processor.hpp"
|
||||
#include "baseband_thread.hpp"
|
||||
#include "rssi_thread.hpp"
|
||||
|
||||
#include "dsp_decimate.hpp"
|
||||
#include "dsp_demodulate.hpp"
|
||||
|
||||
#include "audio_output.hpp"
|
||||
|
||||
#include "fifo.hpp"
|
||||
#include "message.hpp"
|
||||
|
||||
class NRFRxProcessor : public BasebandProcessor {
|
||||
public:
|
||||
void execute(const buffer_c8_t& buffer) override;
|
||||
|
||||
void on_message(const Message* const message) override;
|
||||
|
||||
private:
|
||||
static constexpr size_t baseband_fs = 4000000;
|
||||
static constexpr size_t audio_fs = baseband_fs / 8 / 8 / 2;
|
||||
|
||||
BasebandThread baseband_thread { baseband_fs, this, NORMALPRIO + 20, baseband::Direction::Receive };
|
||||
RSSIThread rssi_thread { NORMALPRIO + 10 };
|
||||
|
||||
std::array<complex16_t, 512> dst { };
|
||||
const buffer_c16_t dst_buffer {
|
||||
dst.data(),
|
||||
dst.size()
|
||||
};
|
||||
|
||||
std::array<complex16_t, 512> spectrum { };
|
||||
const buffer_c16_t spectrum_buffer {
|
||||
spectrum.data(),
|
||||
spectrum.size()
|
||||
};
|
||||
|
||||
const buffer_s16_t work_audio_buffer {
|
||||
(int16_t*)dst.data(),
|
||||
sizeof(dst) / sizeof(int16_t)
|
||||
};
|
||||
|
||||
|
||||
// Array size ok down to 375 bauds (24000 / 375)
|
||||
std::array<int32_t, 64> delay_line { 0 };
|
||||
std::array<int16_t, 1000> rb_buf { 0 };
|
||||
|
||||
/*dsp::decimate::FIRC8xR16x24FS4Decim8 decim_0 { };
|
||||
dsp::decimate::FIRC16xR16x32Decim8 decim_1 { };
|
||||
dsp::decimate::FIRAndDecimateComplex channel_filter { };*/
|
||||
dsp::decimate::FIRC8xR16x24FS4Decim4 decim_0 { };
|
||||
dsp::decimate::FIRC16xR16x16Decim2 decim_1 { };
|
||||
|
||||
dsp::demodulate::FM demod { };
|
||||
int rb_head {-1};
|
||||
int32_t g_threshold {0};
|
||||
//uint8_t g_srate {8};
|
||||
uint8_t channel_number {38};
|
||||
int skipSamples {1000};
|
||||
int RB_SIZE {1000};
|
||||
|
||||
bool configured { false };
|
||||
|
||||
|
||||
void configure(const NRFRxConfigureMessage& message);
|
||||
|
||||
AFSKDataMessage data_message { false, 0 };
|
||||
};
|
||||
|
||||
#endif/*__PROC_NRFRX_H__*/
|
Reference in New Issue
Block a user