2024-01-14 20:16:54 +01:00
|
|
|
#include <pico/stdlib.h>
|
|
|
|
#include <pico/stdio_usb.h>
|
|
|
|
#include <pico/multicore.h>
|
2024-02-19 21:02:57 +01:00
|
|
|
#include <pico/util/queue.h>
|
2024-01-14 20:16:54 +01:00
|
|
|
|
|
|
|
#include <hardware/clocks.h>
|
|
|
|
#include <hardware/dma.h>
|
|
|
|
#include <hardware/gpio.h>
|
|
|
|
#include <hardware/pll.h>
|
|
|
|
#include <hardware/vreg.h>
|
|
|
|
#include <hardware/sync.h>
|
|
|
|
#include <hardware/pio.h>
|
2024-02-21 23:41:29 +01:00
|
|
|
#include <hardware/pwm.h>
|
2024-01-14 20:16:54 +01:00
|
|
|
#include <hardware/interp.h>
|
|
|
|
|
|
|
|
#include <hardware/regs/clocks.h>
|
2024-02-21 23:41:29 +01:00
|
|
|
#include <hardware/structs/bus_ctrl.h>
|
2024-01-14 20:16:54 +01:00
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-03-03 23:57:31 +01:00
|
|
|
#define VREG_VOLTAGE VREG_VOLTAGE_1_20
|
2024-03-06 00:01:57 +01:00
|
|
|
#define CLK_SYS_HZ (300 * MHZ)
|
2024-07-15 19:20:22 +02:00
|
|
|
|
|
|
|
#define LO_PIN 9
|
2024-07-22 17:32:08 +02:00
|
|
|
#define RX_PIN 8
|
|
|
|
#define FB_PIN 5
|
2024-06-06 11:24:31 +02:00
|
|
|
#define PSU_PIN 23
|
2024-02-28 10:10:41 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
#define PIO pio1
|
|
|
|
#define LO_SM 0
|
|
|
|
#define FB_SM 1
|
|
|
|
#define RX_SM 2
|
|
|
|
#define AD_SM 3
|
|
|
|
|
2024-06-06 11:24:31 +02:00
|
|
|
#define IQ_SAMPLES 32
|
|
|
|
#define IQ_BLOCK_LEN (2 * IQ_SAMPLES)
|
2024-07-10 10:10:13 +02:00
|
|
|
#define IQ_QUEUE_LEN 4
|
2024-02-28 10:10:41 +01:00
|
|
|
|
2024-06-06 11:24:31 +02:00
|
|
|
#define LO_BITS_DEPTH 15
|
|
|
|
#define LO_WORDS (1 << (LO_BITS_DEPTH - 2))
|
|
|
|
static uint32_t lo_cos[LO_WORDS] __attribute__((__aligned__(1 << LO_BITS_DEPTH)));
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-02 01:56:08 +02:00
|
|
|
#define DECIMATE 4
|
2024-06-07 21:32:39 +02:00
|
|
|
#define RX_STRIDE (2 * IQ_SAMPLES * DECIMATE)
|
2024-07-02 01:56:08 +02:00
|
|
|
#define RX_BITS_DEPTH 13
|
2024-06-06 11:24:31 +02:00
|
|
|
#define RX_WORDS (1 << (RX_BITS_DEPTH - 2))
|
2024-06-06 22:54:02 +02:00
|
|
|
|
2024-07-16 18:56:30 +02:00
|
|
|
static_assert(RX_STRIDE * 4 <= RX_WORDS, "RX_STRIDE * 4 <= RX_WORDS");
|
2024-06-06 22:54:02 +02:00
|
|
|
|
2024-06-06 11:24:31 +02:00
|
|
|
static uint32_t rx_cos[RX_WORDS] __attribute__((__aligned__(1 << RX_BITS_DEPTH)));
|
|
|
|
|
|
|
|
#define INIT_SAMPLE_RATE 100000
|
2024-06-06 22:54:02 +02:00
|
|
|
#define INIT_FREQ 94600000
|
2024-07-15 19:20:22 +02:00
|
|
|
#define INIT_GAIN 127
|
2024-06-06 11:24:31 +02:00
|
|
|
|
|
|
|
#define NUM_GAINS 29
|
|
|
|
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;
|
2024-07-15 19:20:22 +02:00
|
|
|
static int gain = INIT_GAIN;
|
2024-02-21 23:41:29 +01:00
|
|
|
|
2024-06-15 12:49:03 +02:00
|
|
|
#define SIN_PHASE (0u)
|
|
|
|
#define COS_PHASE (3u << 30)
|
2024-02-25 18:55:57 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static int dma_ch_rx1 = -1;
|
|
|
|
static int dma_ch_rx2 = -1;
|
|
|
|
|
|
|
|
static int dma_ch_mix1 = -1;
|
|
|
|
static int dma_ch_mix2 = -1;
|
2024-02-21 23:41:29 +01:00
|
|
|
|
|
|
|
static int dma_ch_samp_cos = -1;
|
|
|
|
|
|
|
|
static int dma_t_samp = -1;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
static int dma_ch_in_cos = -1;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
static queue_t iq_queue;
|
2024-06-08 20:52:06 +02:00
|
|
|
static uint8_t iq_queue_buffer[IQ_QUEUE_LEN][IQ_BLOCK_LEN];
|
|
|
|
static size_t iq_queue_pos = 0;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static void init_lo()
|
2024-07-10 22:32:45 +02:00
|
|
|
{
|
2024-07-15 19:20:22 +02:00
|
|
|
gpio_disable_pulls(LO_PIN);
|
|
|
|
pio_gpio_init(PIO, LO_PIN);
|
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
|
|
|
pio_program_t prog = {
|
|
|
|
.instructions = insn,
|
|
|
|
.length = sizeof(insn) / sizeof(*insn),
|
|
|
|
.origin = -1,
|
|
|
|
};
|
2024-07-10 22:32:45 +02:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_restart(PIO, LO_SM);
|
|
|
|
pio_sm_clear_fifos(PIO, LO_SM);
|
|
|
|
|
|
|
|
if (pio_can_add_program(PIO, &prog))
|
|
|
|
prog.origin = pio_add_program(PIO, &prog);
|
|
|
|
|
|
|
|
pio_sm_config pc = pio_get_default_sm_config();
|
|
|
|
sm_config_set_out_pins(&pc, LO_PIN, 1);
|
|
|
|
sm_config_set_set_pins(&pc, LO_PIN, 1);
|
|
|
|
sm_config_set_wrap(&pc, prog.origin, prog.origin + prog.length - 1);
|
|
|
|
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
|
|
|
|
sm_config_set_fifo_join(&pc, PIO_FIFO_JOIN_TX);
|
|
|
|
sm_config_set_out_shift(&pc, false, true, 32);
|
|
|
|
pio_sm_init(PIO, LO_SM, prog.origin, &pc);
|
|
|
|
|
|
|
|
pio_sm_set_consecutive_pindirs(PIO, LO_SM, LO_PIN, 1, GPIO_OUT);
|
|
|
|
pio_sm_exec_wait_blocking(PIO, LO_SM, pio_encode_set(pio_pins, 0));
|
2024-07-10 22:32:45 +02:00
|
|
|
}
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static void init_fb()
|
2024-01-14 20:16:54 +01:00
|
|
|
{
|
2024-07-15 19:20:22 +02:00
|
|
|
gpio_disable_pulls(FB_PIN);
|
|
|
|
pio_gpio_init(PIO, FB_PIN);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
// NOTE: Not sure if this is ideal.
|
|
|
|
hw_set_bits(&PIO->input_sync_bypass, 1u << RX_PIN);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
gpio_set_input_hysteresis_enabled(RX_PIN, false);
|
2024-07-17 22:19:36 +02:00
|
|
|
gpio_set_drive_strength(FB_PIN, GPIO_DRIVE_STRENGTH_2MA);
|
2024-07-22 17:32:08 +02:00
|
|
|
gpio_set_slew_rate(FB_PIN, GPIO_SLEW_RATE_SLOW);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
const uint16_t insn[] = {
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_encode_mov_not(pio_pins, pio_pins) | pio_encode_sideset(1, 1) |
|
2024-07-22 17:32:08 +02:00
|
|
|
pio_encode_delay(0),
|
2024-07-17 20:25:50 +02:00
|
|
|
//pio_encode_nop() | pio_encode_sideset(1, 0) | pio_encode_delay(0),
|
2024-01-14 20:16:54 +01:00
|
|
|
};
|
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
pio_program_t prog = {
|
|
|
|
.instructions = insn,
|
2024-02-22 11:20:58 +01:00
|
|
|
.length = sizeof(insn) / sizeof(*insn),
|
2024-07-15 19:20:22 +02:00
|
|
|
.origin = -1,
|
2024-01-14 20:16:54 +01:00
|
|
|
};
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_restart(PIO, FB_SM);
|
|
|
|
pio_sm_clear_fifos(PIO, FB_SM);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
if (pio_can_add_program(PIO, &prog))
|
|
|
|
prog.origin = pio_add_program(PIO, &prog);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
|
|
|
pio_sm_config pc = pio_get_default_sm_config();
|
2024-02-22 11:20:58 +01:00
|
|
|
sm_config_set_sideset(&pc, 1, false, true);
|
2024-07-15 19:20:22 +02:00
|
|
|
sm_config_set_in_pins(&pc, RX_PIN);
|
|
|
|
sm_config_set_out_pins(&pc, FB_PIN, 1);
|
|
|
|
sm_config_set_set_pins(&pc, FB_PIN, 1);
|
|
|
|
sm_config_set_sideset_pins(&pc, FB_PIN);
|
2024-06-03 21:48:36 +02:00
|
|
|
sm_config_set_wrap(&pc, prog.origin, prog.origin + prog.length - 1);
|
2024-02-22 11:20:58 +01:00
|
|
|
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_init(PIO, FB_SM, prog.origin, &pc);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_set_consecutive_pindirs(PIO, FB_SM, FB_PIN, 1, GPIO_OUT);
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static void init_rx()
|
2024-01-14 20:16:54 +01:00
|
|
|
{
|
2024-07-15 19:20:22 +02:00
|
|
|
gpio_disable_pulls(RX_PIN);
|
|
|
|
pio_gpio_init(PIO, RX_PIN);
|
|
|
|
|
2024-01-14 20:16:54 +01:00
|
|
|
const uint16_t insn[] = {
|
2024-07-17 20:25:50 +02:00
|
|
|
pio_encode_in(pio_pins, 1) | pio_encode_delay(0),
|
2024-01-14 20:16:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
pio_program_t prog = {
|
|
|
|
.instructions = insn,
|
2024-07-15 19:20:22 +02:00
|
|
|
.length = sizeof(insn) / sizeof(*insn),
|
|
|
|
.origin = -1,
|
2024-01-14 20:16:54 +01:00
|
|
|
};
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_restart(PIO, RX_SM);
|
|
|
|
pio_sm_clear_fifos(PIO, RX_SM);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
if (pio_can_add_program(PIO, &prog))
|
|
|
|
prog.origin = pio_add_program(PIO, &prog);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
|
|
|
pio_sm_config pc = pio_get_default_sm_config();
|
2024-07-15 19:20:22 +02:00
|
|
|
sm_config_set_in_pins(&pc, RX_PIN);
|
2024-02-21 23:41:29 +01:00
|
|
|
sm_config_set_wrap(&pc, prog.origin, prog.origin + prog.length - 1);
|
|
|
|
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
|
2024-01-14 20:16:54 +01:00
|
|
|
sm_config_set_fifo_join(&pc, PIO_FIFO_JOIN_RX);
|
|
|
|
sm_config_set_in_shift(&pc, false, true, 32);
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_init(PIO, RX_SM, prog.origin, &pc);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_set_consecutive_pindirs(PIO, RX_SM, RX_PIN, 1, GPIO_IN);
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static void init_ad()
|
2024-01-14 20:16:54 +01:00
|
|
|
{
|
2024-02-21 23:41:29 +01:00
|
|
|
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),
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
/* Avoid Y-- on wrap. */
|
|
|
|
pio_encode_out(pio_pc, 2),
|
2024-07-15 19:20:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Should wrap here.
|
|
|
|
* Jump to this portion must be inserted from the outside.
|
|
|
|
*/
|
|
|
|
pio_encode_in(pio_x, 32),
|
2024-07-16 18:56:30 +02:00
|
|
|
pio_encode_in(pio_y, 32),
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_encode_set(pio_x, 0),
|
|
|
|
pio_encode_set(pio_y, 0),
|
2024-07-16 18:56:30 +02:00
|
|
|
pio_encode_out(pio_pc, 2),
|
2024-02-21 23:41:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
pio_program_t prog = {
|
|
|
|
.instructions = insn,
|
|
|
|
.length = sizeof(insn) / sizeof(*insn),
|
|
|
|
.origin = 0,
|
|
|
|
};
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_restart(PIO, AD_SM);
|
|
|
|
pio_sm_clear_fifos(PIO, AD_SM);
|
2024-01-25 21:45:45 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
if (pio_can_add_program(PIO, &prog))
|
|
|
|
pio_add_program(PIO, &prog);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
|
|
|
pio_sm_config pc = pio_get_default_sm_config();
|
2024-07-15 19:20:22 +02:00
|
|
|
sm_config_set_wrap(&pc, prog.origin, prog.origin + 5 - 1);
|
2024-02-21 23:41:29 +01:00
|
|
|
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);
|
2024-07-15 19:20:22 +02:00
|
|
|
pio_sm_init(PIO, AD_SM, prog.origin + 1, &pc);
|
2024-02-21 23:41:29 +01:00
|
|
|
}
|
|
|
|
|
2024-07-09 20:32:58 +02:00
|
|
|
#define STEP_BASE ((UINT_MAX + 1.0) / CLK_SYS_HZ)
|
|
|
|
static uint32_t freq_step = 1;
|
|
|
|
|
2024-06-15 12:49:03 +02:00
|
|
|
static void lo_generate(uint32_t *buf, double freq, uint32_t phase)
|
2024-02-25 18:55:57 +01:00
|
|
|
{
|
2024-07-09 20:32:58 +02:00
|
|
|
freq_step = STEP_BASE * freq;
|
2024-06-06 20:19:07 +02:00
|
|
|
|
2024-06-15 12:49:03 +02:00
|
|
|
for (size_t i = 0; i < LO_WORDS; i++) {
|
2024-07-09 20:32:58 +02:00
|
|
|
uint32_t bits = 0;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
|
|
|
for (int j = 0; j < 32; j++) {
|
2024-07-17 18:58:28 +02:00
|
|
|
bits |= phase >> 31;
|
2024-02-25 18:55:57 +01:00
|
|
|
bits <<= 1;
|
2024-07-09 20:32:58 +02:00
|
|
|
phase += freq_step;
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
|
2024-02-25 18:55:57 +01:00
|
|
|
buf[i] = bits;
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
2024-02-25 18:55:57 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 12:49:03 +02:00
|
|
|
static void rx_lo_init(double req_freq, bool align)
|
2024-02-25 18:55:57 +01:00
|
|
|
{
|
2024-07-10 10:10:29 +02:00
|
|
|
const double step_hz = (double)CLK_SYS_HZ / (8 << LO_BITS_DEPTH);
|
2024-06-15 12:49:03 +02:00
|
|
|
double freq = req_freq;
|
2024-02-25 18:55:57 +01:00
|
|
|
|
2024-06-15 12:49:03 +02:00
|
|
|
if (align)
|
|
|
|
freq = round(freq / step_hz) * step_hz;
|
|
|
|
|
|
|
|
lo_generate(lo_cos, freq, COS_PHASE);
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static const uint32_t samp_insn = 5;
|
2024-02-21 23:41:29 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static void rf_rx_start()
|
2024-01-14 20:16:54 +01:00
|
|
|
{
|
2024-07-15 19:20:22 +02:00
|
|
|
dma_ch_rx1 = dma_claim_unused_channel(true);
|
|
|
|
dma_ch_rx2 = dma_claim_unused_channel(true);
|
|
|
|
|
|
|
|
dma_ch_mix1 = dma_claim_unused_channel(true);
|
|
|
|
dma_ch_mix2 = dma_claim_unused_channel(true);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
|
|
|
dma_ch_samp_cos = dma_claim_unused_channel(true);
|
|
|
|
|
|
|
|
dma_t_samp = dma_claim_unused_timer(true);
|
|
|
|
|
|
|
|
dma_channel_config dma_conf;
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
/* Copy PDM bitstream into decimator. */
|
|
|
|
dma_conf = dma_channel_get_default_config(dma_ch_rx1);
|
2024-02-21 23:41:29 +01:00
|
|
|
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
|
|
|
|
channel_config_set_read_increment(&dma_conf, false);
|
|
|
|
channel_config_set_write_increment(&dma_conf, false);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_dreq(&dma_conf, pio_get_dreq(PIO, RX_SM, GPIO_IN));
|
|
|
|
channel_config_set_chain_to(&dma_conf, dma_ch_rx2);
|
|
|
|
dma_channel_configure(dma_ch_rx1, &dma_conf, &PIO->txf[AD_SM], &PIO->rxf[RX_SM], UINT_MAX,
|
|
|
|
false);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
dma_conf = dma_channel_get_default_config(dma_ch_rx2);
|
2024-02-21 23:41:29 +01:00
|
|
|
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
|
|
|
|
channel_config_set_read_increment(&dma_conf, false);
|
|
|
|
channel_config_set_write_increment(&dma_conf, false);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_dreq(&dma_conf, pio_get_dreq(PIO, RX_SM, GPIO_IN));
|
|
|
|
channel_config_set_chain_to(&dma_conf, dma_ch_rx1);
|
|
|
|
dma_channel_configure(dma_ch_rx2, &dma_conf, &PIO->txf[AD_SM], &PIO->rxf[RX_SM], UINT_MAX,
|
2024-02-21 23:41:29 +01:00
|
|
|
false);
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
/* Drive the LO capacitor. */
|
|
|
|
dma_conf = dma_channel_get_default_config(dma_ch_mix1);
|
2024-02-21 23:41:29 +01:00
|
|
|
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
|
|
|
|
channel_config_set_read_increment(&dma_conf, true);
|
|
|
|
channel_config_set_write_increment(&dma_conf, false);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_ring(&dma_conf, GPIO_IN, LO_BITS_DEPTH);
|
|
|
|
channel_config_set_dreq(&dma_conf, pio_get_dreq(PIO, LO_SM, GPIO_OUT));
|
|
|
|
channel_config_set_chain_to(&dma_conf, dma_ch_mix2);
|
|
|
|
dma_channel_configure(dma_ch_mix1, &dma_conf, &PIO->txf[LO_SM], lo_cos, UINT_MAX, false);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
dma_conf = dma_channel_get_default_config(dma_ch_mix2);
|
2024-02-21 23:41:29 +01:00
|
|
|
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
|
|
|
|
channel_config_set_read_increment(&dma_conf, true);
|
|
|
|
channel_config_set_write_increment(&dma_conf, false);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_ring(&dma_conf, GPIO_IN, LO_BITS_DEPTH);
|
|
|
|
channel_config_set_dreq(&dma_conf, pio_get_dreq(PIO, LO_SM, GPIO_OUT));
|
|
|
|
channel_config_set_chain_to(&dma_conf, dma_ch_mix1);
|
|
|
|
dma_channel_configure(dma_ch_mix2, &dma_conf, &PIO->txf[LO_SM], lo_cos, UINT_MAX, false);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
|
|
|
/* Pacing timer for the sampling script trigger channel. */
|
2024-06-07 21:32:39 +02:00
|
|
|
dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / (sample_rate * DECIMATE));
|
2024-02-21 23:41:29 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
/* Trigger accumulator values push. */
|
2024-02-21 23:41:29 +01:00
|
|
|
dma_conf = dma_channel_get_default_config(dma_ch_samp_cos);
|
|
|
|
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_read_increment(&dma_conf, false);
|
2024-02-21 23:41:29 +01:00
|
|
|
channel_config_set_write_increment(&dma_conf, false);
|
|
|
|
channel_config_set_ring(&dma_conf, false, 4);
|
2024-06-06 11:24:31 +02:00
|
|
|
channel_config_set_high_priority(&dma_conf, true);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_dreq(&dma_conf, dma_get_timer_dreq(dma_t_samp));
|
|
|
|
dma_channel_configure(dma_ch_samp_cos, &dma_conf, &PIO->sm[AD_SM].instr, &samp_insn,
|
|
|
|
UINT_MAX, false);
|
|
|
|
|
|
|
|
init_lo();
|
|
|
|
init_fb();
|
|
|
|
init_rx();
|
|
|
|
init_ad();
|
|
|
|
|
|
|
|
dma_channel_start(dma_ch_rx1);
|
|
|
|
dma_channel_start(dma_ch_mix1);
|
|
|
|
dma_channel_start(dma_ch_samp_cos);
|
|
|
|
|
2024-07-17 18:58:28 +02:00
|
|
|
pio_set_sm_mask_enabled(PIO, 0x0f, true);
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
static void rf_rx_stop(void)
|
2024-01-24 23:49:36 +01:00
|
|
|
{
|
2024-07-17 18:58:28 +02:00
|
|
|
pio_set_sm_mask_enabled(PIO, 0x0f, false);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
|
|
|
sleep_us(10);
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
dma_channel_abort(dma_ch_rx1);
|
|
|
|
dma_channel_abort(dma_ch_rx2);
|
|
|
|
dma_channel_abort(dma_ch_mix1);
|
|
|
|
dma_channel_abort(dma_ch_mix2);
|
2024-02-21 23:41:29 +01:00
|
|
|
dma_channel_abort(dma_ch_samp_cos);
|
2024-07-15 19:20:22 +02:00
|
|
|
|
|
|
|
dma_channel_cleanup(dma_ch_rx1);
|
|
|
|
dma_channel_cleanup(dma_ch_rx2);
|
|
|
|
dma_channel_cleanup(dma_ch_mix1);
|
|
|
|
dma_channel_cleanup(dma_ch_mix2);
|
2024-02-21 23:41:29 +01:00
|
|
|
dma_channel_cleanup(dma_ch_samp_cos);
|
2024-07-15 19:20:22 +02:00
|
|
|
|
|
|
|
dma_channel_unclaim(dma_ch_rx1);
|
|
|
|
dma_channel_unclaim(dma_ch_rx2);
|
|
|
|
dma_channel_unclaim(dma_ch_mix1);
|
|
|
|
dma_channel_unclaim(dma_ch_mix2);
|
2024-02-21 23:41:29 +01:00
|
|
|
dma_channel_unclaim(dma_ch_samp_cos);
|
|
|
|
|
|
|
|
dma_timer_unclaim(dma_t_samp);
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
dma_ch_rx1 = -1;
|
|
|
|
dma_ch_rx2 = -1;
|
|
|
|
dma_ch_mix1 = -1;
|
|
|
|
dma_ch_mix2 = -1;
|
2024-02-21 23:41:29 +01:00
|
|
|
dma_ch_samp_cos = -1;
|
|
|
|
|
|
|
|
dma_t_samp = -1;
|
2024-01-24 23:49:36 +01:00
|
|
|
}
|
|
|
|
|
2024-07-18 12:15:43 +02:00
|
|
|
inline static int next_sample(const uint32_t *buf, int *h)
|
|
|
|
{
|
2024-07-22 20:34:16 +02:00
|
|
|
int x1 = buf[0] - buf[1];
|
2024-07-18 12:15:43 +02:00
|
|
|
int x2 = 0;
|
2024-07-22 20:34:16 +02:00
|
|
|
int x3 = buf[5] - buf[4];
|
2024-07-18 12:15:43 +02:00
|
|
|
int x4 = 0;
|
|
|
|
|
|
|
|
int sample = 18 * h[9] + 14 * h[8] - 32 * h[7] - 33 * h[6] + 106 * h[5] + 362 * h[4] +
|
|
|
|
565 * h[3] + 565 * h[2] + 362 * h[1] + 106 * h[0] - 33 * x1 - 32 * x2 +
|
|
|
|
14 * x3 + 18 * x4;
|
|
|
|
|
|
|
|
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] = x1;
|
|
|
|
h[2] = x2;
|
|
|
|
h[1] = x3;
|
|
|
|
h[0] = x4;
|
|
|
|
|
2024-07-22 20:34:16 +02:00
|
|
|
return sample / 2260;
|
2024-07-18 12:15:43 +02:00
|
|
|
}
|
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
static void rf_rx(void)
|
|
|
|
{
|
2024-06-15 17:57:27 +02:00
|
|
|
const uint32_t base = (uint32_t)rx_cos;
|
2024-07-18 12:15:43 +02:00
|
|
|
int prevI[10] = { 0 };
|
|
|
|
int prevQ[10] = { 0 };
|
2024-06-15 17:57:27 +02:00
|
|
|
int pos = 0;
|
2024-01-14 21:32:53 +01:00
|
|
|
|
2024-01-14 20:16:54 +01:00
|
|
|
while (true) {
|
2024-02-21 23:41:29 +01:00
|
|
|
if (multicore_fifo_rvalid()) {
|
|
|
|
multicore_fifo_pop_blocking();
|
|
|
|
multicore_fifo_push_blocking(0);
|
|
|
|
return;
|
|
|
|
}
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-15 17:57:27 +02:00
|
|
|
int head = (dma_hw->ch[dma_ch_in_cos].write_addr - base) / 4;
|
|
|
|
int delta = (head < pos ? head + RX_WORDS : head) - pos;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
sleep_us(10);
|
|
|
|
|
2024-06-15 17:57:27 +02:00
|
|
|
while (delta < RX_STRIDE) {
|
2024-06-06 11:24:31 +02:00
|
|
|
sleep_us(1);
|
2024-06-15 17:57:27 +02:00
|
|
|
head = (dma_hw->ch[dma_ch_in_cos].write_addr - base) / 4;
|
|
|
|
delta = (head < pos ? head + RX_WORDS : head) - pos;
|
2024-06-06 11:24:31 +02:00
|
|
|
}
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-15 17:57:27 +02:00
|
|
|
const uint32_t *cos_ptr = rx_cos + pos;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-06 11:24:31 +02:00
|
|
|
pos = (pos + RX_STRIDE) & (RX_WORDS - 1);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-08 20:52:06 +02:00
|
|
|
uint8_t *block = iq_queue_buffer[iq_queue_pos];
|
2024-06-06 11:24:31 +02:00
|
|
|
uint8_t *blockptr = block;
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-06 11:24:31 +02:00
|
|
|
/*
|
|
|
|
* Since every 2 samples add to either +1 or -1,
|
|
|
|
* the maximum amplitude in one direction is 1/2.
|
2024-07-18 12:15:43 +02:00
|
|
|
*
|
2024-06-06 11:24:31 +02:00
|
|
|
* We are allowing the counters to only go as high
|
2024-07-18 12:15:43 +02:00
|
|
|
* as sampling rate.
|
2024-06-06 11:24:31 +02:00
|
|
|
*/
|
2024-07-18 12:15:43 +02:00
|
|
|
int64_t max_amplitude = CLK_SYS_HZ / 2 / sample_rate;
|
2024-02-24 11:08:34 +01:00
|
|
|
|
2024-06-06 11:24:31 +02:00
|
|
|
for (int i = 0; i < IQ_SAMPLES; i++) {
|
2024-07-18 12:15:43 +02:00
|
|
|
int64_t I = next_sample(cos_ptr + 0, prevI);
|
|
|
|
int64_t Q = next_sample(cos_ptr + 2, prevQ);
|
|
|
|
cos_ptr += 8;
|
2024-03-05 20:18:08 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
I *= gain;
|
2024-07-02 13:04:39 +02:00
|
|
|
I -= (max_amplitude * 181) / 256;
|
2024-06-06 11:24:31 +02:00
|
|
|
I /= max_amplitude;
|
2024-02-24 22:26:09 +01:00
|
|
|
|
2024-06-06 22:54:02 +02:00
|
|
|
if (I > 127)
|
|
|
|
I = 127;
|
|
|
|
else if (I < -128)
|
|
|
|
I = -128;
|
|
|
|
|
2024-07-02 12:13:54 +02:00
|
|
|
*blockptr++ = (uint8_t)I + 128;
|
2024-02-24 22:26:09 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
Q *= gain;
|
2024-07-02 13:04:39 +02:00
|
|
|
Q -= (max_amplitude * 181) / 256;
|
2024-06-06 11:24:31 +02:00
|
|
|
Q /= max_amplitude;
|
2024-02-24 11:08:34 +01:00
|
|
|
|
2024-06-06 22:54:02 +02:00
|
|
|
if (Q > 127)
|
|
|
|
Q = 127;
|
|
|
|
else if (Q < -128)
|
|
|
|
Q = -128;
|
|
|
|
|
|
|
|
*blockptr++ = (uint8_t)Q + 128;
|
2024-02-21 23:41:29 +01:00
|
|
|
}
|
2024-01-15 23:21:35 +01:00
|
|
|
|
2024-06-08 20:52:06 +02:00
|
|
|
if (queue_try_add(&iq_queue, &block)) {
|
|
|
|
iq_queue_pos = (iq_queue_pos + 1) & (IQ_QUEUE_LEN - 1);
|
|
|
|
}
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 20:02:30 +02:00
|
|
|
static void run_command(uint8_t cmd, uint32_t arg)
|
|
|
|
{
|
|
|
|
if (0x01 == cmd) {
|
|
|
|
/* Tune to a new center frequency */
|
2024-07-15 19:20:22 +02:00
|
|
|
rx_lo_init(arg + sample_rate, true);
|
2024-06-06 20:02:30 +02:00
|
|
|
} else if (0x02 == cmd) {
|
|
|
|
/* Set the rate at which IQ sample pairs are sent */
|
|
|
|
sample_rate = arg;
|
2024-06-07 21:32:39 +02:00
|
|
|
dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / (sample_rate * DECIMATE));
|
2024-07-15 19:20:22 +02:00
|
|
|
rx_lo_init(arg + sample_rate, true);
|
2024-06-06 20:02:30 +02:00
|
|
|
} else if (0x04 == cmd) {
|
|
|
|
/* Set the tuner gain level */
|
2024-07-15 19:20:22 +02:00
|
|
|
gain = INIT_GAIN * powf(10.0f, arg / 200.0f);
|
2024-06-06 20:02:30 +02:00
|
|
|
} else if (0x0d == cmd) {
|
|
|
|
/* Set tuner gain by the tuner's gain index */
|
2024-07-15 19:20:22 +02:00
|
|
|
|
|
|
|
if (arg >= NUM_GAINS)
|
|
|
|
arg = NUM_GAINS - 1;
|
|
|
|
|
|
|
|
gain = INIT_GAIN * powf(10.0f, gains[arg] / 200.0f);
|
2024-06-06 20:02:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 22:53:27 +02:00
|
|
|
static int check_command(void)
|
2024-06-06 20:02:30 +02:00
|
|
|
{
|
|
|
|
static uint8_t buf[5];
|
|
|
|
static int pos = 0;
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = getchar_timeout_us(0)) >= 0) {
|
|
|
|
if (0 == pos && 0 == c)
|
2024-06-06 22:53:27 +02:00
|
|
|
return 0;
|
2024-06-06 20:02:30 +02:00
|
|
|
|
|
|
|
buf[pos++] = c;
|
|
|
|
|
|
|
|
if (5 == pos) {
|
|
|
|
uint32_t arg = (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4];
|
|
|
|
run_command(buf[0], arg);
|
|
|
|
pos = 0;
|
2024-06-06 22:53:27 +02:00
|
|
|
return buf[0];
|
2024-06-06 20:02:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 22:53:27 +02:00
|
|
|
return -1;
|
2024-06-06 20:02:30 +02:00
|
|
|
}
|
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
static void do_rx()
|
2024-02-21 23:41:29 +01:00
|
|
|
{
|
2024-07-15 19:20:22 +02:00
|
|
|
rf_rx_start();
|
2024-02-21 23:41:29 +01:00
|
|
|
sleep_us(100);
|
|
|
|
|
|
|
|
dma_ch_in_cos = dma_claim_unused_channel(true);
|
|
|
|
|
|
|
|
dma_channel_config dma_conf;
|
|
|
|
|
|
|
|
dma_conf = dma_channel_get_default_config(dma_ch_in_cos);
|
|
|
|
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
|
|
|
|
channel_config_set_read_increment(&dma_conf, false);
|
|
|
|
channel_config_set_write_increment(&dma_conf, true);
|
2024-07-15 19:20:22 +02:00
|
|
|
channel_config_set_ring(&dma_conf, GPIO_OUT, RX_BITS_DEPTH);
|
|
|
|
channel_config_set_dreq(&dma_conf, pio_get_dreq(PIO, AD_SM, false));
|
|
|
|
dma_channel_configure(dma_ch_in_cos, &dma_conf, rx_cos, &PIO->rxf[AD_SM], UINT_MAX, true);
|
2024-02-21 23:41:29 +01:00
|
|
|
|
|
|
|
multicore_launch_core1(rf_rx);
|
|
|
|
|
2024-06-08 20:52:06 +02:00
|
|
|
const uint8_t *block;
|
2024-02-21 23:41:29 +01:00
|
|
|
|
2024-06-08 20:52:06 +02:00
|
|
|
while (queue_try_remove(&iq_queue, &block))
|
2024-02-21 23:41:29 +01:00
|
|
|
/* Flush the queue */;
|
|
|
|
|
|
|
|
while (true) {
|
2024-06-08 11:59:14 +02:00
|
|
|
int cmd;
|
|
|
|
|
|
|
|
while ((cmd = check_command()) >= 0)
|
|
|
|
if (0 == cmd)
|
|
|
|
goto done;
|
|
|
|
|
2024-06-08 20:52:06 +02:00
|
|
|
if (queue_try_remove(&iq_queue, &block)) {
|
|
|
|
fwrite(block, IQ_BLOCK_LEN, 1, stdout);
|
2024-06-08 11:59:14 +02:00
|
|
|
fflush(stdout);
|
2024-02-21 23:41:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-08 11:59:14 +02:00
|
|
|
done:
|
2024-02-21 23:41:29 +01:00
|
|
|
multicore_fifo_push_blocking(0);
|
|
|
|
multicore_fifo_pop_blocking();
|
|
|
|
sleep_us(10);
|
|
|
|
multicore_reset_core1();
|
|
|
|
|
|
|
|
rf_rx_stop();
|
|
|
|
|
|
|
|
dma_channel_abort(dma_ch_in_cos);
|
|
|
|
dma_channel_cleanup(dma_ch_in_cos);
|
|
|
|
dma_channel_unclaim(dma_ch_in_cos);
|
|
|
|
dma_ch_in_cos = -1;
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2024-03-03 23:57:31 +01:00
|
|
|
vreg_set_voltage(VREG_VOLTAGE);
|
2024-01-14 20:16:54 +01:00
|
|
|
set_sys_clock_khz(CLK_SYS_HZ / KHZ, true);
|
|
|
|
clock_configure(clk_peri, 0, CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, CLK_SYS_HZ,
|
|
|
|
CLK_SYS_HZ);
|
|
|
|
|
2024-02-27 09:35:03 +01:00
|
|
|
/* Enable PSU PWM mode. */
|
|
|
|
gpio_init(PSU_PIN);
|
|
|
|
gpio_set_dir(PSU_PIN, GPIO_OUT);
|
|
|
|
gpio_put(PSU_PIN, 1);
|
|
|
|
|
2024-02-21 23:41:29 +01:00
|
|
|
bus_ctrl_hw->priority |= BUSCTRL_BUS_PRIORITY_DMA_W_BITS | BUSCTRL_BUS_PRIORITY_DMA_R_BITS;
|
|
|
|
|
2024-01-14 20:16:54 +01:00
|
|
|
stdio_usb_init();
|
2024-06-06 11:24:31 +02:00
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-08 20:52:06 +02:00
|
|
|
queue_init(&iq_queue, sizeof(uint8_t *), IQ_QUEUE_LEN);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-02 01:56:08 +02:00
|
|
|
rx_lo_init(INIT_FREQ - INIT_SAMPLE_RATE, true);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-06-06 22:53:27 +02:00
|
|
|
while (true) {
|
|
|
|
if (check_command() > 0) {
|
2024-06-06 11:24:31 +02:00
|
|
|
static const uint32_t header[3] = { __builtin_bswap32(0x52544c30),
|
2024-06-06 22:11:17 +02:00
|
|
|
__builtin_bswap32(5),
|
2024-06-06 11:24:31 +02:00
|
|
|
__builtin_bswap32(NUM_GAINS) };
|
|
|
|
fwrite(header, sizeof header, 1, stdout);
|
|
|
|
fflush(stdout);
|
2024-01-14 20:16:54 +01:00
|
|
|
|
2024-07-15 19:20:22 +02:00
|
|
|
do_rx();
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
2024-06-06 22:53:27 +02:00
|
|
|
|
|
|
|
sleep_ms(10);
|
2024-01-14 20:16:54 +01:00
|
|
|
}
|
|
|
|
}
|