CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Revision:
207:28a07943dded
Parent:
206:fba4aeebf004
--- a/Compression.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/Compression.h	Sat Jul 02 10:48:30 2016 +0000
@@ -1,19 +1,20 @@
-unsigned int read_2byte(unsigned char* ptr){
-    unsigned int output = (unsigned int) *(ptr+1);
-    output += ( (unsigned int)(*ptr) ) << 8;
+uint8_t read_2byte(uint8_t* ptr){
+    uint8_t output = (uint8_t ) *(ptr+1);
+    output += ( (uint8_t )(*ptr) ) << 8;
     return output;
 }
     
-unsigned int read_4byte(unsigned char* ptr){
-    unsigned int output = (unsigned int) *(ptr+3);
-    output += (unsigned int)*(ptr+2)<<8;
-    output += (unsigned int)*(ptr+1)<<16;
-    output += (unsigned int)*(ptr)<<24;
+uint8_t read_4byte(uint8_t* ptr){
+    uint8_t output = (uint8_t ) *(ptr+3);
+    output += (uint8_t )*(ptr+2)<<8;
+    output += (uint8_t )*(ptr+1)<<16;
+    output += (uint8_t )*(ptr)<<24;
     return output;
 }
 
-int adjust(int size, unsigned int data, unsigned char* ptr , int space){
+int adjust(int size, uint8_t data, uint8_t* ptr , int space){
     space = space&0x0f;
+    if(space == 8)  *ptr = 0;
     data = data&((1<<size)-1);
     if(space >= size){
         *ptr += data<<(space-size);
@@ -42,32 +43,45 @@
     }
 }
 
+//--------------------------------------------------------beacon
+struct beacon_packet{
+    unsigned char data[14];
+    beacon_packet *next;
+    int len;
+};
+
+int beacon_cntr = 0;
+bool beacon_flag = true;
+beacon_packet *head_beacon, *tail_beacon;
+//--------------------------------------------------------beacon
+
 
 
 //variable declerations
-unsigned char srp_mode , at , pzf , ezf ,sfp[65] ,scp[55];; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
+uint8_t srp_mode , at , pzf , ezf ,sfp[65] ,scp[55],beacon_at;; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
 unsigned int sfp_bin[52] , scp_bin[52];         //storing the bin values.
 unsigned int sfp_threshold_m0[52] = {8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128,4032,4032,124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,252,252,252,252,252}; 
-const unsigned int scp_threshold_m0[52] = {245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760,114688,114688,4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,4032,4032,4032,4032};
+const int scp_threshold_m0[52] = {245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760,114688,114688,4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,4032,4032,4032,4032};
 unsigned int scp_threshold_m0_1[9]={114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for mode 0..ie callibrated mode
-const unsigned int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
-unsigned char frames[3][134] = {0};
-unsigned char position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
-unsigned char id;   //sf = 0,sc-at = 1,sc-bt = 2;
-unsigned char TM_interleave_data[512] , TM_convoluted_data[270] = {0};
+const int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
+uint8_t frames[3][134] = {0};
+uint8_t position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
+uint8_t id;   //sf = 0,sc-at = 1,sc-bt = 2;
+uint8_t TM_interleave_data[512] , TM_convoluted_data[270] = {0};
 int proton_scp_sum,electron_scp_sum,length, temp_crc,attitude = 0,FSC_science = 1,debug_cntr = 0, size,space;   
-unsigned char *ptr ,* pointer;
-long long int sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
+uint8_t *ptr ,* pointer;
+uint64_t sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
 int test_cntr = 0;
+unsigned int beacon_threshold[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472};  //fill it bro
 
     Convolution ConvObj;
-    void convolution (unsigned char * ptr){
+    void convolution (uint8_t * ptr){
         ConvObj.convolutionEncode(ptr, TM_convoluted_data);
         ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
     }
 
 //give the pointer of 6 second data to this function
-void srp(unsigned char * head){
+void srp(uint8_t * head){
     //sci_time = FCTN_CDMS_RD_RTC();
     sci_time = 0;
     gPC.puts("enter srp");
@@ -79,6 +93,9 @@
     srp_mode = head[2]&0x1;
     at = 0;     //above threshold is false
     for(int counter = 0 ; counter < 60 ; counter++){
+        //gPC.puts("Y");
+//        gPC.printf("counter = %d\n",counter);
+//        cout<<"counter = "<<counter<<endl;
         pzf = 1;    ezf = 1;
         if(srp_mode == 0){      //calibrated mode
             for(int i=0; i<48 ; i++){
@@ -295,9 +312,10 @@
                 }else if(id == 2){
                     space = adjust(1,0,pointer,8);
                     space = adjust(4,2,pointer,space);
-//                    FSC_science = FCTN_SD_MNGR(2);
+                    FCTN_SD_MNGR();
+                    FSC_science = FSC_CURRENT[2];
                     
-                    //gPC.printf("SID = 2, FSC = %02X\r\n", FSC_science);
+ //                   gPC.printf("SID = 2, FSC = %02X\r\n", FSC_science);
                     frames[id][1] = (FSC_science>>16)&0xff;
                     frames[id][2] = (FSC_science>>8)&0xff;
                     frames[id][3] = FSC_science&0xff;
@@ -305,24 +323,30 @@
                 temp_crc = crc16_gen(frames[id],132);
                 frames[id][132] = temp_crc>>8;
                 frames[id][133] = temp_crc & 0xff;
-                if(test_cntr++ == 0){
-                    gPC.puts("frame start\r\n");
-                    for(int i = 0; i<134; i++){
-                        gPC.printf("%02X",frames[id][i]);
-                    }    
-                    gPC.puts("frame ends\r\n");
-                }
+              //  if(test_cntr++ == 0){
+                    //gPC.puts("frame start\r\n");
+//                    for(int i = 0; i<134; i++){
+//                        gPC.printf("%02X",frames[id][i]);
+//                    }    
+                   // gPC.puts("frame ends\r\n");
+//                }
+                   
+                
                 exor(frames[id]);
                 convolution(frames[id]);
                 interleave(TM_convoluted_data,TM_interleave_data);
                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
-                if(id == 1)
+                
+                if(id == 1){
                     SD_WRITE(TM_interleave_data,FSC_science,3);
-                else if (id == 2)
+                   }
+                else if (id == 2){
                     SD_WRITE(TM_interleave_data,FSC_science,2);
+                  }
                 FSC_science++;      //remove this after testing
                 position_tm_frame[id] = position_tm_starting[id];
                 frames[id][6-id] = (length-1) - j;      // first head pointer.
+                
             }
         }
         
@@ -331,7 +355,7 @@
     //---------------below is scp --------------**************************************************************************************************
     at = 0;     pzf = 1;    ezf = 1;
     srp_mode = head[2]&0x1;
-    unsigned char compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
+    uint8_t compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
     if(srp_mode ==0){
         if(compression_option == 0){
             for(int i=0; i<52 ;i++){
@@ -340,6 +364,27 @@
                     break;
                 }
             }
+            
+            //---------------------------------------------***********************************************************----------------------------beacon
+            beacon_at = 0;
+            if(scp_bin[44]>beacon_threshold[0])     beacon_at = 1;
+            if(scp_bin[48]>beacon_threshold[1])     beacon_at = 1;
+            if(scp_bin[49]>beacon_threshold[2])     beacon_at = 1;
+            if(scp_bin[50]>beacon_threshold[3])     beacon_at = 1;
+            if(scp_bin[51]>beacon_threshold[4])     beacon_at = 1;
+            if(scp_bin[17]>beacon_threshold[5])     beacon_at = 1;
+            if(scp_bin[23]>beacon_threshold[6])     beacon_at = 1;
+            proton_scp_sum = 0; electron_scp_sum = 0;
+            for(int i=0;i<17;i++){
+                proton_scp_sum += scp_bin[i];
+            }
+            for(int i=0; i<5; i++){
+                electron_scp_sum += scp_bin[18+i];
+            }
+            if(proton_scp_sum > beacon_threshold[7]) beacon_at =1;
+            if(electron_scp_sum > beacon_threshold[8]) beacon_at =1;
+            //--------------------------------------------*******************************************************************----------------------beacon
+            
         }
         else if(compression_option == 1){
             if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
@@ -386,10 +431,80 @@
             ezf = 0;
     }
     
+        //------------------------------------------------------------------------beacon
+//  struct beacon_packet{
+//  unsigned char data[12];
+//  beacon_packet *next;
+//  };
+//
+//  int beacon_cntr = 0;
+//  bool beacon_flag = true;
+//  beacon_packet *head_beacon, *tail_beacon;
+
+    if(beacon_cntr == 0){
+        head_beacon = tail_beacon = new beacon_packet;
+        tail_beacon->next = NULL;
+    }
+    else{
+        tail_beacon->next = new beacon_packet;
+        tail_beacon = tail_beacon->next;
+        tail_beacon->next = NULL;
+    }
+    pointer = tail_beacon->data;        debug_cntr = 0;     space = 8;
+    space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(5,(sci_time>>8)&0x1f,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(8, sci_time,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(4, (attitude)&0xf,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    if(beacon_at == 0){
+        tail_beacon->len = 12;
+        space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(6, compress(sfp_bin[44],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[48],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[49],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[50],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[51],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(6, compress(sfp_bin[17],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(6, compress(sfp_bin[23],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(proton_scp_sum ,10,2)>>8,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(8, compress(proton_scp_sum ,10,2),pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(electron_scp_sum ,10,2)>>8,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(8, compress(electron_scp_sum ,10,2),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+//      cout<<"for beacon space = "<<(space&0x0f)<<" counter = "<<debug_cntr;
+        for(int temp_i = 12; temp_i<14; temp_i++){
+            *pointer = 0;
+            pointer = pointer + 1;
+        }
+    }
+    else{
+        tail_beacon->len = 14;
+        space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(sfp_bin[44],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[44],6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[48],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[48],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[49],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[49],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[50],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;                 space = adjust(8, compress(sfp_bin[50],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[51],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;                 space = adjust(8, compress(sfp_bin[51],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(sfp_bin[17],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[17],6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(sfp_bin[23],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[23],6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(proton_scp_sum ,6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;         space = adjust(8, compress(proton_scp_sum ,6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(electron_scp_sum ,6,3)>>8,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;     space = adjust(8, compress(electron_scp_sum ,6,3),pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+//        cout<<"for beacon space = "<<(space&0x0f)<<" counter = "<<debug_cntr;
+    }
+    beacon_cntr++;
+    if(beacon_cntr ==6){
+//        cout<<"i deleted it"<<endl;
+        beacon_packet  *temp_bec;
+        temp_bec = head_beacon;
+        head_beacon = head_beacon->next;
+        delete(temp_bec);
+        beacon_cntr--;
+    }
+
+    //------------------------------------------------------------------------beacon
+    
     gPC.puts("scp\r\n");
     
     pointer = scp;      debug_cntr = 0;     space = 8;
-    unsigned char packet_pp = 1;    //where to get this packet present bit----------------------------------------------------------------------------------
+    uint8_t packet_pp = 1;    //where to get this packet present bit----------------------------------------------------------------------------------
     int sfp_at_counter; //----------------------------------------------------------------------------------------------------------------------------------
     space = adjust(1, packet_pp,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
     space = adjust(5, (sci_time>>8)&0x1f,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
@@ -545,7 +660,6 @@
             if(ezf == 0)
                 length += 40;
         }
-            
     }else if(srp_mode == 0 && compression_option == 1){     //data conservation mode
         if(at == 0){
             length = 94;