From 1e4846dac3fc51fb84aa54f8d1f95f6388c3ee80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= Date: Thu, 3 Jul 2025 16:12:24 +0200 Subject: [PATCH] Use uint32_t freq --- src/main.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/main.c b/src/main.c index d4cd731..0ef69c2 100644 --- a/src/main.c +++ b/src/main.c @@ -16,7 +16,6 @@ #include #include -#include #include #include #include @@ -225,7 +224,7 @@ static void init_adder() origin_adder = pio_add_program(PIO, &prog); pio_sm_config pc = pio_get_default_sm_config(); - sm_config_set_wrap(&pc, origin_adder, origin_adder + 13); + sm_config_set_wrap(&pc, origin_adder, origin_adder + 15); sm_config_set_clkdiv_int_frac(&pc, 1, 0); sm_config_set_in_shift(&pc, false, true, 32); sm_config_set_out_shift(&pc, false, true, 32); @@ -234,13 +233,11 @@ static void init_adder() pio_sm_init(PIO, SM_SIN, origin_adder, &pc); } -#define STEP_BASE ((UINT_MAX + 1.0) / CLK_SYS_HZ) - static void lo_generate_phase(uint32_t *buf, size_t len, uint32_t step, uint32_t phase) { - for (size_t i = 0; i < len; i++) { - uint32_t bits = 0; + uint32_t bits = 0; + for (size_t i = 0; i < len; i++) { for (int j = 0; j < 32; j++) { bits <<= 1; bits |= phase >> 31; @@ -251,16 +248,12 @@ 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(uint32_t freq) { - const double step_hz = (double)CLK_SYS_HZ / ((8 << LO_BITS_DEPTH) / 2.0); - double freq = req_freq; - - if (align) - freq = round(freq / step_hz) * step_hz; - - uint32_t step = STEP_BASE * freq; - + const uint32_t frac = (8 + 16llu * CLK_SYS_HZ / (8 << LO_BITS_DEPTH)) >> 4; + uint32_t step = ((uint64_t)freq << 32) / (uint64_t)CLK_SYS_HZ; + step /= frac; + step *= frac; lo_generate_phase(lo_cos, LO_WORDS, step, COS_PHASE); lo_generate_phase(lo_sin, LO_WORDS, step, SIN_PHASE); } @@ -535,13 +528,13 @@ static void run_command(uint8_t cmd, uint32_t arg) if (0x01 == cmd) { /* Tune to a new center frequency */ frequency = arg; - rx_lo_init(frequency + sample_rate, true); + rx_lo_init(frequency + sample_rate); } else if (0x02 == cmd) { /* Set the rate at which IQ sample pairs are sent */ sample_rate = arg; gain = BASE_GAIN / (CLK_SYS_HZ / sample_rate / 2); dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / (sample_rate * DECIMATE)); - rx_lo_init(frequency + sample_rate, true); + rx_lo_init(frequency + sample_rate); } } @@ -632,7 +625,7 @@ int main() queue_init(&iq_queue, sizeof(uint8_t *), IQ_QUEUE_LEN); - rx_lo_init(frequency + sample_rate, true); + rx_lo_init(frequency + sample_rate); /* We need to have the sampling timer ready. */ dma_t_samp = dma_claim_unused_timer(true);