Compare commits

...

4 commits

2 changed files with 87 additions and 74 deletions

View file

@ -22,10 +22,10 @@
#include <stdlib.h>
#define VREG_VOLTAGE VREG_VOLTAGE_1_20
#define CLK_SYS_HZ (300 * MHZ)
#define CLK_SYS_HZ (288 * MHZ)
#define LO_PIN 9
#define RX_PIN 8
#define RX_PIN 13
#define FB_PIN 5
#define PSU_PIN 23
@ -51,9 +51,9 @@ static uint32_t lo_phase[LO_NUM_PHASES][LO_PHASE_WORDS]
static const uint32_t *lo_cos_phases[LO_COS_PHASES]
__attribute__((__aligned__(1 << LO_PHASE_BITS)));
#define DECIMATE 4
#define DECIMATE 16
#define RX_STRIDE (2 * IQ_SAMPLES * DECIMATE)
#define RX_BITS_DEPTH 13
#define RX_BITS_DEPTH 14
#define RX_WORDS (1 << (RX_BITS_DEPTH - 2))
static_assert(RX_STRIDE * 4 <= RX_WORDS, "RX_STRIDE * 4 <= RX_WORDS");
@ -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,
372, 386, 402, 421, 434, 439, 445, 480, 496 };
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 dma_ch_rx1 = -1;
@ -108,8 +109,8 @@ static void init_lo()
gpio_disable_pulls(LO_PIN);
pio_gpio_init(PIO, LO_PIN);
gpio_set_drive_strength(LO_PIN, GPIO_DRIVE_STRENGTH_2MA);
gpio_set_slew_rate(LO_PIN, GPIO_SLEW_RATE_SLOW);
gpio_set_drive_strength(LO_PIN, GPIO_DRIVE_STRENGTH_12MA);
gpio_set_slew_rate(LO_PIN, GPIO_SLEW_RATE_FAST);
const uint16_t insn[] = {
pio_encode_out(pio_pindirs, 1),
@ -218,23 +219,34 @@ static void init_rx()
static void init_ad()
{
const uint16_t insn[] = {
pio_encode_jmp_y_dec(1),
pio_encode_out(pio_pc, 2),
pio_encode_out(pio_pc, 2),
pio_encode_jmp_x_dec(2),
/* Avoid Y-- on wrap. */
pio_encode_out(pio_pc, 2),
pio_encode_out(pio_pc, 4), // 0000 +0
pio_encode_jmp_x_dec(0), // 0001 +1
pio_encode_jmp_x_dec(0), // 0010 +1
pio_encode_jmp_y_dec(0), // 0011 +2
pio_encode_jmp_x_dec(0), // 0100 +1
pio_encode_jmp_y_dec(0), // 0101 +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.
* Jump to this portion must be inserted from the outside.
*/
pio_encode_in(pio_x, 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_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 = {
@ -250,11 +262,11 @@ static void init_ad()
pio_add_program(PIO, &prog);
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_in_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)
@ -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()
{
@ -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,
UINT_MAX, false);
init_ad();
init_lo();
init_fb();
init_rx();
init_ad();
dma_channel_start(dma_ch_rx1);
dma_channel_start(dma_ch_mix1);
@ -422,50 +434,61 @@ struct IQ {
inline static struct IQ next_sample(const uint32_t *buf)
{
static int h[11];
int x3 = buf[0] - buf[1];
int x2 = buf[2] - buf[3];
int x1 = buf[5] - buf[4];
int x0 = buf[7] - buf[6];
const int c[] = { 4, 2, -8, -9, 19, 55 };
int x15 = gain * (-2 * buf[0] - buf[1] - dc_level);
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);
int I = 0, Q = 0;
Q += (c[0]) * h[10];
I += (c[0] + c[1]) * h[9];
Q += (c[0] + c[1] + c[2]) * h[8];
I += (c[0] + c[1] + c[2] + c[3]) * h[7];
Q += (c[1] + c[2] + c[3] + c[4]) * h[6];
I += (c[2] + c[3] + c[4] + c[5]) * h[5];
Q += (c[3] + c[4] + c[5] + c[5]) * h[4];
I += (c[4] + c[5] + c[5] + c[4]) * h[3];
Q += (c[5] + c[5] + c[4] + c[3]) * h[2];
I += (c[5] + c[4] + c[3] + c[2]) * h[1];
Q += (c[4] + c[3] + c[2] + c[1]) * h[0];
I += (c[3] + c[2] + c[1] + c[0]) * x3;
Q += (c[2] + c[1] + c[0]) * x2;
I += (c[1] + c[0]) * x1;
Q += (c[0]) * x0;
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;
I *= gain;
I /= 128;
Q += 39 * x15;
Q += 71 * x14;
Q += 93 * x13;
Q += 101 * x12;
Q += 93 * x11;
Q += 71 * x10;
Q += 39 * x09;
Q += 0 * x08;
Q += -39 * x07;
Q += -71 * x06;
Q += -93 * x05;
Q += -101 * x04;
Q += -93 * x03;
Q += -71 * x02;
Q += -39 * x01;
Q += 0 * x00;
Q *= gain;
Q /= 128;
h[10] = h[6];
h[9] = h[5];
h[8] = h[4];
h[7] = h[3];
h[6] = h[2];
h[5] = h[1];
h[4] = h[0];
h[3] = x3;
h[2] = x2;
h[1] = x1;
h[0] = x0;
I /= 1024;
Q /= 1024;
return (struct IQ){ I, Q };
}
@ -502,23 +525,13 @@ static void rf_rx(void)
uint8_t *block = iq_queue_buffer[iq_queue_pos];
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++) {
struct IQ IQ = next_sample(cos_ptr);
int64_t I = IQ.I;
int64_t Q = IQ.Q;
cos_ptr += 2 * DECIMATE;
I -= (max_amplitude * 181) / 256;
I /= max_amplitude;
I /= dc_level;
if (I > 127)
I = 127;
@ -527,8 +540,7 @@ static void rf_rx(void)
*blockptr++ = (uint8_t)I + 128;
Q -= (max_amplitude * 181) / 256;
Q /= max_amplitude;
Q /= dc_level;
if (Q > 127)
Q = 127;
@ -552,6 +564,7 @@ static void run_command(uint8_t cmd, uint32_t arg)
} else if (0x02 == cmd) {
/* Set the rate at which IQ sample pairs are sent */
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));
rx_lo_init(arg + sample_rate, true);
} else if (0x04 == cmd) {
@ -667,7 +680,7 @@ int main()
queue_init(&iq_queue, sizeof(uint8_t *), IQ_QUEUE_LEN);
rx_lo_init(INIT_FREQ - INIT_SAMPLE_RATE, true);
rx_lo_init(INIT_FREQ + INIT_SAMPLE_RATE, true);
while (true) {
if (check_command() > 0) {

@ -1 +1 @@
Subproject commit b6b09e34b844326a156bc6734146a88111138473
Subproject commit ed4858dda407ec66626aade9b7c6ad6016539f4c