From: PA3GSB <admin@pa3gsb.nl>
Date: Sat, 25 Feb 2017 09:56:57 +0000 (+0100)
Subject: 2-receivers
X-Git-Url: https://git.rkrishnan.org/pf/content/schema.xhtml?a=commitdiff_plain;h=3d7eb9c51bb34b79eeabaabcee75192020f5ef71;p=pihpsdr.git

2-receivers
---

diff --git a/radio.c b/radio.c
index bf571a5..d0ec898 100644
--- a/radio.c
+++ b/radio.c
@@ -373,11 +373,6 @@ fprintf(stderr,"start_radio: selected radio=%p device=%d\n",radio,radio->device)
     case LIMESDR_PROTOCOL:
       sprintf(property_path,"limesdr.props");
       break;
-#endif
-#ifdef RADIOBERRY
-    case RADIOBERRY_PROTOCOL:
-      sprintf(property_path,"radioberry.props");
-      break;
 #endif
   }
 
diff --git a/radioberry.c b/radioberry.c
index d28925c..0464ea8 100644
--- a/radioberry.c
+++ b/radioberry.c
@@ -45,10 +45,8 @@
 #include "receiver.h"
 #include "transmitter.h"
 #include "vfo.h"
-
 #include <pigpio.h>
 
-#define OUTPUT_BUFFER_SIZE 1024
 #define SPEED_48K                 0x00
 #define SPEED_96K                 0x01
 #define SPEED_192K                0x02
@@ -65,11 +63,13 @@ static pthread_t radioberry_thread_id;
 static void start_radioberry_thread();
 static void *radioberry_thread(void* arg);
 
-static void setSampleSpeed();
-static void handleReceiveStream();
+static void setSampleSpeed(int r);
+static void handleReceiveStream(int r);
 
-struct timeval t0;
-struct timeval t1;
+struct timeval rx1_t0;
+struct timeval rx1_t1;
+struct timeval rx2_t0;
+struct timeval rx2_t1;
 struct timeval t10;
 struct timeval t11;
 struct timeval t20;
@@ -81,12 +81,14 @@ float elapsed;
 static int radiostate = RADIOSTATE_RX;
 
 void spiWriter();
-void spiReader();
+void rx1_spiReader();
+void rx2_spiReader();
 
 
 int prev_drive_level;
 
-static int rxcount =0;
+static int rx1_count =0;
+static int rx2_count =0;
 static int txcount =0;
 
 sem_t mutex;
@@ -96,7 +98,8 @@ static int psk_samples=0;
 static int psk_divisor=6;
 #endif
 
-static int h;
+static int rx1_spi_handler;
+static int rx2_spi_handler;
 
 
 float timedifference_msec(struct timeval t0, struct timeval t1)
@@ -119,20 +122,25 @@ void radioberry_protocol_init(int rx,int pixels) {
 	}
 #endif  
  
-	gpioSetMode(13, PI_INPUT); 
+	gpioSetMode(13, PI_INPUT); 	//rx1_FIFOEmpty
+	gpioSetMode(16, PI_INPUT);	//rx2_FIFOEmpty
 	gpioSetMode(20, PI_INPUT); 
 	gpioSetMode(21, PI_OUTPUT); 
    
