CDMS_CODE_samp_23SEP_DMA_flag

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE_samp_23SEP_DMA by iitm sat

Files at this revision

API Documentation at this revision

Comitter:
prasanthbj05
Date:
Fri Jul 15 14:59:27 2016 +0000
Parent:
264:f7d8f9f361e3
Child:
267:783c248a6207
Commit message:
Compression code modified and RLY_TMTC pointers managed

Changed in this revision

CDMS_PL.h Show annotated file Show diff for this revision Revisions of this file
COM_MNG_TMTC.h Show annotated file Show diff for this revision Revisions of this file
Compression.h Show annotated file Show diff for this revision Revisions of this file
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
TEST_PL.h Show annotated file Show diff for this revision Revisions of this file
adf.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CDMS_PL.h	Thu Jul 14 23:05:09 2016 +0000
+++ b/CDMS_PL.h	Fri Jul 15 14:59:27 2016 +0000
@@ -67,7 +67,7 @@
     pl_tc_standby->TC_string[5] = 0;\
     pl_tc_standby->TC_string[6] = 0;\
     pl_tc_standby->TC_string[7] = 0;\
-    pl_tc_standby->TC_string[8] = 0;\    
+    pl_tc_standby->TC_string[8] = 0;\
     uint16_t crc16 = crc16_gen(pl_tc_standby->TC_string, 9);\
     pl_tc_standby->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
     pl_tc_standby->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
@@ -80,7 +80,7 @@
     {\
         temp = temp->next_TM;\
         delete tm_ptr_standby;\
-        tm_ptr_standby = temp;\        
+        tm_ptr_standby = temp;\
     }\
 }
 
@@ -100,7 +100,7 @@
     pl_tc_hibernate->TC_string[5] = 0;\
     pl_tc_hibernate->TC_string[6] = 0;\
     pl_tc_hibernate->TC_string[7] = 0;\
-    pl_tc_hibernate->TC_string[8] = 0;\    
+    pl_tc_hibernate->TC_string[8] = 0;\
     uint16_t crc16 = crc16_gen(pl_tc_hibernate->TC_string, 9);\
     pl_tc_hibernate->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
     pl_tc_hibernate->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
@@ -113,7 +113,7 @@
     {\
         temp = temp->next_TM;\
         delete tm_ptr_hibernate;\
-        tm_ptr_hibernate = temp;\        
+        tm_ptr_hibernate = temp;\
     }\
 }
 
@@ -125,7 +125,7 @@
     
 #define SET_PL_BEE_SCIENCE(tm_ptr_science) {\
     Base_tc *pl_tc_science = new Short_tc;\
-    pl_tc_science->next_TC = NULL;\    
+    pl_tc_science->next_TC = NULL;\
     pl_tc_science->TC_string[0] = 0x01;\
     pl_tc_science->TC_string[1] = 0xE1;\
     pl_tc_science->TC_string[2] = 0x81;\
@@ -147,7 +147,7 @@
     {\
         temp = temp->next_TM;\
         delete tm_ptr_science;\
-        tm_ptr_science = temp;\        
+        tm_ptr_science = temp;\
     }\
 }
 void print_processed_block(uint8_t index)
--- a/COM_MNG_TMTC.h	Thu Jul 14 23:05:09 2016 +0000
+++ b/COM_MNG_TMTC.h	Fri Jul 15 14:59:27 2016 +0000
@@ -348,11 +348,6 @@
     //uint8_t ACKCODE = 0xB0;
     bool y;
     //gPC.printf("\rTC execution in progress\r\n");
