Compare commits

..

No commits in common. "a2104495c6d997381578cb22905747657855cb62" and "4ecefa1e5b0a673f9804866beac545b192f0f451" have entirely different histories.

2 changed files with 32 additions and 64 deletions

View file

@ -40,15 +40,11 @@ static uint32_t lo_sin[LO_WORDS] __attribute__((__aligned__(1 << LO_BITS_DEPTH))
#define RX_STRIDE (2 * IQ_SAMPLES)
#define RX_BITS_DEPTH 12
#define RX_WORDS (1 << (RX_BITS_DEPTH - 2))
static_assert(RX_STRIDE * 4 < RX_WORDS, "RX_STRIDE * 4 < RX_WORDS");
static uint32_t rx_cos[RX_WORDS] __attribute__((__aligned__(1 << RX_BITS_DEPTH)));
static uint32_t rx_sin[RX_WORDS] __attribute__((__aligned__(1 << RX_BITS_DEPTH)));
#define INIT_GAIN 120
#define INIT_GAIN 256
#define INIT_SAMPLE_RATE 100000
#define INIT_FREQ 94600000
#define NUM_GAINS 29
static int gains[NUM_GAINS] = { 0, 9, 14, 27, 37, 77, 87, 125, 144, 157,
@ -242,7 +238,7 @@ static const uint32_t samp_insn[4] = {
static uint32_t null, one = 1;
static void rf_rx_start(int rx_pin, int bias_pin)
static void rf_rx_start(int rx_pin, int bias_pin, double freq, int rate)
{
dma_ch_rx = dma_claim_unused_channel(true);
dma_ch_cp = dma_claim_unused_channel(true);
@ -318,7 +314,7 @@ static void rf_rx_start(int rx_pin, int bias_pin)
false);
/* Pacing timer for the sampling script trigger channel. */
dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / sample_rate);
dma_timer_set_fraction(dma_t_samp, 1, CLK_SYS_HZ / rate);
/* Sampling trigger channel. */
dma_conf = dma_channel_get_default_config(dma_ch_samp_trig);
@ -353,6 +349,7 @@ static void rf_rx_start(int rx_pin, int bias_pin)
bias_init(rx_pin, bias_pin);
adder_init();
rx_lo_init(freq);
dma_channel_start(dma_ch_rx);
dma_channel_start(dma_ch_samp_trig);
watch_init(rx_pin);
@ -439,7 +436,7 @@ static void rf_rx(void)
int delta = prev_transfers - dma_hw->ch[dma_ch_in_cos].transfer_count;
while (delta < RX_STRIDE) {
while (delta < RX_STRIDE * 2) {
delta = prev_transfers - dma_hw->ch[dma_ch_in_cos].transfer_count;
sleep_us(1);
}
@ -461,13 +458,11 @@ static void rf_rx(void)
/*
* Since the waveform is normally half of the time
* above zero, we can halve once more.
* above zero, we could halve once more.
*
* This is not perfect, so we do not max out the base
* gain but keep it slightly below the maximum to make
* sure we do not overshoot often.
* Instead we use 2/3 to provide 1/3 reserve.
*/
max_amplitude = max_amplitude / 2;
max_amplitude = max_amplitude * 2 / 3;
/*
* We are allowing the counters to only go as high
@ -488,11 +483,6 @@ static void rf_rx(void)
I *= gain;
I /= max_amplitude;
if (I > 127)
I = 127;
else if (I < -128)
I = -128;
*blockptr++ = I + 128;
uint32_t sin_neg = *sin_ptr++;
@ -507,12 +497,7 @@ static void rf_rx(void)
Q *= gain;
Q /= max_amplitude;
if (Q > 127)
Q = 127;
else if (Q < -128)
Q = -128;
*blockptr++ = (uint8_t)Q + 128;
*blockptr++ = Q + 128;
}
(void)queue_try_add(&iq_queue, block);
@ -539,7 +524,7 @@ static void run_command(uint8_t cmd, uint32_t arg)
}
}
static int check_command(void)
static bool check_command(void)
{
static uint8_t buf[5];
static int pos = 0;
@ -548,7 +533,7 @@ static int check_command(void)
while ((c = getchar_timeout_us(0)) >= 0) {
if (0 == pos && 0 == c)
return 0;
return true;
buf[pos++] = c;
@ -556,16 +541,15 @@ static int check_command(void)
uint32_t arg = (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4];
run_command(buf[0], arg);
pos = 0;
return buf[0];
}
}
return -1;
return false;
}
static void do_rx(int rx_pin, int bias_pin)
static void do_rx(int rx_pin, int bias_pin, double freq)
{
rf_rx_start(rx_pin, bias_pin);
rf_rx_start(rx_pin, bias_pin, freq, sample_rate);
sleep_us(100);
dma_ch_in_cos = dma_claim_unused_channel(true);
@ -599,7 +583,7 @@ static void do_rx(int rx_pin, int bias_pin)
/* Flush the queue */;
while (true) {
if (0 == check_command())
if (check_command())
break;
for (int i = 0; i < 32; i++) {
@ -648,19 +632,27 @@ int main()
queue_init(&iq_queue, IQ_BLOCK_LEN, 256);
rx_lo_init(INIT_FREQ);
while (true) {
if (check_command() > 0) {
int c = getchar_timeout_us(0);
if (0 == c) {
continue;
} else if (1 == c) {
/* Tune to a new center frequency */
uint32_t arg;
fread(&arg, sizeof arg, 1, stdin);
arg = __builtin_bswap32(arg);
static const uint32_t header[3] = { __builtin_bswap32(0x52544c30),
__builtin_bswap32(5),
__builtin_bswap32(6),
__builtin_bswap32(NUM_GAINS) };
fwrite(header, sizeof header, 1, stdout);
fflush(stdout);
do_rx(10, 11);
}
gain = INIT_GAIN;
sample_rate = INIT_SAMPLE_RATE;
sleep_ms(10);
do_rx(10, 11, arg);
}
}
}

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python
import binascii
import struct
from socket import (AF_INET, MSG_DONTWAIT, SO_REUSEADDR, SO_SNDBUF,
SOCK_STREAM, SOL_SOCKET, socket)
@ -7,31 +8,6 @@ from socket import (AF_INET, MSG_DONTWAIT, SO_REUSEADDR, SO_SNDBUF,
import click
import serial
COMMAND_NAMES = [
"reset",
"tune_freq",
"sample_rate",
"manual_gain",
"gain",
"ppm_offset",
"if_gain",
"test_mode",
"agc",
"direct_sampling",
"offset_tuning",
"11",
"12",
"gain_index",
"bias_tee",
]
def describe(cmd: int, arg: int):
try:
print("->", COMMAND_NAMES[cmd], arg)
except IndexError:
print("->", cmd, arg)
@click.command()
@click.option("-f", "--frequency", default=88200000, help="Frequency to tune to")
@ -67,7 +43,7 @@ def bridge(frequency):
while len(cmd) >= 5:
fp.write(cmd[:5])
info = struct.unpack(">BL", cmd[:5])
describe(*info)
print("->", hex(info[0]), info[1])
cmd = cmd[5:]
data = fp.read(64)