#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
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;
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;
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)
}
#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");
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);
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);
}
}
tx_iqdata[5] = qsample;
spiWriter();
-
- sem_post(&mutex);
}
}
}
}
-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;
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;
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();
}
-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);
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) {