Wrote demo of SM2000 freedv RX
authorbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 7 Jul 2016 01:00:47 +0000 (01:00 +0000)
committerbaobrien <baobrien@01035d8c-6547-0410-b346-abe4f91aad63>
Thu, 7 Jul 2016 01:00:47 +0000 (01:00 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@2840 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/stm32/src/sm2000_rxdemo.c

index 868a513e5bcfd028fa911f3320ac358fde2b98ae..bc5b7c530eda46f24ce84f040d4abd65ea901554 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <assert.h>
 #include <stdint.h>
+#include <stdio.h>
 #include "new_i2c.h"
 #include "si53xx.h"
 #include "debugblinky.h"
 //#include "stm32f4_usb_vcp.h"
 #include "fsk.h"
 #include "freedv_vhf_framing.h"
+#include "freedv_api.h"
 #include "golay23.h"
 #include "string.h"
-#define SINE_SAMPLES   24
 
-/* 32 sample sine wave which at Fs=16kHz will be 500Hz.  Note samples
-   are 16 bit 2's complement, the DAC driver convertsto 12 bit
-   unsigned. */
+#include "malloc.h"
 
-short aSine[] = {
-     5000,  -2500,  -2500, 5000,  -2500,  -2500, 
-     5000,  -2500,  -2500, 5000,  -2500,  -2500, 
-     5000,  -2500,  -2500, 5000,  -2500,  -2500, 
-     5000,  -2500,  -2500, 5000,  -2500,  -2500
-};
-short zeros[SINE_SAMPLES];
+extern void initialise_monitor_handles(void);
+volatile size_t tos;
+
+size_t stack_size(){
+    volatile int x = 0;
+    return tos-((size_t)&x);
+}
+
+//float modbuf[4040];
+//short speechbuf[320];
 
-uint8_t bit_buf[] = { 1,0,1,1,0,0,0,1,
-                  1,0,1,1,0,1,0,1,
-                  0,1,0,0,1,0,1,1,
-                  1,0,0,0,0,0,0,1,
-                  1,1,0,0,0,0,0,1,
-                  1,0,1,0,0,0,0,1,
-                  1,0,0,1,0,0,0,1,
-                  1,0,0,0,1,0,0,1,
-                  1,0,0,0,0,1,0,1,
-                  1,0,0,0,0,0,1,1,
-                  1,0,0,0,0,0,1,1,
-                  1,0,0,0,0,0,1,1, };
-                  
 int main(void) {
     int ret, ptt, i;
     uint64_t freq_in_Hz_times_100;
+    struct freedv * fdv;
     struct FSK * fsk;
-    struct freedv_vhf_deframer * deframer;
-    char chbuf[100];
-    float * mod_buf;
-    
+    volatile int z = 0;
+    tos = (size_t)&z;
+    //initialise_monitor_handles();
+    //printf("Testing\n");
     sm1000_leds_switches_init();
     led_pwr(1);
     
-    fsk = fsk_create_hbr(96000,1200,10,4,32000-1800,1200);
-    fsk_set_est_limits(fsk,29000,35000);
-    deframer = fvhff_create_deframer(FREEDV_VHF_FRAME_A,1);
-    if(fsk==NULL){
-               led_err(1);
-               while(1);
-       }
-    mod_buf = malloc(sizeof(float)*fsk->Nmem);
     init_debug_blinky();
     txrx_12V(0);
 
+    
     I2C_Setup();
     si5351_init(0, SI5351_CRYSTAL_LOAD_6PF, 0);
     freq_in_Hz_times_100 = 1070000000ULL - 3200000ULL;
     ret = si5351_set_freq(freq_in_Hz_times_100, 0, SI5351_CLK0);
-
-    dac_open(DAC_FS_96KHZ, 2000);
-    adc_open(ADC_FS_96KHZ, 2000);
-
+    
+    /* Open up the FreeDV thing */
+       fdv = freedv_open(FREEDV_MODE_2400A);
+       /* Set 96k samp rate to allow for 32k fsk signal */
+    freedv_set_alt_modem_samp_rate(fdv,96000);
+    /* Get the FSK struct and set up freq estimator params */
+    fsk = freedv_get_fsk(fdv);
+    fsk_set_est_limits(fsk,28000,34000);
+    fsk->f1_tx = 32000-1800;
+    
+    float * modbuf = malloc(sizeof(float)*(freedv_get_n_max_modem_samples(fdv)+10));
+    short * speechbuf = malloc(sizeof(short)*freedv_get_n_speech_samples(fdv));
+    //short buf[ADC_BUF_SZ*4];
+    dac_open(DAC_FS_16KHZ*2, DAC_BUF_SZ*4);
+    adc_open(ADC_FS_96KHZ, ADC_BUF_SZ*11);
+    size_t lss;
     //usb_vcp_init();
     int mbptr = 0;
-       int golay_ctr = 0;
-       uint8_t c2_buffer[8];
        int k;
        int spstate;
-       ptt = 1;
-       int nin = fsk_nin(fsk);
+       ptt = 0;
+       int nin = freedv_nin(fdv);
        spstate = 0;
+    for(int i=0;i<freedv_get_n_max_modem_samples(fdv);i++){
+        modbuf[i]=0;
+    }
     while(1) {
                if(switch_ptt() && (!spstate)){
                        ptt = ptt ? 0 : 1;
@@ -111,60 +107,48 @@ int main(void) {
                }
                spstate = switch_ptt();
                
-        led_ptt(ptt);
         txrx_12V(ptt);
         
         if(ptt){
-                       int n = dac1_free();
-                       if (n) {
-                               int16_t  buf[n];
-                               for(i=0; i<n && mbptr<fsk->N; i++,mbptr++) {
-                                       buf[i] = (short)(mod_buf[mbptr]*700);
-                               }
-                               dac1_write((short*)buf, i);
-                       }
-                       if(mbptr>=fsk->N){
-                               /* Encode frame index into golay codeword to protect from test BER*/
-                               k = golay23_encode((golay_ctr)&0x0FFF);
-                               c2_buffer[5] = (k    )&0xFF;
-                               c2_buffer[1] = (k>>8 )&0xFF;
-                               c2_buffer[0] = (k>>16)&0x7F;
-                               /* Frame the bits */
-                               fvhff_frame_bits(FREEDV_VHF_FRAME_A, bit_buf, c2_buffer,NULL,NULL);
-                               /* Mod the FSK */
-                               fsk_mod(fsk,mod_buf,bit_buf);
-                               mbptr = 0;
-                               golay_ctr++;
-                       }
                }else{
-                       
                        int n = adc1_samps();
-                       if (n) {
-                               int16_t buf[n];
-                               adc1_read(buf,n);
-                               for(i=0; i<n && mbptr<nin; i++,mbptr++){
-                                       mod_buf[mbptr] = ((float)buf[i]);
-                               }
+            if (n) {
+                if((n+mbptr)>nin){
+                    n = nin-mbptr;
+                }
+                //In it's own scope so buf is deallocated after it's used 
+                {
+                    short buf[n];
+                    adc1_read(buf,n);
+                    for(int i=0;i<n&&mbptr<nin;i++,mbptr++){
+                        modbuf[mbptr] = buf[i];
+                        //printf("%d\n",stack_size(tos));
+                    }
+                }
+               
                                if(mbptr>=nin){
+                                       mbptr = 0;
                                        led_rt(1);
-                                       fsk_demod(fsk,bit_buf,mod_buf);
-                                       led_rt(0);
-                                       if(fvhff_deframe_bits(deframer,c2_buffer,NULL,NULL,bit_buf)){
-                                               led_err(1);             
+                                       freedv_floatrx(fdv,speechbuf,modbuf);
+                    led_rt(0);
+    
+                                       if(freedv_get_sync(fdv)){
+                                               led_err(1);
                                        }else{
-                                               //led_err(0);
                                                led_err(0);
                                        }
-                                       mbptr = 0;
-                                       if(fsk_nin(fsk)!=nin){
+                                       if(freedv_nin(fdv)!=nin){
                                                led_ptt(1);
                                        }else{
                                                led_ptt(0);
                                        }
-                                       nin = fsk_nin(fsk);
+                                       nin = freedv_nin(fdv);
                                        
                                }
                        }
+            if(dac2_free()>freedv_get_n_speech_samples(fdv)){
+                dac2_write(speechbuf,freedv_get_n_speech_samples(fdv));
+            }
                }
     }
 }