Save memory on RX, use it for LO

This commit is contained in:
Jan Hamal Dvořák 2024-08-03 00:07:38 +02:00
parent 5868a1ade9
commit 82c1c12195

View file

@ -39,12 +39,14 @@
#define IQ_BLOCK_LEN (2 * IQ_SAMPLES) #define IQ_BLOCK_LEN (2 * IQ_SAMPLES)
#define IQ_QUEUE_LEN 8 #define IQ_QUEUE_LEN 8
#define LO_NUM_PHASES 64 #define LO_NUM_PHASES (1 << 6)
#define LO_PHASE_BITS 10 #define LO_PHASE_BITS 10
#define LO_PHASE_WORDS (1 << (LO_PHASE_BITS - 2)) #define LO_PHASE_WORDS (1 << (LO_PHASE_BITS - 2))
#define LO_COS_PHASES 4096 #define LO_COS_PHASES (1 << 14)
#define LO_EFFECTIVE_BITS (32 * LO_PHASE_WORDS * LO_COS_PHASES) #define LO_EFFECTIVE_BITS (32 * LO_PHASE_WORDS * LO_COS_PHASES)
const double step_hz = (double)CLK_SYS_HZ / (LO_EFFECTIVE_BITS / 2.0);
static uint32_t lo_phase[LO_NUM_PHASES][LO_PHASE_WORDS] static uint32_t lo_phase[LO_NUM_PHASES][LO_PHASE_WORDS]
__attribute__((__aligned__(LO_NUM_PHASES * 4 * LO_PHASE_WORDS))); __attribute__((__aligned__(LO_NUM_PHASES * 4 * LO_PHASE_WORDS)));
@ -52,14 +54,14 @@ static const uint32_t *lo_cos_phases[LO_COS_PHASES]
__attribute__((__aligned__(1 << LO_PHASE_BITS))); __attribute__((__aligned__(1 << LO_PHASE_BITS)));
#define DECIMATE 16 #define DECIMATE 16
#define RX_STRIDE (2 * IQ_SAMPLES * DECIMATE) #define RX_BITS_DEPTH 10
#define RX_BITS_DEPTH 14
#define RX_WORDS (1 << (RX_BITS_DEPTH - 2)) #define RX_WORDS (1 << (RX_BITS_DEPTH - 2))
static_assert(RX_STRIDE * 4 <= RX_WORDS, "RX_STRIDE * 4 <= RX_WORDS");
static uint32_t rx_cos[RX_WORDS] __attribute__((__aligned__(1 << RX_BITS_DEPTH))); static uint32_t rx_cos[RX_WORDS] __attribute__((__aligned__(1 << RX_BITS_DEPTH)));
static const uint32_t *rx_start = rx_cos;
static const uint32_t *rx_end = rx_cos + RX_WORDS - 1;
#define INIT_SAMPLE_RATE 100000 #define INIT_SAMPLE_RATE 100000
#define INIT_FREQ 94600000 #define INIT_FREQ 94600000
#define INIT_GAIN 127 #define INIT_GAIN 127
@ -294,7 +296,6 @@ static void lo_generate_phase(uint32_t *buf, size_t len, uint32_t step, uint32_t
static void rx_lo_init(double req_freq, bool align) static void rx_lo_init(double req_freq, bool align)
{ {
const double step_hz = (double)CLK_SYS_HZ / (LO_EFFECTIVE_BITS / 2.0);
double freq = req_freq; double freq = req_freq;
if (align) if (align)
@ -418,74 +419,104 @@ static void rf_rx_stop(void)
dma_channel_unclaim(dma_ch_mix2); dma_channel_unclaim(dma_ch_mix2);
dma_channel_unclaim(dma_ch_samp_cos); dma_channel_unclaim(dma_ch_samp_cos);
dma_timer_unclaim(dma_t_samp);
dma_ch_rx1 = -1; dma_ch_rx1 = -1;
dma_ch_rx2 = -1; dma_ch_rx2 = -1;
dma_ch_mix1 = -1; dma_ch_mix1 = -1;
dma_ch_mix2 = -1; dma_ch_mix2 = -1;
dma_ch_samp_cos = -1; dma_ch_samp_cos = -1;
dma_t_samp = -1;
} }
struct IQ { struct IQ {
int I, Q; int I, Q;
}; };
inline static struct IQ next_sample(const uint32_t *buf) inline static int get_next_sample()
{ {
int x15 = gain * (-2 * buf[0] - buf[1] - dc_level); static const uint32_t *tail = rx_cos;
int x14 = gain * (-2 * buf[2] - buf[3] - dc_level);
int x13 = gain * (-2 * buf[4] - buf[5] - dc_level);
int x12 = gain * (-2 * buf[6] - buf[7] - dc_level);
int x11 = gain * (-2 * buf[8] - buf[9] - dc_level);
int x10 = gain * (-2 * buf[10] - buf[11] - dc_level);
int x09 = gain * (-2 * buf[12] - buf[13] - dc_level);
int x08 = gain * (-2 * buf[14] - buf[15] - dc_level);
int x07 = gain * (-2 * buf[16] - buf[17] - dc_level);
int x06 = gain * (-2 * buf[18] - buf[19] - dc_level);
int x05 = gain * (-2 * buf[20] - buf[21] - dc_level);
int x04 = gain * (-2 * buf[22] - buf[23] - dc_level);
int x03 = gain * (-2 * buf[24] - buf[25] - dc_level);
int x02 = gain * (-2 * buf[26] - buf[27] - dc_level);
int x01 = gain * (-2 * buf[28] - buf[29] - dc_level);
int x00 = gain * (-2 * buf[30] - buf[31] - dc_level);
const uint32_t *head = (const uint32_t *)dma_hw->ch[dma_ch_in_cos].write_addr;
while (head == tail) {
asm volatile("nop; nop; nop; nop");
head = (const uint32_t *)dma_hw->ch[dma_ch_in_cos].write_addr;
}
int value = -(*tail++);
value *= 2;
value -= *tail++;
if (tail > rx_end)
tail = rx_start;
return gain * value - dc_level;
}
inline static struct IQ next_sample()
{
int I = 0, Q = 0; int I = 0, Q = 0;
int x15 = get_next_sample();
I += 93 * x15; I += 93 * x15;
I += 71 * x14;
I += 39 * x13;
I += 0 * x12;
I += -39 * x11;
I += -71 * x10;
I += -93 * x09;
I += -101 * x08;
I += -93 * x07;
I += -71 * x06;
I += -39 * x05;
I += 0 * x04;
I += 39 * x03;
I += 71 * x02;
I += 93 * x01;
I += 101 * x00;
Q += 39 * x15; Q += 39 * x15;
int x14 = get_next_sample();
I += 71 * x14;
Q += 71 * x14; Q += 71 * x14;
int x13 = get_next_sample();
I += 39 * x13;
Q += 93 * x13; Q += 93 * x13;
int x12 = get_next_sample();
I += 0 * x12;
Q += 101 * x12; Q += 101 * x12;
int x11 = get_next_sample();
I += -39 * x11;
Q += 93 * x11; Q += 93 * x11;
int x10 = get_next_sample();
I += -71 * x10;
Q += 71 * x10; Q += 71 * x10;
int x09 = get_next_sample();
I += -93 * x09;
Q += 39 * x09; Q += 39 * x09;
int x08 = get_next_sample();
I += -101 * x08;
Q += 0 * x08; Q += 0 * x08;
int x07 = get_next_sample();
I += -93 * x07;
Q += -39 * x07; Q += -39 * x07;
int x06 = get_next_sample();
I += -71 * x06;
Q += -71 * x06; Q += -71 * x06;
int x05 = get_next_sample();
I += -39 * x05;
Q += -93 * x05; Q += -93 * x05;
int x04 = get_next_sample();
I += 0 * x04;
Q += -101 * x04; Q += -101 * x04;
int x03 = get_next_sample();
I += 39 * x03;
Q += -93 * x03; Q += -93 * x03;
int x02 = get_next_sample();
I += 71 * x02;
Q += -71 * x02; Q += -71 * x02;
int x01 = get_next_sample();
I += 93 * x01;
Q += -39 * x01; Q += -39 * x01;
int x00 = get_next_sample();
I += 101 * x00;
Q += 0 * x00; Q += 0 * x00;
I /= 1024; I /= 1024;
@ -496,9 +527,6 @@ inline static struct IQ next_sample(const uint32_t *buf)
static void rf_rx(void) static void rf_rx(void)
{ {
const uint32_t base = (uint32_t)rx_cos;
int pos = 0;
while (true) { while (true) {
if (multicore_fifo_rvalid()) { if (multicore_fifo_rvalid()) {
multicore_fifo_pop_blocking(); multicore_fifo_pop_blocking();
@ -506,31 +534,13 @@ static void rf_rx(void)
return; return;
} }
int head = (dma_hw->ch[dma_ch_in_cos].write_addr - base) / 4;
int delta = (head < pos ? head + RX_WORDS : head) - pos;
while (delta < RX_STRIDE) {
int wait = rnd_next() & 63;
for (int i = 0; i < wait; i++)
asm volatile("nop");
head = (dma_hw->ch[dma_ch_in_cos].write_addr - base) / 4;
delta = (head < pos ? head + RX_WORDS : head) - pos;
}
const uint32_t *cos_ptr = rx_cos + pos;
pos = (pos + RX_STRIDE) & (RX_WORDS - 1);
uint8_t *block = iq_queue_buffer[iq_queue_pos]; uint8_t *block = iq_queue_buffer[iq_queue_pos];
uint8_t *blockptr = block; uint8_t *blockptr = block;
for (int i = 0; i < IQ_SAMPLES; i++) { for (int i = 0; i < IQ_SAMPLES; i++) {
struct IQ IQ = next_sample(cos_ptr); struct IQ IQ = next_sample();
int64_t I = IQ.I; int64_t I = IQ.I;
int64_t Q = IQ.Q; int64_t Q = IQ.Q;
cos_ptr += 2 * DECIMATE;
I /= dc_level; I /= dc_level;