#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
+#include <sys/time.h>
#include <string.h>
#include <errno.h>
#define SPEED_192K 0x02
#define SPEED_384K 0x03
-static int display_width;
-static int running;
-static int sampleSpeed =0;
-
-unsigned char tx_iqdata[6];
+#define RECEIVED_SAMPLES 128
static GThread *radioberry_thread_id;
static gpointer radioberry_thread(gpointer arg);
-static GThread *rx_stream_thread_id;
-static gpointer rx_stream_thread(gpointer arg);
+static int running;
static void setSampleSpeed(int r);
-static void handleReceiveStream(int r);
-
-struct timeval rx1_t0;
-struct timeval rx1_t1;
-struct timeval rx2_t0;
-struct timeval rx2_t1;
-struct timeval t10;
-struct timeval t11;
+static int sampleSpeed[2];
+
+unsigned char tx_iqdata[6];
struct timeval t20;
struct timeval t21;
-float elapsed;
void spiWriter();
-void rx1_spiReader();
-void rx2_spiReader();
+void rx1_spiReader(unsigned char iqdata[]);
+void rx2_spiReader(unsigned char iqdata[]);
int prev_drive_level;
-
-static int rx1_count =0;
-static int rx2_count =0;
static int txcount =0;
-GMutex buf_rx1_mutex;
-GMutex buf_rx2_mutex;
GMutex mutex_rxtx;
-#define MAX_RX_BUFFER (24000)
-
-double buffer_rx1[MAX_RX_BUFFER][2];
-int fill_rx1 = 0;
-int use_rx1 = 0;
-sem_t empty;
-sem_t full;
-
-void put_rx1(unsigned char[]);
-void get_rx1(double buffer[]);
-
-double buffer_rx2[MAX_RX_BUFFER][2];
-int fill_rx2 = 0;
-int use_rx2 = 0;
-sem_t empty_rx2;
-sem_t full_rx2;
-
-void put_rx2(unsigned char[]);
-void get_rx2(double buffer[]);
-
-#ifdef PSK
-static int psk_samples=0;
-static int psk_divisor=6;
-#endif
-
static int rx1_spi_handler;
static int rx2_spi_handler;
void radioberry_protocol_init(int rx,int pixels) {
int i;
- sem_init(&empty, 0, MAX_RX_BUFFER);
- sem_init(&full, 0, 0);
- sem_init(&empty_rx2, 0, MAX_RX_BUFFER);
- sem_init(&full_rx2, 0, 0);
-
- fprintf(stderr,"radioberry_protocol_init\n");
- display_width=pixels;
- fprintf(stderr,"radioberry_protocol: buffer size: =%d\n", buffer_size);
+ fprintf(stderr,"\n");
+ fprintf(stderr, "====================================================================\n");
+ fprintf(stderr, "====================================================================\n");
+ fprintf(stderr, " Radioberry V2.0 beta 2.\n");
+ fprintf(stderr, "\n");
+ fprintf(stderr, " PIHPSDR plugin version 10-5-2018 \n");
+ fprintf(stderr, "\n");
+ fprintf(stderr, "\n");
+ fprintf(stderr, " Have fune Johan PA3GSB\n");
+ fprintf(stderr, "\n");
+ fprintf(stderr, "\n");
+ fprintf(stderr, "====================================================================\n");
+ fprintf(stderr, "====================================================================\n");
#ifndef GPIO
if (gpioInitialise() < 0) {
exit(-1);
}
#endif
-
- gpioSetMode(13, PI_INPUT); //rx1_FIFOEmpty
- gpioSetMode(16, PI_INPUT); //rx2_FIFOEmpty
+ gpioSetMode(13, PI_INPUT); //rx1_x-samples received.
+ gpioSetMode(16, PI_INPUT); //rx2_x-samples received.
gpioSetMode(20, PI_INPUT);
gpioSetMode(21, PI_OUTPUT);
exit( -1 );
}
fprintf(stderr, "radioberry_thread: id=%p\n",radioberry_thread_id);
-
- rx_stream_thread_id = g_thread_new( "rx-streaming", rx_stream_thread, NULL);
- if( ! rx_stream_thread_id )
- {
- fprintf(stderr,"g_thread_new failed on rx_stream_thread\n");
- exit( -1 );
- }
- fprintf(stderr, "rx1-streaming: id=%p\n",rx_stream_thread_id);
+
}
-static gpointer rx_stream_thread(gpointer arg) {
+void convertToDouble(unsigned char* value, double buffer[]) {
- double _iqdata[2];
+ int left_sample;
+ int right_sample;
+ double left_sample_double;
+ double right_sample_double;
- fprintf(stderr, "radioberry_protocol: rx_stream_thread\n");
+ left_sample = (int)((signed char) value[0]) << 16;
+ left_sample |= (int)((((unsigned char)value[1]) << 8)&0xFF00);
+ left_sample |= (int)((unsigned char)value[2]&0xFF);
+ right_sample = (int)((signed char) value[3]) << 16;
+ right_sample |= (int)((((unsigned char)value[4]) << 8)&0xFF00);
+ right_sample |= (int)((unsigned char)value[5]&0xFF);
- while(running) {
-
- get_rx1(_iqdata);
- // add the samples to the receiver..
- add_iq_samples(receiver[0], _iqdata[0], _iqdata[1]);
-
- if (receivers==2) {
- get_rx2(_iqdata);
- // add the samples to the receiver..
- add_iq_samples(receiver[1], _iqdata[0], _iqdata[1]);
- };
- }
+ 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
- fprintf(stderr, "radioberry_protocol stop: rx_stream_thread\n");
+ buffer[0] = left_sample_double;
+ buffer[1] = right_sample_double;
}
-
-
static gpointer radioberry_thread(gpointer arg) {
fprintf(stderr, "radioberry_protocol: radioberry_thread\n");
-
- gettimeofday(&t20, 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);
+ unsigned char iqdata[6];
+ double _iqdata[2];
+
while(running) {
if (!isTransmitting())
gpioWrite(21, 0);
- rx1_spiReader();
- rx2_spiReader();
+ setSampleSpeed(0);
+ setSampleSpeed(1);
+
+ if (gpioRead(13) == 1) {
+ int i=0;
+ for (i=0; i< RECEIVED_SAMPLES; i++) {
+ rx1_spiReader(iqdata);
+ convertToDouble(iqdata, _iqdata);
+ add_iq_samples(receiver[0], _iqdata[0], _iqdata[1]);
+ }
+ }
+
+ if (receivers==2) {
+ if (gpioRead(16) == 1) {
+ int i=0;
+ for (i=0; i< RECEIVED_SAMPLES; i++) {
+ rx2_spiReader(iqdata);
+ convertToDouble(iqdata, _iqdata);
+ add_iq_samples(receiver[1], _iqdata[0], _iqdata[1]);
+ }
+ }
+ }
g_mutex_unlock(&mutex_rxtx);
}
switch(receiver[r]->sample_rate) {
case 48000:
- sampleSpeed=SPEED_48K;
+ sampleSpeed[r]=SPEED_48K;
break;
case 96000:
- sampleSpeed=SPEED_96K;
+ sampleSpeed[r]=SPEED_96K;
break;
case 192000:
- sampleSpeed=SPEED_192K;
+ sampleSpeed[r]=SPEED_192K;
break;
case 384000:
- sampleSpeed=SPEED_384K;
+ sampleSpeed[r]=SPEED_384K;
break;
}
}
}
-void rx1_spiReader() {
- unsigned char iqdata[6];
-
- // return till rxFIFO buffer is filled with at least one element
- while ( gpioRead(13) == 1) {return;};
+void rx1_spiReader(unsigned char iqdata[]) {
- setSampleSpeed(0);
-
long long rxFrequency=vfo[0].frequency-vfo[0].lo;
if(vfo[0].rit_enabled) {
rxFrequency+=vfo[0].rit;
}
}
- iqdata[0] = (sampleSpeed & 0x03);
- iqdata[1] = (((active_receiver->random << 6) & 0x40) | ((active_receiver->dither <<5) & 0x20) | (attenuation & 0x1F));
+ iqdata[0] = (sampleSpeed[0] & 0x03);
+ iqdata[1] = (((active_receiver->random << 6) & 0x40) | ((receiver[0]->dither <<5) & 0x20) | (attenuation & 0x1F));
iqdata[2] = ((rxFrequency >> 24) & 0xFF);
iqdata[3] = ((rxFrequency >> 16) & 0xFF);
iqdata[4] = ((rxFrequency >> 8) & 0xFF);
iqdata[5] = (rxFrequency & 0xFF);
spiXfer(rx1_spi_handler, iqdata, iqdata, 6);
-
- put_rx1(iqdata);
-
- 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);
- }
}
-void rx2_spiReader() {
-
- unsigned char iqdata[6];
-
- if (receivers==1) return;
-
- // return till rx2FIFO buffer is filled with at least one element
- while ( gpioRead(16) == 1) {return;};
-
- setSampleSpeed(1);
+void rx2_spiReader(unsigned char iqdata[]) {
long long rxFrequency=vfo[1].frequency-vfo[1].lo;
if(vfo[1].rit_enabled) {
}
}
- iqdata[0] = (sampleSpeed & 0x03);
+ iqdata[0] = (sampleSpeed[1] & 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);
-
- put_rx2(iqdata);
-
- 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);
- }
-
+ spiXfer(rx2_spi_handler, iqdata, iqdata, 6);
}
void spiWriter() {
gettimeofday(&t20, 0);
}
}
-
-void put_rx1(unsigned char* value) {
-
- int left_sample;
- int right_sample;
- double left_sample_double;
- double right_sample_double;
-
- sem_wait(&empty);
-
- left_sample = (int)((signed char) value[0]) << 16;
- left_sample |= (int)((((unsigned char)value[1]) << 8)&0xFF00);
- left_sample |= (int)((unsigned char)value[2]&0xFF);
- right_sample = (int)((signed char) value[3]) << 16;
- right_sample |= (int)((((unsigned char)value[4]) << 8)&0xFF00);
- right_sample |= (int)((unsigned char)value[5]&0xFF);
-
- 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
-
- buffer_rx1[fill_rx1][0] = left_sample_double;
- buffer_rx1[fill_rx1][1] = right_sample_double;
-
- g_mutex_lock(&buf_rx1_mutex);
- fill_rx1 = (fill_rx1 + 1) % MAX_RX_BUFFER;
- g_mutex_unlock(&buf_rx1_mutex);
-
- sem_post(&full);
-}
-
-void get_rx1(double buffer[]) {
- sem_wait(&full);
-
- buffer[0] = buffer_rx1[use_rx1][0];
- buffer[1] = buffer_rx1[use_rx1][1];
-
- g_mutex_lock(&buf_rx1_mutex);
- use_rx1 = (use_rx1 + 1) % MAX_RX_BUFFER;
- g_mutex_unlock(&buf_rx1_mutex);
-
- sem_post(&empty);
-}
-
-void put_rx2(unsigned char* value) {
-
- int left_sample;
- int right_sample;
- double left_sample_double;
- double right_sample_double;
-
- sem_wait(&empty_rx2);
-
- left_sample = (int)((signed char) value[0]) << 16;
- left_sample |= (int)((((unsigned char)value[1]) << 8)&0xFF00);
- left_sample |= (int)((unsigned char)value[2]&0xFF);
- right_sample = (int)((signed char) value[3]) << 16;
- right_sample |= (int)((((unsigned char)value[4]) << 8)&0xFF00);
- right_sample |= (int)((unsigned char)value[5]&0xFF);
-
- 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
-
- buffer_rx2[fill_rx2][0] = left_sample_double;
- buffer_rx2[fill_rx2][1] = right_sample_double;
-
- g_mutex_lock(&buf_rx2_mutex);
- fill_rx2 = (fill_rx2 + 1) % MAX_RX_BUFFER;
- g_mutex_unlock(&buf_rx2_mutex);
-
- sem_post(&full_rx2);
-}
-
-void get_rx2(double buffer[]) {
- sem_wait(&full_rx2);
-
- buffer[0] = buffer_rx2[use_rx2][0];
- buffer[1] = buffer_rx2[use_rx2][1];
-
- g_mutex_lock(&buf_rx2_mutex);
- use_rx2 = (use_rx2 + 1) % MAX_RX_BUFFER;
- g_mutex_unlock(&buf_rx2_mutex);
-
- sem_post(&empty_rx2);
-}
-
// end of source