Compare commits

...

4 commits

Author SHA1 Message Date
a2104495c6 Improve gain handling
Now it's possible to set gain in gqrx.
2024-06-06 22:54:02 +02:00
40a0b843fb Streamline configuration 2024-06-06 22:53:45 +02:00
78bb05cd1f Fake R820T to enable gqrx gain control 2024-06-06 22:11:17 +02:00
edfe86793d util/bridge: print command names 2024-06-06 22:04:47 +02:00
2 changed files with 64 additions and 32 deletions

View file

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

View file

@ -1,6 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
import binascii
import struct import struct
from socket import (AF_INET, MSG_DONTWAIT, SO_REUSEADDR, SO_SNDBUF, from socket import (AF_INET, MSG_DONTWAIT, SO_REUSEADDR, SO_SNDBUF,
SOCK_STREAM, SOL_SOCKET, socket) SOCK_STREAM, SOL_SOCKET, socket)
@ -8,6 +7,31 @@ from socket import (AF_INET, MSG_DONTWAIT, SO_REUSEADDR, SO_SNDBUF,
import click import click
import serial 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.command()
@click.option("-f", "--frequency", default=88200000, help="Frequency to tune to") @click.option("-f", "--frequency", default=88200000, help="Frequency to tune to")
@ -43,7 +67,7 @@ def bridge(frequency):
while len(cmd) >= 5: while len(cmd) >= 5:
fp.write(cmd[:5]) fp.write(cmd[:5])
info = struct.unpack(">BL", cmd[:5]) info = struct.unpack(">BL", cmd[:5])
print("->", hex(info[0]), info[1]) describe(*info)
cmd = cmd[5:] cmd = cmd[5:]
data = fp.read(64) data = fp.read(64)