From 71c8ceb8bb8d4d876e3741f757747962b8525715 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= <mordae@anilinux.org>
Date: Sun, 9 Jun 2024 13:05:52 +0200
Subject: [PATCH] Make bias strength settable

---
 src/main.c     | 39 ++++++++++++++++++++++++++++++++++++++-
 util/bridge.py |  2 +-
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/src/main.c b/src/main.c
index e1472cc..ca7c19c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -83,6 +83,40 @@ static queue_t iq_queue;
 static uint8_t iq_queue_buffer[IQ_QUEUE_LEN][IQ_BLOCK_LEN];
 static size_t iq_queue_pos = 0;
 
+static void bias_set_delay(int delay)
+{
+	delay += 200;
+
+	if (delay < 0)
+		delay = 0;
+
+	if (delay >= 200)
+		delay = 512;
+
+	int bulk = delay / 16;
+	int rest = delay % 16;
+
+	if (0 == rest) {
+		bulk -= 1;
+		rest = 16;
+	}
+
+	if (delay > 0) {
+		if (bulk) {
+			pio1->instr_mem[11] = pio_encode_set(pio_x, bulk) |
+					      pio_encode_sideset(1, 0) | pio_encode_delay(rest - 1);
+		} else {
+			pio1->instr_mem[11] = pio_encode_jmp(10) | pio_encode_sideset(1, 0) |
+					      pio_encode_delay(rest - 1);
+		}
+
+		pio_sm_set_wrap(pio1, 0, 10, 12);
+	} else {
+		pio_sm_set_wrap(pio1, 0, 10, 10);
+		pio_sm_exec(pio1, 0, pio_encode_jmp(10));
+	}
+}
+
 static void bias_init(int in_pin, int out_pin)
 {
 	gpio_disable_pulls(in_pin);
@@ -97,7 +131,7 @@ static void bias_init(int in_pin, int out_pin)
 
 	const uint16_t insn[] = {
 		pio_encode_mov_not(pio_pins, pio_pins) | pio_encode_sideset(1, 1),
-		pio_encode_set(pio_x, 4) | pio_encode_sideset(1, 0) | pio_encode_delay(15),
+		pio_encode_set(pio_x, 31) | pio_encode_sideset(1, 0) | pio_encode_delay(15),
 		pio_encode_jmp_x_dec(2) | pio_encode_sideset(1, 0) | pio_encode_delay(15),
 	};
 
@@ -546,6 +580,9 @@ static void run_command(uint8_t cmd, uint32_t arg)
 	} else if (0x04 == cmd) {
 		/* Set the tuner gain level */
 		gain = INIT_GAIN * powf(10.0f, 0.005f * arg);
+	} else if (0x05 == cmd) {
+		/* Set PPM error - hack to tweak bias strength */
+		bias_set_delay(arg);
 	} else if (0x0d == cmd) {
 		/* Set tuner gain by the tuner's gain index */
 		if (arg < NUM_GAINS) {
diff --git a/util/bridge.py b/util/bridge.py
index 80dbe7a..0cb4a43 100755
--- a/util/bridge.py
+++ b/util/bridge.py
@@ -72,7 +72,7 @@ def bridge(frequency):
 
                     while len(cmd) >= 5:
                         fp.write(cmd[:5])
-                        info = struct.unpack(">BL", cmd[:5])
+                        info = struct.unpack(">Bl", cmd[:6])
                         describe(*info)
                         cmd = cmd[5:]