From 2825e5e49dfa7ccd2cb8ba68e7d1d24df814e057 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= <mordae@anilinux.org>
Date: Sat, 8 Jun 2024 17:05:56 +0200
Subject: [PATCH] Improve bpsk test transmission timing

---
 src/main.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/main.c b/src/main.c
index 3360a13..6c67be0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -947,10 +947,21 @@ static void command(const char *cmd)
 				actual = rx_lo_init(actual - step_hz);
 				printf("Frequency: %.0f\n", actual);
 			} else if ((c >= '1') && (c <= '9')) {
-				for (int i = 0; i < 1000; i++) {
-					phase = !phase;
+				uint32_t t0 = time_us_32();
+				uint32_t tmax = 1000000 / (c - '0');
+				uint32_t step = 1000 / (c - '0') / 2;
+
+				while (true) {
+					uint32_t t1 = time_us_32();
+					uint32_t te = t1 - t0;
+
+					if (te >= tmax)
+						break;
+
+					phase = (te / step) & 1;
+
 					gpio_set_outover(n, phase);
-					sleep_us(1000 / (c - '0'));
+					sleep_us(1);
 				}
 			}
 		}