From beb00dc53dbae020a822ff037509e00744b2ac6a Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Tue, 13 Aug 2019 14:03:07 +0200
Subject: [PATCH] activate correct TX filter when going "manual"

---
 alex.h         |  6 ++--
 hpsdrsim.c     | 61 +++++++++++++++++++++++++++------
 new_protocol.c | 69 ++++++++++++--------------------------
 old_protocol.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++--
 4 files changed, 165 insertions(+), 62 deletions(-)

diff --git a/alex.h b/alex.h
index 14b3093..67fa08d 100644
--- a/alex.h
+++ b/alex.h
@@ -109,9 +109,9 @@
 
 // These bits only have a function on ANAN-7000
 
-#define ALEX_ANAN7000_RX_30_20_17_BPF	0x00000002   // bit 1, 11.0 - 20.9 MHz
-#define ALEX_ANAN7000_RX_15_12_BPF	0x00000004   // bit 2, 21.0 - 26.9 MHz
-#define ALEX_ANAN7000_RX_10_6_PRE_BPF	0x00000008   // bit 3, 27.0 - 61.4 MHz with preamp
+#define ALEX_ANAN7000_RX_20_15_BPF	0x00000002   // bit 1, 11.0 - 22.0 MHz
+#define ALEX_ANAN7000_RX_12_10_BPF	0x00000004   // bit 2, 22.0 - 35.6 MHz
+#define ALEX_ANAN7000_RX_6_PRE_BPF	0x00000008   // bit 3,      > 35.6 MHz with preamp
 #define ALEX_ANAN7000_RX_40_30_BPF	0x00000010   // bit 4,  5.5 - 10.9 MHz
 #define ALEX_ANAN7000_RX_80_60_BPF	0x00000020   // bit 5,  2.1 -  5.4 MHz
 #define ALEX_ANAN7000_RX_160_BPF	0x00000040   // bit 6,  1.5 -  2.0 MHz
