diff --git a/src/main.c b/src/main.c index 6c67be0..536e204 100644 --- a/src/main.c +++ b/src/main.c @@ -96,6 +96,14 @@ static int agc = 0; #define PSU_PIN 23 +static uint32_t rnd = 0; + +inline static uint32_t rnd_next() +{ + rnd = rnd * 0x41c64e6d + 12345; + return rnd; +} + static void bias_init(int in_pin, int out_pin) { gpio_disable_pulls(in_pin); @@ -264,7 +272,7 @@ static void adder_init() pio_sm_set_enabled(pio1, 3, true); } -inline static float lo_round_freq(size_t bits, float req_freq) +inline static double lo_round_freq(size_t bits, double req_freq) { const double step_hz = (double)CLK_SYS_HZ / bits; return round(req_freq / step_hz) * step_hz; @@ -272,7 +280,7 @@ inline static float lo_round_freq(size_t bits, float req_freq) static void lo_generate(uint32_t *buf, size_t len, double freq, unsigned phase) { - unsigned step = ((double)UINT_MAX + 1.0) / (double)CLK_SYS_HZ * freq; + unsigned step = (UINT_MAX + 1.0) / CLK_SYS_HZ * freq; unsigned accum = phase; for (size_t i = 0; i < len; i++) { @@ -280,10 +288,7 @@ static void lo_generate(uint32_t *buf, size_t len, double freq, unsigned phase) for (int j = 0; j < 32; j++) { #if LO_DITHER - int n0 = rand() - RAND_MAX / 2; - int n1 = rand() - RAND_MAX / 2; - int noise = (n0 + n1) / 2; - + int noise = (rnd_next() >> 3) - (rnd_next() >> 3); bits |= (accum + noise) >> 31; #else bits |= accum >> 31; @@ -296,9 +301,9 @@ static void lo_generate(uint32_t *buf, size_t len, double freq, unsigned phase) } } -static float rx_lo_init(double req_freq) +static double rx_lo_init(double req_freq) { - float freq = lo_round_freq(LO_WORDS * 32, req_freq); + double freq = lo_round_freq(LO_WORDS * 8, req_freq); lo_generate(lo_cos, LO_WORDS, freq, COS_PHASE); lo_generate(lo_sin, LO_WORDS, freq, SIN_PHASE); @@ -306,10 +311,10 @@ static float rx_lo_init(double req_freq) return freq; } -static float tx_fsk_lo_init(float req_freq, float separation) +static double tx_fsk_lo_init(double req_freq, double separation) { - float hi = lo_round_freq(LO_WORDS * 32, req_freq + separation / 2); - float lo = lo_round_freq(LO_WORDS * 32, hi - separation); + double hi = lo_round_freq(LO_WORDS * 8, req_freq + separation / 2); + double lo = lo_round_freq(LO_WORDS * 8, hi - separation); lo_generate(lo_cos, LO_WORDS, hi, COS_PHASE); lo_generate(lo_sin, LO_WORDS, lo, SIN_PHASE); @@ -364,7 +369,7 @@ static const uint32_t samp_insn[] __attribute__((__aligned__(16))) = { static uint32_t null, one = 1; -static float rf_rx_start(int rx_pin, int bias_pin, float freq, int frac_num, int frac_denom) +static double rf_rx_start(int rx_pin, int bias_pin, double freq, int frac_num, int frac_denom) { dma_ch_rx = dma_claim_unused_channel(true); dma_ch_cp = dma_claim_unused_channel(true); @@ -472,7 +477,7 @@ static float rf_rx_start(int rx_pin, int bias_pin, float freq, int frac_num, int bias_init(rx_pin, bias_pin); adder_init(); - float actual = rx_lo_init(freq); + double actual = rx_lo_init(freq); dma_channel_start(dma_ch_rx); dma_channel_start(dma_ch_samp_trig); @@ -782,9 +787,9 @@ static void __unused plot_IQ(int I, int Q) } } -static void do_rx(int rx_pin, int bias_pin, float freq, char mode) +static void do_rx(int rx_pin, int bias_pin, double freq, char mode) { - float actual = rf_rx_start(rx_pin, bias_pin, freq, 1, CLK_SYS_HZ / BANDWIDTH); + double actual = rf_rx_start(rx_pin, bias_pin, freq, 1, CLK_SYS_HZ / BANDWIDTH); sleep_us(100); dma_ch_in_cos = dma_claim_unused_channel(true); @@ -837,8 +842,8 @@ static void do_rx(int rx_pin, int bias_pin, float freq, char mode) fflush(stdout); } else { /* Because AGC is kept 1 bit below to accomodate for jitter. */ - float agc_frac = 2.0f * (float)agc / (float)INT_MAX; - float rssi = 10.0f * log10f(powf(agc_frac, 2)); + double agc_frac = 2.0f * (double)agc / (double)INT_MAX; + double rssi = 10.0f * log10f(powf(agc_frac, 2)); for (int i = 0; i < IQ_BLOCK_LEN / 2; i += 2) { int I = block[i] >> 8; @@ -879,7 +884,7 @@ static void command(const char *cmd) { static char tmp[83]; int n, x; - float f, g; + double f, g; if (1 == sscanf(cmd, " help %[\a]", tmp)) { puts("help - this help"); @@ -912,25 +917,25 @@ static void command(const char *cmd) return; } - if (4 == sscanf(cmd, " rx %i %i %f %[\a]", &n, &x, &f, tmp)) { + if (4 == sscanf(cmd, " rx %i %i %lf %[\a]", &n, &x, &f, tmp)) { do_rx(n, x, f, 'a'); return; } - if (4 == sscanf(cmd, " brx %i %i %f %[\a]", &n, &x, &f, tmp)) { + if (4 == sscanf(cmd, " brx %i %i %lf %[\a]", &n, &x, &f, tmp)) { do_rx(n, x, f, 'b'); return; } - if (3 == sscanf(cmd, " bpsk %i %f %[\a]", &n, &f, tmp)) { - float actual = rx_lo_init(f); + if (3 == sscanf(cmd, " bpsk %i %lf %[\a]", &n, &f, tmp)) { + double actual = rx_lo_init(f); printf("Frequency: %.0f\n", actual); rf_tx_start(n); puts("Transmitting, press ENTER to stop."); bool phase = false; - const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 32); + const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 8); while (true) { int c = getchar_timeout_us(10000); @@ -972,8 +977,8 @@ static void command(const char *cmd) return; } - if (4 == sscanf(cmd, " fsk %i %f %f %[\a]", &n, &f, &g, tmp)) { - g = lo_round_freq(LO_WORDS * 32, g); + if (4 == sscanf(cmd, " fsk %i %lf %lf %[\a]", &n, &f, &g, tmp)) { + g = lo_round_freq(LO_WORDS * 8, g); f = tx_fsk_lo_init(f, g); printf("Frequency: %.0f +/- %.f\n", f, g / 2.0f); @@ -981,7 +986,7 @@ static void command(const char *cmd) puts("Transmitting, press ENTER to stop."); bool high = true; - const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 32); + const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 8); while (true) { int c = getchar_timeout_us(10000); @@ -1024,15 +1029,15 @@ static void command(const char *cmd) return; } - if (3 == sscanf(cmd, " ook %i %f %[\a]", &n, &f, tmp)) { - float actual = rx_lo_init(f); + if (3 == sscanf(cmd, " ook %i %lf %[\a]", &n, &f, tmp)) { + double actual = rx_lo_init(f); printf("Frequency: %.0f\n", actual); rf_tx_start(n); puts("Transmitting, press ENTER to stop."); bool off = false; - const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 32); + const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 8); while (true) { int c = getchar_timeout_us(10000); @@ -1063,12 +1068,12 @@ static void command(const char *cmd) return; } - if (5 == sscanf(cmd, " sweep %i %f %f %i %[\a]", &n, &f, &g, &x, tmp)) { - const float step_hz = (float)CLK_SYS_HZ / (LO_WORDS * 32); - const float start = roundf(f / step_hz) * step_hz; - const float stop = roundf(g / step_hz) * step_hz; + if (5 == sscanf(cmd, " sweep %i %lf %lf %i %[\a]", &n, &f, &g, &x, tmp)) { + const double step_hz = (double)CLK_SYS_HZ / (LO_WORDS * 8); + const double start = round(f / step_hz) * step_hz; + const double stop = round(g / step_hz) * step_hz; - int steps = roundf((stop - start) / step_hz); + int steps = round((stop - start) / step_hz); for (int i = 0; i < LO_WORDS; i++) lo_cos[i] = 0; @@ -1080,7 +1085,7 @@ static void command(const char *cmd) if ('\r' == c) break; - float actual = rx_lo_init(start + i * step_hz); + double actual = rx_lo_init(start + i * step_hz); printf("Frequency: %.0f\n", actual); } @@ -1091,7 +1096,7 @@ static void command(const char *cmd) if (2 == sscanf(cmd, " noise %i %[\a]", &n, tmp)) { for (int i = 0; i < LO_WORDS; i++) - lo_cos[i] = rand(); + lo_cos[i] = rnd_next(); rf_tx_start(n); @@ -1103,7 +1108,7 @@ static void command(const char *cmd) break; for (int i = 0; i < LO_WORDS; i++) - lo_cos[i] = rand(); + lo_cos[i] = rnd_next(); } rf_tx_stop(); @@ -1138,7 +1143,7 @@ int main() } printf("\nPuppet Online!\n"); - printf("clk_sys = %10.6f MHz\n", (float)clock_get_hz(clk_sys) / MHZ); + printf("clk_sys = %10.6f MHz\n", (double)clock_get_hz(clk_sys) / MHZ); queue_init(&iq_queue, IQ_BLOCK_LEN * sizeof(int16_t), 256);