From: PA3GSB Date: Sat, 18 Feb 2017 14:34:33 +0000 (+0100) Subject: rebased on original branch X-Git-Url: https://git.rkrishnan.org/uri/%3C?a=commitdiff_plain;h=47f5b7fde3832e911c6b772aed3228195ed236f6;p=pihpsdr.git rebased on original branch --- diff --git a/Makefile b/Makefile index abb4e3f..5a53a9e 100644 --- a/Makefile +++ b/Makefile @@ -52,7 +52,7 @@ endif #LIMESDR_INCLUDE=LIMESDR # uncomment the line below when Radioberry radio cape is plugged in -#RADIOBERRY_INCLUDE=RADIOBERRY +RADIOBERRY_INCLUDE=RADIOBERRY ifeq ($(RADIOBERRY_INCLUDE),RADIOBERRY) RADIOBERRY_OPTIONS=-D RADIOBERRY diff --git a/audio_menu.c b/audio_menu.c index 2826dce..4725411 100644 --- a/audio_menu.c +++ b/audio_menu.c @@ -160,7 +160,11 @@ void audio_menu(GtkWidget *parent) { int row=0; - if(protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL) { +#ifdef RADIOBERRY + if(protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL || protocol==RADIOBERRY_PROTOCOL) { +#else + if(protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL) { +#endif linein_b=gtk_check_button_new_with_label("Mic Line In (ACC connector)"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (linein_b), mic_linein); gtk_grid_attach(GTK_GRID(grid),linein_b,0,++row,1,1); @@ -229,7 +233,11 @@ void audio_menu(GtkWidget *parent) { gtk_widget_show_all(dialog); - if(local_microphone && (protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL)) { +#ifdef RADIOBERRY + if(local_microphone && (protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL || protocol==RADIOBERRY_PROTOCOL)) { +#else + if(local_microphone && (protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL)) { +#endif gtk_widget_hide(linein_b); gtk_widget_hide(micboost_b); } diff --git a/discovered.h b/discovered.h index 44c7f7f..d44fcde 100644 --- a/discovered.h +++ b/discovered.h @@ -54,7 +54,7 @@ #endif #ifdef RADIOBERRY -#define RADIOBERRY_SPI_DEVICE 0 +#define RADIOBERRY_SPI_DEVICE 100 #endif #define STATE_AVAILABLE 2 diff --git a/discovery.c b/discovery.c index 2781504..fa46f02 100644 --- a/discovery.c +++ b/discovery.c @@ -111,7 +111,7 @@ fprintf(stderr,"discovery\n"); #endif #ifdef RADIOBERRY - splash_status("Radioberry SDR ... Discovering Device"); + status_text("Radioberry SDR ... Discovering Device"); radioberry_discovery(); #endif status_text("Discovery"); diff --git a/radio.c b/radio.c index 10e2e17..203b793 100644 --- a/radio.c +++ b/radio.c @@ -41,6 +41,9 @@ #include "property.h" #include "new_protocol.h" #include "old_protocol.h" +#ifdef RADIOBERRY +#include "radioberry.h" +#endif #include "store.h" #ifdef LIMESDR #include "lime_protocol.h" @@ -432,6 +435,19 @@ fprintf(stderr,"receiver %d: height=%d y=%d\n",receiver[i]->id,rx_height,y); transmitter=create_transmitter(CHANNEL_TX, buffer_size, fft_size, updates_per_second, display_width, tx_height); g_object_ref((gpointer)transmitter->panel); + + #ifdef GPIO + if(gpio_init()<0) { + fprintf(stderr,"GPIO failed to initialize\n"); + } +#ifdef LOCALCW + // init local keyer if enabled + else if (cw_keyer_internal == 0) + keyer_update(); +#endif +#endif + + switch(radio->protocol) { case ORIGINAL_PROTOCOL: old_protocol_init(0,display_width,receiver[0]->sample_rate); @@ -443,19 +459,15 @@ fprintf(stderr,"receiver %d: height=%d y=%d\n",receiver[i]->id,rx_height,y); case LIMESDR_PROTOCOL: lime_protocol_init(0,display_width); break; +#endif +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + radioberry_protocol_init(0,display_width); + break; #endif } -#ifdef GPIO - if(gpio_init()<0) { - fprintf(stderr,"GPIO failed to initialize\n"); - } -#ifdef LOCALCW - // init local keyer if enabled - else if (cw_keyer_internal == 0) - keyer_update(); -#endif -#endif + #ifdef I2C i2c_init(); diff --git a/radioberry.c b/radioberry.c index baa3516..d28925c 100644 --- a/radioberry.c +++ b/radioberry.c @@ -42,44 +42,22 @@ #ifdef PSK #include "psk.h" #endif +#include "receiver.h" +#include "transmitter.h" +#include "vfo.h" #include - -#define PI 3.1415926535897932F - #define OUTPUT_BUFFER_SIZE 1024 #define SPEED_48K 0x00 #define SPEED_96K 0x01 #define SPEED_192K 0x02 #define SPEED_384K 0x03 - -static int output_buffer_size; -static int buffer_size=BUFFER_SIZE; - -static int receiver; static int display_width; - static int running; - -static int samples=0; -static int txsamples=0; - -static short tx_sample; -static int left_tx_sample; -static int right_tx_sample; - -static int left_rx_sample; -static int right_rx_sample; - static int sampleSpeed =0; -static double iqinputbuffer[BUFFER_SIZE*2]; -static double micinputbuffer[BUFFER_SIZE*2]; -static double audiooutputbuffer[BUFFER_SIZE*2]; -static double micoutputbuffer[BUFFER_SIZE*2]; - unsigned char iqdata[6]; unsigned char tx_iqdata[6]; @@ -126,47 +104,14 @@ float timedifference_msec(struct timeval t0, struct timeval t1) return (t1.tv_sec - t0.tv_sec) * 1000.0f + (t1.tv_usec - t0.tv_usec) / 1000.0f; } -void radioberry_calc_buffers() { - switch(sample_rate) { - case 48000: - output_buffer_size=OUTPUT_BUFFER_SIZE; - break; - case 96000: - output_buffer_size=OUTPUT_BUFFER_SIZE/2; - break; - case 192000: - output_buffer_size=OUTPUT_BUFFER_SIZE/4; - break; - case 384000: - output_buffer_size=OUTPUT_BUFFER_SIZE/8; - break; - default: - fprintf(stderr,"Invalid sample rate: %d. Defaulting to 48K.\n",sample_rate); - break; - } -} - -void radioberry_new_sample_rate(int rate){ - sample_rate=rate; - radioberry_calc_buffers(); - setSampleSpeed(); - wdsp_new_sample_rate(rate); -} - void radioberry_protocol_init(int rx,int pixels) { - int i; - - fprintf(stderr,"radioberry_protocol_init\n"); - - sem_init(&mutex, 0, 1); //mutal exlusion - - receiver=rx; - display_width=pixels; - - radioberry_calc_buffers(); + int i; + fprintf(stderr,"radioberry_protocol_init\n"); + sem_init(&mutex, 0, 1); //mutal exlusion + display_width=pixels; fprintf(stderr,"radioberry_protocol: buffer size: =%d\n", buffer_size); - + #ifndef GPIO if (gpioInitialise() < 0) { fprintf(stderr,"radioberry_protocol: gpio could not be initialized. \n"); @@ -187,17 +132,13 @@ void radioberry_protocol_init(int rx,int pixels) { printf("init done \n"); setSampleSpeed(); - local_audio=1; - if(audio_open_output()!=0) { - fprintf(stderr,"audio_open_output failed\n"); - local_audio=0; - } - - local_microphone = 1; - if(audio_open_input()!=0) { - fprintf(stderr,"audio_open_input failed\n"); - local_microphone=0; - } + + if(transmitter->local_microphone) { + if(audio_open_input()!=0) { + fprintf(stderr,"audio_open_input failed\n"); + transmitter->local_microphone=0; + } + } start_radioberry_thread(); } @@ -213,11 +154,8 @@ static void start_radioberry_thread() { } static void *radioberry_thread(void* arg) { - unsigned char buffer[2048]; fprintf(stderr, "radioberry_protocol: radioberry_thread\n"); - samples = 0; - txsamples=0; running=1; gettimeofday(&t20, 0); @@ -249,81 +187,51 @@ static void *radioberry_thread(void* arg) { } } -int correctSampleValue(int value) { +void radioberry_protocol_iq_samples(int isample,int qsample) { - if(value>32767) { - value=32767; - } else if(value<-32767) { - value=-32767; - } + if(radiostate == RADIOSTATE_TX) { - return value; + tx_iqdata[0] = 0; + tx_iqdata[1] = drive / 6.4; // convert drive level from 0-255 to 0-39 ) + if (prev_drive_level != drive) { + printf("drive level %d - corrected drive level %d \n", drive_level, tx_iqdata[1]); + prev_drive_level = drive; + } + tx_iqdata[2] = isample>>8; + tx_iqdata[3] = isample; + tx_iqdata[4] = qsample>>8; + tx_iqdata[5] = qsample; + + spiWriter(); + + sem_post(&mutex); + } + } void *radioberry_protocol_process_local_mic(unsigned char *buffer,int le) { -int b; -int leftmicsample; -double mic_sample_double; -double leftmicsampledouble; -double gain=32767.0; // 2^16-1 + int b; + short mic_sample; + // always 48000 samples per second b=0; int i; for(i=0;i<1024;i++) { - leftmicsample = (int)((unsigned char)buffer[b++] & 0xFF); - leftmicsample |= (int)((signed char) buffer[b++]) << 8; - //rightmicsample=leftmicsample; - - mic_sample_double=(1.0 / 2147483648.0) * (double)(leftmicsample<<16); - - micinputbuffer[i*2]=mic_sample_double; - micinputbuffer[(i*2)+1]=mic_sample_double; - } - - if(vox_enabled && (vox || local_microphone)) { - update_vox(micinputbuffer,BUFFER_SIZE); - } - - if(radiostate == RADIOSTATE_TX) { - - int error; - fexchange0(CHANNEL_TX, micinputbuffer, micoutputbuffer, &error); - if(error!=0) { - fprintf(stderr,"fexchange0 (CHANNEL_TX) returned error: %d\n", error); + if(le) { + mic_sample = (short)((buffer[b++]&0xFF) | (buffer[b++]<<8)); + } else { + mic_sample = (short)((buffer[b++]<<8) | (buffer[b++]&0xFF)); } - Spectrum0(1, CHANNEL_TX, 0, 0, micoutputbuffer); - - int j; - for(j=0;j<1024;j++) { - left_tx_sample=(int)(micoutputbuffer[j*2]*gain); - left_tx_sample = correctSampleValue(left_tx_sample); - right_tx_sample=(int)(micoutputbuffer[(j*2)+1]*gain); - right_tx_sample = correctSampleValue(right_tx_sample); - - tx_iqdata[0] = 0; - tx_iqdata[1] = drive / 6.4; // convert drive level from 0-255 to 0-39 ) - if (prev_drive_level != drive) { - printf("drive level %d - corrected drive level %d \n", drive_level, tx_iqdata[1]); - prev_drive_level = drive; - } - tx_iqdata[2] = left_tx_sample>>8; - tx_iqdata[3] = left_tx_sample; - tx_iqdata[4] = right_tx_sample>>8; - tx_iqdata[5] = right_tx_sample; - - spiWriter(); - } - sem_post(&mutex); + add_mic_sample(transmitter,mic_sample); } } static void handleReceiveStream() { - int error; int left_sample; int right_sample; - int mic_sample; - float left_sample_float; - float right_sample_float; + 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; @@ -332,57 +240,16 @@ static void handleReceiveStream() { right_sample += (int)((unsigned char)iqdata[4]) << 8; right_sample += (int)((unsigned char)iqdata[5]); - left_sample_float=(float)left_sample/8388607.0; // 24 bit sample 2^23-1 - right_sample_float=(float)right_sample/8388607.0; // 24 bit sample 2^23-1 - iqinputbuffer[samples*2]=(double)left_sample_float; - iqinputbuffer[(samples*2)+1]=(double)right_sample_float; - samples++; + 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 - if(samples==buffer_size) { - // process the input - fexchange0(CHANNEL_RX0, iqinputbuffer, audiooutputbuffer, &error); - if(error!=0) { - samples=0; - fprintf(stderr,"fexchange2 (CHANNEL_RX0) returned error: %d\n", error); - } - - if(local_audio) { - int j; - for(j=0;jsample_rate) { case 48000: sampleSpeed=SPEED_48K; break; @@ -409,18 +276,27 @@ void radioberry_protocol_stop() { gpioTerminate(); #endif - audio_close_input(); - audio_close_output(); } void spiReader() { // wait till rxFIFO buffer is filled with at least one element while ( gpioRead(13) == 1) {}; - long long rxFrequency=ddsFrequency+(long long)rit; + setSampleSpeed(); + + int v=receiver[0]->id; + long long rxFrequency=vfo[v].frequency-vfo[v].lo; + if(vfo[v].rit_enabled) { + rxFrequency+=vfo[v].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; + } iqdata[0] = (sampleSpeed & 0x03); - iqdata[1] = (((rx_random << 6) & 0x40) | ((rx_dither <<5) & 0x20) | (attenuation & 0x1F)); + iqdata[1] = (((active_receiver->random << 6) & 0x40) | ((active_receiver->dither <<5) & 0x20) | (attenuation & 0x1F)); iqdata[2] = ((rxFrequency >> 24) & 0xFF); iqdata[3] = ((rxFrequency >> 16) & 0xFF); iqdata[4] = ((rxFrequency >> 8) & 0xFF); diff --git a/radioberry.h b/radioberry.h index 9d651ae..3e8a090 100644 --- a/radioberry.h +++ b/radioberry.h @@ -21,4 +21,4 @@ void radioberry_protocol_stop(); void radioberry_protocol_init(int rx,int pixels); void *radioberry_protocol_process_local_mic(unsigned char *buffer,int le); -void radioberry_new_sample_rate(int rate); +extern void radioberry_protocol_iq_samples(int isample,int qsample); diff --git a/receiver.c b/receiver.c index 221572c..b490d9c 100644 --- a/receiver.c +++ b/receiver.c @@ -45,6 +45,9 @@ #ifdef FREEDV #include "freedv.h" #endif +#ifdef RADIOBERRY +#include "radioberry.h" +#endif #define min(x,y) (xadc=0; break; default: @@ -921,6 +930,11 @@ static void process_freedv_rx_buffer(RECEIVER *rx) { #ifdef LIMESDR case LIMESDR_PROTOCOL: break; +#endif +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + //no audio stream to radioberry hardware, using local audio of rpi. + break; #endif } } @@ -986,6 +1000,11 @@ static void process_rx_buffer(RECEIVER *rx) { #ifdef LIMESDR case LIMESDR_PROTOCOL: break; +#endif +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + //no audio stream to radioberry hardware, using local audio of rpi. + break; #endif } } diff --git a/rx_menu.c b/rx_menu.c index 3d1318d..bea30ff 100644 --- a/rx_menu.c +++ b/rx_menu.c @@ -134,6 +134,9 @@ void rx_menu(GtkWidget *parent) { int x=0; switch(protocol) { +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: +#endif case NEW_PROTOCOL: { GtkWidget *sample_rate_label=gtk_label_new("Sample Rate"); @@ -196,6 +199,9 @@ void rx_menu(GtkWidget *parent) { } switch(protocol) { +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: +#endif case ORIGINAL_PROTOCOL: case NEW_PROTOCOL: { diff --git a/soundio.c b/soundio.c index 622d54a..8b1f983 100644 --- a/soundio.c +++ b/soundio.c @@ -220,6 +220,11 @@ fprintf(stderr,"audio_open_input: %d\n",n_selected_input_device); fprintf(stderr,"audio_open_input: selected=%d:%s\n",n_selected_input_device,selected); switch(protocol) { +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + mic_buffer_size = 1024; + break; +#endif case ORIGINAL_PROTOCOL: mic_buffer_size = 720; break; @@ -414,6 +419,11 @@ fprintf(stderr,"mic_read_thread: mic_buffer_size=%d\n",mic_buffer_size); } else { // process the mic input switch(protocol) { +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + radioberry_protocol_iq_samples(mic_buffer,1); + break; +#endif case ORIGINAL_PROTOCOL: old_protocol_process_local_mic(mic_buffer,1); break; diff --git a/transmitter.c b/transmitter.c index f4a7812..0a161d3 100644 --- a/transmitter.c +++ b/transmitter.c @@ -119,7 +119,11 @@ static gint update_display(gpointer data) { double constant1=3.3; double constant2=0.095; - if(protocol==ORIGINAL_PROTOCOL) { +#ifdef RADIOBERRY + if(protocol==ORIGINAL_PROTOCOL || protocol==RADIOBERRY_PROTOCOL) { +#else + if(protocol==ORIGINAL_PROTOCOL) { +#endif switch(device) { case DEVICE_METIS: constant1=3.3; @@ -143,6 +147,10 @@ static gint update_display(gpointer data) { break; case DEVICE_HERMES_LITE: break; +#ifdef RADIOBERRY + case RADIOBERRY_SPI_DEVICE: + break; +#endif } int power=alex_forward_power; @@ -193,6 +201,12 @@ static gint update_display(gpointer data) { constant1=3.3; constant2=0.09; break; +#ifdef RADIOBERRY + case RADIOBERRY_SPI_DEVICE: + constant1=3.3; + constant2=0.09; + break; +#endif } int power=alex_forward_power; @@ -309,7 +323,12 @@ TRANSMITTER *create_transmitter(int id, int buffer_size, int fft_size, int fps, tx->buffer_size=buffer_size; tx->fft_size=fft_size; tx->fps=fps; - if(protocol==ORIGINAL_PROTOCOL) { + +#ifdef RADIOBERRY + if(protocol==ORIGINAL_PROTOCOL || protocol==RADIOBERRY_PROTOCOL) { +#else + if(protocol==ORIGINAL_PROTOCOL) { +#endif tx->mic_sample_rate=48000; // tx->mic_sample_rate=receiver[0]->sample_rate; tx->mic_dsp_rate=48000; @@ -505,6 +524,9 @@ static void full_tx_buffer(TRANSMITTER *tx) { int mode; switch(protocol) { +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: +#endif case ORIGINAL_PROTOCOL: gain=32767.0; // 16 bit break; @@ -575,6 +597,11 @@ if(isTransmitting()) { case NEW_PROTOCOL: new_protocol_iq_samples(isample,qsample); break; +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + radioberry_protocol_iq_samples(isample,qsample); + break; +#endif } } } diff --git a/tx_menu.c b/tx_menu.c index e6bdfd3..c3df39f 100644 --- a/tx_menu.c +++ b/tx_menu.c @@ -163,8 +163,11 @@ void tx_menu(GtkWidget *parent) { row++; col=0; - - if(protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL) { +#ifdef RADIOBERRY + if(protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL || protocol==RADIOBERRY_PROTOCOL) { +#else + if(protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL)) { +#endif linein_b=gtk_check_button_new_with_label("Mic Line In (ACC connector)"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (linein_b), mic_linein); gtk_grid_attach(GTK_GRID(grid),linein_b,col,++row,3,1); @@ -215,7 +218,11 @@ void tx_menu(GtkWidget *parent) { gtk_widget_show_all(dialog); - if(transmitter->local_microphone && (protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL)) { +#ifdef RADIOBERRY + if(transmitter->local_microphone && (protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL || protocol==RADIOBERRY_PROTOCOL)) { +#else + if(transmitter->local_microphone && (protocol==ORIGINAL_PROTOCOL || protocol==NEW_PROTOCOL)) { +#endif gtk_widget_hide(linein_b); gtk_widget_hide(micboost_b); } diff --git a/vfo.c b/vfo.c index 58533fa..0e9cea1 100644 --- a/vfo.c +++ b/vfo.c @@ -50,6 +50,9 @@ #include "wdsp.h" #include "new_menu.h" #include "rigctl.h" +#ifdef RADIOBERRY +#include "radioberry.h" +#endif static GtkWidget *parent_window; static int my_width; @@ -552,7 +555,11 @@ int vfo_update(void *data) { char version[16]; char text[128]; - if(radio->protocol==ORIGINAL_PROTOCOL) { +#ifdef RADIOBERRY + if(radio->protocol==ORIGINAL_PROTOCOL || radio->protocol==RADIOBERRY_PROTOCOL ) { +#else + if(radio->protocol==ORIGINAL_PROTOCOL) { +#endif switch(radio->device) { #ifdef USBOZY case DEVICE_OZY: @@ -597,6 +604,11 @@ int vfo_update(void *data) { case LIMESDR_PROTOCOL: sprintf(text,"%s", radio->name); break; +#endif +#ifdef RADIOBERRY + case RADIOBERRY_PROTOCOL: + sprintf(text,"%s", radio->name); + break; #endif } cairo_set_source_rgb(cr, 0.7, 0.7, 0.7);