Improve bit counting

This commit is contained in:
Jan Hamal Dvořák 2024-07-31 20:53:05 +02:00
parent 0db366b602
commit 2068d3a01f

View file

@ -22,10 +22,10 @@
#include <stdlib.h> #include <stdlib.h>
#define VREG_VOLTAGE VREG_VOLTAGE_1_20 #define VREG_VOLTAGE VREG_VOLTAGE_1_20
#define CLK_SYS_HZ (300 * MHZ) #define CLK_SYS_HZ (288 * MHZ)
#define LO_PIN 9 #define LO_PIN 9
#define RX_PIN 8 #define RX_PIN 13
#define FB_PIN 5 #define FB_PIN 5
#define PSU_PIN 23 #define PSU_PIN 23
@ -69,6 +69,7 @@ static int gains[NUM_GAINS] = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157
166, 197, 207, 229, 254, 280, 297, 328, 338, 364, 166, 197, 207, 229, 254, 280, 297, 328, 338, 364,
372, 386, 402, 421, 434, 439, 445, 480, 496 }; 372, 386, 402, 421, 434, 439, 445, 480, 496 };
static int sample_rate = INIT_SAMPLE_RATE; static int sample_rate = INIT_SAMPLE_RATE;
static int dc_level = CLK_SYS_HZ / INIT_SAMPLE_RATE / 2;
static int gain = INIT_GAIN; static int gain = INIT_GAIN;
static int dma_ch_rx1 = -1; static int dma_ch_rx1 = -1;
@ -108,8 +109,8 @@ static void init_lo()
gpio_disable_pulls(LO_PIN); gpio_disable_pulls(LO_PIN);
pio_gpio_init(PIO, LO_PIN); pio_gpio_init(PIO, LO_PIN);
gpio_set_drive_strength(LO_PIN, GPIO_DRIVE_STRENGTH_2MA); gpio_set_drive_strength(LO_PIN, GPIO_DRIVE_STRENGTH_12MA);
gpio_set_slew_rate(LO_PIN, GPIO_SLEW_RATE_SLOW); gpio_set_slew_rate(LO_PIN, GPIO_SLEW_RATE_FAST);
const uint16_t insn[] = { const uint16_t insn[] = {
pio_encode_out(pio_pindirs, 1), pio_encode_out(pio_pindirs, 1),
@ -218,23 +219,34 @@ static void init_rx()
static void init_ad() static void init_ad()
{ {
const uint16_t insn[] = { const uint16_t insn[] = {
pio_encode_jmp_y_dec(1), pio_encode_out(pio_pc, 4), // 0000 +0
pio_encode_out(pio_pc, 2), pio_encode_jmp_x_dec(0), // 0001 +1
pio_encode_out(pio_pc, 2), pio_encode_jmp_x_dec(0), // 0010 +1
pio_encode_jmp_x_dec(2), pio_encode_jmp_y_dec(0), // 0011 +2
pio_encode_jmp_x_dec(0), // 0100 +1
/* Avoid Y-- on wrap. */ pio_encode_jmp_y_dec(0), // 0101 +2
pio_encode_out(pio_pc, 2), pio_encode_jmp_y_dec(0), // 0110 +2
pio_encode_jmp_y_dec(1), // 0111 +2 +1
pio_encode_jmp_x_dec(0), // 1000 +1
pio_encode_jmp_y_dec(0), // 1001 +2
pio_encode_jmp_y_dec(0), // 1010 +2
pio_encode_jmp_y_dec(1), // 1011 +2 +1
pio_encode_jmp_y_dec(0), // 1100 +2
pio_encode_jmp_y_dec(1), // 1101 +2 +1
pio_encode_jmp_y_dec(1), // 1110 +2 +1
pio_encode_jmp_y_dec(3), // 1111 +2 +2
/* /*
* Should wrap here. * Should wrap here.
* Jump to this portion must be inserted from the outside. * Jump to this portion must be inserted from the outside.
*/ */
pio_encode_in(pio_x, 32),
pio_encode_in(pio_y, 32), pio_encode_in(pio_y, 32),
pio_encode_set(pio_x, 0), pio_encode_in(pio_x, 32),
pio_encode_set(pio_y, 0), pio_encode_set(pio_y, 0),
pio_encode_out(pio_pc, 2), pio_encode_set(pio_x, 0),
pio_encode_jmp_y_dec(21),
pio_encode_jmp_x_dec(22),
pio_encode_out(pio_pc, 4),
}; };
pio_program_t prog = { pio_program_t prog = {
@ -250,11 +262,11 @@ static void init_ad()
pio_add_program(PIO, &prog); pio_add_program(PIO, &prog);
pio_sm_config pc = pio_get_default_sm_config(); pio_sm_config pc = pio_get_default_sm_config();
sm_config_set_wrap(&pc, prog.origin, prog.origin + 5 - 1); sm_config_set_wrap(&pc, prog.origin, prog.origin + 15);
sm_config_set_clkdiv_int_frac(&pc, 1, 0); sm_config_set_clkdiv_int_frac(&pc, 1, 0);
sm_config_set_in_shift(&pc, false, true, 32); sm_config_set_in_shift(&pc, false, true, 32);
sm_config_set_out_shift(&pc, false, true, 32); sm_config_set_out_shift(&pc, false, true, 32);
pio_sm_init(PIO, AD_SM, prog.origin + 1, &pc); pio_sm_init(PIO, AD_SM, prog.origin, &pc);
} }
#define STEP_BASE ((UINT_MAX + 1.0) / CLK_SYS_HZ) #define STEP_BASE ((UINT_MAX + 1.0) / CLK_SYS_HZ)
@ -297,7 +309,7 @@ static void rx_lo_init(double req_freq, bool align)
} }
} }
static const uint32_t samp_insn = 5; static const uint32_t samp_insn = 16;
static void rf_rx_start() static void rf_rx_start()
{ {
@ -363,10 +375,10 @@ static void rf_rx_start()
dma_channel_configure(dma_ch_samp_cos, &dma_conf, &PIO->sm[AD_SM].instr, &samp_insn, dma_channel_configure(dma_ch_samp_cos, &dma_conf, &PIO->sm[AD_SM].instr, &samp_insn,
UINT_MAX, false); UINT_MAX, false);
init_ad();
init_lo(); init_lo();
init_fb(); init_fb();
init_rx(); init_rx();
init_ad();
dma_channel_start(dma_ch_rx1); dma_channel_start(dma_ch_rx1);
dma_channel_start(dma_ch_mix1); dma_channel_start(dma_ch_mix1);
@ -424,10 +436,10 @@ inline static struct IQ next_sample(const uint32_t *buf)
{ {
static int h[11]; static int h[11];
int x3 = buf[0] - buf[1]; int x3 = gain * (-2 * buf[0] - buf[1] - dc_level);
int x2 = buf[2] - buf[3]; int x2 = gain * (-2 * buf[2] - buf[3] - dc_level);
int x1 = buf[5] - buf[4]; int x1 = -(gain * (-2 * buf[4] - buf[5] - dc_level));
int x0 = buf[7] - buf[6]; int x0 = -(gain * (-2 * buf[6] - buf[7] - dc_level));
const int c[] = { 4, 2, -8, -9, 19, 55 }; const int c[] = { 4, 2, -8, -9, 19, 55 };
@ -449,10 +461,7 @@ inline static struct IQ next_sample(const uint32_t *buf)
I += (c[1] + c[0]) * x1; I += (c[1] + c[0]) * x1;
Q += (c[0]) * x0; Q += (c[0]) * x0;
I *= gain;
I /= 128; I /= 128;
Q *= gain;
Q /= 128; Q /= 128;
h[10] = h[6]; h[10] = h[6];
@ -502,23 +511,13 @@ static void rf_rx(void)
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;
/*
* Since every 2 samples add to either +1 or -1,
* the maximum amplitude in one direction is 1/2.
*
* We are allowing the counters to only go as high
* as sampling rate.
*/
int64_t max_amplitude = CLK_SYS_HZ / 2 / sample_rate;
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(cos_ptr);
int64_t I = IQ.I; int64_t I = IQ.I;
int64_t Q = IQ.Q; int64_t Q = IQ.Q;
cos_ptr += 2 * DECIMATE; cos_ptr += 2 * DECIMATE;
I -= (max_amplitude * 181) / 256; I /= dc_level;
I /= max_amplitude;
if (I > 127) if (I > 127)
I = 127; I = 127;
@ -527,8 +526,7 @@ static void rf_rx(void)
*blockptr++ = (uint8_t)I + 128; *blockptr++ = (uint8_t)I + 128;
Q -= (max_amplitude * 181) / 256; Q /= dc_level;
Q /= max_amplitude;
if (Q > 127) if (Q > 127)
Q = 127; Q = 127;
@ -552,6 +550,7 @@ static void run_command(uint8_t cmd, uint32_t arg)
} else if (0x02 == cmd) { } else if (0x02 == cmd) {
/* 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;
dc_level = CLK_SYS_HZ / sample_rate / 2;
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 * DECIMATE));
rx_lo_init(arg + sample_rate, true); rx_lo_init(arg + sample_rate, true);
} else if (0x04 == cmd) { } else if (0x04 == cmd) {