Fix DC offset removal, simplify...

This commit is contained in:
Jan Hamal Dvořák 2025-08-30 20:33:37 +02:00
parent 5f264e023d
commit 4b9f377bb0
Signed by: mordae
GPG key ID: 1782BCC23EE007B9

View file

@ -21,8 +21,8 @@
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#define VREG_VOLTAGE VREG_VOLTAGE_1_20 #define VREG_VOLTAGE VREG_VOLTAGE_1_30
#define CLK_SYS_HZ (320 * MHZ) #define CLK_SYS_HZ (300 * MHZ)
#define RX_PIN 10 #define RX_PIN 10
#define FB_PIN 6 #define FB_PIN 6
@ -55,9 +55,9 @@ static uint32_t lo_sin[LO_WORDS] __attribute__((__aligned__(1 << LO_BITS_DEPTH))
static int frequency = INIT_FREQ; static int frequency = INIT_FREQ;
static int sample_rate = INIT_SAMPLE_RATE; static int sample_rate = INIT_SAMPLE_RATE;
#define BASE_GAIN (1 << 15) #define ATTN_BITS 16
#define ATTN_BITS 12 #define BASE_GAIN (1 << 23)
#define DECIMATE 4 #define DC_OFFSET (int)(127.4 * (1 << ATTN_BITS))
static int gain = BASE_GAIN / (CLK_SYS_HZ / INIT_SAMPLE_RATE); static int gain = BASE_GAIN / (CLK_SYS_HZ / INIT_SAMPLE_RATE);
@ -108,7 +108,7 @@ static void init_rx()
pio_gpio_init(PIO, RX_PIN); pio_gpio_init(PIO, RX_PIN);
const uint16_t insn[] = { const uint16_t insn[] = {
pio_encode_in(pio_pins, 1) | pio_encode_delay(0), pio_encode_in(pio_pins, 1),
}; };
pio_program_t prog = { pio_program_t prog = {
@ -143,16 +143,8 @@ static void init_bias()
gpio_set_slew_rate(FB_PIN, GPIO_SLEW_RATE_SLOW); gpio_set_slew_rate(FB_PIN, GPIO_SLEW_RATE_SLOW);
const uint16_t insn[] = { const uint16_t insn[] = {
pio_encode_mov(pio_isr, pio_null), pio_encode_mov_not(pio_pins, pio_pins) | pio_encode_sideset(1, 1) |
pio_encode_in(pio_y, 4), pio_encode_delay(0),
pio_encode_in(pio_pins, 1) | pio_encode_delay(15),
pio_encode_in(pio_pins, 1) | pio_encode_delay(15),
pio_encode_mov(pio_y, pio_isr),
pio_encode_mov(pio_x, pio_isr),
pio_encode_jmp_x_dec(6),
pio_encode_mov_not(pio_pins, pio_pins) | pio_encode_sideset(1, 1),
}; };
pio_program_t prog = { pio_program_t prog = {
@ -426,92 +418,50 @@ inline static void led_set(bool on)
gpio_put(PICO_DEFAULT_LED_PIN, on); gpio_put(PICO_DEFAULT_LED_PIN, on);
} }
inline static uint32_t pio_sm_get_blocking_unsafe(pio_hw_t *pio, int sm)
{
while (pio->fstat & (1u << (PIO_FSTAT_RXEMPTY_LSB + sm)))
asm volatile("nop");
return pio->rxf[sm];
}
static int Ia1, Ia2, Ia3;
static int Ib1, Ib2, Ib3;
static int Qa1, Qa2, Qa3;
static int Qb1, Qb2, Qb3;
inline static void accI()
{
static uint16_t py, px;
uint32_t yx = pio_sm_get_blocking_unsafe(PIO, SM_COS);
uint16_t y = yx >> 16;
uint16_t x = yx;
uint16_t ny = py - y;
uint16_t nx = px - x;
py = y;
px = x;
uint32_t s = (ny << 1) + nx;
Ia1 += s;
Ia2 += Ia1;
Ia3 += Ia2;
}
inline static void accQ()
{
static uint16_t py, px;
uint32_t yx = pio_sm_get_blocking_unsafe(PIO, SM_SIN);
uint16_t y = yx >> 16;
uint16_t x = yx;
uint16_t ny = py - y;
uint16_t nx = px - x;
py = y;
px = x;
uint32_t s = (ny << 1) + nx;
Qa1 += s;
Qa2 += Qa1;
Qa3 += Qa2;
}
inline static int getI() inline static int getI()
{ {
uint32_t c1, c2, c3; static uint16_t py, px;
c3 = Ia3 - Ib3; uint32_t yx = pio_sm_get_blocking(PIO, SM_COS);
Ib3 = Ia3; uint16_t y = yx >> 16;
uint16_t x = yx;
c2 = c3 - Ib2; uint16_t ny = py - y;
Ib2 = c3; uint16_t nx = px - x;
c1 = c2 - Ib1; py = y;
Ib1 = c2; px = x;
return (int)c1; int s = ((ny << 1) + nx) * gain;
static int dc;
dc += (s - dc) >> ATTN_BITS;
s -= dc;
return (s + DC_OFFSET) >> ATTN_BITS;
} }
inline static int getQ() inline static int getQ()
{ {
uint32_t c1, c2, c3; static uint16_t py, px;
c3 = Qa3 - Qb3; uint32_t yx = pio_sm_get_blocking(PIO, SM_SIN);
Qb3 = Qa3; uint16_t y = yx >> 16;
uint16_t x = yx;
c2 = c3 - Qb2; uint16_t ny = py - y;
Qb2 = c3; uint16_t nx = px - x;
c1 = c2 - Qb1; py = y;
Qb1 = c2; px = x;
return (int)c1; int s = ((ny << 1) + nx) * gain;
static int dc;
dc += (s - dc) >> ATTN_BITS;
s -= dc;
return (s + DC_OFFSET) >> ATTN_BITS;
} }
static void rf_rx(void) static void rf_rx(void)
@ -527,19 +477,21 @@ static void rf_rx(void)
uint8_t *blockptr = block; uint8_t *blockptr = block;
for (int i = 0; i < IQ_SAMPLES; i++) { for (int i = 0; i < IQ_SAMPLES; i++) {
for (int d = 0; d < DECIMATE; d++) { int I = getI();
accI(); int Q = getQ();
accQ();
}
int I = getI() * gain; if (I < 0)
int Q = getQ() * gain; I = 0;
else if (I > 255)
I = 255;
I >>= ATTN_BITS; if (Q < 0)
*blockptr++ = I + 128; Q = 0;
else if (Q > 255)
Q = 255;
Q >>= ATTN_BITS; *blockptr++ = I;
*blockptr++ = Q + 128; *blockptr++ = Q;
} }
if (queue_try_add(&iq_queue, &block)) { if (queue_try_add(&iq_queue, &block)) {
@ -561,7 +513,7 @@ static void run_command(uint8_t cmd, uint32_t arg)
/* Set the rate at which IQ sample pairs are sent */ /* Set the rate at which IQ sample pairs are sent */
sample_rate = arg; sample_rate = arg;
gain = BASE_GAIN / (CLK_SYS_HZ / sample_rate); gain = BASE_GAIN / (CLK_SYS_HZ / sample_rate);
dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / (sample_rate * DECIMATE)); dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / sample_rate);
rx_lo_init(frequency); rx_lo_init(frequency);
} }
} }
@ -613,10 +565,7 @@ static void do_rx()
fwrite(block, IQ_BLOCK_LEN, 1, stdout); fwrite(block, IQ_BLOCK_LEN, 1, stdout);
fflush(stdout); fflush(stdout);
} else { } else {
int wait = xorshift() >> (32 - 15); sleep_us(1);
for (int i = 0; i < wait; i++)
asm volatile("nop");
} }
} }
@ -657,7 +606,7 @@ int main()
/* We need to have the sampling timer ready. */ /* We need to have the sampling timer ready. */
dma_t_samp = dma_claim_unused_timer(true); dma_t_samp = dma_claim_unused_timer(true);
dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / (sample_rate * DECIMATE)); dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / sample_rate);
while (true) { while (true) {
if (check_command() > 0) { if (check_command() > 0) {