-	h = spiOpen(0, 15625000, 49155); 
-	if (h < 0) {
-		fprintf(stderr,"radioberry_protocol: spi bus could not be initialized. \n");
+	rx1_spi_handler = spiOpen(0, 15625000, 49155);  //channel 0
+	if (rx1_spi_handler < 0) {
+		fprintf(stderr,"radioberry_protocol: spi bus rx1 could not be initialized. \n");
 		exit(-1);
 	}
 	
-	printf("init done \n");
-  
-	setSampleSpeed();
+	rx2_spi_handler = spiOpen(1, 15625000, 49155); 	//channel 1
+	if (rx2_spi_handler < 0) {
+		fprintf(stderr,"radioberry_protocol: spi bus rx2 could not be initialized. \n");
+		exit(-1);
+	}
 
+	printf("init done \n");
+ 
 	if(transmitter->local_microphone) {
 		if(audio_open_input()!=0) {
 		  fprintf(stderr,"audio_open_input failed\n");
@@ -159,9 +167,11 @@ static void *radioberry_thread(void* arg) {
 	running=1;
 
 	gettimeofday(&t20, 0);
-	gettimeofday(&t0, 0);
+	gettimeofday(&rx1_t0, 0);
+	gettimeofday(&rx2_t0, 0);
 	
 	gpioSetMode(13, PI_INPUT); 
+	gpioSetMode(16, PI_INPUT); 
 	gpioSetMode(20, PI_INPUT); 
 	gpioSetMode(21, PI_OUTPUT); 
 	
@@ -176,12 +186,34 @@ static void *radioberry_thread(void* arg) {
 		
 		if(radiostate == RADIOSTATE_TX) {
 			gpioWrite(21, 1); 
+			
+			sem_post(&mutex);
 		}
 		else 
 		{
 			gpioWrite(21, 0);
-			spiReader();
-			handleReceiveStream();
+			
+			//possible to use different sample rates per receiver... 
+			//if the sample rate differs between the receivers.. the highest sample rate 
+			//must be called an extra time.
+			if (receivers==1){
+				rx1_spiReader();
+			} else {
+				if (receiver[0]->sample_rate == receiver[1]->sample_rate) {
+					rx1_spiReader();
+					rx2_spiReader();
+				} else {
+					if (receiver[0]->sample_rate > receiver[1]->sample_rate) {
+							rx1_spiReader();
+							rx1_spiReader();
+							rx2_spiReader();
+					} else {
+							rx1_spiReader();
+							rx2_spiReader();
+							rx2_spiReader();
+					}
+				}
+			}
 			sem_post(&mutex);
 		}
 	}
@@ -203,8 +235,6 @@ void radioberry_protocol_iq_samples(int isample,int qsample) {
 		tx_iqdata[5] = qsample;
 
 		spiWriter();
-		
-		sem_post(&mutex);
 	}
 
 }
@@ -226,12 +256,11 @@ void *radioberry_protocol_process_local_mic(unsigned char *buffer,int le) {
 	}
 }
 
-static void handleReceiveStream() {
+static void handleReceiveStream(int r) {
 	int left_sample;
 	int right_sample;
 	double left_sample_double;
 	double right_sample_double;
-	int r;
   
 	left_sample   = (int)((signed char) iqdata[0]) << 16;
 	left_sample  += (int)((unsigned char)iqdata[1]) << 8;
@@ -243,13 +272,14 @@ static void handleReceiveStream() {
 	left_sample_double=(double)left_sample/8388607.0; // 24 bit sample 2^23-1
     right_sample_double=(double)right_sample/8388607.0; // 24 bit sample 2^23-1
 	
-	for(r=0;r<RECEIVERS;r++) {
-		add_iq_samples(receiver[r], left_sample_double,right_sample_double);
-	}
+	// add the samples to the receiver..
+	add_iq_samples(receiver[r], left_sample_double,right_sample_double);
+	
 }
 
-void setSampleSpeed() {
-     switch(active_receiver->sample_rate) {
+void setSampleSpeed(int r) {
+	
+     switch(receiver[r]->sample_rate) {
         case 48000:
           sampleSpeed=SPEED_48K;
           break;
@@ -269,8 +299,10 @@ void radioberry_protocol_stop() {
   
 	running=FALSE;
 
-	if (h !=0)
-		spiClose(h);
+	if (rx1_spi_handler !=0)
+		spiClose(rx1_spi_handler);
+	if (rx2_spi_handler !=0)
+		spiClose(rx2_spi_handler);
 		
 #ifndef GPIO 
 	gpioTerminate();
@@ -278,21 +310,22 @@ void radioberry_protocol_stop() {
 	
 }
 
-void spiReader() {
+void rx1_spiReader() {
 	// wait till rxFIFO buffer is filled with at least one element
 	while ( gpioRead(13) == 1) {};
 	
-	setSampleSpeed();
-	
-	int v=receiver[0]->id;
-	long long rxFrequency=vfo[v].frequency-vfo[v].lo;
-	if(vfo[v].rit_enabled) {
-	  rxFrequency+=vfo[v].rit;
+	setSampleSpeed(0);
+
+	long long rxFrequency=vfo[0].frequency-vfo[0].lo;
+	if(vfo[0].rit_enabled) {
+	  rxFrequency+=vfo[0].rit;
 	}
-	if(vfo[active_receiver->id].mode==modeCWU) {
-	  rxFrequency-=(long long)cw_keyer_sidetone_frequency;
-	} else if(vfo[active_receiver->id].mode==modeCWL) {
-	  rxFrequency+=(long long)cw_keyer_sidetone_frequency;
+	if (active_receiver->id==0) {
+		if(vfo[0].mode==modeCWU) {
+			rxFrequency-=(long long)cw_keyer_sidetone_frequency;
+		} else if(vfo[0].mode==modeCWL) {
+			rxFrequency+=(long long)cw_keyer_sidetone_frequency;
+		}
 	}
 	
 	iqdata[0] = (sampleSpeed & 0x03);
@@ -302,23 +335,67 @@ void spiReader() {
 	iqdata[4] = ((rxFrequency >> 8) & 0xFF);
 	iqdata[5] = (rxFrequency & 0xFF);
 			
-	spiXfer(h, iqdata, iqdata, 6);
+	spiXfer(rx1_spi_handler, iqdata, iqdata, 6);
 	
 	//firmware: tdata(56'h00010203040506) -> 0-1-2-3-4-5-6 (element 0 contains 0; second element contains 1)
-	rxcount ++;
-	if (rxcount == 48000) {
-		rxcount = 0;
-		gettimeofday(&t1, 0);
-		elapsed = timedifference_msec(t0, t1);
-		printf("Code rx mode spi executed in %f milliseconds.\n", elapsed);
-		gettimeofday(&t0, 0);
+	rx1_count ++;
+	if (rx1_count == 48000) {
+		rx1_count = 0;
+		gettimeofday(&rx1_t1, 0);
+		elapsed = timedifference_msec(rx1_t0, rx1_t1);
+		printf("Code rx1 mode spi executed in %f milliseconds.\n", elapsed);
+		gettimeofday(&rx1_t0, 0);
 	}
+	
+	handleReceiveStream(0);
 }
 
+void rx2_spiReader() {
+
+	// wait till rx2FIFO buffer is filled with at least one element
+	while ( gpioRead(16) == 1) {};
+	
+	setSampleSpeed(1);
+	
+	long long rxFrequency=vfo[1].frequency-vfo[1].lo;
+	if(vfo[1].rit_enabled) {
+	  rxFrequency+=vfo[1].rit;
+	}
+	if (active_receiver->id==1) {
+		if(vfo[1].mode==modeCWU) {
+			rxFrequency-=(long long)cw_keyer_sidetone_frequency;
+		} else if(vfo[1].mode==modeCWL) {
+			rxFrequency+=(long long)cw_keyer_sidetone_frequency;
+		}
+	}
+	
+	iqdata[0] = (sampleSpeed & 0x03);
+	iqdata[1] = 0x00;	//not used by firmware; the active receiver settings are set via rx1
+	iqdata[2] = ((rxFrequency >> 24) & 0xFF);
+	iqdata[3] = ((rxFrequency >> 16) & 0xFF);
+	iqdata[4] = ((rxFrequency >> 8) & 0xFF);
+	iqdata[5] = (rxFrequency & 0xFF);
+			
+	spiXfer(rx2_spi_handler, iqdata, iqdata, 6);
+	
+	//firmware: tdata(56'h00010203040506) -> 0-1-2-3-4-5-6 (element 0 contains 0; second element contains 1)
+	rx2_count ++;
+	if (rx2_count == 48000) {
+		rx2_count = 0;
+		gettimeofday(&rx2_t1, 0);
+		elapsed = timedifference_msec(rx2_t0, rx2_t1);
+		printf("Code rx2 mode spi executed in %f milliseconds.\n", elapsed);
+		gettimeofday(&rx2_t0, 0);
+	}
+	
+	handleReceiveStream(1);
+}
+
+
 void spiWriter() {
 	while ( gpioRead(20) == 1) {};
 
-	spiXfer(h, tx_iqdata, tx_iqdata, 6);
+	spiXfer(rx1_spi_handler, tx_iqdata, tx_iqdata, 6);
 	
 	txcount ++;
 	if (txcount == 48000) {