CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Revision:
268:ded5306a1fd1
Parent:
267:783c248a6207
Child:
269:72529b247333
--- a/Compression.h	Sat Jul 16 16:04:14 2016 +0000
+++ b/Compression.h	Wed Jul 20 09:58:59 2016 +0000
@@ -14,6 +14,10 @@
 //using namespace std;
 
 //reading functions 2byte, 4byte
+#define srpz 1
+#define scpz 1
+#define scp_last 1
+#define srp_last 1
 
 uint16_t read_2byte(uint8_t* ptr)
 {
@@ -101,14 +105,15 @@
 //give the pointer of 6 second data to this function
 void srp(uint8_t * head)
 {
-    gPC.printf("\n\rEntered srp");
+    uint8_t sd_stat = 0;
+    gPC.printf("\n\rEntered srp function");
     debug_cntr = 0;
     sci_time = FCTN_CDMS_RD_RTC();
     if(time_prev_scp==0){
         time_prev_scp = sci_time;
     }    
     FCTN_SD_MNGR();                                                             ///changed recently
-    for (int i = 0; i < 56 ; i++)
+    for (int i = 0; i < 52 ; i++)
     {
         scp_bin[i] = 0;
     }
@@ -116,16 +121,23 @@
     srp_mode = head[2]&0x1;
     for(uint8_t counter = 0 ; counter < 60 ; counter++)
     {       
+        #if srpz
+        //gPC.printf("\n\n\rSRP count:%d",counter+1);
+        #endif
         /*-------------------- ------------populating sfp_bin and scp_bin[] starts here(also tag AT or BT)-----------------------*/
         at = 0;     pzf = 1;    ezf = 1;
         if(srp_mode == 0)                                   //calibrated mode 
         {
+            #if srpz
+            //gPC.printf("\n\rSRP - Calibrated Mode");
+            #endif
             for(int i=0; i<48 ; i++)                        //first 48 bins or SRP are of 16 bits size
             {
                 sfp_bin[i] = read_2byte(ptr + i*2);
                 scp_bin[i] += sfp_bin[i];
                 if(sfp_bin[i]>sfp_threshold_m0[i])
                 {
+                    //gPC.printf("\n\rSFP above threshold index:%d",i);
                     at = 1;
                 }
                 if(i<17)
@@ -143,31 +155,41 @@
             {
                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
                 scp_bin[i+48] += sfp_bin[i+48];
-                if(sfp_bin[i+48]>sfp_threshold_m0[i+48])
+                if(sfp_bin[i+48]>sfp_threshold_m0[i+48]){
+                    //gPC.printf("\n\rSP above threshold index: %d",i+48);
                     at = 1;
+                }
             }
         }
         else if(srp_mode == 1)                              //scattered mode
         {
+            #if srpz
+            //gPC.printf("\n\rSRP - Scatter Plot Mode");
+            #endif
             for(int i = 0; i <48; i++)                      //first 48 bins of SRP are of 16 bit size
             {
                 sfp_bin[i]  = read_2byte(ptr+2*i);
                 scp_bin[i] += sfp_bin[i];
-                if(sfp_bin[i] > sfp_threshold_m1[i])
+                //gPC.printf("\n\rsfp_bin[%d]= %u\t\tscp_bin[%d]= %u",i,sfp_bin[i],i,scp_bin[i]);
+                if(sfp_bin[i] > sfp_threshold_m1[i]){
+                    //gPC.printf("\n\rSP above threshold index: %d",i);
                     at = 1;
+                }
             }
             for(int i = 0; i < 4 ; i++)                     //next 4 bins are of 32 bit size
             {
                 ///sfp_bin[i+48] = read_4byte( (ptr+64) + 4*i );
                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
                 scp_bin[i+48] += sfp_bin[i+48];
-                if(sfp_bin[i+48] > sfp_threshold_m1[i+48])
+                if(sfp_bin[i+48] > sfp_threshold_m1[i+48]){
+                    //gPC.printf("\n\rSP above threshold index: %d",i+48);
                     at = 1;
+                }
             }
         }
         /*---------------------------populating sfp_bin[] and scp_bin[] from srp ends here(also tagging AT or BT)------------------*/
 
-        ptr = ptr + 112;                                    //moving the pointer to next srp packet in the DMA buffer
+        //ptr = ptr + 112;                                    //moving the pointer to next srp packet in the DMA buffer
         
         /*------------------------------------------forming a science fine packet starts here----------------------------------------
         First we fill packet header, then fill packet data and finally assign its length
@@ -178,6 +200,9 @@
         {
             if(at == 0)
             {
+                #if srpz
+                //gPC.printf("\n\rSFP Below Threshold");
+                #endif
                 pointer = sfp; debug_cntr = 0;
                 space = adjust(4, attitude,pointer,8);      pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
@@ -215,6 +240,9 @@
                 }
             }                                                                                           //below thershold ends here.
             if(at == 1){
+                #if srpz
+                //gPC.printf("\n\rSFP Above Threshold");
+                #endif
                 pointer = sfp + 6; debug_cntr = 6;space = 8;
                 sfp[0] = (sci_time>>27)&0xff;   sfp[1] = (sci_time>>19)&0xff;   sfp[2] = (sci_time>>11)&0xff;   sfp[3] = (sci_time>>3)&0xff;
                 sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5);
@@ -242,6 +270,9 @@
 
                 if(pzf == 0){
                     //cout<<"proton bins ";
+                    #if srpz
+                    //gPC.printf("\n\rProton bins present in SRP");
+                    #endif
                     for(uint8_t i = 0; i<17 ; i++){
                         space = adjust(2, ((compress(sfp_bin[i],8,2))>>8) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
                         space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
@@ -250,6 +281,9 @@
                 }
                 if(ezf == 0){
                     //cout<<"electron bins ";
+                    #if srpz
+                    //gPC.printf("\n\rElectron bins present in SRP");
+                    #endif
                     for(int i = 0; i<5 ; i++){
                         space = adjust(2,((compress(sfp_bin[18+i],8,2))>>8),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                         space = adjust(8, compress(sfp_bin[18+i],8,2),pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
@@ -278,6 +312,9 @@
         {
             if(at == 0)
             {
+                #if srpz
+                //gPC.printf("\n\rSFP below threshold");
+                #endif
                 pointer = sfp; debug_cntr = 0;  space = 8;
                 space = adjust(4, attitude,pointer,8);      pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
@@ -294,6 +331,10 @@
             }
             if(at == 1)
             {
+                
+                #if srpz                
+                //gPC.printf("\n\rSFP below threshold");
+                #endif
                 pointer = sfp; debug_cntr = 0;  space = 8;
                 space = adjust(3, sci_time>>32 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, sci_time>>24 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
@@ -324,11 +365,17 @@
             }
         }
         length = (length%8==0)?(length/8):(length/8)+1;                         //converting length to mulitple of 8
+        #if srpz
+        //gPC.printf("\n\rLength of SFP packet in bytes = %d",length);
+        #endif
         /*----------------------------------------forming a science packet ends here-----------------------------------------------*/
 
         /*-----------------------------Inserting the above packet(sfp) into the TM frame(s) starts here----------------------------*/
         if(id==1){
             if(position_tm_frame[id]>130){
+                #if srpz
+                //gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame.");
+                #endif
                 while(position_tm_frame[id]<132){
                     frames[id][position_tm_frame[id]] = 0;
                     position_tm_frame[id]++;
@@ -337,6 +384,9 @@
         }
         else if(id==2){
             if(position_tm_frame[id]>126){
+                #if srpz
+                //gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame.");
+                #endif
                 while(position_tm_frame[id]<132){
                     frames[id][position_tm_frame[id]] = 0;
                     position_tm_frame[id]++;
@@ -350,6 +400,9 @@
         {            
             if(position_tm_frame[id]==frames[id][6-id]){
                 if(id==1){
+                    #if srpz
+                    //gPC.printf("\n\rAdding Time to TM frame");
+                    #endif
                     frames[id][6]  = (uint8_t)((sci_time>>32)&0x07);
                     frames[id][7]  = (uint8_t)((sci_time>>24)&0xff);
                     frames[id][8]  = (uint8_t)((sci_time>>16)&0xff);
@@ -365,11 +418,16 @@
             if(position_tm_frame[id] == 132)
             {
                 /*-----------the current frame is completely filled and we fill frame header to write into SD card---------*/
+                #if srpz
+                //gPC.printf("\n\rSFP_TM frame full. Length of current packet = %d",j);
+                //gPC.printf("\n\rFrame Header: %d",frames[id][6-id]);
+                //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]);
+                #endif
                 pointer = frames[id];
                 if(id == 1){    //below threshold
                     space = adjust(1,0,pointer,8);
                     space = adjust(4,3,pointer,space);
-                    FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit
+                    FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
                     frames[id][1] = (FSC_science>>24)&0xff;
                     frames[id][2] = (FSC_science>>16)&0xff;
                     frames[id][3] = (FSC_science>>8)&0xff;
@@ -387,7 +445,14 @@
                 temp_crc = crc16_gen(frames[id],132);
                 frames[id][132] = temp_crc>>8;
                 frames[id][133] = temp_crc & 0xff;
-
+                
+                #if srpz
+                gPC.printf("\n\rPrinting SFP_TM\n\r{");                
+                for(uint8_t z=0;z<134;z++){
+                    gPC.printf("0x%02X,",frames[id][z]);
+                }
+                gPC.printf("}\n\r");
+                #endif
                 /*------------------current TM frame completely filled-----------------------*/
                 
                 exor(frames[id]);
@@ -396,18 +461,61 @@
                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
                 if(id == 1)
                 {
-                    SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
+                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
                 }
                 else if (id == 2)
                 {
-                    SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
+                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
                 }
+                if(sd_stat)
+                {
+                    gPC.puts("sd write fail");
+                    }
                 position_tm_frame[id] = position_tm_starting[id];
-                frames[id][6-id] = ((length-1) - j)+position_tm_starting[id];
+                frames[id][6-id] = (length - j)+position_tm_starting[id];
             }
         }
         /*------------------------------finished inserting the sfp packet into the TM frame(s)-------------------------------------*/
-    }                                                                                           // for loop bracket which runs 60 times    
+    }                                                                               // for loop bracket which runs 60 times    
+    #if srpz_last
+    //gPC.printf("\n\rPrinting the last SFP_TM frame");
+    //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]);
+    while(position_tm_frame[id]<132){
+        frames[id][position_tm_frame[id]] = 0;
+        position_tm_frame[id]++;
+    }
+    if(position_tm_frame[id]==132){
+        //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]);
+        pointer = frames[id];
+        if(id == 1){    //below threshold
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,3,pointer,space);
+            FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
+            frames[id][1] = (FSC_science>>24)&0xff;
+            frames[id][2] = (FSC_science>>16)&0xff;
+            frames[id][3] = (FSC_science>>8)&0xff;
+            frames[id][4] = FSC_science&0xff;                    
+        }
+        else if(id == 2)    //above threshold
+        {
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,2,pointer,space);                    
+            FSC_science = FSC_CURRENT[2];                                               ///to be used as this
+            frames[id][1] = (FSC_science>>16)&0xff;
+            frames[id][2] = (FSC_science>>8)&0xff;
+            frames[id][3] = FSC_science&0xff;
+        }
+        temp_crc = crc16_gen(frames[id],132);
+        frames[id][132] = temp_crc>>8;
+        frames[id][133] = temp_crc & 0xff;        
+        
+        gPC.printf("\n\rPrinting SFP_TM\n\r{");
+        for(uint8_t z=0;z<134;z++){
+            gPC.printf("0x%02X,",frames[id][z]);
+        }
+        gPC.printf("}\n\r");
+    }
+    #endif
     at = 0;     pzf = 1;    ezf = 1;
     srp_mode = head[2]&0x1;
     uint8_t compression_option = (head[2]>>1)&0x3;
@@ -421,6 +529,7 @@
             {
                 if(scp_bin[i] > scp_threshold_m0[i])
                 {
+                    //gPC.printf("\n\rSCP above threshold index: %d",i);
                     at = 1;
                     break;
                 }
@@ -428,17 +537,25 @@
         }
         else if(compression_option == 1)
         {
-            if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
+            if(scp_bin[44] > scp_threshold_m0_1[0]){//gPC.printf("\n\rSCP above threshold index: 44"); 
+            at=1;}
             for(int i=0; i<4 ;i++)
             {
                 if(scp_bin[48+i] > scp_threshold_m0_1[i+1])
                 {
+                    //gPC.printf("\n\rSCP above threshold index: %d",i+48);
                     at = 1;
                     break;
                 }
             }
-            if(scp_bin[17] > scp_threshold_m0_1[5]) at=1;
-            if(scp_bin[23] > scp_threshold_m0_1[6]) at=1;
+            if(scp_bin[17] > scp_threshold_m0_1[5]){ 
+                //gPC.printf("\n\rSP above threshold index: 17");
+                at=1;
+            }
+            if(scp_bin[23] > scp_threshold_m0_1[6]){ 
+                //gPC.printf("\n\rSP above threshold index: 23");
+                at=1;
+            }
 
             proton_scp_sum = 0; electron_scp_sum = 0;
             for(int i=0;i<17;i++)
@@ -449,12 +566,15 @@
             {
                 electron_scp_sum += scp_bin[18+i];
             }
-            if(proton_scp_sum > scp_threshold_m0_1[7]) at=1;
-            if(electron_scp_sum > scp_threshold_m0_1[8]) at=1;
+            if(proton_scp_sum > scp_threshold_m0_1[7]){ //gPC.printf("\n\rProton_scp_sum above threshold");
+            at=1;}
+            if(electron_scp_sum > scp_threshold_m0_1[8]){//gPC.printf("\n\rElectron_scp_sum above threshold");
+             at=1;}
         }
         else if(compression_option == 2)
         {
-            if(scp_bin[50] > scp_sfp_threshold_m0_2[0]) at=1;
+            if(scp_bin[50] > scp_sfp_threshold_m0_2[0]){//gPC.printf("\n\rSCP above threshold index:50"); 
+            at=1;}
             proton_scp_sum = 0; electron_scp_sum = 0;
             for(int i=0;i<17;i++)
             {
@@ -464,16 +584,19 @@
             {
                 electron_scp_sum += scp_bin[18+i];
             }
-            if(proton_scp_sum > scp_sfp_threshold_m0_2[1]) at=1;
-            if(electron_scp_sum > scp_sfp_threshold_m0_2[2]) at=1;
+            if(proton_scp_sum > scp_sfp_threshold_m0_2[1]){//gPC.printf("\n\rProton_SCP_sum above threshold"); 
+            at=1;}
+            if(electron_scp_sum > scp_sfp_threshold_m0_2[2]){//gPC.printf("\n\rElectron above threshold");
+             at=1;}
         }
     }
     else if(srp_mode ==1)
     {
-        if(compression_option==0){ 
+        if(compression_option==0){
             for(int i=0; i<48; i++)
             {
                 if(scp_bin[i] > scp_threshold_m1[i]){
+                    //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]);
                     at = 1;
                     break;
                 }
@@ -481,13 +604,17 @@
             for(int i=48; i<52; i++)
             {
                 if(scp_bin[i] > scp_threshold_m1[i]){
+                    //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]);
                     at = 1;
                     break;
                 }
             }
         }
-        else if(compression_option==2){
-            if(scp_bin[50]>scp_sfp_threshold_m0_2[0])   at=1;
+        else if(compression_option==2){            
+            if(scp_bin[50]>scp_sfp_threshold_m0_2[0]){
+                //gPC.printf("\nSCP above threshold index:50, Value:%",scp_bin[50]);
+                at=1;
+            }
         }
     }
     /*----------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT ends here--------------------------*/
@@ -645,7 +772,7 @@
     beacon_cntr++;
     /*--------------------------------------------------beacon packet ends here--------------------------------------------------------*/
 
-    gPC.printf("scp\r\n");
+    //gPC.printf("\n\rSCP count: %d");
     uint8_t packet_pp,time_diff;
     /*------------------------------------------Forming a science packet(scp) starts here----------------------------------------------*/
 
@@ -679,9 +806,15 @@
         space = adjust(8, sfp_at_counter,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
     }
     if(srp_mode == 0 && compression_option == 0){       //normal callibrated mode
+        #if scpz
+        //gPC.printf("\n\rSCP - Normal calibrated mode");
+        #endif
         space = adjust(1, pzf,pointer,space);                       pointer += space>>4;    debug_cntr += space>>4;
         space = adjust(1, ezf,pointer,space);                       pointer += space>>4;    debug_cntr += space>>4;
         if(at == 0 ){
+            #if scpz
+            //gPC.printf("\n\rBelow threshold");
+            #endif
             for(int i = 0; i<8 ;i++){
                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -713,6 +846,9 @@
             }
         }// below threshold ends here
         if(at == 1){
+            #if scpz
+            //gPC.printf("\n\rAbove threshold");
+            #endif
             for(int i = 0; i<8 ;i++){
                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -745,7 +881,13 @@
         }   //above thresholds ends
     }   //srp_mode == 0 ends
     if(srp_mode == 1){
+        #if scpz
+        //gPC.printf("\n\rSCP - Scatter plot mode");
+        #endif
         if(at == 0){
+            #if scpz
+            //gPC.printf("\n\rBelow Threshold");
+            #endif
             for(int i=0; i<48; i++){
                 space = adjust(6, ((compress(scp_bin[i],3,3))&0xff) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -754,6 +896,9 @@
             }
         }
         if(at == 1){
+            #if scpz
+            //gPC.printf("\n\rAbove threshold");
+            #endif
             for(int i=0; i<48; i++){
                 space = adjust(7, (compress((scp_bin[i]/2),4,3))>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -764,9 +909,15 @@
         }
     }// scp mode 1 end
     if( srp_mode == 0 && compression_option == 1 ){ //scp data conservation mode
+        #if scpz
+        //gPC.printf("\n\rSCP - Calibrated data conservation mode");
+        #endif
         space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
         space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
         if(at == 0){
+            #if scpz
+            //gPC.printf("\n\rBelow threshold");
+            #endif
             space = adjust(6, (compress(scp_bin[44],3,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
             for(int i=0; i<4; i++){
                 space = adjust(7, (compress(scp_bin[48+i],4,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
@@ -782,6 +933,9 @@
                 space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
             }
         }else if(at == 1){
+            #if scpz
+            //gPC.printf("\n\rAbove threshold");
+            #endif
             space = adjust(1, (compress(scp_bin[44],6,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
             space = adjust(8, (compress(scp_bin[44],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
             for(int i=0; i<4; i++){
@@ -806,9 +960,15 @@
     {
         if(srp_mode==0)
         {
+            #if scpz
+            //gPC.printf("\n\rSCP - calibrated extreme data conservation mode");
+            #endif
             space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             if(at==0){
+                #if scpz
+                //gPC.printf("\n\rBelow threshold");
+                #endif
                 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 if(pzf==0){
                     space = adjust(4, (compress(proton_scp_sum,10,2))>>8 ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
@@ -819,6 +979,9 @@
                     space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
                 }
             }else if(at==1){
+                #if scpz
+                //gPC.printf("\n\rAbove threshold");
+                #endif
                 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 if(pzf==0){
@@ -833,10 +996,16 @@
         }
         else if(srp_mode==1)
         {
+            #if scpz
+            //gPC.printf("\n\rSCP - Scatter plot, extreme data conservation mode");
+            #endif
             pzf = 0;    ezf = 0;    //because proton energy bins are not available in scatter plot mode and hence in the packet we send dummy data(which is nothng but the maximum value which fits into that particular compression scheme)
             space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             if(at==0){
+                #if scpz
+                //gPC.printf("\n\rBelow threshold");
+                #endif
                 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
                 // below 4 lines - filling the proton ane electron energy bins with zeros(junk value) since they're not available in srp
                 space = adjust(4, (compress(0,10,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
@@ -845,6 +1014,9 @@
                 space = adjust(8, (compress(0,10,2)) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
             }
             else if(at==1){
+                #if scpz
+                //gPC.printf("\n\rAbove threshold");
+                #endif
                 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(1, (compress(0,6,3))>>8 ,pointer,space);                 pointer += space>>4;    debug_cntr += space>>4;
@@ -909,7 +1081,14 @@
         }
     }
     length = (length%8==0)?(length/8):(length/8)+1;
-    if(position_tm_frame[id]>129){
+    #if scpz
+    //gPC.printf("\n\rSCP packet length = %d",length);
+    #endif
+    if(position_tm_frame[id]>129){        
+        #if scpz
+        //gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame");
+        //gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]);
+        #endif
         while(position_tm_frame[id]<132){
             frames[id][position_tm_frame[id]] = 0;
             position_tm_frame[id]++;
@@ -921,6 +1100,9 @@
     for(int j= 0; j < length ;)
     {        
         if(position_tm_frame[id]==frames[id][4]){
+            #if scpz
+            //gPC.printf("\n\rInserting Time into SCP_TM");
+            #endif
             frames[id][5] = (uint8_t)((sci_time>>29)&0x3f);
             frames[id][6] = (uint8_t)((sci_time>>21)&0xff);
             frames[id][7] = (uint8_t)((sci_time>>13)&0x3f);
@@ -932,6 +1114,9 @@
         }
         if(position_tm_frame[id] == 132)                                                //space full in the frame bro
         {
+            #if scpz
+            //gPC.printf("\n\rSCP_TM frame full. Length of current SCP packet = %d",j);
+            #endif
             pointer = frames[id];
             space = adjust(1,0,pointer,8);
             space = adjust(4,1,pointer,space);
@@ -942,15 +1127,67 @@
             temp_crc = crc16_gen(frames[id],132);
             frames[id][132] = temp_crc>>8;
             frames[id][133] = temp_crc & 0xff;
+            #if scpz
+            //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]);
+            gPC.printf("\n\rPrinting SCP_TM\n\r{");
+            for(uint8_t z=0;z<134;z++){
+                gPC.printf("0x%02X,",frames[id][z]);
+            }
+            gPC.printf("}\n\r");
+            #endif
             exor(frames[id]);
             convolution(frames[id]);
             interleave(TM_convoluted_data,TM_interleave_data);
             interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
 
-            SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
-
+            sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
+            if(sd_stat)
+                {
+                    gPC.puts("sd write fail");
+                    }
+            
             position_tm_frame[id] = position_tm_starting[id];
-            frames[id][4] = ((length-1) - j)+position_tm_starting[id];
+            frames[id][4] = (length - j)+position_tm_starting[id];
         }
     }
+    #if scpz_last
+    //gPC.printf("\n\rPrinting the last SFP_TM frame");
+    //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]);
+    while(position_tm_frame[id]<132){
+        frames[id][position_tm_frame[id]] = 0;
+        position_tm_frame[id]++;
+    }
+    if(position_tm_frame[id]==132){
+        //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]);
+        pointer = frames[id];
+        if(id == 1){    //below threshold
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,3,pointer,space);
+            FSC_science = FSC_CURRENT[1];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
+            frames[id][1] = (FSC_science>>24)&0xff;
+            frames[id][2] = (FSC_science>>16)&0xff;
+            frames[id][3] = (FSC_science>>8)&0xff;
+            frames[id][4] = FSC_science&0xff;                    
+        }
+        else if(id == 2)    //above threshold
+        {
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,2,pointer,space);                    
+            FSC_science = FSC_CURRENT[2];                                               ///to be used as this
+            frames[id][1] = (FSC_science>>16)&0xff;
+            frames[id][2] = (FSC_science>>8)&0xff;
+            frames[id][3] = FSC_science&0xff;
+        }
+        temp_crc = crc16_gen(frames[id],132);
+        frames[id][132] = temp_crc>>8;
+        frames[id][133] = temp_crc & 0xff;
+        
+        
+        gPC.printf("\n\rPrinting SCP_TM\n\r{");
+        for(uint8_t z=0;z<134;z++){
+            gPC.printf("0x%02X,",frames[id][z]);
+        }
+        gPC.printf("}\n\r");
+    }
+    #endif    
 }
\ No newline at end of file