diff --git a/hpsdrsim.c b/hpsdrsim.c
index fa48ca3..1da2359 100644
--- a/hpsdrsim.c
+++ b/hpsdrsim.c
@@ -847,8 +847,12 @@ void process_ep2(uint8_t *frame)
 {
 
 	uint16_t data;
+        int rc;
+        int mod_ptt;
+        int mod;
 
-        chk_data(frame[0] & 1, ptt, "PTT");
+        
+	chk_data(frame[0] & 1, ptt, "PTT");
 	switch (frame[0])
 	{
 	case 0:
@@ -862,19 +866,56 @@ void process_ep2(uint8_t *frame)
           chk_data(frame[2] & 1, TX_class_E, "TX CLASS-E");
           chk_data((frame[2] & 0xfe) >> 1, OpenCollectorOutputs,"OpenCollector");
 
-          chk_data((frame[3] & 0x03) >> 0, AlexAtt, "ALEX Attenuator");
-          chk_data((frame[3] & 0x04) >> 2, preamp, "ALEX preamp");
-          chk_data((frame[3] & 0x08) >> 3, LTdither, "LT2208 Dither");
-          chk_data((frame[3] & 0x10) >> 4, LTrandom, "LT2208 Random");
-          chk_data((frame[3] & 0x60) >> 5, alexRXant, "ALEX RX ant");
-          chk_data((frame[3] & 0x80) >> 7, alexRXout, "ALEX RX out");
-
-          chk_data(((frame[4] >> 0) & 3), AlexTXrel, "ALEX TX relay");
-          chk_data(((frame[4] >> 2) & 1), duplex,    "DUPLEX");
           chk_data(((frame[4] >> 3) & 7) + 1, receivers, "RECEIVERS");
           chk_data(((frame[4] >> 6) & 1), MicTS, "TimeStampMic");
           chk_data(((frame[4] >> 7) & 1), CommonMercuryFreq,"Common Mercury Freq");
 
+	  mod=0;
+          rc=frame[3] & 0x03;
+	  if (rc != AlexAtt) {
+	    mod=1;
+	    AlexAtt=rc;
+	  }
+	  rc=(frame[3] & 0x04) >> 2;
+	  if (rc != preamp) {
+	    mod=1;
+	    preamp=rc;
+	  }
+	  rc=(frame[3] & 0x08) >> 3;
+	  if (rc != LTdither) {
+	    mod=1;
+	    LTdither=rc;
+	  }
+	  rc=(frame[3] & 0x10) >> 4;
+	  if (rc != LTrandom) {
+	    mod=1;
+	    LTrandom=rc;
+	  }
+	  if (mod) fprintf(stderr,"AlexAtt=%d Preamp=%d Dither=%d Random=%d\n", AlexAtt,preamp,LTdither,LTrandom);
+
+	  mod=0;
+	  rc=(frame[3] & 0x60) >> 5;
+	  if (rc != alexRXant) {
+	    mod=1;
+	    alexRXant=rc;
+	  }
+	  rc=(frame[3] & 0x80) >> 7;
+	  if (rc != alexRXout) {
+	    mod=1;
+	    alexRXout=rc;
+	  }
+	  rc=(frame[4] >> 0) & 3;
+	  if (rc != AlexTXrel) {
+	    mod=1;
+	    AlexTXrel=rc;
+	  }
+	  rc=(frame[4] >> 2) & 1;
+	  if (rc != duplex) {
+	    mod=1;
+            duplex=rc;
+	  }
+	  if (mod) fprintf(stderr,"RXout=%d RXant=%d TXrel=%d Duplex=%d\n",alexRXout,alexRXant,AlexTXrel,duplex);
+
 	  if (OLDDEVICE == DEVICE_C25) {
               // Charly25: has two 18-dB preamps that are switched with "preamp" and "dither"
               //           and two attenuators encoded in Alex-ATT
diff --git a/new_protocol.c b/new_protocol.c
index 70898ba..0d9f833 100644
--- a/new_protocol.c
+++ b/new_protocol.c
@@ -861,8 +861,6 @@ static void new_protocol_high_priority() {
       case NEW_DEVICE_ORION2:
 //
 //	new ANAN-7000/8000 band-pass RX filters
-//      This info comes from file bpf2_select.v in the
-//      P1 firmware
 //
 //	To support the ANAN-8000 we
 //	should bypass BPFs while transmitting in PURESIGNAL,
@@ -877,12 +875,12 @@ static void new_protocol_high_priority() {
           alex0|=ALEX_ANAN7000_RX_80_60_BPF;
         } else if(rxFrequency<11000000L) {
           alex0|=ALEX_ANAN7000_RX_40_30_BPF;
-        } else if(rxFrequency<20900000L) {
-          alex0|=ALEX_ANAN7000_RX_30_20_17_BPF;
+        } else if(rxFrequency<22000000L) {
+          alex0|=ALEX_ANAN7000_RX_20_15_BPF;
         } else if(rxFrequency<35000000L) {
-          alex0|=ALEX_ANAN7000_RX_15_12_BPF;
+          alex0|=ALEX_ANAN7000_RX_12_10_BPF;
         } else {
-          alex0|=ALEX_ANAN7000_RX_10_6_PRE_BPF;
+          alex0|=ALEX_ANAN7000_RX_6_PRE_BPF;
         }
         break;
       default:
@@ -921,41 +919,20 @@ static void new_protocol_high_priority() {
     if (!isTransmitting() && device != NEW_DEVICE_ORION2 && receiver[0]->alex_antenna < 3) {
 	txFrequency = rxFrequency;
     }
-    switch(device) {
-      case NEW_DEVICE_ORION2:
-        if(txFrequency>32000000) {
-          alex0|=ALEX_6_BYPASS_LPF;
-        } else if(txFrequency>22000000) {
-          alex0|=ALEX_12_10_LPF;
-        } else if(txFrequency>15000000) {
-          alex0|=ALEX_17_15_LPF;
-        } else if(txFrequency>8000000) {
-          alex0|=ALEX_30_20_LPF;
-        } else if(txFrequency>4500000) {
-          alex0|=ALEX_60_40_LPF;
-        } else if(txFrequency>2400000) {
-          alex0|=ALEX_80_LPF;
-        } else {
-          alex0|=ALEX_160_LPF;
-        }
-        break;
-      default:
-        if(txFrequency>35600000) {
-          alex0|=ALEX_6_BYPASS_LPF;
-        } else if(txFrequency>24000000) {
-          alex0|=ALEX_12_10_LPF;
-        } else if(txFrequency>16500000) {
-          alex0|=ALEX_17_15_LPF;
-        } else if(txFrequency>8000000) {
-          alex0|=ALEX_30_20_LPF;
-        } else if(txFrequency>5000000) {
-          alex0|=ALEX_60_40_LPF;
-        } else if(txFrequency>2500000) {
-          alex0|=ALEX_80_LPF;
-        } else {
-          alex0|=ALEX_160_LPF;
-        }
-        break;
+    if(txFrequency>35600000L) {
+      alex0|=ALEX_6_BYPASS_LPF;
+    } else if(txFrequency>24000000L) {
+      alex0|=ALEX_12_10_LPF;
+    } else if(txFrequency>16500000L) {
+      alex0|=ALEX_17_15_LPF;
+    } else if(txFrequency>8000000L) {
+      alex0|=ALEX_30_20_LPF;
+    } else if(txFrequency>5000000L) {
+      alex0|=ALEX_60_40_LPF;
+    } else if(txFrequency>2500000L) {
+      alex0|=ALEX_80_LPF;
+    } else {
+      alex0|=ALEX_160_LPF;
     }
 
 //
@@ -1080,8 +1057,6 @@ static void new_protocol_high_priority() {
 	}
 //
 //      new ANAN-7000/8000 band-pass RX filters
-//      This info comes from file bpf2_select.v in the
-//      P1 firmware
 //
         if(rxFrequency<1500000L) {
           alex1|=ALEX_ANAN7000_RX_BYPASS_BPF;
@@ -1091,12 +1066,12 @@ static void new_protocol_high_priority() {
           alex1|=ALEX_ANAN7000_RX_80_60_BPF;
         } else if(rxFrequency<11000000L) {
           alex1|=ALEX_ANAN7000_RX_40_30_BPF;
-        } else if(rxFrequency<20900000L) {
-          alex1|=ALEX_ANAN7000_RX_30_20_17_BPF;
+        } else if(rxFrequency<22000000L) {
+          alex1|=ALEX_ANAN7000_RX_20_15_BPF;
         } else if(rxFrequency<35000000L) {
-          alex1|=ALEX_ANAN7000_RX_15_12_BPF;
+          alex1|=ALEX_ANAN7000_RX_12_10_BPF;
         } else {
-          alex1|=ALEX_ANAN7000_RX_10_6_PRE_BPF;
+          alex1|=ALEX_ANAN7000_RX_6_PRE_BPF;
         }
 
 //
diff --git a/old_protocol.c b/old_protocol.c
index 88b7a3a..5b00905 100644
--- a/old_protocol.c
+++ b/old_protocol.c
@@ -104,6 +104,36 @@
 #define LT2208_RANDOM_OFF         0x00
 #define LT2208_RANDOM_ON          0x10
 
+#define DEBUG_PROTO 1
+
+#ifdef DEBUG_PROTO
+/*
+ * This is for debugging the protocol
+ */
+
+static int C3_RX1_OUT=-1;
+static int C3_RX1_ANT=-1;
+static int C4_TX_REL=-1;
+static int PC_PTT=-1;
+static int CASE1=-1;
+static int CASE2=-1;
+
+static int C2_FB=-1;
+static int C3_HPF=-1;
+static int C3_BP=-1;
+static int C3_LNA=-1;
+static int C3_TXDIS=-1;
+
+static int C4_LPF=-1;
+
+static int C1_DRIVE=-1;
+
+static int proto_mod=0;
+static int proto_val;
+
+#define CHECK(x, var) proto_val=(x); if ((x) != var) { proto_mod=1; var=proto_val;}
+#endif
+
 //static int buffer_size=BUFFER_SIZE;
 
 static int display_width;
@@ -1185,6 +1215,11 @@ void ozy_send_buffer() {
         output_buffer[C3] |= 0x80;
 	break;
     }
+#ifdef DEBUG_PROTO
+    CHECK(i, CASE1);
+    CHECK((output_buffer[C3] & 0x80) >> 7, C3_RX1_OUT);
+    CHECK((output_buffer[C3] & 0x60) >> 5, C3_RX1_ANT);
+#endif
 
 
     output_buffer[C4]=0x04;  // duplex
@@ -1230,6 +1265,11 @@ void ozy_send_buffer() {
           output_buffer[C4]|=0x03;
           break;
     }
+#ifdef DEBUG_PROTO
+    CHECK(i, CASE2);
+    CHECK(output_buffer[C4] & 0x03, C4_TX_REL);
+#endif
+
     // end of "C0=0" packet
   } else {
     //
@@ -1316,8 +1356,41 @@ void ozy_send_buffer() {
         if (isTransmitting() && transmitter->puresignal && receiver[PS_RX_FEEDBACK]->alex_antenna == 6) {
           output_buffer[C2] |= 0x40;  // enable manual filter selection
           output_buffer[C3] &= 0x80;  // preserve ONLY "PA enable" bit and clear all filters including "6m LNA"
-          output_buffer[C3] |= 0x20;  // bypass all filters
-        }
+          output_buffer[C3] |= 0x20;  // bypass all RX filters
+	  //
+	  // For "manual" filter selection we also need to select the appropriate TX LPF
+	  // 
+	  // We here use the transition frequencies used in Thetis by default. Note the
+	  // P1 firmware has different default transition frequences.
+	  // Even more odd, HERMES routes 15m through the 10/12 LPF, while
+	  // Angelia routes 12m through the 17/15m LPF.
+	  //
+	  long long txFrequency = channel_freq(-1);
+	  if (txFrequency > 35600000L) {		// > 10m so use 6m LPF
+	    output_buffer[C4] = 0x10;
+	  } else if (txFrequency > 24000000L)  {	// > 15m so use 10/12m LPF
+	    output_buffer[C4] = 0x20;
+	  } else if (txFrequency > 16500000L) {		// > 20m so use 17/15m LPF
+	    output_buffer[C4] = 0x40;
+	  } else if (txFrequency >  8000000L) {		// > 40m so use 30/20m LPF
+	    output_buffer[C4] = 0x01;
+	  } else if (txFrequency >  5000000L) {		// > 80m so use 60/40m LPF
+	    output_buffer[C4] = 0x02;
+	  } else if (txFrequency >  2500000L) {		// > 160m so use 80m LPF
+	    output_buffer[C4] = 0x04;
+	  } else {					// < 2.5 MHz use 160m LPF
+	    output_buffer[C4] = 0x08;
+	  }
+	}
+#endif
+#ifdef DEBUG_PROTO
+	CHECK(output_buffer[C1], C1_DRIVE);
+	CHECK((output_buffer[C2] & 0xE0) >> 5, C2_FB);
+	CHECK(output_buffer[C3] & 0x1F, C3_HPF);
+        CHECK((output_buffer[C3] & 0x20) >> 5, C3_BP);
+        CHECK((output_buffer[C3] & 0x40) >> 6, C3_LNA);
+        CHECK((output_buffer[C3] & 0x80) >> 7, C3_TXDIS);
+        CHECK(output_buffer[C4], C4_LPF);
 #endif
         }
         break;
@@ -1486,6 +1559,20 @@ void ozy_send_buffer() {
       output_buffer[C0]|=0x01;
     }
   }
+#ifdef DEBUG_PROTO
+  CHECK(output_buffer[C0] & 1, PC_PTT);
+
+/*
+ * ship out line after each complete run
+ */
+  if (proto_mod && command == 1) {
+    fprintf(stderr,"DIS=%d DRIVE=%3d PTT=%d i1=%4d RXout=%d RXant=%d i2=%d TXrel=%d FB=%d BP=%d LNA=%d HPF=%02x LPF=%02x\n",
+     C3_TXDIS, C1_DRIVE, PC_PTT, CASE1, C3_RX1_OUT, C3_RX1_ANT, CASE2, C4_TX_REL,
+     C2_FB, C3_BP, C3_LNA, C3_HPF, C4_LPF);
+    proto_mod=0;
+  }
+#endif
+     
 
 #ifdef USBOZY
 //
-- 
2.45.2