-    Base_tm *tm_pointer = new Long_tm;
-    Base_tm *tm_ptr = tm_pointer;
-    //Base_tm *tm_ptr = new Long_tm;
-    Base_tm *tm_pointer_short = new Short_tm;
-    Base_tm *tm_ptr_short = tm_pointer_short;
     received = 0;
     switch(GETapid(tc_ptr))             //checking APID's
     {
@@ -366,44 +361,46 @@
                 
             if(y==1)                                                            
             {        
-                gPC.printf("BAE write fail\r\n");                                                                       
+                gPC.printf("Write fail\r\n");
+                Base_tm *tm_ptr_short = new Short_tm;
                 ACK_L234(tm_ptr_short,0x03,GETpacket_seq_count(tc_ptr));
                 return tm_ptr_short;
             }                                  
             else if(y==0)
             {
-                gPC.printf("BAE write succe\r\n");
+                gPC.printf("Write success\r\n");
                 //wait(0.2);
                 int poll=0;
-                while(poll<9000 && BAE_I2C_GPIO==0)
+                while(poll<20000 && BAE_I2C_GPIO==0)
                 {
                     wait_us(10);
                     poll+=1;
                 }
-                //while(BAE_I2C_GPIO==0){}   
-                if(BAE_I2C_GPIO == 1)                                            //name to be changed later
+                if(BAE_I2C_GPIO == 1)
                 {
-                    gPC.printf("BAE_I2C_GPIO is high\r\n");
-                    
-                    y=FCTN_I2C_READ((char*)tm_pointer->TM_string,TM_LONG_SIZE);            //rify later about the size
+                    //gPC.printf("BAE_I2C_GPIO is high\r\n");
+                    Base_tm *tm_ptr = new Long_tm;
+                    y=FCTN_I2C_READ((char*)tm_ptr->TM_string,TM_LONG_SIZE);
                     if(y==0)
                     {
-                        gPC.printf("BAE read success\r\n");
-                        tm_pointer->next_TM=NULL;
+                        gPC.printf("Read success\r\n");
+                        tm_ptr->next_TM=NULL;
                         return tm_ptr;
                     }
                     else if(y==1)
                     {
-                        gPC.printf("BAE read fail\r\n");
+                        delete tm_ptr;
+                        Base_tm *tm_ptr_short = new Short_tm;
+                        gPC.printf("Read fail\r\n");
                         ACK_L234(tm_ptr_short,0x85,GETpacket_seq_count(tc_ptr));                
                         //gPC.printf("\n\rPTE->PDIR = 0x%08X",PTE->PDIR);
                         return tm_ptr_short;
-                        break;  
                     }
                 }
-                else if(BAE_I2C_GPIO == 0)                                       //name to be changed later
+                else if(BAE_I2C_GPIO == 0)
                 {
                     gPC.printf("BAE_I2C_GPIO is not high\r\n");                    
+                    Base_tm *tm_ptr_short = new Short_tm;
                     ACK_L234(tm_ptr_short,0x84,GETpacket_seq_count(tc_ptr));
                     return tm_ptr_short;
                 }
@@ -427,19 +424,20 @@
                             mid=((uint16_t)(tc_ptr->TC_string[3]<<4))+((uint16_t)(tc_ptr->TC_string[4]));                                                       
                             if(mid!=0x0000 && mid!=0x0001)
                             {
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
-
-                            tm_pointer->TM_string[0] = 0x30;
-                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                            tm_pointer->TM_string[2] = 0xA0;
-                            tm_pointer->TM_string[3] = 0x00;                            //spare bits
-                            FCTN_CDMS_RD_L_RAM(tm_pointer);
+                            Base_tm *tm_ptr = new Long_tm;
+                            tm_ptr->TM_string[0] = 0x30;
+                            tm_ptr->TM_string[1] = GETpacket_seq_count(tc_ptr);
+                            tm_ptr->TM_string[2] = 0xA0;
+                            tm_ptr->TM_string[3] = 0x00;                            //spare bits
+                            FCTN_CDMS_RD_L_RAM(tm_ptr);
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
-                            tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
-                            tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                            tm_pointer->next_TM = NULL;
+                            tm_ptr->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm_ptr->TM_string[133] = (uint8_t)(crc16&0x00FF);
+                            tm_ptr->next_TM = NULL;
                             return tm_ptr;
                         }                           
                         case 0x5:                           // change the cases appropriately
@@ -447,9 +445,10 @@
                             //gPC.printf("WRITE ON SHORT_FLASH_MEMORY\r\n");                          
                             mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);
                             //32 bits at a time           
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(mid == 0x0101 || mid==0x0102 || mid == 0x0103 || mid == 0x0100)                                  //allowable MID values?
                             {
+                                
                                 ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                                                        
@@ -477,6 +476,7 @@
                                     flasharray_thres[z] = scp_threshold_m0[z+8];
                                 erase_sector(strt_add_thres);
                                 program_flash(strt_add_thres, (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -490,6 +490,7 @@
                                     flasharray_thres[z] = scp_threshold_m0[z+40];
                                 erase_sector(strt_add_thres+SECTOR_SIZE);
                                 program_flash(strt_add_thres+SECTOR_SIZE, (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -503,6 +504,7 @@
                                     flasharray_thres[z] = scp_threshold_m1[z];
                                 erase_sector(strt_add_thres+(2*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(2*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -516,6 +518,7 @@
                                     flasharray_thres[z] = scp_threshold_m1[z+32];
                                 erase_sector(strt_add_thres+(3*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(3*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -527,9 +530,9 @@
                                 scp_threshold_m0_1[6] = tc_ptr->TC_string[8];
                                 for(uint8_t z=0;z<5;z++)
                                     scp_threshold_m0_1[z] = tc_ptr->TC_string[z+9];
-                                //scp_sfp_threshold_m0_2[0] = tc_ptr->TC_string[14];
-                                //scp_sfp_threshold_m0_2[1] = tc_ptr->TC_string[15];
-                                //scp_sfp_threshold_m0_2[2] = tc_ptr->TC_string[16];
+                                scp_sfp_threshold_m0_2[0] = tc_ptr->TC_string[14];
+                                scp_sfp_threshold_m0_2[1] = tc_ptr->TC_string[15];
+                                scp_sfp_threshold_m0_2[2] = tc_ptr->TC_string[16];
                                 //int strt_add = flash_size() - (7*SECTOR_SIZE);
                                 //uint32_t flasharray[32] = {0};
                                 flasharray_thres[0] = scp_threshold_m0_1[7];
@@ -538,11 +541,12 @@
                                 flasharray_thres[3] = scp_threshold_m0_1[6];
                                 for(uint8_t z=0;z<5;z++)
                                     flasharray_thres[z+4] = scp_threshold_m0_1[z];
-                                //flasharray_thres[9]  = scp_sfp_threshold_m0_2[0];
-                                //flasharray_thres[10] = scp_sfp_threshold_m0_2[1];
-                                //flasharray_thres[11] = scp_sfp_threshold_m0_2[2];
+                                flasharray_thres[9]  = scp_sfp_threshold_m0_2[0];
+                                flasharray_thres[10] = scp_sfp_threshold_m0_2[1];
+                                flasharray_thres[11] = scp_sfp_threshold_m0_2[2];
                                 erase_sector(strt_add_thres+(4*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(4*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -560,6 +564,7 @@
                                     flasharray_thres[z] = sfp_threshold_m0[z+8];
                                 erase_sector(strt_add_thres+(5*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(5*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -573,6 +578,7 @@
                                     flasharray_thres[z] = sfp_threshold_m0[z+40];
                                 erase_sector(strt_add_thres+(6*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(6*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -586,6 +592,7 @@
                                     flasharray_thres[z] = sfp_threshold_m1[z];
                                 erase_sector(strt_add_thres+(7*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(7*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }
@@ -599,6 +606,7 @@
                                     flasharray_thres[z] = sfp_threshold_m1[z+32];
                                 erase_sector(strt_add_thres+(8*SECTOR_SIZE));
                                 program_flash(strt_add_thres+(8*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                        
@@ -606,6 +614,7 @@
                         default:
                         {
                             gPC.printf("INVALID SERVICE SUBTYPE\r\n");
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;
                         }
@@ -618,12 +627,13 @@
                     {                               
                         if(GETpid(tc_ptr)==0x01)
                         {
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(CDMS_STANDBY==1)
                             {   
                                 //gPC.printf("TC_PL_INIT\r\n");                         // call PWR_SWCH_ON function
                                 
                                 P_PL_INIT();
-
+                                    
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                
@@ -635,6 +645,7 @@
                         }                                
                         else if(GETpid(tc_ptr)==0x02)
                         {
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(CDMS_STANDBY==1)
                             {   
                                 //gPC.printf("TC_PL_MAIN\r\n");                         // call PWR_SWCH_ON function
@@ -652,6 +663,7 @@
                         }
                         else if(GETpid(tc_ptr)==0x03)
                         {                            
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(CDMS_STANDBY==1)
                             {   
                                 //gPC.printf("PL_COM_INIT\r\n");                        // call PWR_SWCH_ON function
@@ -669,6 +681,7 @@
                         }
                         else if(GETpid(tc_ptr)==0x04)
                         {                            
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(CDMS_STANDBY==1)
                             {   
                                 //gPC.printf("TC_P_CDMS_HK_MAIN\r\n"); // call PWR_SWCH_ON function
@@ -686,6 +699,7 @@
                         }
                         else if(GETpid(tc_ptr)==0x05)
                         {                            
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(CDMS_STANDBY==1)
                             {   
                                 //gPC.printf("TC_SW_ON_SD\r\n");                        // call PWR_SWCH_ON function
@@ -703,6 +717,7 @@
                         }
                         else if(GETpid(tc_ptr)==0x06)
                         {                            
+                            Base_tm *tm_ptr_short = new Short_tm;
                             if(CDMS_STANDBY==1)
                             {   
                                 //gPC.printf("TC_SW_ON_RTC\r\n"); // call PWR_SWCH_ON function
@@ -723,7 +738,7 @@
                             //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
                             
                             CDMS_SD_SW_ON();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                       
                         }
@@ -732,7 +747,7 @@
                             //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
                             
                             CDMS_SD_SW_OFF();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;     
                         }
@@ -741,7 +756,7 @@
                             //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
                             
                             CDMS_RTC_ON();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                       
                         }
@@ -750,7 +765,7 @@
                             //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
                             
                             CDMS_RTC_OFF();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;
                         } 
@@ -759,7 +774,7 @@
                             //gPC.printf("TC_SW_ON_BAE\r\n"); // call PWR_SWCH_ON function
                             
                             SW_ON_BAE();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                            
                         }
@@ -768,7 +783,7 @@
                             //gPC.printf("TC_SW_OFF_BAE\r\n"); // call PWR_SWCH_ON function
                             
                             SW_OFF_BAE();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                        
                         }
@@ -777,7 +792,7 @@
                             //gPC.printf("TC_SW_ON_PL_BEE\r\n"); // call PWR_SWCH_ON function
                             
                             SW_ON_PL_BEE();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                        
                         }
@@ -786,7 +801,7 @@
                             //gPC.printf("TC_SW_OFF_PL_BEE\r\n"); // call PWR_SWCH_ON function
                             
                             SW_OFF_PL_BEE();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;    
                         }
@@ -795,7 +810,7 @@
                             //gPC.printf("TC_SW_ON_PL_EPS\r\n"); // call PWR_SWCH_ON function
                             
                             SW_ON_PL_EPS();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));     
                             return tm_ptr_short;                            
                         }
@@ -804,7 +819,7 @@
                             //gPC.printf("TC_SW_OFF_PL_EPS\r\n"); // call PWR_SWCH_ON function
                             
                             SW_OFF_PL_EPS();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                            
                         }
@@ -814,7 +829,7 @@
                             //gPC.printf("TC_SW_ON_V_A_EN\r\n"); // call PWR_SWCH_ON function
                             
                             SW_ON_V_A_EN();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                        
                         }
@@ -823,7 +838,7 @@
                             //gPC.printf("TC_SW_OFF_V_A_EN\r\n"); // call PWR_SWCH_ON function
                             
                             SW_OFF_V_A_EN();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                    
                         }
@@ -832,7 +847,7 @@
                             //gPC.printf("TC_RST_SD\r\n"); // call PWR_SWCH_ON function
                             
                             RST_SD();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                        
                         }
@@ -841,7 +856,7 @@
                             //gPC.printf("TC_RST_BAE\r\n"); // call PWR_SWCH_ON function
                             
                             RST_BAE();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                            
                         }
@@ -850,7 +865,7 @@
                             //gPC.printf("TC_RST_PL_BEE\r\n"); // call PWR_SWCH_ON function
                             
                             RST_PL_BEE();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                        
                         }
@@ -859,6 +874,7 @@
                             //gPC.printf()
                             
                             CDMS_INTERNAL_RESET();
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                         }
                         else if(GETpid(tc_ptr)==0x40)
@@ -866,7 +882,7 @@
                             //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
                             
                             STANDBY_PRCS();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                    
                         }
@@ -875,7 +891,7 @@
                             //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
                             
                             RST_HK_COUNTER();
-
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;    
                         }
@@ -884,62 +900,67 @@
                             //gPC.printf("TC_CDMS_RD_RTC\r\n"); // call PWR_SWCH_ON function
                             
                             uint32_t time_temp = FCTN_CDMS_RD_RTC();                        //RTC_RD_EROR has to incorporated
-                            tm_pointer->TM_string[0] = 0x78;     
-                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                            tm_pointer->TM_string[2] = 0xA0;
-                            tm_pointer->TM_string[3] = 0x00;
-                            tm_pointer->TM_string[4] = (uint8_t)((time_temp & 0xFF000000)>>24);
-                            tm_pointer->TM_string[5] = (uint8_t)((time_temp & 0x00FF0000)>>16);
-                            tm_pointer->TM_string[6] = (uint8_t)((time_temp & 0x0000FF00)>>8);
-                            tm_pointer->TM_string[7] = (uint8_t)((time_temp & 0x000000FF));
+                            Base_tm *tm_ptr = new Long_tm;
+                            tm_ptr->TM_string[0] = 0x78;     
+                            tm_ptr->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
+                            tm_ptr->TM_string[2] = 0xA0;
+                            tm_ptr->TM_string[3] = 0x00;
+                            tm_ptr->TM_string[4] = (uint8_t)((time_temp & 0xFF000000)>>24);
+                            tm_ptr->TM_string[5] = (uint8_t)((time_temp & 0x00FF0000)>>16);
+                            tm_ptr->TM_string[6] = (uint8_t)((time_temp & 0x0000FF00)>>8);
+                            tm_ptr->TM_string[7] = (uint8_t)((time_temp & 0x000000FF));
                             for(uint8_t i=0;i<124;i++)
                             {
-                                tm_pointer->TM_string[i+8] = 0x00;
+                                tm_ptr->TM_string[i+8] = 0x00;
                             }
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
-                            tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
-                            tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                            tm_pointer->next_TM = NULL;                 
+                            tm_ptr->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm_ptr->TM_string[133] = (uint8_t)(crc16&0x00FF);
+                            tm_ptr->next_TM = NULL;                 
                             return tm_ptr;                                                     //incomplete
                         }
                         else if(GETpid(tc_ptr)==0xF0)
                         {
                             uint8_t statusbits[64];
                             if( CDMS_RD_SD_HK(statusbits) != 0){
-                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr;
+                                Base_tm *tm_ptr_short = new Short_tm;
+                                ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
                             }
                             else
                             {
                                 uint32_t time_temp = FCTN_CDMS_RD_RTC();                        //RTC_RD_EROR has to incorporated
-                                tm_pointer->TM_string[0] = 0x78;     
-                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                tm_pointer->TM_string[2] = 0xA0;    // ACK CODE
-                                tm_pointer->TM_string[3] = 0x00;
+                                Base_tm *tm_ptr = new Long_tm;
+                                tm_ptr->TM_string[0] = 0x78;     
+                                tm_ptr->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
+                                tm_ptr->TM_string[2] = 0xA0;    // ACK CODE
+                                tm_ptr->TM_string[3] = 0x00;
                                 for(int i=4;i<64;i++)
                                 {
-                                    tm_pointer->TM_string[i+4] = (i<68)?statusbits[i]:0x00;
+                                    tm_ptr->TM_string[i+4] = (i<68)?statusbits[i]:0x00;
                                 }
                                 for(uint8_t i=68;i<132;i++)
                                 {
-                                    tm_pointer->TM_string[i] = 0x00;
+                                    tm_ptr->TM_string[i] = 0x00;
                                 }
                                 crc16 = crc16_gen(tm_ptr->TM_string,132);
-                                tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
-                                tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                                tm_pointer->next_TM = NULL;                                                                     //incomplete
+                                tm_ptr->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+                                tm_ptr->TM_string[133] = (uint8_t)(crc16&0x00FF);
+                                tm_ptr->next_TM = NULL;                                                                     //incomplete
                                 return tm_ptr;
                             }
                         }
                         else if(GETpid(tc_ptr)==0xF1)
                         {
                             CDMS_CALIB_RTC((uint64_t)(tc_ptr->TC_string[4])<<32 + (uint64_t)(tc_ptr->TC_string[5])<<24 + (uint64_t)(tc_ptr->TC_string[5])<<16 + (uint64_t)(tc_ptr->TC_string[6])<<8  +(uint64_t)(tc_ptr->TC_string[7]));
-                            ACK_L234(tm_pointer,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr;
+                            Base_tm *tm_ptr_short = new Short_tm;
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;
                         }                               
                         else 
                         {
                             gPC.printf("INVALID FID\r\n");
+                            Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
                             return tm_ptr_short;
                         }
@@ -947,6 +968,7 @@
                     else 
                     {
                         gPC.printf("INVALID SERVICE SUBTYPE\r\n");
+                        Base_tm *tm_ptr_short = new Short_tm;
                         ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
                         return tm_ptr_short;
                     }                                    
@@ -957,6 +979,7 @@
                     if(GETservice_subtype(tc_ptr)==0x1)                     //make sure it is LONG TC before executing else INVALID TC
                     {
                         gPC.printf("\n\rTC with subtype = 0x1");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         for(uint8_t i=0,j=0;i<32;i++)
                         {
@@ -972,6 +995,7 @@
                     else if(GETservice_subtype(tc_ptr)==0x2)
                     {
                         gPC.printf("\n\rTC with subtype = 0x2");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         for(uint8_t i=32,j=0;i<64;i++,j+=4)
                         {
@@ -986,6 +1010,7 @@
                     else if(GETservice_subtype(tc_ptr)==0x3)
                     {
                         gPC.printf("\n\rTC with subtype = 0x3");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         for(uint8_t i=64,j=0;i<96;i++,j+=4)
                         {
@@ -1000,6 +1025,7 @@
                     else if(GETservice_subtype(tc_ptr)==0x4)
                     {
                         gPC.printf("\n\rTC with subtype = 0x4");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         for(uint8_t i=96,j=0;i<128;i++,j+=4)
                         {
@@ -1014,6 +1040,7 @@
                     else if(GETservice_subtype(tc_ptr)==0x5)
                     {   
                         gPC.printf("\n\rTC with subtype = 0x5");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         for(uint8_t i=128,j=0;i<160;i++,j+=4)
                         {
@@ -1028,6 +1055,7 @@
                     else if(GETservice_subtype(tc_ptr)==0x6)
                     {
                         gPC.printf("\n\rTC with subtype = 0x6");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         for(uint8_t i=160,j=0;i<192;i++,j+=4)
                         {
@@ -1042,19 +1070,22 @@
                     else if(GETservice_subtype(tc_ptr)==0xF)
                     {
                         gPC.printf("\n\rTC with subtype = 0xF");
+                        Base_tm *tm_ptr = new Long_tm;
                         TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
                         return tm_ptr;
                     }
                     else
                     {
                         gPC.printf("INVALID SERVICE SUBTYPE\r\n");
+                        Base_tm *tm_ptr_short = new Short_tm;
                         ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
                         return tm_ptr_short;
                     }
                 }
                 default:
                 {
-                    gPC.printf("ILLEGAL TC"); 
+                    gPC.printf("ILLEGAL TC");
+                    Base_tm *tm_ptr_short = new Short_tm;
                     ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
                     return tm_ptr_short;                    
                 }
@@ -1071,42 +1102,43 @@
                 y=FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_SHORT_SIZE);
                 
             if(y==1)                                                            
-            {                                                                               
+            {
+                Base_tm *tm_ptr_short = new Short_tm;
                 ACK_L234(tm_ptr_short,0x03,GETpacket_seq_count(tc_ptr));
                 return tm_ptr_short;
-                break;
             }                                  
             else if(y==0)
             {
                 //wait(0.2);
                 int poll=0;
-                while(poll<900 && PL_I2C_Intr==0)
+                while(poll<2000 && PL_I2C_Intr==0)
                 {
                     wait_us(10);
                     poll+=1;
                 }    
-                if(PL_I2C_Intr == 1)                                            //name to be changed later
+            if(PL_I2C_Intr == 1)
                 {
                     gPC.printf("PL_I2C_Intr is high\r\n");
-                    
-                    y=FCTN_I2C_READ_PL((char*)tm_pointer->TM_string,TM_LONG_SIZE);            //rify later about the size
+                    Base_tm *tm_ptr = new Long_tm;
+                    y=FCTN_I2C_READ_PL((char*)tm_ptr->TM_string,TM_LONG_SIZE);
                     if(y==0)
                     {
-                        tm_pointer->next_TM=NULL;
+                        tm_ptr->next_TM=NULL;
                         return tm_ptr;
-                        break;  
                     }
                     else if(y==1)
                     {
+                        delete tm_ptr;
+                        Base_tm *tm_ptr_short = new Short_tm;
                         ACK_L234(tm_ptr_short,0x85,GETpacket_seq_count(tc_ptr));                
                         //gPC.printf("\n\rPTE->PDIR = 0x%08X",PTE->PDIR);
                         return tm_ptr_short;
-                        break;  
                     }
                 }
                 else if(PL_I2C_Intr == 0)                                       //name to be changed later
                 {
                     gPC.printf("PL_I2C_Intr is not high\r\n");                    
+                    Base_tm *tm_ptr_short = new Short_tm;
                     ACK_L234(tm_ptr_short,0x84,GETpacket_seq_count(tc_ptr));
                     return tm_ptr_short;
                 }
@@ -1114,12 +1146,13 @@
         }
         default:    //invalid TC
         {
-            gPC.printf("INVALID TC\r\n");                 
+            gPC.printf("INVALID TC\r\n");
+            Base_tm *tm_ptr_short = new Short_tm;
             ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
             return tm_ptr_short;
         }
     }
-    return;
+    //return NULL;
 }
 // CDMS TEAM CODE END
 // EXECUTE OBOSC
@@ -1241,8 +1274,8 @@
     int reset_flag = 0;\
     if (DEBUG)\
         gPC.puts("iNSIDE EXECUTE_OBOSC_ONLY\r\n");\
-    Base_tm *obosc_tm_core = NULL;\            
-    Base_tm *obosc_tm = obosc_tm_core;\                    
+    Base_tm *obosc_tm_core = NULL;\
+    Base_tm *obosc_tm = obosc_tm_core;\
     for(uint8_t execute_psc = PSC_START_VALUE ; execute_psc < gTOTAL_VALID_TC ; ++execute_psc){\
         Base_tc* current_TC = gHEAD_NODE_TCL;\
         int overCount = 0;\
@@ -1341,7 +1374,7 @@
         gPAY_SPI->bulkRead_resume(&payload_isr_fun);\
         HK_counter->start(10000);\
         gSESSION_TIMEOUT.detach();\
-    }\    
+    }\
 }
 
 #define EXECUTE_TC {\
@@ -1389,7 +1422,7 @@
                                 /*gPC.putc(tm_ptr->TM_string[i]);*/\
                             }\
                             tm_ptr = tm_ptr->next_TM;\
-                        }\ 
+                        }\
                         /*Send only call sign, ACK_L1 to GS*/\
                         /*snd_tm.head_pointer(tm_ptr_head);*/\
                         /*transmit_adf;*/\
@@ -1523,7 +1556,7 @@
                             snd_tm.head_pointer(tm_ptr_head);\
                             gPC.puts("enter_adf\r\n");\
                             transmit_adf;\
-                            gPC.puts("exit_adf\r\n");\ 
+                            gPC.puts("exit_adf\r\n");\
                             /*DELETE THE TM AFTER USE*/\
                             tm_ptr = tm_ptr_head;\
                             overflowCountExecute = 0;\
@@ -1537,7 +1570,7 @@
                                 else{\
                                     RESET_CDMS;\
                                     break;\
-                                }\ 
+                                }\
                             }\
                         }\
                         else{\
--- a/Compression.h	Thu Jul 14 23:05:09 2016 +0000
+++ b/Compression.h	Fri Jul 15 14:59:27 2016 +0000
@@ -1,18 +1,38 @@
-unsigned int read_2byte(uint8_t* ptr){
-    unsigned int output = (unsigned int) *(ptr+1);
-    output += ( (unsigned int)(*ptr) ) << 8;
-    return output;
-}
-    
-unsigned int read_4byte(uint8_t* 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;
+/*-----------------------------------------to_do_compression.h--------------------------------------------------
+
+    -> SD_Write error to be checked
+    -> fsc_counter is 32 bit but in the code 24 bit. Talk to chaitu
+---------------------------------------------------------------------------------------------------------------*/
+
+//updated compression algorithm
+/*#include <iostream>
+#include "stdio.h"
+#include "crc.h"
+#include "interleave.h"
+#include "Convolution.h"
+#include "bitset"*/
+//using namespace std;
+
+//reading functions 2byte, 4byte
+
+uint16_t read_2byte(uint8_t* ptr)
+{
+    uint16_t output = (uint8_t ) *(ptr+1);
+    output += ( (uint8_t )(*ptr) ) << 8;
     return output;
 }
 
-int adjust(int size, unsigned int data, uint8_t* ptr , int space){
+uint32_t read_4byte(uint8_t* ptr)
+{
+    uint32_t output = (uint32_t) (*(ptr+3));
+    output += (uint32_t)(*(ptr+2)<<8);
+    output += (uint32_t)(*(ptr+1)<<16);
+    output += (uint32_t)(*(ptr)<<24);
+    return output;
+}
+
+uint8_t adjust(uint8_t size, uint8_t data, uint8_t* ptr , uint8_t space)
+{
     space = space&0x0f;
     if(space == 8)  *ptr = 0;
     data = data&((1<<size)-1);
@@ -29,284 +49,337 @@
         return 0x10 + 8-(size - space);
     }
 }
-
-
-int compress (int data, int x, int y){  //to be compressed with scheme (msb x)*4^y ;
-    for(int i = 0 ; i < 1<<y ; i++){
-        if(data <= ( (1<<x)-1) * (1<<(2*i)) ){
+int compress (int data, int x, int y)   //to be compressed with scheme (msb x)*4^y ;
+{
+    for(int i = 0 ; i < (1<<y) ; i++)
+    {
+        if(data <= ( (1<<x)-1) * (1<<(2*i)) )
+        {
             return ( ((data>>i*2)<<y) + i);
         }
     }
-    if ( data > ( (1<<x)-1) * (1<<(2*((1<<y)-1))) ){
-//      cout <<"compression exception"<<endl;
-        return 0;
+    if ( data > (( (1<<x)-1) * (1<<(2*((1<<y)-1)))) )
+    {
+        //cout <<"compression exception"<<endl;
+        return (((1<<x)-1) * (1<<(2*((1<<y)-1))));
     }
 }
 
-
+//--------------------------------------------------------beacon
+uint8_t beacon_array[134];
+uint8_t beacon_cntr = 1;
+uint8_t *beacon_ptr;
+//--------------------------------------------------------beacon
 
-//variable declerations
-uint8_t 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.
-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}; 
-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};
-unsigned int scp_threshold_m0_1[9]={114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for mode 0..ie callibrated mode
-unsigned int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
+//variable declarations
+uint8_t srp_mode , at , pzf , ezf ,sfp[71] ,scp[56],beacon_at; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
+uint32_t sfp_bin[52] , scp_bin[52];         //storing the bin values.
+uint32_t sfp_threshold_m0[52]  = {124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,4032,252,252,252,252,252,4032,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};
+uint32_t scp_threshold_m0[52]  = {4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,114688,8128,4032,4032,4032,4032,114688,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};
+uint32_t scp_threshold_m0_1[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for callibrated mode with data conservation
+uint32_t scp_sfp_threshold_m0_2[3] = {245760,65472,65472};
+uint32_t sfp_threshold_m1[52]  = {1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,8128,8128,8128};
+uint32_t scp_threshold_m1[52]  = {114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,245760,245760,245760,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,debug_cntr = 0, size,space;
-uint32_t FSC_science;   
+uint64_t proton_scp_sum,electron_scp_sum,FSC_science;
+uint16_t temp_crc;
+uint32_t debug_cntr, size,space,length;
 uint8_t *ptr ,* pointer;
-long long int sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
-int test_cntr = 0;
+uint64_t sci_time = 0, time_prev_scp = 0;
+uint32_t attitude = 0;
+uint32_t beacon_threshold[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472};
 
-    Convolution ConvObj;
-    void convolution (uint8_t * ptr){
-        ConvObj.convolutionEncode(ptr, TM_convoluted_data);
-        ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
-    }
+Convolution ConvObj;
+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(uint8_t * head){
-    //sci_time = FCTN_CDMS_RD_RTC();
-    sci_time = 0;
-    gPC.puts("enter srp");
-    
-    
-    
-    //Call FCTN_SD_MNGR for latest fsc
-    FCTN_SD_MNGR();
-    uint8_t sd_stat;
-    
-    for (int i = 0; i < 52 ; i++){
+void srp(uint8_t * head)
+{
+    gPC.printf("\n\rEntered srp");
+    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++)
+    {
         scp_bin[i] = 0;
     }
-    ptr = head + 3;
+    ptr = head + 3;                                         //ptr points to proton energy bin of srp
     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++){
+    for(uint8_t counter = 0 ; counter < 60 ; counter++)
+    {       
+        /*-------------------- ------------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 
+        {
+            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]){
+                if(sfp_bin[i]>sfp_threshold_m0[i])
+                {
                     at = 1;
                 }
-                if(i<17){
+                if(i<17)
+                {
                     if(sfp_bin[i] > 0)
                         pzf = 0;
                 }
-                else if (i>17 && i < 23){
+                else if (i>17 && i < 23)
+                {
                     if(sfp_bin[i]>0)
                         ezf = 0;
                 }
             }
-            for(int i=0; i<4; i++){
+            for(int i=0; i<4; i++)                          //last 4 bins(excluding the spare bin) are 32 bit each
+            {
                 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])
                     at = 1;
             }
         }
-        else if(srp_mode == 1){ //scattered mode
-            for(int i = 0; i <32; i++){
-                sfp_bin[i] = read_2byte(ptr+2*i);
+        else if(srp_mode == 1)                              //scattered mode
+        {
+            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[0])
+                if(sfp_bin[i] > sfp_threshold_m1[i])
                     at = 1;
             }
-            for(int i = 0; i < 4 ; i++){
-                sfp_bin[i+32] = read_4byte( (ptr+64) + 4*i );
-                scp_bin[i+32] += sfp_bin[i+32];
-                if(sfp_bin[i+32] > sfp_threshold_m1[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])
                     at = 1;
             }
         }
-        ptr = ptr + 112;
-        for(int i = 0; i<65; i++)
+        /*---------------------------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
+        
+        /*------------------------------------------forming a science fine packet starts here----------------------------------------
+        First we fill packet header, then fill packet data and finally assign its length
+        ----------------------------------------------------------------------------------------------------------------------------*/
+        for(int i = 0; i<71; i++)    
             sfp[i] = 0;
-        if(srp_mode == 0){      //calibrated mode
-            if(at == 0){
+        if(srp_mode == 0)                                                                   //calibrated mode
+        {
+            if(at == 0)
+            {
                 pointer = sfp; debug_cntr = 0;
-                space = adjust(4, attitude,pointer,8);  pointer += space>>4;    debug_cntr += space>>4;
+                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;
                 space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
-                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;
-                for(int i = 0 ; i < 8 ; i++){
-                    space = adjust(1, ((compress(sfp_bin[24+i],7,2))&0x100)>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                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;
+                for(uint8_t i = 0 ; i < 8 ; i++){
+                    space = adjust(1, ((compress(sfp_bin[24+i],7,2))&0x100)>>8 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
                     space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                 }
-                for(int i = 0 ; i <12 ; i++){
-                    space = adjust(6, (compress(sfp_bin[32+i],4,2)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i = 0 ; i <12 ; i++){
+                    space = adjust(6, (compress(sfp_bin[32+i],4,2)) ,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
                 }
-                for(int i = 0; i < 4; i++){
-                    space = adjust(8, (compress(sfp_bin[44+i],6,2)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i = 0; i < 4; i++){
+                    space = adjust(8, (compress(sfp_bin[44+i],6,2)) ,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
                 }
-                for(int i = 0 ; i < 4 ; i++){
-                    space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                    space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i = 0 ; i < 4 ; i++){
+                    space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 }
-                space = adjust(8, ((compress(sfp_bin[17],6,2))&0xff) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8, ((compress(sfp_bin[23],6,2))&0xff) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, ((compress(sfp_bin[17],6,2))&0xff) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, ((compress(sfp_bin[23],6,2))&0xff) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
                 if(pzf == 0){
-                    for(int i = 0; i<2 ; i++){
-                        space = adjust(6, ((compress(sfp_bin[i],5,1))&0xff) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                    for(uint8_t i = 0; i<2 ; i++){
+                        space = adjust(6, ((compress(sfp_bin[i],5,1))&0xff) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
                     }
-                    for(int i = 0; i<15 ; i++){
-                        space = adjust(5, ((compress(sfp_bin[i+2],4,1))&0xff) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                    for(uint8_t i = 0; i<15 ; i++){
+                        space = adjust(5, ((compress(sfp_bin[i+2],4,1))&0xff) ,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
                     }
                 }
                 if(ezf == 0){
-                    for(int i = 0; i <5 ;i++){
-                        space = adjust(7, ((compress(sfp_bin[18+i],6,1))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    for(uint8_t i = 0; i <5 ;i++){
+                        space = adjust(7, ((compress(sfp_bin[18+i],6,1))&0xff) ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
                     }
                 }
-            }//below thershold ends here.
-        
-        //gPC.puts("below_thresh\r\n");
-        
+            }                                                                                           //below thershold ends here.
             if(at == 1){
                 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);    
-                sfp[5] = ((counter&0x0f)<<3) + (srp_mode<<2);   
+                sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5);
+                sfp[5] = ((counter&0x1f)<<3) + (srp_mode<<2);
                 sfp[5] += (pzf<<1) + ezf ;
-                for(int i = 0 ; i < 8 ; i++){
-                    space = adjust(1, (compress(sfp_bin[24+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i = 0 ; i < 8 ; i++){
+                    space = adjust(1, (compress(sfp_bin[24+i],7,2))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
                     space = adjust(8, ((compress(sfp_bin[24+i],7,2))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                 }
-                for(int i = 0 ; i <12 ; i++){
+                for(uint8_t i = 0 ; i <12 ; i++){
                     space = adjust(6, ((compress(sfp_bin[32+i],3,3))&0xff) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                 }
-                for(int i = 0 ; i < 4 ; i++){
-                    space = adjust(2, (compress(sfp_bin[44+i],8,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                    space = adjust(8, compress(sfp_bin[44+i],8,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i = 0 ; i < 4 ; i++){
+                    space = adjust(2, (compress(sfp_bin[44+i],8,2))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[44+i],8,2) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 }
-                for(int i = 0 ; i < 4 ; i++){
-                    space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                    space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i = 0 ; i < 4 ; i++){
+                    space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 }
-                space = adjust(2, (compress(sfp_bin[17],8,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8, compress(sfp_bin[17],8,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(2, (compress(sfp_bin[23],8,2))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8, compress(sfp_bin[23],8,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
-                
+                space = adjust(2, (compress(sfp_bin[17],8,2))>>8 ,pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, compress(sfp_bin[17],8,2) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(2, (compress(sfp_bin[23],8,2))>>8 ,pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, compress(sfp_bin[23],8,2) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
+
                 if(pzf == 0){
-//                  cout<<"proton bins ";
-                    for(int i = 0; i<17 ; i++){
+                    //cout<<"proton bins ";
+                    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;
-//                      printf("%02X ",compress(sfp_bin[i],8,2));
+                        space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
+                        //printf("%02X ",compress(sfp_bin[i],8,2));
                     }
                 }
                 if(ezf == 0){
-//                  cout<<"electron bins ";
+                    //cout<<"electron bins ";
                     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;
-//                      printf("%02X ",compress(sfp_bin[i],8,2));
+                        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;
+                        //printf("%02X ",compress(sfp_bin[i],8,2));
                     }
                 }
-//              cout<<debug_cntr<<" "<<(space&0xf)<<endl;
-//              cout<<"packet ";
-//              for(int i = 0; i< 64; i++){
-//                  cout<<bitset<8>(sfp[i]);
-//              }
-//              cout<<"ends"<<endl;
-            }//above threshold ends here.
-        }else if(srp_mode == 1){    //scattered mode
-            if(at == 0){
-                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;
-                space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
-                for(int i=0; i<32; i++){
-                    space = adjust(7, compress(sfp_bin[i],6,1) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                }
-                for(int i = 0 ; i < 4 ; i++){
-                    space = adjust(1, (compress(sfp_bin[32+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                    space = adjust(8, compress(sfp_bin[32+i],7,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-                }
-            }
-            if(at == 1){
-                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;
-                space = adjust(8, sci_time>>16 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8, sci_time>>8 ,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,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
-                for(int i=0; i<32; 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;
-                }
-                for(int i = 0 ; i < 4 ; i++){
-                    space = adjust(1, (compress(sfp_bin[32+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                    space = adjust(8, compress(sfp_bin[32+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-                }
-            }
-        }   
-        
-        //gPC.puts("sci fine pack\r\n");
-        //science fine packet is complete here.
-        //lets try to make frame now******************************************************************************************************************************
-        if(srp_mode == 0){
-            if(at == 0){
-                id = 1;     length = 241;
+            }                                                                                   //above threshold ends here.            
+            if(at == 0)
+            {
+                id = 1;     length = 241+5;                                 //5 spare bits
                 if(pzf == 0)
                     length += 87;
                 if(ezf == 0)
                     length += 35;
-            }else{
-                id = 2;     length = 288;
+            }
+            else
+            {
+                id = 2;     length = 288;                                   //0 spare bits
                 if(pzf == 0)
                     length += 170;
                 if(ezf == 0)
                     length += 50;
             }
         }
-        else if(srp_mode == 1){
-            if(at == 0){
-                id = 1;     length = 272;
-            }else{
-                id = 2;     length = 408;
+        else if(srp_mode == 1)                                                                 //scattered mode
+        {
+            if(at == 0)
+            {
+                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;
+                space = adjust(1, srp_mode,pointer,space);  pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i=0; i<48; i++)
+                {
+                    space = adjust(7, compress(sfp_bin[i],5,2) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
+                }
+                for(uint8_t i = 0 ; i < 4 ; i++)
+                {
+                    space = adjust(1, (compress(sfp_bin[48+i],7,2))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[48+i],7,2) ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+            if(at == 1)
+            {
+                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;
+                space = adjust(8, sci_time>>16 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, sci_time>>8 ,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,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(6, counter,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1, srp_mode,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                for(uint8_t i=0; i<48; 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;
+                }
+                for(uint8_t i = 0 ; i < 4 ; i++)
+                {
+                    space = adjust(1, (compress(sfp_bin[48+i],6,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, compress(sfp_bin[48+i],6,3) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+                }
+            }
+            if(at == 0)
+            {
+                id = 1;     length = 384;                                       //0 spare bits
+            }
+            else
+            {
+                id = 2;     length = 568;                                       //0 spare bits
             }
         }
-        length = (length%8==0)?(length/8):(length/8)+1;
-        for(int j=0 ; j<length ;j++){
-//          printf("%02X",sfp[j]);
-            frames[id][position_tm_frame[id]] = sfp[j];
-            position_tm_frame[id]++;
-            if(position_tm_frame[id] == 132){   //space full in the frame bro
+        length = (length%8==0)?(length/8):(length/8)+1;                         //converting length to mulitple of 8
+        /*----------------------------------------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){
+                while(position_tm_frame[id]<132){
+                    frames[id][position_tm_frame[id]] = 0;
+                    position_tm_frame[id]++;
+                }
+            }
+        }
+        else if(id==2){
+            if(position_tm_frame[id]>126){
+                while(position_tm_frame[id]<132){
+                    frames[id][position_tm_frame[id]] = 0;
+                    position_tm_frame[id]++;
+                }
+            }
+        }
+        if(position_tm_frame[id]==position_tm_starting[id]){
+            frames[id][6-id] = position_tm_starting[id];
+        }
+        for(uint16_t j=0 ; j<length ;)
+        {            
+            if(position_tm_frame[id]==frames[id][6-id]){
+                if(id==1){
+                    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);
+                    frames[id][9]  = (uint8_t)((sci_time>>8)&0xff);
+                    frames[id][10] = (uint8_t)((sci_time)&0xff);
+                }
+            }
+            if(position_tm_frame[id]<132){
+                frames[id][position_tm_frame[id]] = sfp[j];
+                j++;
+                position_tm_frame[id]++;                                            //incrementing position of write pointer in TM frame
+            }
+            if(position_tm_frame[id] == 132)
+            {
+                /*-----------the current frame is completely filled and we fill frame header to write into SD card---------*/
                 pointer = frames[id];
-                if(id == 1){    //below thereshold
+                if(id == 1){    //below threshold
                     space = adjust(1,0,pointer,8);
                     space = adjust(4,3,pointer,space);
-                    FSC_science = FSC_CURRENT[3]+1;
-                    //gPC.printf("SID = 3, FSC = %02X\r\n", FSC_CURRENT[3]+1);
+                    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;
-                    frames[id][6] = (sci_time>>32)&0xff;
-                    frames[id][7] = (sci_time>>24)&0xff;
-                    frames[id][8] = (sci_time>>16)&0xff;
-                    frames[id][9] = (sci_time>>8)&0xff;
-                    frames[id][10] = (sci_time)&0xff;
-                }else if(id == 2){
+                    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]+1;
-                    //gPC.printf("SID = 2, FSC = %02X\r\n", FSC_science);
+                    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;
@@ -314,121 +387,302 @@
                 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");*/
-                //}
-                   
+
+                /*------------------current TM frame completely filled-----------------------*/
                 
                 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){
-                    sd_stat = SD_WRITE(TM_interleave_data,FSC_CURRENT[3]+1,3);
-                   }
-                else if (id == 2){
-                    sd_stat = SD_WRITE(TM_interleave_data,FSC_CURRENT[2]+1,2);
-                  }
-                if(sd_stat)
+                if(id == 1)
                 {
-                    //gPC.puts("sd write failure");
-                    break;
-                    }
+                    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
+                }
                 position_tm_frame[id] = position_tm_starting[id];
-                frames[id][6-id] = (length-1) - j;      // first head pointer.
-                
+                frames[id][6-id] = ((length-1) - j)+position_tm_starting[id];
             }
         }
-        
-    }//for loop bracket which runs 60 times
-    
-    //---------------below is scp --------------**************************************************************************************************
+        /*------------------------------finished inserting the sfp packet into the TM frame(s)-------------------------------------*/
+    }                                                                                           // for loop bracket which runs 60 times    
     at = 0;     pzf = 1;    ezf = 1;
     srp_mode = head[2]&0x1;
-    uint8_t compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
-    if(srp_mode ==0){
-        if(compression_option == 0){
-            for(int i=0; i<52 ;i++){
-                if(scp_bin[i] > scp_threshold_m0[i]){
+    uint8_t compression_option = (head[2]>>1)&0x3;
+    /*-------------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT starts here-------------------------
+    ------------------(in case compression option = 0 , then tagging beacon packet(not yet formed) as AT or BT is also done)----------*/
+    if(srp_mode ==0)
+    {
+        if(compression_option == 0)
+        {
+            for(int i=0; i<52 ;i++)
+            {
+                if(scp_bin[i] > scp_threshold_m0[i])
+                {
                     at = 1;
                     break;
                 }
             }
         }
-        else if(compression_option == 1){
+        else if(compression_option == 1)
+        {
             if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
-            for(int i=0; i<4 ;i++){
-                if(scp_bin[48+i] > scp_threshold_m0_1[i+1]){
+            for(int i=0; i<4 ;i++)
+            {
+                if(scp_bin[48+i] > scp_threshold_m0_1[i+1])
+                {
                     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;
-            
+
             proton_scp_sum = 0; electron_scp_sum = 0;
-            for(int i=0;i<17;i++){
+            for(int i=0;i<17;i++)
+            {
                 proton_scp_sum += scp_bin[i];
             }
-            for(int i=0; i<5; i++){
+            for(int i=0; i<5; i++)
+            {
                 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;
         }
-    }else if(srp_mode ==1){
-        for(int i=0; i<32; i++){
-            if(scp_bin[i] > scp_threshold_m1[0]){
-                at = 1;
-                break;
+        else if(compression_option == 2)
+        {
+            if(scp_bin[50] > scp_sfp_threshold_m0_2[0]) 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 > scp_sfp_threshold_m0_2[1]) at=1;
+            if(electron_scp_sum > scp_sfp_threshold_m0_2[2]) at=1;
+        }
+    }
+    else if(srp_mode ==1)
+    {
+        if(compression_option==0){ 
+            for(int i=0; i<48; i++)
+            {
+                if(scp_bin[i] > scp_threshold_m1[i]){
+                    at = 1;
+                    break;
+                }
+            }
+            for(int i=48; i<52; i++)
+            {
+                if(scp_bin[i] > scp_threshold_m1[i]){
+                    at = 1;
+                    break;
+                }
             }
         }
-        for(int i=32; i<36; i++){
-            if(scp_bin[i] > scp_threshold_m1[1]){
-                at = 1;
-                break;
+        else if(compression_option==2){
+            if(scp_bin[50]>scp_sfp_threshold_m0_2[0])   at=1;
+        }
+    }
+    /*----------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT ends here--------------------------*/
+
+    if(srp_mode == 0)
+    {
+        //determining if non zero values of proton and electron bins exist for calibrated mode (srp)
+        for(int i=0; i<17 ;i++)
+        {
+            if(scp_bin[i]>0)
+                pzf = 0;
+        }
+        for(int i=18; i<23 ;i++)
+        {
+            if(scp_bin[i]>0)
+                ezf = 0;
+        }
+    }
+    /*-----------------------------------------Tagging beacon packet as AT or BT starts here-------------------------------------*/
+    beacon_at = 0;
+    if(srp_mode==0)     //those bins which can be meaningfully compared only in srp_mode==0
+    {
+        if(scp_bin[44]>beacon_threshold[0])     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;
+    }
+    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;   
+    /*----------------------------------------Tagging beacon packet as AT or BT ends here------------------------------------------*/
+
+    /*-----------------------------------------------Forming the beacon packet starts here-----------------------------------------*/
+    //Last 512 bits of beacon_array excluding CRC bits is used for storing the 5 most recent beacon packets
+
+    if(beacon_cntr == 1)
+    {
+        beacon_ptr = &(beacon_array[62]);   // starting block address, stores the oldest(among the 5 most recent) beacon packet
+    }
+    else if(beacon_cntr>1 && beacon_cntr<6)
+    {
+        beacon_ptr +=14;                    // increase the block number(totally 5 blocks for 5 beacon packets)
+    }
+    else if(beacon_cntr == 6)
+    {
+        for(uint16_t i=0;i<14;i++)          // when all 5 blocks have been used shift the last 4 blocks up along the array and
+        {                                   // overwrite the new beacon packet in the 5th block
+            beacon_array[62+i] = beacon_array[76+i];
+            beacon_array[76+i] = beacon_array[90+i];
+            beacon_array[90+i] = beacon_array[104+i];
+            beacon_array[104+i] = beacon_array[118+i];
+        }
+        beacon_ptr = &(beacon_array[118]);  //address of the 5th block
+        beacon_cntr = 5;
+    }
+    pointer = beacon_ptr;        debug_cntr = 0;     space = 8;
+    space = adjust(1, 1,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(srp_mode==0){
+        if(beacon_at == 0){
+            space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6, compress(scp_bin[44],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(7, compress(scp_bin[48],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(7, compress(scp_bin[49],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(7, compress(scp_bin[50],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(7, compress(scp_bin[51],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6, compress(scp_bin[17],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6, compress(scp_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;
+            pointer = pointer + 1;
+            for(int temp_i = 12; temp_i<14; temp_i++)
+            {            
+                *pointer = 0;
+                pointer = pointer + 1;
+            }
+        }
+        else if(beacon_at==1){
+            space = adjust(1, 1,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;
+        }
+    }else if(srp_mode==1){
+        if(beacon_at==0){
+            space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6, compress(0,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;
+            pointer = pointer + 1;
+            for(uint8_t temp_i = 7; temp_i<14; temp_i++)
+            {            
+                *pointer = 0;
+                pointer = pointer + 1;
+            }
+        }
+        else if(beacon_at==1){
+            space = adjust(1, 1,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;             
+            space = adjust(8, compress(0,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;
+            pointer = pointer + 1;
+            for(uint8_t temp_i = 10; temp_i<14; temp_i++)
+            {            
+                *pointer = 0;
+                pointer = pointer + 1;
             }
         }
     }
-    
-    for(int i=0; i<17 ;i++){
-        if(scp_bin[i]>0)
-            pzf = 0;
-    }
-    for(int i=18; i<23 ;i++){
-        if(scp_bin[i]>0)
-            ezf = 0;
-    }
-    
-    gPC.puts("scp\r\n");
-    
+    beacon_cntr++;
+    /*--------------------------------------------------beacon packet ends here--------------------------------------------------------*/
+
+    gPC.printf("scp\r\n");
+    uint8_t packet_pp,time_diff;
+    /*------------------------------------------Forming a science packet(scp) starts here----------------------------------------------*/
+
     pointer = scp;      debug_cntr = 0;     space = 8;
-    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;
-    space = adjust(8, (sci_time)&0xff,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(4, (attitude)&0xf,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(2, compression_option,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(1,srp_mode,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(1,srp_mode,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(8, sfp_at_counter>>16,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(8, sfp_at_counter>>8,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-    space = adjust(8, sfp_at_counter,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    time_diff = 0;
+    
+    packet_pp = 1;
+    if((((uint8_t)(sci_time>>7))&0x3f)<(((uint8_t)(time_prev_scp>>7))&0x3f))
+    {
+        time_diff = time_diff+60;
+    }
+    time_diff += (((uint8_t)(sci_time>>7))&0x3f);
+    time_diff -= (((uint8_t)(time_prev_scp>>7))&0x3f);
+    if(time_diff>10){
+        packet_pp = 0;
+    }
+    time_prev_scp = sci_time;
+    uint32_t sfp_at_counter;
+    sfp_at_counter = FSC_CURRENT[2];
+    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;
+    space = adjust(8, (sci_time)&0xff,pointer,space);               pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(4, (attitude)&0xf,pointer,space);                pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(2, compression_option,pointer,space);            pointer += space>>4;    debug_cntr += space>>4; //first two bits of compression option
+    space = adjust(1, at,pointer,space);                            pointer += space>>4;    debug_cntr += space>>4; //last bit of compression option
+    space = adjust(1, srp_mode,pointer,space);                      pointer += space>>4;    debug_cntr += space>>4;
+    if(!(srp_mode==0 && compression_option==2))
+    {
+        space = adjust(8, sfp_at_counter>>16,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(8, sfp_at_counter>>8,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
+        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
-        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;
+        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 ){
             for(int i = 0; i<8 ;i++){
-                space = adjust(7,compress(sfp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
             for(int i = 0; i<12 ;i++){
-                space = adjust(5,compress(sfp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
             for(int i = 0; i<4 ;i++){
                 space = adjust(6,compress(scp_bin[44+i],3,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
@@ -436,21 +690,21 @@
             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;
             }
-            space = adjust(6,compress(scp_bin[17],3,3) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(6,compress(scp_bin[23],3,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6,compress(scp_bin[17],3,3) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6,compress(scp_bin[23],3,3),pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
             if(pzf == 0){
                 for(int i = 0; i<2 ;i++){
                     space = adjust(8,compress(scp_bin[i],6,2) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
                 }
                 for(int i = 0; i<15 ;i++){
-                    space = adjust(7,compress(scp_bin[i+2],5,2) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(7,compress(scp_bin[i+2],5,2) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
                 }
             }
             if(ezf == 0){
                 space = adjust(1,compress(scp_bin[18],7,2)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8,compress(scp_bin[18],7,2) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8,compress(scp_bin[18],7,2) ,pointer,space);         pointer += space>>4;    debug_cntr += space>>4;
                 for(int i=0; i<4; i++){
-                    space = adjust(8,compress(scp_bin[19+i],6,2) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8,compress(scp_bin[19+i],6,2) ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
                 }
             }
         }// below threshold ends here
@@ -462,20 +716,21 @@
                 space = adjust(5,compress(scp_bin[32+i],2,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
             for(int i = 0; i<4 ;i++){
-                space = adjust(1,compress(scp_bin[44+i],6,3)>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(1,compress(scp_bin[44+i],6,3)>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8,compress(scp_bin[44+i],6,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
             for(int i=0; i<4 ;i++){
-                space = adjust(2,compress(scp_bin[48+i],6,4)>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8,compress(scp_bin[48+i],6,4) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(4,compress(scp_bin[48+i],9,3)>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8,compress(scp_bin[48+i],9,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
-            space = adjust(1,compress(scp_bin[17],6,3)>>8 ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(8,compress(scp_bin[17],6,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(1,compress(scp_bin[23],6,3)>>8,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(8,compress(scp_bin[23],6,3),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(1,compress(scp_bin[17],6,3)>>8 ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8,compress(scp_bin[17],6,3),pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(1,compress(scp_bin[23],6,3)>>8,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8,compress(scp_bin[23],6,3),pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
+
             if(pzf == 0){
                 for(int i = 0; i<17 ; i++){
-                    space = adjust(8, (compress(scp_bin[i],5,3)) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+                    space = adjust(8, (compress(scp_bin[i],5,3)) ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
                 }
             }
             if(ezf == 0){
@@ -487,20 +742,20 @@
     }   //srp_mode == 0 ends
     if(srp_mode == 1){
         if(at == 0){
-            for(int i=0; i<32; i++){
-                space = adjust(6, ((compress(scp_bin[i],4,2))&0xff) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+            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;
             }
             for(int i = 0 ; i < 4 ; i++){
-                space = adjust(7, (compress(scp_bin[32+i],4,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(7, (compress(scp_bin[48+i],4,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
             }
         }
         if(at == 1){
-            for(int i=0; i<32; i++){
-                space = adjust(6, (compress(scp_bin[i],3,3))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+            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;
             }
             for(int i = 0 ; i < 4 ; i++){
-                space = adjust(4, (compress(scp_bin[32+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(8, (compress(scp_bin[32+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
             }
         }
     }// scp mode 1 end
@@ -508,120 +763,190 @@
         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(at == 0){
-                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;
-                }
-                space = adjust(6, (compress(scp_bin[17],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-                space = adjust(6, (compress(scp_bin[23],3,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-                if(pzf==0){
-                    space = adjust(2, (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;
-                }
-                if(ezf==0){
-                    space = adjust(2, (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;
-                }
-            }   // scp data conservaton mode 
+            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;
+            }
+            space = adjust(6, (compress(scp_bin[17],3,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(6, (compress(scp_bin[23],3,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;
+                space = adjust(8, ( compress(proton_scp_sum,10,2)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+            }
+            if(ezf==0){
+                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;
+            }
         }else if(at == 1){
-            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;
+            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++){
                 space = adjust(4, (compress(scp_bin[48+i],9,3))>>8 ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, (compress(scp_bin[48+i],9,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
             }
-            space = adjust(1, (compress(scp_bin[17],6,3)>>8) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(8, (compress(scp_bin[17],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(1, (compress(scp_bin[23],6,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
-            space = adjust(8, (compress(scp_bin[23],6,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(1, (compress(scp_bin[17],6,3)>>8) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8, (compress(scp_bin[17],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(1, (compress(scp_bin[23],6,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
+            space = adjust(8, (compress(scp_bin[23],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
             if(pzf==0){
-                space = adjust(1, (compress(proton_scp_sum,6,3))>>8 ,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;
             }
             if(ezf==0){
-                space = adjust(1, (compress(electron_scp_sum,6,3))>>8 ,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;
             }
         }
-    }//-----------------------------------------------------------------------------------------------------------------------------------------------------
-    
-    gPC.puts("sci frame starts\r\n");
-    
-    //time to make scp frame
+    }
+    if(compression_option == 2 )  //scp extreme data conservation mode
+    {
+        if(srp_mode==0)
+        {
+            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){
+                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;
+                    space = adjust(8, ( compress(proton_scp_sum,10,2)) ,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+                }
+                if(ezf==0){
+                    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;
+                }
+            }else if(at==1){
+                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){
+                    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;
+                }
+                if(ezf==0){
+                    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;
+                }
+            }
+        }
+        else if(srp_mode==1)
+        {
+            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){
+                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;
+                space = adjust(8, (compress(0,10,2)) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(4, (compress(0,10,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+                space = adjust(8, (compress(0,10,2)) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
+            }
+            else if(at==1){
+                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;
+                space = adjust(8, (compress(0,6,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;
+                space = adjust(8, (compress(0,6,3)) ,pointer,space);                    pointer += space>>4;    debug_cntr += space>>4;
+            }
+        }
+    }
+    /*---------------------------------------------forming a science packet ends here----------------------------------------------*/
+
     id = 0;
     if(srp_mode == 0 && compression_option == 0){
         if(at == 0){
-            length = 228;
+            length = 228+2;                                                       //2 spare bits
             if(pzf == 0)
                 length += 121;
             if(ezf == 0)
                 length +=41;
         }else if(at == 1){
-            length = 266;
+            length = 266+6;                                                       //6 spare bits
             if(pzf == 0)
                 length += 136;
             if(ezf == 0)
                 length += 40;
         }
-            
-    }else if(srp_mode == 0 && compression_option == 1){     //data conservation mode
+    }else if(srp_mode == 0 && compression_option == 1){                         //data conservation mode
         if(at == 0){
-            length = 94;
+            length = 94+2;                                                        //2 spare bits
             if(pzf == 0)
                 length += 12;
             if(ezf == 0)
                 length += 12;
         }else if(at == 1){
-            length = 123;
+            length = 123+3;                                                       //3 spare bits
+            if(pzf == 0)
+                length += 9;
+            if(ezf == 0)
+                length += 9;
+        }
+    }
+    else if( srp_mode == 1){
+        if(at == 0)
+            length = 368;
+        else if(at == 1)
+            length = 432;
+    }
+    else if(compression_option == 2)                                //in extreme data conservation mode, length is same for both srp_modes
+    {                         
+        if(at == 0){
+            length = 31+1;                                                        //1 spare bits
+            if(pzf == 0)
+                length += 12;
+            if(ezf == 0)
+                length += 12;
+        }else if(at == 1){
+            length = 36+2;                                                       //2 spare bits
             if(pzf == 0)
                 length += 9;
             if(ezf == 0)
                 length += 9;
         }
-    }else if( srp_mode == 1){
-        if(at == 0)
-            length = 272;
-        else if(at == 1)
-            length = 288;
     }
     length = (length%8==0)?(length/8):(length/8)+1;
-    for(int j= 0; j < length ; j++){
-        frames[id][position_tm_frame[id]] = scp[j];
+    if(position_tm_frame[id]>129){
+        while(position_tm_frame[id]<132){
+            frames[id][position_tm_frame[id]] = 0;
+            position_tm_frame[id]++;
+        }
+    }
+    if(position_tm_frame[id]==position_tm_starting[id]){
+        frames[id][4] = position_tm_starting[id];
+    }
+    for(int j= 0; j < length ;)
+    {        
+        if(position_tm_frame[id]==frames[id][4]){
+            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);
+        }
+        if(position_tm_frame[id]<132){
+            frames[id][position_tm_frame[id]] = scp[j];
+            j++;
             position_tm_frame[id]++;
-            if(position_tm_frame[id] == 132){   //space full in the frame bro
-                pointer = frames[id];
-                space = adjust(1,0,pointer,8);
-                space = adjust(4,1,pointer,space);
-                FSC_science = FSC_CURRENT[1]+1;
-                //gPC.printf("SID = 1, 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;
-                frames[id][5] = (sci_time>>16)&0x3f;
-                frames[id][6] = (sci_time>>8)&0xff;
-                frames[id][7] = (sci_time)&0xff;
-                temp_crc = crc16_gen(frames[id],132);
-                frames[id][132] = temp_crc>>8;
-                frames[id][133] = temp_crc & 0xff;
-                /*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);
-                sd_stat = SD_WRITE(TM_interleave_data,FSC_CURRENT[1]+1,1);
-                if(sd_stat)
-                {
-                    //gPC.puts("sd write failure");
-                    break;
-                }
-                position_tm_frame[id] = position_tm_starting[id];
-                frames[id][4] = (length-1) - j;
-            }
+        }
+        if(position_tm_frame[id] == 132)                                                //space full in the frame bro
+        {
+            pointer = frames[id];
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,1,pointer,space);
+            FSC_science = FSC_CURRENT[1];
+            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;
+            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
+
+            position_tm_frame[id] = position_tm_starting[id];
+            frames[id][4] = ((length-1) - j)+position_tm_starting[id];
+        }
     }
-    gPC.puts("end srp");
-}//srp fucntion end bracket
\ No newline at end of file
+}
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Thu Jul 14 23:05:09 2016 +0000
+++ b/DefinitionsAndGlobals.h	Fri Jul 15 14:59:27 2016 +0000
@@ -39,13 +39,13 @@
     #define SBC 1
     
     #if SBC
-    DigitalOut CDMS_I2C_GPIO(PIN67);
+    DigitalOut CDMS_I2C_GPIO(PIN67,0);
     DigitalIn BAE_I2C_GPIO(PIN1);
     #endif
     
     #if !SBC
     DigitalIn BAE_I2C_GPIO(PIN67);
-    DigitalOut CDMS_I2C_GPIO(PIN39);    
+    DigitalOut CDMS_I2C_GPIO(PIN39,0);    
     #endif
 
 // TC LIST
--- a/TEST_PL.h	Thu Jul 14 23:05:09 2016 +0000
+++ b/TEST_PL.h	Fri Jul 15 14:59:27 2016 +0000
@@ -240,7 +240,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 #define test_schedule_2(test_tm_ptr) {\
@@ -263,7 +263,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 #define test_schedule_3(test_tm_ptr) {\
@@ -286,7 +286,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 #define test_schedule_4(test_tm_ptr) {\
@@ -309,7 +309,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 #define test_schedule_5(test_tm_ptr) {\
@@ -332,7 +332,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 #define test_schedule_6(test_tm_ptr) {\
@@ -355,7 +355,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 #define test_schedule_F(test_tm_ptr) {\
@@ -451,7 +451,7 @@
     {\
         temp = temp->next_TM;\
         delete test_tm_ptr;\
-        test_tm_ptr = temp;\        
+        test_tm_ptr = temp;\
     }\
 }
 void verify_extracted(uint8_t c)
--- a/adf.h	Thu Jul 14 23:05:09 2016 +0000
+++ b/adf.h	Fri Jul 15 14:59:27 2016 +0000
@@ -312,7 +312,7 @@
         if(IRQ || bypass_adf){\
             if(finish_write_data){\
                 if(ADF_off){\
-                    SPI_mutex.lock();\ 
+                    SPI_mutex.lock();\
                     gCS_ADF=0;\
                     spi.write(0xB1);\
                     gCS_ADF=1;\
@@ -324,12 +324,12 @@
                 else{\
                     ADF_off = true;\
                 }\
-            }else{\ 
-                gLEDG = !gLEDG;\  
+            }else{\
+                gLEDG = !gLEDG;\
                 write_data;\
                 if(sent_tmfrom_SDcard)\
                 send_tm_from_SD_card_fun();\
-                else snd_tm.transmit_data(buffer_112,&last_buffer);\ 
+                else snd_tm.transmit_data(buffer_112,&last_buffer);\
             }\
         }\
     }\
--- a/main.cpp	Thu Jul 14 23:05:09 2016 +0000
+++ b/main.cpp	Fri Jul 15 14:59:27 2016 +0000
@@ -55,6 +55,15 @@
     beacon_tc->TC_string[10] = (crc16 & 0x00FF);\
     for(int i = 11; i<135;i++) beacon_tc->TC_string[i] = 0;\
     tm_ptr =  FCTN_CDMS_RLY_TMTC(beacon_tc);\
+    delete PL_TC;\
+    Base_tm *temp;\
+    temp = tm_ptr;\
+    while(tm_ptr!=NULL)\
+    {\
+        temp = temp->next_TM;\
+        delete tm_ptr;\
+        tm_ptr = temp;\
+    }\
 }
 #define OBSRS_TC(tm_ptr){\
     Base_tc *beacon_tc = new Short_tc;\
@@ -85,7 +94,6 @@
                             gPC.puts("exit_adf\r\n");\
 }
 
-
 #define BAE_tc(tm_ptr) {\
     Base_tc *bae_tc = new  Long_tc;\
     bae_tc->next_TC = NULL;\
@@ -100,7 +108,7 @@
     bae_tc->TC_string[5] = 0;\
     bae_tc->TC_string[6] = 0;\
     bae_tc->TC_string[7] = 0;\
-    bae_tc->TC_string[8] = 0;\    
+    bae_tc->TC_string[8] = 0;\
     uint16_t crc16 = crc16_gen(bae_tc->TC_string, 9);\
     bae_tc->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
     bae_tc->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
@@ -114,7 +122,7 @@
     {\
         temp = temp->next_TM;\
         delete tm_ptr;\
-        tm_ptr = temp;\        
+        tm_ptr = temp;\
     }\
 }
 
@@ -178,7 +186,7 @@
     gSCIENCE_THREAD->set_priority(osPriorityAboveNormal);
     
     #if DEBUG
-    gPC.puts("competed allocating threads\r\n");
+    gPC.puts("completed allocating threads\r\n");
     #endif