Repository for CDMS code

Dependencies:   SimpleDMA mbed-rtos mbed eeprom

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Files at this revision

API Documentation at this revision

Comitter:
shreeshas95
Date:
Mon Dec 14 12:04:01 2015 +0000
Parent:
0:f016e9e8d48b
Child:
2:2caf2a9a13aa
Commit message:
Simple working version code

Changed in this revision

COM_MNG_TMTC.h Show annotated file Show diff for this revision Revisions of this file
COM_SND_TM.h Show annotated file Show diff for this revision Revisions of this file
COM_SND_TM_functions.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
SDC_init.h Show annotated file Show diff for this revision Revisions of this file
SDCread.h Show annotated file Show diff for this revision Revisions of this file
ThreadsAndFunctions.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/COM_MNG_TMTC.h	Tue Dec 01 10:56:10 2015 +0000
+++ b/COM_MNG_TMTC.h	Mon Dec 14 12:04:01 2015 +0000
@@ -198,8 +198,9 @@
         }\
         put_crc_l1_ack(l1_ack, gTOTAL_CRC_FAIL_TC);\
     }\
-    /*snd_tm.head_pointer(l1_ack_head);*/\
-    /*adf_not_SDcard();*/\
+    gPC.puts("Sending l1 ack\r\n");\
+    snd_tm.head_pointer(l1_ack_head);\
+    adf_not_SDcard();\
     /*delete the TM*/\
     l1_ack = l1_ack_head;\
     while(l1_ack != NULL){\
@@ -277,8 +278,8 @@
                         /*EXECUTION OF OBOSC TC*/\
                         execute_obosc_core(current_TC, tm_ptr);\
                     }\
-                    /*snd_tm.head_pointer(tm_ptr);*/\
-                    /*adf_not_SDcard();*/\
+                    snd_tm.head_pointer(tm_ptr);\
+                    adf_not_SDcard();\
                     uint8_t temp83 = 0x00;\
                     detect_ack(tm_ptr, temp83);\
                     if( temp83 == 0x01){\
@@ -340,7 +341,7 @@
                             /*call CDMS_RLY_TMTC*/\
                             CDMS_RLY_TMTC(current_TC, tm_ptr);\
                         }\
-                        /*snd_tm.head_pointer(tm_ptr);*/\
+                        snd_tm.head_pointer(tm_ptr);\
                         /*adf_not_SDcard();*/\
                         uint8_t temp83 = 0x00;\
                         detect_ack(tm_ptr, temp83);\
@@ -368,7 +369,7 @@
                     }\
                     else{\
                         /*EXECUTION OF SD-CARD DATA SENDING (OBSRS)*/\
-                        /*read_TC(tc_ptr);*/\
+                        read_TC(current_TC);\
                     }\
                     /*ABORT ON NACK*/\
                     if( (GETexec_status(current_TC) == TC_STATE_EXECUTION_FAILED) && (GETabort_on_nack(current_TC) == 1) ){\
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COM_SND_TM.h	Mon Dec 14 12:04:01 2015 +0000
@@ -0,0 +1,414 @@
+void adf_not_SDcard();
+ 
+#define S_FRAME_SIZE 48
+#define ISR 30
+#define TM_FRAME_SIZE 134           //in bytes
+#define T_FRAME_SIZE 159
+#define EOS_SIZE 120
+#define MAX_ADF_LEN 65535                    
+
+const unsigned char S_frame[] = {0x46,0x9D,0xFB,0xF0,0xD2,0xBA,0x89,0xE4,0x46,0x9D,0xFB,0xF0,0xD2,0xBA,0x89,0xE4,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+const unsigned char EoS[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x91,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x91,0x1A,0x77,0xEF,0xC3,0x4A,0xEA,0x27,0x90,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+const unsigned char FCCH80[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
+const unsigned char SCH40[] = {0x0a,0x3f,0x46,0xb4,0x00};
+
+class SND_TM{
+    
+    Base_tm * head_ptr;   
+    
+//    transmit data variables
+    unsigned char Tframe_c;                                      //_c represents counter, counts Tframe written in a segment
+    unsigned char EOS_c;                                         //count no of bytes of eos have been weitten in the rolling buffer
+    unsigned char Sframe_c;                                      //count no of bytes of Sframe have been weitten in the rolling buffer
+    unsigned char SCH40_c;                                       //count no of bytes of SCH40 have been weitten in the rolling buffer
+    unsigned char data_c;
+    unsigned char FCCH80_c;
+    unsigned char RB_c;                                          //rolling buffer counter
+    unsigned char segment_c;                                    //counts the numver of segments sent
+    unsigned char MAX_SEGMENT;
+    bool Sframe;                                                //Sframe = 1bit of CF1
+    bool SCH40_f;
+    bool FCCH80_f;
+    bool data_f;
+    bool transmit_data_f;
+    bool eos_f;
+    bool junk_f;
+
+    
+//  type 1 frame
+
+    Base_tm *T0F_pointer ;
+    
+    int diff_prev;
+    void differential_encode(unsigned char* ptr, int length){
+        for(int i=0; i<length;i++){
+            unsigned char s = ptr[i] , t;
+            t = s ^ (s >> 1);
+            (diff_prev == 0) ? t=t^0x00 : t=t^0x80 ;
+            diff_prev = int(s & 0x01);
+            ptr[i] = t;
+        }
+    }    
+    
+    int type0_no;                                               //number of type zero frames
+    int type1_no;                                               
+    int total_frames;                                           //number of type one packe (TMP)
+    int SEGMENT_NO;                                             //number of type one packe (TMP)
+    void inline data_number(){
+        int type1_frame_no;
+        Base_tm*head = head_ptr;
+        type0_no = 0;
+        type1_no = 0;
+        while(head != NULL){
+            switch( GETshort_or_long_tm(head->fields) ){
+                case 0:
+                    type0_no++;
+                    break;
+                case 1:
+                    type1_no++;
+                    break;
+            }
+            head=head->next_TM;
+        }
+        type1_frame_no = ( type1_no % 10 == 0 )? (type1_no / 10) : ( (type1_no / 10) + 1);
+        total_frames = type0_no + type1_frame_no ;
+        SEGMENT_NO = (total_frames*2 % (ISR) == 0) ? ( total_frames*2/(ISR) ) : (total_frames*2/(ISR) + 1);            //subtracting 1 since the last SCH40_f is not detected
+    }
+    
+#define next_type_structure(ptr){\
+        unsigned char temp = GETshort_or_long_tm(ptr->fields);\
+        if((temp == 0) && (ptr->next_TM != NULL) ){\
+            do{\
+                ptr = ptr->next_TM;\
+                temp =  GETshort_or_long_tm(ptr->fields);\
+            }\
+            while(temp == 1 && ptr->next_TM != NULL);\
+            if(temp == 1){\
+                ptr = NULL;\
+                break;\
+            }\
+        }\
+        else if((temp == 1) && (ptr->next_TM != NULL)){\
+            do{\
+                ptr = ptr->next_TM;\
+                temp = GETshort_or_long_tm(ptr->fields);\
+            }\
+            while(temp == 0 && ptr->next_TM != NULL);\
+            if(temp == 0){\
+                ptr NULL;\
+                break;\
+            }\
+        }\
+        else{\
+            ptr NULL;\
+        }\
+    }
+    
+    /*
+        brief:      take input type 0  or  1 and return the address of the first node in the list that have that type
+        parameter:  type 0 or 1
+        return:     pointer of the first node having same type      
+    */
+//    Base_tm* first_type_structure(int type){
+//        Base_tm* temp_ptr = head_ptr;
+//        if(type == 0){
+//            while(GETshort_or_long_tm(temp_ptr->fields) == 1){
+//                temp_ptr = temp_ptr->next_TM;
+//                if(temp_ptr == NULL){
+//                    return NULL ;
+//                }       
+//            }   
+//        }
+//        else if(type == 1){
+//            while(GETshort_or_long_tm(temp_ptr->fields) == 0){
+//                temp_ptr = temp_ptr->next_TM;
+//                if(temp_ptr == NULL){
+//                    return NULL;    
+//                }   
+//            }   
+//        }   
+//        return temp_ptr;
+//    }
+
+
+    #define first_type_structure( type,return_ptr) {\
+        Base_tm* temp_ptr = head_ptr;\
+        if(type == 0){\
+            while(GETshort_or_long_tm(temp_ptr->fields) == 1){\
+                temp_ptr = temp_ptr->next_TM;\
+                if(temp_ptr == NULL){\
+                    return_ptr = NULL ;\
+                }\
+            }\
+        }\
+        else if(type == 1){\
+            while(GETshort_or_long_tm(temp_ptr->fields) == 0){\
+                temp_ptr = temp_ptr->next_TM;\
+                if(temp_ptr == NULL){\
+                    return_ptr = NULL;\
+                }\
+            }\
+        }\
+        return_ptr = temp_ptr;\
+    }
+
+
+    
+    Base_tm *T1F_pointer ;
+    int T1F_counter;
+    unsigned char TMframe_type1[TM_FRAME_SIZE];
+    bool type1_frame_flag;                                               //should be true for every new list
+    
+    #define type1_frame(ptr){\
+        int i=4;\
+        if(type1_frame_flag){\
+            first_type_structure(1,T1F_pointer);\
+            T1F_counter = 0;\
+            type1_frame_flag = false;\
+        }\
+        for(i = 4 ; (i < 134) && (T1F_pointer != NULL) ; i++){\
+             TMframe_type1[i] = T1F_pointer->TM_string[T1F_counter];\
+              if(T1F_counter++ == 12){\
+                T1F_counter = 0;\
+                next_type_structure(T1F_pointer);\
+              }\
+        }\
+        TMframe_type1[0] = 1<<7 + ( (i-4)/10 )<<3;\
+        TMframe_type1[3] = crc16_gen(TMframe_type1,3);\
+        if(T1F_pointer == NULL && i>12){\
+            for( ; i < 134 ; i++){\
+                TMframe_type1[i] = TMframe_type1[i-13];\
+            }\
+        }\
+    }
+    
+    
+//    unsigned char * type1_frame(){
+//        int i=4;
+//        if(type1_frame_flag){
+//            T1F_pointer = first_type_structure(1);
+//            T1F_counter = 0;
+//            type1_frame_flag = false;
+//        }
+//        for(i = 4 ; (i < 134) && (T1F_pointer != NULL) ; i++){
+//             TMframe_type1[i] = T1F_pointer->TM_string[T1F_counter];
+//              if(T1F_counter++ == 12){
+//                T1F_counter = 0;
+//                T1F_pointer = next_type_structure(T1F_pointer);
+//              }
+//        }
+//        // header
+//        TMframe_type1[0] = 1<<7 + ( (i-4)/10 )<<3;                          //( (i-4)/10 ) gives number of packets in the frame
+//            //insert time ;
+//        TMframe_type1[3] = CRC::crc16_gen(TMframe_type1,3);
+//        //end header
+//        if(T1F_pointer == NULL && i>12){
+//            for( ; i < 134 ; i++){                                          //repeating ;ast packet to fill up the extra space
+//                TMframe_type1[i] = TMframe_type1[i-13]; 
+//            }
+//        }
+//        return TMframe_type1;
+//    }
+    
+    bool type0_frame_flag;
+    
+    
+ #define type0_frame(ptr){\
+        if(type0_frame_flag){\
+            first_type_structure(0,T0F_pointer);\
+            type0_frame_flag = false;\
+        }\
+        else {\
+            next_type_structure(T0F_pointer);\
+        }\
+        ptr =  T0F_pointer->TM_string;\
+    }
+    
+    
+//    unsigned char* type0_frame(){
+//        if(type0_frame_flag){
+//            T0F_pointer = first_type_structure(0);  
+//            type0_frame_flag = false;
+//        }            
+//        else {
+//            T0F_pointer = next_type_structure(T0F_pointer);
+//        }
+//        return T0F_pointer->TM_string;
+//    }
+     
+    unsigned char convoluted_frame[270];                    
+    Convolution ConvObj2;
+    void convolution (unsigned char * ptr){
+        ConvObj2.convolutionEncode(ptr, convoluted_frame);
+        ConvObj2.convolutionEncode(ptr + 67, convoluted_frame + 135);
+    }
+        
+        
+    unsigned char interleave_data[2][144];                 
+    unsigned char DS_index;
+    bool DS_state;
+    int DS_counter;
+    bool DS_f;
+    bool make_DataStream_f;
+//    #define make_DataStream {\
+//        unsigned char* ptr;\
+//        if(make_DataStream_f == true){\
+//            DS_state = true;\
+//            DS_counter = 0;\
+//            DS_f = false;\
+//            make_DataStream_f = false;\
+//        }\
+//        if(DS_state){\
+//            if(DS_counter < total_frames-type0_no){\
+//                type1_frame(ptr); \
+//                DS_f = true;\
+//            }\
+//            else if(DS_counter < total_frames ){\
+//                type0_frame(ptr);\
+//                DS_f = true;\
+//            }\
+//            DS_counter++;\
+//        }\
+//        DS_state = !DS_state;\
+//        if (DS_f){\
+//            DS_f = false;\
+//            convolution(ptr);\
+//            interleave(convoluted_frame,interleave_data[0]);\
+//            interleave(convoluted_frame+ 135,interleave_data[1]);\
+//        }\
+//        DS_index = (DS_state==true)?1:0;\
+//    }
+    
+    public:
+        
+        bool reset_adf;
+        SND_TM(){
+            MAX_SEGMENT = (MAX_ADF_LEN-T_FRAME_SIZE)/( S_FRAME_SIZE + ISR*T_FRAME_SIZE);                //(max length - eos size)/(sframe length + tFrameLength*ISR)
+//            cout<<int(MAX_SEGMENT)<<endl;
+        }
+        
+ 
+        
+        void head_pointer(Base_tm* ptr){
+            head_ptr = ptr ;
+            type1_frame_flag = true;
+            type0_frame_flag = true;
+            make_DataStream_f = true;
+            transmit_data_f = true;
+            diff_prev = 0;
+            data_number();
+//            make_DataStream;
+        }
+        
+         
+        void inline transmit_data(unsigned char * transmit , bool * last_buffer){
+            if(transmit_data_f){
+                RB_c = 0;
+                Sframe_c = 0;
+                SCH40_c = 0;
+                FCCH80_c = 0;
+                Tframe_c = 0;
+                EOS_c = 0;
+                segment_c = 0;
+                data_c = 0;
+                
+                Sframe = true;
+                SCH40_f = false;
+                data_f = false;
+                FCCH80_f = false;
+                transmit_data_f = false;
+                reset_adf = false;
+                *last_buffer = false;
+            }
+            
+            for(RB_c = 0 ; RB_c<112 ; RB_c++){
+                
+                if(junk_f){
+                    transmit[RB_c] = 0xaa;
+                }
+                
+                if(Sframe){
+                    transmit[RB_c] = S_frame[Sframe_c++];
+                    if(Sframe_c == 48){
+ //                     printf("exit s frame\n");
+                        Sframe_c = 0;
+                        Sframe = false;
+                        SCH40_f =true;
+                    }
+                    continue;
+                }
+//                
+                if(SCH40_f){
+                    transmit[RB_c] = SCH40[SCH40_c++];      
+                    if(SCH40_c == 5 ){
+                        SCH40_c = 0;
+                        SCH40_f = false; 
+                        data_f = true; 
+//                      printf("exit SCH40 frame\n");
+                    }
+                    continue;
+                }
+//                    
+                if(data_f){
+                    transmit[RB_c] = interleave_data[DS_index][data_c++];
+                    if(data_c == 144){
+//                        make_DataStream;
+                        data_c = 0;
+                        FCCH80_f = true;
+                        data_f = false;
+//                        printf("exit Data\n");
+                    }
+                    continue;
+                }
+
+                if(FCCH80_f){
+                    transmit[RB_c] = FCCH80[FCCH80_c++];
+                    if(FCCH80_c == 10){
+                        FCCH80_c = 0;
+                        FCCH80_f = false;
+//                        printf("exit FCCH80\n");
+                        if(++Tframe_c != ISR)
+                            SCH40_f =  true;
+                        else{
+                            Tframe_c = 0;
+                            segment_c++;
+                            if(segment_c == SEGMENT_NO||segment_c == MAX_SEGMENT)
+                                {eos_f = true;}
+                            else{
+                                Sframe = true;
+                            }
+                        }
+                    }
+                    continue;
+                }       
+                
+                if(eos_f){    
+                    transmit[RB_c] = EoS[EOS_c++];
+                    if(EOS_c == EOS_SIZE){
+                        EOS_c = 0;
+                        eos_f = false;
+ //                     printf("exit EOS\n");
+                        if(segment_c == MAX_SEGMENT){
+                            segment_c = 0;
+                            SEGMENT_NO = SEGMENT_NO - MAX_SEGMENT;
+                            reset_adf = true;
+                            Sframe = true;
+                        }else{
+                            *last_buffer = true;
+                        }
+                        junk_f = true;
+                    }
+                    continue;
+                }                   
+            }
+        
+            differential_encode(transmit,112);
+            if(reset_adf == true){
+                diff_prev = 0;
+                junk_f = false;
+            }
+                
+        }
+        
+};
+SND_TM snd_tm;
\ No newline at end of file
--- a/COM_SND_TM_functions.h	Tue Dec 01 10:56:10 2015 +0000
+++ b/COM_SND_TM_functions.h	Mon Dec 14 12:04:01 2015 +0000
@@ -1,1021 +1,4 @@
-// ********************************** COMPRESSION **********************************
-int disk_write(const uint8_t *, uint64_t);
-uint64_t  RTC_TIME;                                    //need to  be changed to uint_64
-unsigned char SDcard_lastWritten[512]  = {0};
- 
-namespace Science_TMframe {
-    
-    #define OUTLENGTH 360                                           //length of the output frame after convolution
-    #define SDcard_block 512                                        //block size of the sd card
-    
-    Convolution ConvObj;                                            //object which stores the frame after convolution
-    bool fresh[3] = {true,true,true};                               // True only for the first time                                                                 
-    unsigned char frames[3][134] = {0};                             // "frame" stores the address of the current frame...."first_frame_address" stores the address of the first node made.
-    unsigned int FCN[4] = {0};                                      //frame count number
-    unsigned int data_starting_point[3] = {8,5,10};             
-    unsigned int max_data[3] = {124,127,122};                       //number of bytes in each frame excluding TMID,FCN,first_header_point,crc
-    unsigned char TM_convoluted_data[270] = {0};                    //270 bytes is the size after convolution of 1072 bits
-    unsigned char complete_frame[SDcard_block] = {0};
-    uint64_t  SDC_address = 200;
-    bool SCH_FCCH_FLAG = true;
-    
-    
-    void add_SCH_FCCH(){
-        int i = 0;
-        complete_frame[0] = 0x0a;
-        complete_frame[1] = 0x3f;;
-        complete_frame[2] = 0x46;
-        complete_frame[3] = 0xb4;
-        complete_frame[4] = 0x00;
-        
-        for(i = 149 ; i < 159 ; i ++){
-            complete_frame[i] = 0 ; 
-        }
-        
-        complete_frame[159] = 0x0a;
-        complete_frame[160] = 0x3f;;
-        complete_frame[161] = 0x46;
-        complete_frame[162] = 0xb4;
-        complete_frame[163] = 0x00;
-        
-        for(i = 308 ; i < 318 ; i ++){
-            complete_frame[i] = 0 ; 
-        }
-        
-    }
-    
-    void making_frameHeader(unsigned char  TMID){
-    
-        unsigned char frame_type_identifier = 0;                                                        // not conform about the values , yet to be done    
-        frames[TMID][0] = (frame_type_identifier<<7) + ( (TMID + 1)<<3 ) + ( (FCN[TMID]>>24) & 0x7 );   //frame number should be less than 2^23 since 23 bits are assigned for that
-        frames[TMID][1] = ((FCN[TMID]>>16) & 0xff );
-        frames[TMID][2] = ( (FCN[TMID]>>8 )& 0xff );
-        frames[TMID][3] = ( FCN[TMID] & 0xff      );                                                        // first bit for (frame identifier), next 4 for (TMID) and next 27 for FCN
-        
-        if(TMID == 0){
-            frames[TMID][5] =( (RTC_TIME>>29) & 0xff );
-            frames[TMID][6] =( (RTC_TIME>>21) & 0xff ); 
-            frames[TMID][7] =( (RTC_TIME>>13) & 0xff );
-            
-        }else if(TMID == 2){
-            frames[TMID][5] =( (RTC_TIME>>32) & 0xff );
-            frames[TMID][6] =( (RTC_TIME>>24) & 0xff ); 
-            frames[TMID][7] =( (RTC_TIME>>16) & 0xff );
-            frames[TMID][8] =( (RTC_TIME>>8 ) & 0xff );
-            frames[TMID][9] =( (RTC_TIME    ) & 0xff );
-        }
-        
-    }
- 
-    void convolution (unsigned char * ptr){
-    
-        ConvObj.convolutionEncode(ptr , TM_convoluted_data);
-        ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
- 
-    }
- 
-    /*
-        @brief : take the address of array of LCR or HCR and stores it into a frame
-        @parameters: type->L or H , deprnding on wheather it is LCR or HCR respectively
-        @return: nothing
-    */
- 
-//  type 2 yet to be done
-    void making_frame(unsigned char TMID ,unsigned char type, unsigned char* pointer){
-        
-        TMID--;                                          //TMID goes from 1 to 3 , convinient to ue from 0 to 2
-        static int frame_space_number[3] = {0};          //this variable represents the register number of the frame in which LCR or HCR data to be written not including header
-        int  packet_len = 0;
-        int copy_count = 0 ;
-        
-        switch(int(TMID)){
-            case 0:                                     //SCP
-                if(type == 'L'){                        //below threshold
-                    packet_len = 22;   
-                }
-                else if(type == 'H'){                   //above threshold
-                    packet_len = 26;
-                }
-                break;
-            
-            case 1:                                     //SFP above threshold
-                packet_len = 35;
-                break;
-        
-            case 2:                                     //SFP  below threshold
-                packet_len = 23;
-                break;  
-        }
-        
-        if(SCH_FCCH_FLAG){
-            add_SCH_FCCH();
-            SCH_FCCH_FLAG = false;
-        }
- 
-        if(fresh[TMID]){
-            //welcome to first frame
-            making_frameHeader(TMID);
-            frames[TMID][4] = 0;
-            fresh[TMID] = false;
-        }
-    
-    
-        while(copy_count < packet_len){                                                                  // 22 bytes is the size of the LCR
-            frames[TMID][ frame_space_number[TMID] + data_starting_point[TMID] ]= *(pointer + copy_count);
-            frame_space_number[TMID]++;                                          
-            copy_count++;
-            if( frame_space_number[TMID] == max_data[TMID] ){                                //frame space number can go from 0 to 126 as data is written from 0+5 to 126+5
-                FCN[TMID]++;
-                // convolution and save frame in the sd card
-                
-                // copying crc in 132 and 133
-                int temp_crc;
-                temp_crc = CRC::crc16_gen(frames[TMID],132);
-                frames[TMID][132] = temp_crc>>8;
-                frames[TMID][133] = temp_crc & 0xff;                
-                // xor 
-                for(int j = 0 ; j < 134 ; j++){
- //                   frames[TMID][j] = frames[TMID][j]^exorThisWithTMFrame[j];
-                }
-                //convolution and interleaving  
-                convolution(frames[TMID]);
-                interleave(TM_convoluted_data , complete_frame + 5);
-                interleave(TM_convoluted_data+ 135,complete_frame + 164);
-                
-                // writing the SDC_address in a buffer , to store it in SDcard at address 5
-                SDcard_lastWritten[0] = SDC_address>>56;
-                SDcard_lastWritten[1] = (SDC_address>>48)&0xFF;
-                SDcard_lastWritten[2] = (SDC_address>>40)&0xFF;
-                SDcard_lastWritten[3] = (SDC_address>>32)&0xFF;
-                SDcard_lastWritten[4] = (SDC_address>>24)&0xFF;
-                SDcard_lastWritten[5] = (SDC_address>>16)&0xFF;
-                SDcard_lastWritten[6] = (SDC_address>>8)&0xFF;
-                SDcard_lastWritten[7] = (SDC_address)&0xFF;
-                
-                SPI_mutex.lock();
-                disk_write(complete_frame , SDC_address);
-                SPI_mutex.unlock();
-                SDC_address++;
-               
-            
-                
-                //now save to the sd card TM_convoluted_data
-//              std::bitset<8> b;
-//              printf("\nthis is frame %d\n",TMID);                                        //for printing frame 
-//                  for(int j =0; j<134;j++){
-//                  printf(" %d",frames[TMID][j]);
-////                    b =  frames[TMID][j];
-////                    cout<<b;
-//                  }
-                
-                frame_space_number[TMID] = 0;
-                making_frameHeader(TMID);
-                frames[TMID][4]=packet_len - copy_count;
-                //write time  here also
-                continue;
-            }       
-        }
-    
-//      printf("\nthis is frame %d\n",TMID);                                              //for printing frame 
-//      for(int j =0; j<134;j++){
-//          printf(" %d",frames[TMID][j]);
-//      }
- 
-    }   
- 
- 
-}
- 
- 
- 
-// *************************************************** COMPRESSION *************************************************** 
- 
-namespace Science_Data_Compression{
- 
-    # define PACKET_SEQUENCE_COUNT 1                         //1 byte
-    # define NUM_PROTON_BIN 17                               //2 byte each
-    # define NUM_ELECTRON_BIN 14                             //2 byte each 
-    # define VETO 1                                          //2 byte
-    # define FASTCHAIN 2                                     //4 byte each
-    #define RAW_PACKET_LENGTH 73                             //73 bytes
- 
-//  #define PACKET_SEQ_COUNT 1
-//  #define PROTON_BIN_SIZE 2
-//  #define ELECTRON_BIN_SIZE 2
-//  #define VETO 2
-//  #define FAST_CHAIN 4
-    
-    
-    /*
-    @brief:     read one uint16_t equivalent of first two chars from the stream. short int because 16 bits
-    @param:     pointer to the start of the short int
-    @return:    uint16_t 
-    */
-    
-    
-    unsigned int read_2byte(unsigned char* ptr){
-        unsigned int output = (unsigned int) *(ptr+1);
-        output += ( (unsigned int)(*ptr) ) << 8;
-        return output;
-    }
-    
-    /*
-    @brief:     read one int equivalent of first four chars from the stream.  int because 32 bits
-    @param:     pointer to the start of the short int
-    @return:    unsigned int  
-    */
- 
-    unsigned int read_4byte(unsigned char* ptr){
-        unsigned int output = (unsigned int) *(ptr+3);
-        output += (unsigned int)*(ptr+2)<<8;
-        output += (unsigned int)*(ptr+1)<<16;
-        output += (unsigned int)*(ptr)<<24;
-        return output;
-    }
- 
-    unsigned int SFP_thresholds[35]={0  ,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};//threashold values 
-    unsigned int SCP_thresholds[35]={0  ,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,0  ,0  ,0  };
-    unsigned int SFP_bin[35];
-    unsigned int SCP_bin[35]={0};
-    unsigned char SFP_outputBT[23];                         //BT = below threshold
-    unsigned char SFP_outputAT[35];
-    unsigned char SCP_outputLCR[22];
-    unsigned char SCP_outputHCR[26];
- 
-    
-//**********************************************************************************************************************************************************************
-    //lots of compression functions are listed below
- 
-    
-    unsigned char SFP_compress4_BT(unsigned int input){                                 //for veto
-        int de_4 = 0;
-        unsigned char output;
-        
-        if(input<= 3){
-        //  DE = 0;
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 12){
-        //  DE = 01;
-            output = 0x1;
-            de_4 = 2;   
-        }
-        else if(input <= 48){
-        //  DE = 10
-            output = 0x2;
-            de_4 = 4;   
-        }
-        else {
-        //  DE = 11
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 2;
-        return output;
-    }
-    
-    
-    unsigned char SFP_compress5_BT(unsigned int input){
-        int de_4 = 0;                                   //number by which bin value need to be shifted
-        unsigned char output;
-        
-        if(input <= 15){
-//            D = 0 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 60){
-//            D = 1
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 1;
-        
-        return output;      
-    };
-    
-    
-    unsigned char SFP_compress6_BT(unsigned int input){
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 31){
-//          E = 0 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 124){
-//            E = 1
-            output = 0x1;
-            de_4 = 2;
-        }
-         
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 1;
-        
-        return output;
-    };
-    
-    unsigned char SFP_compress7_AT(unsigned int input){
-        int de_4  = 0;
-        unsigned char output;
-        
-        if(input <= 31){
-//            DE = 00 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 124){
-//            DE = 01 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 496){
-//            DE = 10 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else if(input <= 1984){
-//           DE = 11 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 2;
-        
-        return output;
-        
-    };
-    
-    
-    unsigned char SFP_compress8_AT(unsigned int input){
-        
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 63){
-//            DE = 00 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 252){
-//            DE = 01 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 1008){
-//            DE = 10 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else {
-//           DE = 11 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 2;
-        
-        return output;
-    };
-    
-    unsigned char SFP_compress5_AT(unsigned int input){
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 3){
-//            DE = 000 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 12){
-//            DE = 001 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 48){
-//            DE = 010 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else if(input <= 192) {
-//           DE = 011 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        else if(input <= 768) {
-//           DE = 100 [4]
-            output = 0x4;
-            de_4 = 8;
-        }
-        
-        else if(input <= 3072) {
-//           DE = 101 [5]
-            output = 0x5;
-            de_4 = 10;
-        }
-        
-        else if(input <= 12288) {
-//           DE = 110 [6]
-            output = 0x6;
-            de_4 = 12;
-        }
-        
-        else  {
-//          DE = 111 [7]
-            output = 0x7;
-            de_4 = 14;
-        }
-           
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 3;
-        
-        return output;
-    }
-    
-    unsigned char SFP_compress7FC_AT(unsigned int input){                   // for fast chain above threshold
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 15){
-//            DE = 000 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        else if(input <= 60){
-//            DE = 001 [1]
-            output = 0x1;
-            de_4 = 2;
-        }
-        
-        else if(input <= 240){
-//            DE = 010 [2]
-            output = 0x2;
-            de_4 = 4;
-        }
-        
-        else if(input <= 960) {
-//           DE = 011 [3]
-            output = 0x3;
-            de_4 = 6;
-        }
-        
-        else if(input <= 3840) {
-//           DE = 100 [4]
-            output = 0x4;
-            de_4 = 8;
-        }
-        
-        else if(input <= 15360) {
-//           DE = 101 [5]
-            output = 0x5;
-            de_4 = 10;
-        }
-        
-        else if(input <= 61440) {
-//           DE = 110 [6]
-            output = 0x6;
-            de_4 = 12;
-        }
-        
-        else  {
-//          DE = 111 [7]
-            output = 0x7;
-            de_4 = 14;
-        }
-           
-        unsigned short int temp = input >> de_4;
-        output += (temp ) << 3;
-        
-        return output;
-    }
-    
-    
-    
-    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-    unsigned char SCP_compress6(unsigned int input){
-        int ef_4;
-        unsigned char output;
-        
-        if(input <= 15){
-//      EF = 00
-        output = 0x0;
-        ef_4 = 0;
-        }
-        
-        else if(input <= 60 ){
-//          EF = 01 [1]
-            output = 0x01;
-            ef_4 = 2;
-        }
-        else if(input <= 240){
-//          EF = 10 [2]
-            output = 0x02;
-            ef_4 = 4;
-        }
-        else{
-//          EF = 11 [3]
-            output = 0x03;
-            ef_4 = 6;
-        }
-            
-            unsigned short int temp = input >> ef_4;
-            output += (temp & 0xf) << 2;
-            
-            return output;
-    }
-    
-    unsigned char SCP_compress5(unsigned int input){
-        
-        int de_4 = 0;;
-        unsigned char output;
-        
-        if(input <= 7){
-//            DE = 00 [0]
-            output = 0x0;
-            de_4 = 0;
-        }
-        
-        else if(input <= 28){
-//            DE = 01 [1]
-            output = 0x01;
-            de_4 = 2;
-        }
-        else if(input <= 112){
-//            DE = 10 [2]
-            output = 0x02;
-            de_4 = 4;
-        }
-        else{
-//        DE = 11 [3]
-            output = 0x03;
-            de_4 = 6;
-        }
-        
-        unsigned short int temp = input >> de_4;
-        output += (temp & 0x7) << 2;
-        
-        return output;
-    }
-    
-        unsigned char SCP_compress6h(unsigned int input) {
-    
-        int ef_4;
-        unsigned char output;
-        
-        if(input <=7){
-//          EF = 000 [0]
-            output = 0x00;
-            ef_4 = 0;
-        }
-        
-        else if(input <=28){
-//          EF = 001 [1]
-            output = 0x01;
-            ef_4 = 2;
-        }
-        else if(input <= 112){
-        
-//          EF = 010 [2]
-            output = 0x02;
-            ef_4 = 4;
-        }
-        else if(input <= 448){
-//          EF = 011 [3]
-            output = 0x03;
-            ef_4 = 6;
-        }
-        else if(input <= 1792){
-//          EF = 100 [4]
-            output = 0x04;
-            ef_4 = 8;
-            
-        }
-        else if(input <= 7168){
-//          EF = 101 [5]
-            output = 0x05;
-            ef_4 = 10;
-            
-        }
-        else if(input <= 28672){
-//          EF = 110 [6]
-            output = 0x06;
-            ef_4 = 12;
-        }
-        else{
-//          EF = 111 [7]
-            output = 0x07;
-            ef_4 =14;
-        }
-        
-        unsigned short int temp = input >> ef_4;
-        output += (temp & 0x7) << 3;
-        
-        return output;
-        
-    }
-    
-    
-    unsigned char SCP_compress7h(unsigned int input) {
-    
-        int fg_4;
-        unsigned char output;
-        
-        if(input <= 15){
-//          EF = 000 [0]
-            output = 0x0;
-            fg_4 = 0;
-        }
-        
-        else if(input <= 60){
-//          EF = 001 [1]
-            output = 0x01;
-            fg_4 = 2;
-        }
-        else if(input <= 240){
-        
-//          EF = 010 [2]                    
-            output = 0x02;
-            fg_4 = 4;
-        }
-        else if(input <= 960){
-//          EF = 011 [3]
-            output = 0x03;
-            fg_4 = 6;
-        }
-        else if(input <= 3840){
-//          EF = 100 [4]
-            output = 0x04;
-            fg_4 = 8;
-            
-        }
-        else if(input <= 15360){
-//          EF = 101 [5]
-            output = 0x05;
-            fg_4 = 10;
-            
-        }
-        else if(input <= 61440){
-//          EF = 110 [6]
-            output = 0x06;
-            fg_4 = 12;
-        }
-        else{
-//          EF = 111 [7]
-            output = 0x07;
-            fg_4 =14;
-        }
-        
-        unsigned short int temp = input >> fg_4;
-        output += (temp & 0xf) << 3;
-        
-        return output;
-        
-    }
-    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-    void SCP_compress_data();                   
-    void SFP_compress_data(unsigned char* input){
- 
-            bool LCR = true;
-            int i = 0;
-            static int packet_no = 0;                       //takes value from 0 to 29
-            //TRAVERSE THE LIST TO DETERMINE LCR OR HCR and stroing the values in proton_bin and electron bin
-            SFP_bin[0] = *input;
-            for(i=1 ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){          //storing the bin values into an array name bin
-                SFP_bin[i]=read_2byte(input+1+((i-1)<<1));                             //proton bin and elecron bin are 2 byte, hence read_2byte and 
-                SCP_bin[i]+=SFP_bin[i];
-                if(SFP_bin[i] > SFP_thresholds[i]){                                         //fast cahin is 4 byte hence read_4byte
-                    LCR = false;                                                    // if a single value is above threshold then lcr becomes false
-                    i++;
-                    break;
-                }
-            }
-            
-            for( ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){
-                SCP_bin[i]+=SFP_bin[i];
-                SFP_bin[i] = read_2byte(input + 1 + ( (i-1)<<1) );
-            }
-            
-            SFP_bin[i] = read_4byte(input+1+ ((i-1)<<1))    ;
-            SCP_bin[i]+=SFP_bin[i];
-            
-            if(SFP_bin[i]>SFP_thresholds[i])
-                LCR = false;                                                    //since veto starts from location (input + 65) and (input + 69)
-                    
-            SFP_bin[i+1] = read_4byte(input+69);
-            SCP_bin[i]+=SFP_bin[i];
-            
-            if(SFP_bin[i]>SFP_thresholds[i])
-                LCR = false;
-            
-            
-//          printf("\n");                       //for printing the sfp bin
-//          for (i=0;i<35;i++){
-//              printf("sfp[%d] = %d",i,SFP_bin[i]);
-//          }
-//          printf("\n");
- 
-            if(LCR){   
-                
-                 SFP_outputBT[0]  =  (packet_no<<3) + ( SFP_compress5_BT(SFP_bin[1])>>2 );
-                 SFP_outputBT[1]  =  ( SFP_compress5_BT(SFP_bin[1])<<6 ) + ( SFP_compress5_BT(SFP_bin[2])<<1 ) +   ( SFP_compress5_BT(SFP_bin[3])>>4 );
-                 SFP_outputBT[2]  =  ( SFP_compress5_BT(SFP_bin[3])<<4 ) + ( SFP_compress5_BT(SFP_bin[4])>>1 );
-                 SFP_outputBT[3]  =  ( SFP_compress5_BT(SFP_bin[4])<<7 ) + ( SFP_compress5_BT(SFP_bin[5])<<2 ) + ( SFP_compress5_BT(SFP_bin[6])>>3 );            
-                 SFP_outputBT[4]  =  ( SFP_compress5_BT(SFP_bin[6])<<5 ) + ( SFP_compress5_BT(SFP_bin[7]) );
-                 SFP_outputBT[5]  =  ( SFP_compress5_BT(SFP_bin[8])<<3 ) + ( SFP_compress5_BT(SFP_bin[9])>>2 );
-                 SFP_outputBT[6]  =  ( SFP_compress5_BT(SFP_bin[9])<<6 ) + ( SFP_compress5_BT(SFP_bin[10])<<1 ) +  ( SFP_compress5_BT(SFP_bin[11])>>4 );
-                 SFP_outputBT[7]  =  ( SFP_compress5_BT(SFP_bin[11])<<4 ) + ( SFP_compress5_BT(SFP_bin[12])>>1 );            
-                 SFP_outputBT[8]  =  ( SFP_compress5_BT(SFP_bin[12])<<7 ) + ( SFP_compress5_BT(SFP_bin[13])<<2 ) + ( SFP_compress5_BT(SFP_bin[14])>>3 );
-                 SFP_outputBT[9]  =  ( SFP_compress5_BT(SFP_bin[14])<<5 ) + ( SFP_compress5_BT(SFP_bin[15]) );
-                 SFP_outputBT[10] =  ( SFP_compress5_BT(SFP_bin[16])<<3 ) + ( SFP_compress5_BT(SFP_bin[17])>>2 );
-                 SFP_outputBT[11] =  ( SFP_compress5_BT(SFP_bin[17])<<6 ) + ( SFP_compress6_BT(SFP_bin[18]) );
-                 SFP_outputBT[12] =  ( SFP_compress6_BT(SFP_bin[19])<<2 ) + ( SFP_compress6_BT(SFP_bin[20])>>4 );
-                 SFP_outputBT[13] =  ( SFP_compress6_BT(SFP_bin[20])<<4 ) + ( SFP_compress5_BT(SFP_bin[21])>>1 );        
-                 SFP_outputBT[14] =  ( SFP_compress5_BT(SFP_bin[21])<<7 ) + ( SFP_compress5_BT(SFP_bin[22])<<2 )  + ( SFP_compress5_BT(SFP_bin[23])>>3 );
-                 SFP_outputBT[15] =  ( SFP_compress5_BT(SFP_bin[23])<<5 ) + ( SFP_compress5_BT(SFP_bin[24]) );
-                 SFP_outputBT[16] =  ( SFP_compress5_BT(SFP_bin[25])<<3 ) + ( SFP_compress5_BT(SFP_bin[26])>>2 );
-                 SFP_outputBT[17] =  ( SFP_compress5_BT(SFP_bin[26])<<6 ) + ( SFP_compress5_BT(SFP_bin[27])<<1 ) + ( SFP_compress5_BT(SFP_bin[28])>>4);
-                 SFP_outputBT[18] =  ( SFP_compress5_BT(SFP_bin[28])<<4 ) + ( SFP_compress5_BT(SFP_bin[29])>>1) ;
-                 SFP_outputBT[19] =  ( SFP_compress5_BT(SFP_bin[29])<<7 ) + ( SFP_compress5_BT(SFP_bin[30])<<2 ) + ( SFP_compress5_BT(SFP_bin[31])>>3)  ; 
-                 SFP_outputBT[20] =  ( SFP_compress5_BT(SFP_bin[31])<<5 ) + ( SFP_compress4_BT(SFP_bin[32])<<1)  + ( SCP_compress5(SFP_bin[33])>>4 ) ; //
-                 SFP_outputBT[21] =  ( SCP_compress5(SFP_bin[33])<<4 ) + ( SCP_compress5(SFP_bin[34])>>1 );        //here intentionally SCP_compress is used instead of SCP_compress5 is different than SFP_compress5
-                 SFP_outputBT[22] =  ( SCP_compress5(SFP_bin[34])<<7 );                                             //7 bits are spare
-                 
-                 Science_TMframe::making_frame(3,'L',SFP_outputBT);
-                 if(++packet_no == 30){
-                    packet_no=0;
-                    SCP_compress_data();
-                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
-                        if(packet_no==0){
-                            SCP_bin[i]=0;
-                        }
-                    }
-                 }
-                 
-                 
-            }
- 
-            else {
-                
-                
-                SFP_outputAT[0]  = (RTC_TIME>>27)&(0xff);
-                SFP_outputAT[1]  = (RTC_TIME>>19)&(0xff);
-                SFP_outputAT[2]  = (RTC_TIME>>11)&(0xff);
-                SFP_outputAT[3]  = (RTC_TIME>>3 )&(0xff);
-                SFP_outputAT[4]  = (RTC_TIME<<5 )&(0xff) + (packet_no);
-                SFP_outputAT[5]  = ( SFP_compress7_AT(SFP_bin[1])<<1 ) + ( SFP_compress7_AT(SFP_bin[2])>>6 );
-                SFP_outputAT[6]  = ( SFP_compress7_AT(SFP_bin[2])<<2 ) + ( SFP_compress7_AT(SFP_bin[3])>>5 );
-                SFP_outputAT[7]  = ( SFP_compress7_AT(SFP_bin[3])<<3 ) + ( SFP_compress7_AT(SFP_bin[4])>>4 );
-                SFP_outputAT[8]  = ( SFP_compress7_AT(SFP_bin[4])<<4 ) + ( SFP_compress7_AT(SFP_bin[5])>>3 );
-                SFP_outputAT[9]  = ( SFP_compress7_AT(SFP_bin[5])<<5 ) + ( SFP_compress7_AT(SFP_bin[6])>>2 );
-                SFP_outputAT[10] = ( SFP_compress7_AT(SFP_bin[6])<<6 ) + ( SFP_compress7_AT(SFP_bin[7])>>1 );
-                SFP_outputAT[11] = ( SFP_compress7_AT(SFP_bin[7])<<7 ) + ( SFP_compress7_AT(SFP_bin[8]) );
-                SFP_outputAT[12] = ( SFP_compress7_AT(SFP_bin[9])<<1 ) + ( SFP_compress7_AT(SFP_bin[10])>>6);
-                SFP_outputAT[13] = ( SFP_compress7_AT(SFP_bin[10])<<2 ) + ( SFP_compress7_AT(SFP_bin[11])>>5);
-                SFP_outputAT[14] = ( SFP_compress7_AT(SFP_bin[11])<<3 ) + ( SFP_compress7_AT(SFP_bin[12])>>4);
-                SFP_outputAT[15] = ( SFP_compress7_AT(SFP_bin[12])<<4 ) + ( SFP_compress7_AT(SFP_bin[13])>>3);
-                SFP_outputAT[16] = ( SFP_compress7_AT(SFP_bin[13])<<5 ) + ( SFP_compress7_AT(SFP_bin[14])>>2);
-                SFP_outputAT[17] = ( SFP_compress7_AT(SFP_bin[14])<<6 ) + ( SFP_compress7_AT(SFP_bin[15])>>1);
-                SFP_outputAT[18] = ( SFP_compress7_AT(SFP_bin[15])<<7 ) + ( SFP_compress7_AT(SFP_bin[16]));
-                SFP_outputAT[19] = ( SFP_compress7_AT(SFP_bin[17])<<1 ) + ( SFP_compress8_AT(SFP_bin[18])>>7 );
-                SFP_outputAT[20] = ( SFP_compress8_AT(SFP_bin[18])<<1 ) + ( SFP_compress8_AT(SFP_bin[19])>>7 );
-                SFP_outputAT[21] = ( SFP_compress8_AT(SFP_bin[19])<<1 ) + ( SFP_compress8_AT(SFP_bin[20])>>7 );
-                SFP_outputAT[22] = ( SFP_compress8_AT(SFP_bin[20])<<1 ) + ( SFP_compress7_AT(SFP_bin[21])>>6 );
-                SFP_outputAT[23] = ( SFP_compress7_AT(SFP_bin[21])<<2 ) + ( SFP_compress7_AT(SFP_bin[22])>>5 );
-                SFP_outputAT[24] = ( SFP_compress7_AT(SFP_bin[22])<<3 ) + ( SFP_compress7_AT(SFP_bin[23])>>4 );
-                SFP_outputAT[25] = ( SFP_compress7_AT(SFP_bin[23])<<4 ) + ( SFP_compress7_AT(SFP_bin[24])>>3 );
-                SFP_outputAT[26] = ( SFP_compress7_AT(SFP_bin[24])<<5 ) + ( SFP_compress7_AT(SFP_bin[25])>>2 );
-                SFP_outputAT[27] = ( SFP_compress7_AT(SFP_bin[25])<<6 ) + ( SFP_compress7_AT(SFP_bin[26])>>1 );
-                SFP_outputAT[28] = ( SFP_compress7_AT(SFP_bin[26])<<7 ) + ( SFP_compress7_AT(SFP_bin[27]) );
-                SFP_outputAT[29] = ( SFP_compress7_AT(SFP_bin[28])<<1 ) + ( SFP_compress7_AT(SFP_bin[29])>>6);
-                SFP_outputAT[30] = ( SFP_compress7_AT(SFP_bin[29])<<2 ) + ( SFP_compress7_AT(SFP_bin[30])>>5);
-                SFP_outputAT[31] = ( SFP_compress7_AT(SFP_bin[30])<<3 ) +( SFP_compress7_AT(SFP_bin[31])>>4);  
-                SFP_outputAT[32] = ( SFP_compress7_AT(SFP_bin[31])<<4 ) +( SFP_compress5_AT(SFP_bin[32])>>1); 
-                SFP_outputAT[33] = ( SFP_compress5_AT(SFP_bin[32])<<7 ) +( SFP_compress7FC_AT(SFP_bin[33])); 
-                SFP_outputAT[34] =  ( SFP_compress7FC_AT(SFP_bin[34])<<1 );                                             // 1 bit is spare          
-                
-                Science_TMframe::making_frame(2,'H',SFP_outputAT);
-                if(++packet_no == 30){
-                    packet_no=0;
-                    SCP_compress_data();
-                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
-                        if(packet_no==0){
-                            SCP_bin[i]=0;
-                        }
-                    }
-                }
-                            
-            }
- 
-    }
- 
- 
-/*
-    brief: takes the  pointer of the raw data string and return the address of the array which stores the address of 30 packets.
-    input: pointer to the raw data.
-    output : void
-*/
- 
-    void complete_compression(unsigned char *SRP,uint64_t x){
-        RTC_TIME = x;
-        int i;                  //30 times because 3 second data
-    
-        for(i=0;i<30;i++){
-         SFP_compress_data(SRP + 73*i); 
-        }
- 
-    }
- 
- 
- 
-/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 
-    
-    
-    /*
-    @brief:     compresses the given input stream and return output packet
-    @param:     pointer to input stream. Input stream always has the fixed size of RAW_PACKET_LENGTH
-    @return:    pointer to output stream. Output stream has the size of 22 or 26 bytes
-    */
-    void SCP_compress_data(){
-        
-        bool LCR = true;
-        int i = 0;
-        
-        for(i=1;i<=PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN ;i++){
-            if(SCP_bin[i]>SCP_thresholds[i]){
-            LCR = false;
-            break;
-            }
-        }
-//      printf("\n");                    //for printing the scp bin
-//          for (i=0;i<35;i++){
-//              printf(" scp[%d] = %d    ",i,SCP_bin[i]);
-//          }
-//          printf("\n");         
-        // compressing the data
-        if(LCR){   
-             SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
-             SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);                //then 4 bits for attitude tag
-             SCP_outputLCR[2] = 0x00;                                //only attitude tag is left
-             SCP_outputLCR[2] += ( SCP_compress5(SCP_bin[0])<<1 ) + ( SCP_compress5(SCP_bin[1])>>4 );
-             SCP_outputLCR[3] = ( SCP_compress5(SCP_bin[1])<<4 ) + ( SCP_compress5(SCP_bin[2])>>1 );
-             SCP_outputLCR[4] = ( SCP_compress5(SCP_bin[2])<<7 ) + ( SCP_compress5(SCP_bin[3])<<2 ) + ( SCP_compress5(SCP_bin[4])>>3 );
-             SCP_outputLCR[5] = ( SCP_compress5(SCP_bin[4])<<5 ) + ( SCP_compress5(SCP_bin[5]) );            
-             SCP_outputLCR[6] = ( SCP_compress5(SCP_bin[6])<<3 ) + ( SCP_compress5(SCP_bin[7])>>2 );
-             SCP_outputLCR[7] = ( SCP_compress5(SCP_bin[7])<<6 ) + ( SCP_compress5(SCP_bin[8])<<1 ) +  ( SCP_compress5(SCP_bin[9])>>4 );
-             SCP_outputLCR[8] = ( SCP_compress5(SCP_bin[9])<<4 ) + ( SCP_compress5(SCP_bin[10])>>1 );
-             SCP_outputLCR[9] = ( SCP_compress5(SCP_bin[10])<<7 ) + ( SCP_compress5(SCP_bin[11])<<2) + ( SCP_compress5(SCP_bin[12])>>3 );            
-             SCP_outputLCR[10] = ( SCP_compress5(SCP_bin[12])<<5 ) + ( SCP_compress5(SCP_bin[13]) );
-             SCP_outputLCR[11] = ( SCP_compress5(SCP_bin[14])<<3 ) + ( SCP_compress5(SCP_bin[15])>>2 );
-             SCP_outputLCR[12] = ( SCP_compress5(SCP_bin[15])<<6 ) + ( SCP_compress5(SCP_bin[16])<<1) +  ( SCP_compress6(SCP_bin[17])>>5 );
-             SCP_outputLCR[13] = ( SCP_compress6(SCP_bin[17])<<3 ) + ( SCP_compress6(SCP_bin[18])>>3 );
-             SCP_outputLCR[14] = ( SCP_compress5(SCP_bin[18])<<5 ) + ( SCP_compress6(SCP_bin[19])>>1 );
-             SCP_outputLCR[15] = ( SCP_compress6(SCP_bin[19])<<7 ) + ( SCP_compress5(SCP_bin[20])<<2 ) + ( SCP_compress5(SCP_bin[21])>>3 );          
-             SCP_outputLCR[16] = ( SCP_compress5(SCP_bin[21])<<5 ) + ( SCP_compress5(SCP_bin[22]) );
-             SCP_outputLCR[17] = ( SCP_compress5(SCP_bin[23])<<3 ) + ( SCP_compress5(SCP_bin[24])>>2 );
-             SCP_outputLCR[18] = ( SCP_compress5(SCP_bin[24])<<6 ) + ( SCP_compress5(SCP_bin[25])<<1) +  ( SCP_compress5(SCP_bin[26])>>4 );
-             SCP_outputLCR[19] = ( SCP_compress5(SCP_bin[26])<<4 ) + ( SCP_compress5(SCP_bin[27])>>1 );
-             SCP_outputLCR[20] = ( SCP_compress5(SCP_bin[27])<<7 ) + ( SCP_compress5(SCP_bin[28])<<2 ) + ( SCP_compress5(SCP_bin[29])>>3 ); 
-             SCP_outputLCR[21] = ( SCP_compress5(SCP_bin[29])<<5 ) + ( SCP_compress5(SCP_bin[30]) );
-         
-             Science_TMframe::making_frame(1,'L',SCP_outputLCR);
-        }
-        else{
-            
-            SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
-            SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);
-            SCP_outputHCR[2] = 0x40;
-            SCP_outputHCR[2] += ( SCP_compress6h(SCP_bin[0])<<6 ) ;
-            SCP_outputHCR[3] = ( SCP_compress6h(SCP_bin[1])<<2 ) + ( SCP_compress6h(SCP_bin[2])>>4) ;
-            SCP_outputHCR[4] = ( SCP_compress6h(SCP_bin[2])<<4 ) + ( SCP_compress6h(SCP_bin[3])>>2) ;
-            SCP_outputHCR[5] = ( SCP_compress6h(SCP_bin[3])<<6 ) + ( SCP_compress6h(SCP_bin[4])) ;
-            SCP_outputHCR[6] = ( SCP_compress6h(SCP_bin[5])<<2 ) + ( SCP_compress6h(SCP_bin[6])>>4) ;
-            SCP_outputHCR[7] = ( SCP_compress6h(SCP_bin[6])<<4 ) + ( SCP_compress6h(SCP_bin[7])>>2) ;
-            SCP_outputHCR[8] = ( SCP_compress6h(SCP_bin[7])<<6 ) + ( SCP_compress6h(SCP_bin[8])) ;
-            SCP_outputHCR[9] = ( SCP_compress6h(SCP_bin[9])<<2 ) + ( SCP_compress6h(SCP_bin[10])>>4) ;
-            SCP_outputHCR[10] = ( SCP_compress6h(SCP_bin[10])<<4 ) + ( SCP_compress6h(SCP_bin[11])>>2);
-            SCP_outputHCR[11] = ( SCP_compress6h(SCP_bin[11])<<6 ) + ( SCP_compress6h(SCP_bin[12])) ;
-            SCP_outputHCR[12] = ( SCP_compress6h(SCP_bin[13])<<2 ) + ( SCP_compress6h(SCP_bin[14])>>4)  ;
-            SCP_outputHCR[13] = ( SCP_compress6h(SCP_bin[14])<<4 ) + ( SCP_compress6h(SCP_bin[15])>>2)  ;
-            SCP_outputHCR[14] = ( SCP_compress6h(SCP_bin[15])<<6 ) + ( SCP_compress6h(SCP_bin[16]))     ;
-            SCP_outputHCR[15] = ( SCP_compress7h(SCP_bin[17])<<1 ) + ( SCP_compress7h(SCP_bin[18])>>6)  ;
-            SCP_outputHCR[16] = ( SCP_compress7h(SCP_bin[18])<<2 ) + ( SCP_compress7h(SCP_bin[19])>>5)  ;
-            SCP_outputHCR[17] = ( SCP_compress7h(SCP_bin[19])<<3 ) + ( SCP_compress6h(SCP_bin[20])>>3)  ;
-            SCP_outputHCR[18] = ( SCP_compress6h(SCP_bin[20])<<5 ) + ( SCP_compress6h(SCP_bin[21])>>1)  ;
-            SCP_outputHCR[19] = ( SCP_compress6h(SCP_bin[21])<<7 ) + ( SCP_compress6h(SCP_bin[22])<<1) +  ( SCP_compress6h(SCP_bin[23])>>5) ;
-            SCP_outputHCR[20] = ( SCP_compress6h(SCP_bin[23])<<3 ) + ( SCP_compress6h(SCP_bin[24])>>3)  ;
-            SCP_outputHCR[21] = ( SCP_compress6h(SCP_bin[24])<<5 ) + ( SCP_compress6h(SCP_bin[25])>>1)  ;
-            SCP_outputHCR[22] = ( SCP_compress6h(SCP_bin[25])<<7 ) + ( SCP_compress6h(SCP_bin[26])<<1) +  ( SCP_compress6h(SCP_bin[27])>>5) ;
-            SCP_outputHCR[23] = ( SCP_compress6h(SCP_bin[27])<<3 ) + ( SCP_compress6h(SCP_bin[28])>>3)  ;
-            SCP_outputHCR[24] = ( SCP_compress6h(SCP_bin[28])<<5 ) + ( SCP_compress6h(SCP_bin[29])>>1)  ;
-            SCP_outputHCR[25] = ( SCP_compress6h(SCP_bin[29])<<7 ) + ( SCP_compress6h(SCP_bin[30])<<1)  ; //last bit is empty
-        
-        /*  for (i=0;i<26;i++){
-                printf("\nscp[%d] = %d",i,SCP_outputHCR[i]);
-            }*/
-            Science_TMframe::making_frame(1,'H',SCP_outputHCR);
-        }
-    }
-    
-}
-
-
-// ******************************************** INTERLEAVE **************************************
-    void interleave( unsigned char *input, unsigned char *output ){
-    
-    unsigned int outState = 0;
-    unsigned int outByte = 0;
-    
-    for( unsigned int i = 0 ; i < 36 ; ++i ){
-        for(unsigned int j = 0 ; j < 30 ; ++j){
-            unsigned int x = j*36+i;
-            unsigned char tempBit = ((input[x >> 3]) >> (7-(x % 8))) & 1;
-            switch(outState){
-                case 0:
-                    outState = 1;
-                    output[outByte] = tempBit << 7;
-                    break;
-                case 1:
-                    outState = 2;
-                    output[outByte] += tempBit << 6;
-                    break;
-                case 2:
-                    outState = 3;
-                    output[outByte] += tempBit << 5;
-                    break;
-                case 3:
-                    outState = 4;
-                    output[outByte] += tempBit << 4;
-                    break;
-                case 4:
-                    outState = 5;
-                    output[outByte] += tempBit << 3;
-                    break;
-                case 5:
-                    outState = 6;
-                    output[outByte] += tempBit << 2;
-                    break;
-                case 6:
-                    outState = 7;
-                    output[outByte] += tempBit << 1;
-                    break;
-                case 7:
-                    outState = 0;
-                    output[outByte] += tempBit;
-                    ++outByte;
-                    break;
-            }
-        }
-        for(unsigned int j = 0 ; j < 2 ; ++j){
-            switch(outState){
-                case 0:
-                    output[outByte] = 0;
-                    outState = 1;
-                    break;
-                case 1:
-                    outState = 2;
-                    break;
-                case 2:
-                    outState = 3;
-                    break;
-                case 3:
-                    outState = 4;
-                    break;
-                case 4:
-                    outState = 5;
-                    break;
-                case 5:
-                    outState = 6;
-                    break;
-                case 6:
-                    outState = 7;
-                    break;
-                case 7:
-                    outState = 0;
-                    ++outByte;
-                    break;
-            }
-        }
-    }
-}
-
-// CONVOLUTION
+// ***************************************** CONVOLUTION *****************************************
 class Convolution{
     
 private:
@@ -1184,3 +167,83 @@
     }
     
 };
+ 
+
+// ******************************************** INTERLEAVE **************************************
+    void interleave( unsigned char *input, unsigned char *output ){
+    
+    unsigned int outState = 0;
+    unsigned int outByte = 0;
+    
+    for( unsigned int i = 0 ; i < 36 ; ++i ){
+        for(unsigned int j = 0 ; j < 30 ; ++j){
+            unsigned int x = j*36+i;
+            unsigned char tempBit = ((input[x >> 3]) >> (7-(x % 8))) & 1;
+            switch(outState){
+                case 0:
+                    outState = 1;
+                    output[outByte] = tempBit << 7;
+                    break;
+                case 1:
+                    outState = 2;
+                    output[outByte] += tempBit << 6;
+                    break;
+                case 2:
+                    outState = 3;
+                    output[outByte] += tempBit << 5;
+                    break;
+                case 3:
+                    outState = 4;
+                    output[outByte] += tempBit << 4;
+                    break;
+                case 4:
+                    outState = 5;
+                    output[outByte] += tempBit << 3;
+                    break;
+                case 5:
+                    outState = 6;
+                    output[outByte] += tempBit << 2;
+                    break;
+                case 6:
+                    outState = 7;
+                    output[outByte] += tempBit << 1;
+                    break;
+                case 7:
+                    outState = 0;
+                    output[outByte] += tempBit;
+                    ++outByte;
+                    break;
+            }
+        }
+        for(unsigned int j = 0 ; j < 2 ; ++j){
+            switch(outState){
+                case 0:
+                    output[outByte] = 0;
+                    outState = 1;
+                    break;
+                case 1:
+                    outState = 2;
+                    break;
+                case 2:
+                    outState = 3;
+                    break;
+                case 3:
+                    outState = 4;
+                    break;
+                case 4:
+                    outState = 5;
+                    break;
+                case 5:
+                    outState = 6;
+                    break;
+                case 6:
+                    outState = 7;
+                    break;
+                case 7:
+                    outState = 0;
+                    ++outByte;
+                    break;
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Compression.h	Mon Dec 14 12:04:01 2015 +0000
@@ -0,0 +1,936 @@
+int disk_write(const uint8_t *, uint64_t);
+uint64_t  RTC_TIME;                                    //need to  be changed to uint_64
+unsigned char SDcard_lastWritten[512]  = {0};
+ 
+namespace Science_TMframe {
+    
+    #define OUTLENGTH 360                                           //length of the output frame after convolution
+    #define SDcard_block 512                                        //block size of the sd card
+    
+    Convolution ConvObj;                                            //object which stores the frame after convolution
+    bool fresh[3] = {true,true,true};                               // True only for the first time                                                                 
+    unsigned char frames[3][134] = {0};                             // "frame" stores the address of the current frame...."first_frame_address" stores the address of the first node made.
+    unsigned int FCN[4] = {0};                                      //frame count number
+    unsigned int data_starting_point[3] = {8,5,10};             
+    unsigned int max_data[3] = {124,127,122};                       //number of bytes in each frame excluding TMID,FCN,first_header_point,crc
+    unsigned char TM_convoluted_data[270] = {0};                    //270 bytes is the size after convolution of 1072 bits
+    unsigned char complete_frame[SDcard_block] = {0};
+    uint64_t  SDC_address = 200;
+    bool SCH_FCCH_FLAG = true;
+    
+    
+    void add_SCH_FCCH(){
+        int i = 0;
+        complete_frame[0] = 0x0a;
+        complete_frame[1] = 0x3f;;
+        complete_frame[2] = 0x46;
+        complete_frame[3] = 0xb4;
+        complete_frame[4] = 0x00;
+        
+        for(i = 149 ; i < 159 ; i ++){
+            complete_frame[i] = 0 ; 
+        }
+        
+        complete_frame[159] = 0x0a;
+        complete_frame[160] = 0x3f;;
+        complete_frame[161] = 0x46;
+        complete_frame[162] = 0xb4;
+        complete_frame[163] = 0x00;
+        
+        for(i = 308 ; i < 318 ; i ++){
+            complete_frame[i] = 0 ; 
+        }
+        
+    }
+    
+    void making_frameHeader(unsigned char  TMID){
+    
+        unsigned char frame_type_identifier = 0;                                                        // not conform about the values , yet to be done    
+        frames[TMID][0] = (frame_type_identifier<<7) + ( (TMID + 1)<<3 ) + ( (FCN[TMID]>>24) & 0x7 );   //frame number should be less than 2^23 since 23 bits are assigned for that
+        frames[TMID][1] = ((FCN[TMID]>>16) & 0xff );
+        frames[TMID][2] = ( (FCN[TMID]>>8 )& 0xff );
+        frames[TMID][3] = ( FCN[TMID] & 0xff      );                                                        // first bit for (frame identifier), next 4 for (TMID) and next 27 for FCN
+        
+        if(TMID == 0){
+            frames[TMID][5] =( (RTC_TIME>>29) & 0xff );
+            frames[TMID][6] =( (RTC_TIME>>21) & 0xff ); 
+            frames[TMID][7] =( (RTC_TIME>>13) & 0xff );
+            
+        }else if(TMID == 2){
+            frames[TMID][5] =( (RTC_TIME>>32) & 0xff );
+            frames[TMID][6] =( (RTC_TIME>>24) & 0xff ); 
+            frames[TMID][7] =( (RTC_TIME>>16) & 0xff );
+            frames[TMID][8] =( (RTC_TIME>>8 ) & 0xff );
+            frames[TMID][9] =( (RTC_TIME    ) & 0xff );
+        }
+        
+    }
+ 
+    void convolution (unsigned char * ptr){
+    
+        ConvObj.convolutionEncode(ptr , TM_convoluted_data);
+        ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
+ 
+    }
+ 
+    /*
+        @brief : take the address of array of LCR or HCR and stores it into a frame
+        @parameters: type->L or H , deprnding on wheather it is LCR or HCR respectively
+        @return: nothing
+    */
+ 
+//  type 2 yet to be done
+    void making_frame(unsigned char TMID ,unsigned char type, unsigned char* pointer){
+        
+        TMID--;                                          //TMID goes from 1 to 3 , convinient to ue from 0 to 2
+        static int frame_space_number[3] = {0};          //this variable represents the register number of the frame in which LCR or HCR data to be written not including header
+        int  packet_len = 0;
+        int copy_count = 0 ;
+        
+        switch(int(TMID)){
+            case 0:                                     //SCP
+                if(type == 'L'){                        //below threshold
+                    packet_len = 22;   
+                }
+                else if(type == 'H'){                   //above threshold
+                    packet_len = 26;
+                }
+                break;
+            
+            case 1:                                     //SFP above threshold
+                packet_len = 35;
+                break;
+        
+            case 2:                                     //SFP  below threshold
+                packet_len = 23;
+                break;  
+        }
+        
+        if(SCH_FCCH_FLAG){
+            add_SCH_FCCH();
+            SCH_FCCH_FLAG = false;
+        }
+ 
+        if(fresh[TMID]){
+            //welcome to first frame
+            making_frameHeader(TMID);
+            frames[TMID][4] = 0;
+            fresh[TMID] = false;
+        }
+    
+    
+        while(copy_count < packet_len){                                                                  // 22 bytes is the size of the LCR
+            frames[TMID][ frame_space_number[TMID] + data_starting_point[TMID] ]= *(pointer + copy_count);
+            frame_space_number[TMID]++;                                          
+            copy_count++;
+            if( frame_space_number[TMID] == max_data[TMID] ){                                //frame space number can go from 0 to 126 as data is written from 0+5 to 126+5
+                FCN[TMID]++;
+                // convolution and save frame in the sd card
+                
+                // copying crc in 132 and 133
+                int temp_crc;
+                temp_crc = crc16_gen(frames[TMID],132);
+                frames[TMID][132] = temp_crc>>8;
+                frames[TMID][133] = temp_crc & 0xff;                
+                // xor 
+                for(int j = 0 ; j < 134 ; j++){
+ //                   frames[TMID][j] = frames[TMID][j]^exorThisWithTMFrame[j];
+                }
+                //convolution and interleaving  
+                convolution(frames[TMID]);
+                interleave(TM_convoluted_data , complete_frame + 5);
+                interleave(TM_convoluted_data+ 135,complete_frame + 164);
+                
+                // writing the SDC_address in a buffer , to store it in SDcard at address 5
+                SDcard_lastWritten[0] = SDC_address>>56;
+                SDcard_lastWritten[1] = (SDC_address>>48)&0xFF;
+                SDcard_lastWritten[2] = (SDC_address>>40)&0xFF;
+                SDcard_lastWritten[3] = (SDC_address>>32)&0xFF;
+                SDcard_lastWritten[4] = (SDC_address>>24)&0xFF;
+                SDcard_lastWritten[5] = (SDC_address>>16)&0xFF;
+                SDcard_lastWritten[6] = (SDC_address>>8)&0xFF;
+                SDcard_lastWritten[7] = (SDC_address)&0xFF;
+                
+                SPI_mutex.lock();
+                disk_write(complete_frame , SDC_address);
+                SPI_mutex.unlock();
+                SDC_address++;
+               
+            
+                
+                //now save to the sd card TM_convoluted_data
+//              std::bitset<8> b;
+//              printf("\nthis is frame %d\n",TMID);                                        //for printing frame 
+//                  for(int j =0; j<134;j++){
+//                  printf(" %d",frames[TMID][j]);
+////                    b =  frames[TMID][j];
+////                    cout<<b;
+//                  }
+                
+                frame_space_number[TMID] = 0;
+                making_frameHeader(TMID);
+                frames[TMID][4]=packet_len - copy_count;
+                //write time  here also
+                continue;
+            }       
+        }
+    
+//      printf("\nthis is frame %d\n",TMID);                                              //for printing frame 
+//      for(int j =0; j<134;j++){
+//          printf(" %d",frames[TMID][j]);
+//      }
+ 
+    }   
+ 
+ 
+}
+ 
+ 
+ 
+ 
+ 
+namespace Science_Data_Compression{
+ 
+    # define PACKET_SEQUENCE_COUNT 1                         //1 byte
+    # define NUM_PROTON_BIN 17                               //2 byte each
+    # define NUM_ELECTRON_BIN 14                             //2 byte each 
+    # define VETO 1                                          //2 byte
+    # define FASTCHAIN 2                                     //4 byte each
+    #define RAW_PACKET_LENGTH 73                             //73 bytes
+ 
+//  #define PACKET_SEQ_COUNT 1
+//  #define PROTON_BIN_SIZE 2
+//  #define ELECTRON_BIN_SIZE 2
+//  #define VETO 2
+//  #define FAST_CHAIN 4
+    
+    
+    /*
+    @brief:     read one uint16_t equivalent of first two chars from the stream. short int because 16 bits
+    @param:     pointer to the start of the short int
+    @return:    uint16_t 
+    */
+    
+    
+    unsigned int read_2byte(unsigned char* ptr){
+        unsigned int output = (unsigned int) *(ptr+1);
+        output += ( (unsigned int)(*ptr) ) << 8;
+        return output;
+    }
+    
+    /*
+    @brief:     read one int equivalent of first four chars from the stream.  int because 32 bits
+    @param:     pointer to the start of the short int
+    @return:    unsigned int  
+    */
+ 
+    unsigned int read_4byte(unsigned char* ptr){
+        unsigned int output = (unsigned int) *(ptr+3);
+        output += (unsigned int)*(ptr+2)<<8;
+        output += (unsigned int)*(ptr+1)<<16;
+        output += (unsigned int)*(ptr)<<24;
+        return output;
+    }
+ 
+    unsigned int SFP_thresholds[35]={0  ,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};//threashold values 
+    unsigned int SCP_thresholds[35]={0  ,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,500,0  ,0  ,0  };
+    unsigned int SFP_bin[35];
+    unsigned int SCP_bin[35]={0};
+    unsigned char SFP_outputBT[23];                         //BT = below threshold
+    unsigned char SFP_outputAT[35];
+    unsigned char SCP_outputLCR[22];
+    unsigned char SCP_outputHCR[26];
+ 
+    
+//**********************************************************************************************************************************************************************
+    //lots of compression functions are listed below
+ 
+    
+    unsigned char SFP_compress4_BT(unsigned int input){                                 //for veto
+        int de_4 = 0;
+        unsigned char output;
+        
+        if(input<= 3){
+        //  DE = 0;
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 12){
+        //  DE = 01;
+            output = 0x1;
+            de_4 = 2;   
+        }
+        else if(input <= 48){
+        //  DE = 10
+            output = 0x2;
+            de_4 = 4;   
+        }
+        else {
+        //  DE = 11
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 2;
+        return output;
+    }
+    
+    
+    unsigned char SFP_compress5_BT(unsigned int input){
+        int de_4 = 0;                                   //number by which bin value need to be shifted
+        unsigned char output;
+        
+        if(input <= 15){
+//            D = 0 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 60){
+//            D = 1
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 1;
+        
+        return output;      
+    };
+    
+    
+    unsigned char SFP_compress6_BT(unsigned int input){
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 31){
+//          E = 0 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 124){
+//            E = 1
+            output = 0x1;
+            de_4 = 2;
+        }
+         
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 1;
+        
+        return output;
+    };
+    
+    unsigned char SFP_compress7_AT(unsigned int input){
+        int de_4  = 0;
+        unsigned char output;
+        
+        if(input <= 31){
+//            DE = 00 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 124){
+//            DE = 01 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 496){
+//            DE = 10 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else if(input <= 1984){
+//           DE = 11 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 2;
+        
+        return output;
+        
+    };
+    
+    
+    unsigned char SFP_compress8_AT(unsigned int input){
+        
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 63){
+//            DE = 00 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 252){
+//            DE = 01 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 1008){
+//            DE = 10 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else {
+//           DE = 11 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 2;
+        
+        return output;
+    };
+    
+    unsigned char SFP_compress5_AT(unsigned int input){
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 3){
+//            DE = 000 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 12){
+//            DE = 001 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 48){
+//            DE = 010 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else if(input <= 192) {
+//           DE = 011 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        else if(input <= 768) {
+//           DE = 100 [4]
+            output = 0x4;
+            de_4 = 8;
+        }
+        
+        else if(input <= 3072) {
+//           DE = 101 [5]
+            output = 0x5;
+            de_4 = 10;
+        }
+        
+        else if(input <= 12288) {
+//           DE = 110 [6]
+            output = 0x6;
+            de_4 = 12;
+        }
+        
+        else  {
+//          DE = 111 [7]
+            output = 0x7;
+            de_4 = 14;
+        }
+           
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 3;
+        
+        return output;
+    }
+    
+    unsigned char SFP_compress7FC_AT(unsigned int input){                   // for fast chain above threshold
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 15){
+//            DE = 000 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        else if(input <= 60){
+//            DE = 001 [1]
+            output = 0x1;
+            de_4 = 2;
+        }
+        
+        else if(input <= 240){
+//            DE = 010 [2]
+            output = 0x2;
+            de_4 = 4;
+        }
+        
+        else if(input <= 960) {
+//           DE = 011 [3]
+            output = 0x3;
+            de_4 = 6;
+        }
+        
+        else if(input <= 3840) {
+//           DE = 100 [4]
+            output = 0x4;
+            de_4 = 8;
+        }
+        
+        else if(input <= 15360) {
+//           DE = 101 [5]
+            output = 0x5;
+            de_4 = 10;
+        }
+        
+        else if(input <= 61440) {
+//           DE = 110 [6]
+            output = 0x6;
+            de_4 = 12;
+        }
+        
+        else  {
+//          DE = 111 [7]
+            output = 0x7;
+            de_4 = 14;
+        }
+           
+        unsigned short int temp = input >> de_4;
+        output += (temp ) << 3;
+        
+        return output;
+    }
+    
+    
+    
+    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+    unsigned char SCP_compress6(unsigned int input){
+        int ef_4;
+        unsigned char output;
+        
+        if(input <= 15){
+//      EF = 00
+        output = 0x0;
+        ef_4 = 0;
+        }
+        
+        else if(input <= 60 ){
+//          EF = 01 [1]
+            output = 0x01;
+            ef_4 = 2;
+        }
+        else if(input <= 240){
+//          EF = 10 [2]
+            output = 0x02;
+            ef_4 = 4;
+        }
+        else{
+//          EF = 11 [3]
+            output = 0x03;
+            ef_4 = 6;
+        }
+            
+            unsigned short int temp = input >> ef_4;
+            output += (temp & 0xf) << 2;
+            
+            return output;
+    }
+    
+    unsigned char SCP_compress5(unsigned int input){
+        
+        int de_4 = 0;;
+        unsigned char output;
+        
+        if(input <= 7){
+//            DE = 00 [0]
+            output = 0x0;
+            de_4 = 0;
+        }
+        
+        else if(input <= 28){
+//            DE = 01 [1]
+            output = 0x01;
+            de_4 = 2;
+        }
+        else if(input <= 112){
+//            DE = 10 [2]
+            output = 0x02;
+            de_4 = 4;
+        }
+        else{
+//        DE = 11 [3]
+            output = 0x03;
+            de_4 = 6;
+        }
+        
+        unsigned short int temp = input >> de_4;
+        output += (temp & 0x7) << 2;
+        
+        return output;
+    }
+    
+        unsigned char SCP_compress6h(unsigned int input) {
+    
+        int ef_4;
+        unsigned char output;
+        
+        if(input <=7){
+//          EF = 000 [0]
+            output = 0x00;
+            ef_4 = 0;
+        }
+        
+        else if(input <=28){
+//          EF = 001 [1]
+            output = 0x01;
+            ef_4 = 2;
+        }
+        else if(input <= 112){
+        
+//          EF = 010 [2]
+            output = 0x02;
+            ef_4 = 4;
+        }
+        else if(input <= 448){
+//          EF = 011 [3]
+            output = 0x03;
+            ef_4 = 6;
+        }
+        else if(input <= 1792){
+//          EF = 100 [4]
+            output = 0x04;
+            ef_4 = 8;
+            
+        }
+        else if(input <= 7168){
+//          EF = 101 [5]
+            output = 0x05;
+            ef_4 = 10;
+            
+        }
+        else if(input <= 28672){
+//          EF = 110 [6]
+            output = 0x06;
+            ef_4 = 12;
+        }
+        else{
+//          EF = 111 [7]
+            output = 0x07;
+            ef_4 =14;
+        }
+        
+        unsigned short int temp = input >> ef_4;
+        output += (temp & 0x7) << 3;
+        
+        return output;
+        
+    }
+    
+    
+    unsigned char SCP_compress7h(unsigned int input) {
+    
+        int fg_4;
+        unsigned char output;
+        
+        if(input <= 15){
+//          EF = 000 [0]
+            output = 0x0;
+            fg_4 = 0;
+        }
+        
+        else if(input <= 60){
+//          EF = 001 [1]
+            output = 0x01;
+            fg_4 = 2;
+        }
+        else if(input <= 240){
+        
+//          EF = 010 [2]                    
+            output = 0x02;
+            fg_4 = 4;
+        }
+        else if(input <= 960){
+//          EF = 011 [3]
+            output = 0x03;
+            fg_4 = 6;
+        }
+        else if(input <= 3840){
+//          EF = 100 [4]
+            output = 0x04;
+            fg_4 = 8;
+            
+        }
+        else if(input <= 15360){
+//          EF = 101 [5]
+            output = 0x05;
+            fg_4 = 10;
+            
+        }
+        else if(input <= 61440){
+//          EF = 110 [6]
+            output = 0x06;
+            fg_4 = 12;
+        }
+        else{
+//          EF = 111 [7]
+            output = 0x07;
+            fg_4 =14;
+        }
+        
+        unsigned short int temp = input >> fg_4;
+        output += (temp & 0xf) << 3;
+        
+        return output;
+        
+    }
+    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+    void SCP_compress_data();                   
+    void SFP_compress_data(unsigned char* input){
+ 
+            bool LCR = true;
+            int i = 0;
+            static int packet_no = 0;                       //takes value from 0 to 29
+            //TRAVERSE THE LIST TO DETERMINE LCR OR HCR and stroing the values in proton_bin and electron bin
+            SFP_bin[0] = *input;
+            for(i=1 ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){          //storing the bin values into an array name bin
+                SFP_bin[i]=read_2byte(input+1+((i-1)<<1));                             //proton bin and elecron bin are 2 byte, hence read_2byte and 
+                SCP_bin[i]+=SFP_bin[i];
+                if(SFP_bin[i] > SFP_thresholds[i]){                                         //fast cahin is 4 byte hence read_4byte
+                    LCR = false;                                                    // if a single value is above threshold then lcr becomes false
+                    i++;
+                    break;
+                }
+            }
+            
+            for( ; i<= NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO ; ++i){
+                SCP_bin[i]+=SFP_bin[i];
+                SFP_bin[i] = read_2byte(input + 1 + ( (i-1)<<1) );
+            }
+            
+            SFP_bin[i] = read_4byte(input+1+ ((i-1)<<1))    ;
+            SCP_bin[i]+=SFP_bin[i];
+            
+            if(SFP_bin[i]>SFP_thresholds[i])
+                LCR = false;                                                    //since veto starts from location (input + 65) and (input + 69)
+                    
+            SFP_bin[i+1] = read_4byte(input+69);
+            SCP_bin[i]+=SFP_bin[i];
+            
+            if(SFP_bin[i]>SFP_thresholds[i])
+                LCR = false;
+            
+            
+//          printf("\n");                       //for printing the sfp bin
+//          for (i=0;i<35;i++){
+//              printf("sfp[%d] = %d",i,SFP_bin[i]);
+//          }
+//          printf("\n");
+ 
+            if(LCR){   
+                
+                 SFP_outputBT[0]  =  (packet_no<<3) + ( SFP_compress5_BT(SFP_bin[1])>>2 );
+                 SFP_outputBT[1]  =  ( SFP_compress5_BT(SFP_bin[1])<<6 ) + ( SFP_compress5_BT(SFP_bin[2])<<1 ) +   ( SFP_compress5_BT(SFP_bin[3])>>4 );
+                 SFP_outputBT[2]  =  ( SFP_compress5_BT(SFP_bin[3])<<4 ) + ( SFP_compress5_BT(SFP_bin[4])>>1 );
+                 SFP_outputBT[3]  =  ( SFP_compress5_BT(SFP_bin[4])<<7 ) + ( SFP_compress5_BT(SFP_bin[5])<<2 ) + ( SFP_compress5_BT(SFP_bin[6])>>3 );            
+                 SFP_outputBT[4]  =  ( SFP_compress5_BT(SFP_bin[6])<<5 ) + ( SFP_compress5_BT(SFP_bin[7]) );
+                 SFP_outputBT[5]  =  ( SFP_compress5_BT(SFP_bin[8])<<3 ) + ( SFP_compress5_BT(SFP_bin[9])>>2 );
+                 SFP_outputBT[6]  =  ( SFP_compress5_BT(SFP_bin[9])<<6 ) + ( SFP_compress5_BT(SFP_bin[10])<<1 ) +  ( SFP_compress5_BT(SFP_bin[11])>>4 );
+                 SFP_outputBT[7]  =  ( SFP_compress5_BT(SFP_bin[11])<<4 ) + ( SFP_compress5_BT(SFP_bin[12])>>1 );            
+                 SFP_outputBT[8]  =  ( SFP_compress5_BT(SFP_bin[12])<<7 ) + ( SFP_compress5_BT(SFP_bin[13])<<2 ) + ( SFP_compress5_BT(SFP_bin[14])>>3 );
+                 SFP_outputBT[9]  =  ( SFP_compress5_BT(SFP_bin[14])<<5 ) + ( SFP_compress5_BT(SFP_bin[15]) );
+                 SFP_outputBT[10] =  ( SFP_compress5_BT(SFP_bin[16])<<3 ) + ( SFP_compress5_BT(SFP_bin[17])>>2 );
+                 SFP_outputBT[11] =  ( SFP_compress5_BT(SFP_bin[17])<<6 ) + ( SFP_compress6_BT(SFP_bin[18]) );
+                 SFP_outputBT[12] =  ( SFP_compress6_BT(SFP_bin[19])<<2 ) + ( SFP_compress6_BT(SFP_bin[20])>>4 );
+                 SFP_outputBT[13] =  ( SFP_compress6_BT(SFP_bin[20])<<4 ) + ( SFP_compress5_BT(SFP_bin[21])>>1 );        
+                 SFP_outputBT[14] =  ( SFP_compress5_BT(SFP_bin[21])<<7 ) + ( SFP_compress5_BT(SFP_bin[22])<<2 )  + ( SFP_compress5_BT(SFP_bin[23])>>3 );
+                 SFP_outputBT[15] =  ( SFP_compress5_BT(SFP_bin[23])<<5 ) + ( SFP_compress5_BT(SFP_bin[24]) );
+                 SFP_outputBT[16] =  ( SFP_compress5_BT(SFP_bin[25])<<3 ) + ( SFP_compress5_BT(SFP_bin[26])>>2 );
+                 SFP_outputBT[17] =  ( SFP_compress5_BT(SFP_bin[26])<<6 ) + ( SFP_compress5_BT(SFP_bin[27])<<1 ) + ( SFP_compress5_BT(SFP_bin[28])>>4);
+                 SFP_outputBT[18] =  ( SFP_compress5_BT(SFP_bin[28])<<4 ) + ( SFP_compress5_BT(SFP_bin[29])>>1) ;
+                 SFP_outputBT[19] =  ( SFP_compress5_BT(SFP_bin[29])<<7 ) + ( SFP_compress5_BT(SFP_bin[30])<<2 ) + ( SFP_compress5_BT(SFP_bin[31])>>3)  ; 
+                 SFP_outputBT[20] =  ( SFP_compress5_BT(SFP_bin[31])<<5 ) + ( SFP_compress4_BT(SFP_bin[32])<<1)  + ( SCP_compress5(SFP_bin[33])>>4 ) ; //
+                 SFP_outputBT[21] =  ( SCP_compress5(SFP_bin[33])<<4 ) + ( SCP_compress5(SFP_bin[34])>>1 );        //here intentionally SCP_compress is used instead of SCP_compress5 is different than SFP_compress5
+                 SFP_outputBT[22] =  ( SCP_compress5(SFP_bin[34])<<7 );                                             //7 bits are spare
+                 
+                 Science_TMframe::making_frame(3,'L',SFP_outputBT);
+                 if(++packet_no == 30){
+                    packet_no=0;
+                    SCP_compress_data();
+                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
+                        if(packet_no==0){
+                            SCP_bin[i]=0;
+                        }
+                    }
+                 }
+                 
+                 
+            }
+ 
+            else {
+                
+                
+                SFP_outputAT[0]  = (RTC_TIME>>27)&(0xff);
+                SFP_outputAT[1]  = (RTC_TIME>>19)&(0xff);
+                SFP_outputAT[2]  = (RTC_TIME>>11)&(0xff);
+                SFP_outputAT[3]  = (RTC_TIME>>3 )&(0xff);
+                SFP_outputAT[4]  = (RTC_TIME<<5 )&(0xff) + (packet_no);
+                SFP_outputAT[5]  = ( SFP_compress7_AT(SFP_bin[1])<<1 ) + ( SFP_compress7_AT(SFP_bin[2])>>6 );
+                SFP_outputAT[6]  = ( SFP_compress7_AT(SFP_bin[2])<<2 ) + ( SFP_compress7_AT(SFP_bin[3])>>5 );
+                SFP_outputAT[7]  = ( SFP_compress7_AT(SFP_bin[3])<<3 ) + ( SFP_compress7_AT(SFP_bin[4])>>4 );
+                SFP_outputAT[8]  = ( SFP_compress7_AT(SFP_bin[4])<<4 ) + ( SFP_compress7_AT(SFP_bin[5])>>3 );
+                SFP_outputAT[9]  = ( SFP_compress7_AT(SFP_bin[5])<<5 ) + ( SFP_compress7_AT(SFP_bin[6])>>2 );
+                SFP_outputAT[10] = ( SFP_compress7_AT(SFP_bin[6])<<6 ) + ( SFP_compress7_AT(SFP_bin[7])>>1 );
+                SFP_outputAT[11] = ( SFP_compress7_AT(SFP_bin[7])<<7 ) + ( SFP_compress7_AT(SFP_bin[8]) );
+                SFP_outputAT[12] = ( SFP_compress7_AT(SFP_bin[9])<<1 ) + ( SFP_compress7_AT(SFP_bin[10])>>6);
+                SFP_outputAT[13] = ( SFP_compress7_AT(SFP_bin[10])<<2 ) + ( SFP_compress7_AT(SFP_bin[11])>>5);
+                SFP_outputAT[14] = ( SFP_compress7_AT(SFP_bin[11])<<3 ) + ( SFP_compress7_AT(SFP_bin[12])>>4);
+                SFP_outputAT[15] = ( SFP_compress7_AT(SFP_bin[12])<<4 ) + ( SFP_compress7_AT(SFP_bin[13])>>3);
+                SFP_outputAT[16] = ( SFP_compress7_AT(SFP_bin[13])<<5 ) + ( SFP_compress7_AT(SFP_bin[14])>>2);
+                SFP_outputAT[17] = ( SFP_compress7_AT(SFP_bin[14])<<6 ) + ( SFP_compress7_AT(SFP_bin[15])>>1);
+                SFP_outputAT[18] = ( SFP_compress7_AT(SFP_bin[15])<<7 ) + ( SFP_compress7_AT(SFP_bin[16]));
+                SFP_outputAT[19] = ( SFP_compress7_AT(SFP_bin[17])<<1 ) + ( SFP_compress8_AT(SFP_bin[18])>>7 );
+                SFP_outputAT[20] = ( SFP_compress8_AT(SFP_bin[18])<<1 ) + ( SFP_compress8_AT(SFP_bin[19])>>7 );
+                SFP_outputAT[21] = ( SFP_compress8_AT(SFP_bin[19])<<1 ) + ( SFP_compress8_AT(SFP_bin[20])>>7 );
+                SFP_outputAT[22] = ( SFP_compress8_AT(SFP_bin[20])<<1 ) + ( SFP_compress7_AT(SFP_bin[21])>>6 );
+                SFP_outputAT[23] = ( SFP_compress7_AT(SFP_bin[21])<<2 ) + ( SFP_compress7_AT(SFP_bin[22])>>5 );
+                SFP_outputAT[24] = ( SFP_compress7_AT(SFP_bin[22])<<3 ) + ( SFP_compress7_AT(SFP_bin[23])>>4 );
+                SFP_outputAT[25] = ( SFP_compress7_AT(SFP_bin[23])<<4 ) + ( SFP_compress7_AT(SFP_bin[24])>>3 );
+                SFP_outputAT[26] = ( SFP_compress7_AT(SFP_bin[24])<<5 ) + ( SFP_compress7_AT(SFP_bin[25])>>2 );
+                SFP_outputAT[27] = ( SFP_compress7_AT(SFP_bin[25])<<6 ) + ( SFP_compress7_AT(SFP_bin[26])>>1 );
+                SFP_outputAT[28] = ( SFP_compress7_AT(SFP_bin[26])<<7 ) + ( SFP_compress7_AT(SFP_bin[27]) );
+                SFP_outputAT[29] = ( SFP_compress7_AT(SFP_bin[28])<<1 ) + ( SFP_compress7_AT(SFP_bin[29])>>6);
+                SFP_outputAT[30] = ( SFP_compress7_AT(SFP_bin[29])<<2 ) + ( SFP_compress7_AT(SFP_bin[30])>>5);
+                SFP_outputAT[31] = ( SFP_compress7_AT(SFP_bin[30])<<3 ) +( SFP_compress7_AT(SFP_bin[31])>>4);  
+                SFP_outputAT[32] = ( SFP_compress7_AT(SFP_bin[31])<<4 ) +( SFP_compress5_AT(SFP_bin[32])>>1); 
+                SFP_outputAT[33] = ( SFP_compress5_AT(SFP_bin[32])<<7 ) +( SFP_compress7FC_AT(SFP_bin[33])); 
+                SFP_outputAT[34] =  ( SFP_compress7FC_AT(SFP_bin[34])<<1 );                                             // 1 bit is spare          
+                
+                Science_TMframe::making_frame(2,'H',SFP_outputAT);
+                if(++packet_no == 30){
+                    packet_no=0;
+                    SCP_compress_data();
+                    for(i=0;i<PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN;i++){
+                        if(packet_no==0){
+                            SCP_bin[i]=0;
+                        }
+                    }
+                }
+                            
+            }
+ 
+    }
+ 
+ 
+/*
+    brief: takes the  pointer of the raw data string and return the address of the array which stores the address of 30 packets.
+    input: pointer to the raw data.
+    output : void
+*/
+ 
+    void complete_compression(unsigned char *SRP,uint64_t x){
+        RTC_TIME = x;
+        int i;                  //30 times because 3 second data
+    
+        for(i=0;i<30;i++){
+         SFP_compress_data(SRP + 73*i); 
+        }
+ 
+    }
+ 
+ 
+ 
+/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ 
+    
+    
+    /*
+    @brief:     compresses the given input stream and return output packet
+    @param:     pointer to input stream. Input stream always has the fixed size of RAW_PACKET_LENGTH
+    @return:    pointer to output stream. Output stream has the size of 22 or 26 bytes
+    */
+    void SCP_compress_data(){
+        
+        bool LCR = true;
+        int i = 0;
+        
+        for(i=1;i<=PACKET_SEQUENCE_COUNT + NUM_PROTON_BIN + NUM_ELECTRON_BIN + VETO + FASTCHAIN ;i++){
+            if(SCP_bin[i]>SCP_thresholds[i]){
+            LCR = false;
+            break;
+            }
+        }
+//      printf("\n");                    //for printing the scp bin
+//          for (i=0;i<35;i++){
+//              printf(" scp[%d] = %d    ",i,SCP_bin[i]);
+//          }
+//          printf("\n");         
+        // compressing the data
+        if(LCR){   
+             SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
+             SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);                //then 4 bits for attitude tag
+             SCP_outputLCR[2] = 0x00;                                //only attitude tag is left
+             SCP_outputLCR[2] += ( SCP_compress5(SCP_bin[0])<<1 ) + ( SCP_compress5(SCP_bin[1])>>4 );
+             SCP_outputLCR[3] = ( SCP_compress5(SCP_bin[1])<<4 ) + ( SCP_compress5(SCP_bin[2])>>1 );
+             SCP_outputLCR[4] = ( SCP_compress5(SCP_bin[2])<<7 ) + ( SCP_compress5(SCP_bin[3])<<2 ) + ( SCP_compress5(SCP_bin[4])>>3 );
+             SCP_outputLCR[5] = ( SCP_compress5(SCP_bin[4])<<5 ) + ( SCP_compress5(SCP_bin[5]) );            
+             SCP_outputLCR[6] = ( SCP_compress5(SCP_bin[6])<<3 ) + ( SCP_compress5(SCP_bin[7])>>2 );
+             SCP_outputLCR[7] = ( SCP_compress5(SCP_bin[7])<<6 ) + ( SCP_compress5(SCP_bin[8])<<1 ) +  ( SCP_compress5(SCP_bin[9])>>4 );
+             SCP_outputLCR[8] = ( SCP_compress5(SCP_bin[9])<<4 ) + ( SCP_compress5(SCP_bin[10])>>1 );
+             SCP_outputLCR[9] = ( SCP_compress5(SCP_bin[10])<<7 ) + ( SCP_compress5(SCP_bin[11])<<2) + ( SCP_compress5(SCP_bin[12])>>3 );            
+             SCP_outputLCR[10] = ( SCP_compress5(SCP_bin[12])<<5 ) + ( SCP_compress5(SCP_bin[13]) );
+             SCP_outputLCR[11] = ( SCP_compress5(SCP_bin[14])<<3 ) + ( SCP_compress5(SCP_bin[15])>>2 );
+             SCP_outputLCR[12] = ( SCP_compress5(SCP_bin[15])<<6 ) + ( SCP_compress5(SCP_bin[16])<<1) +  ( SCP_compress6(SCP_bin[17])>>5 );
+             SCP_outputLCR[13] = ( SCP_compress6(SCP_bin[17])<<3 ) + ( SCP_compress6(SCP_bin[18])>>3 );
+             SCP_outputLCR[14] = ( SCP_compress5(SCP_bin[18])<<5 ) + ( SCP_compress6(SCP_bin[19])>>1 );
+             SCP_outputLCR[15] = ( SCP_compress6(SCP_bin[19])<<7 ) + ( SCP_compress5(SCP_bin[20])<<2 ) + ( SCP_compress5(SCP_bin[21])>>3 );          
+             SCP_outputLCR[16] = ( SCP_compress5(SCP_bin[21])<<5 ) + ( SCP_compress5(SCP_bin[22]) );
+             SCP_outputLCR[17] = ( SCP_compress5(SCP_bin[23])<<3 ) + ( SCP_compress5(SCP_bin[24])>>2 );
+             SCP_outputLCR[18] = ( SCP_compress5(SCP_bin[24])<<6 ) + ( SCP_compress5(SCP_bin[25])<<1) +  ( SCP_compress5(SCP_bin[26])>>4 );
+             SCP_outputLCR[19] = ( SCP_compress5(SCP_bin[26])<<4 ) + ( SCP_compress5(SCP_bin[27])>>1 );
+             SCP_outputLCR[20] = ( SCP_compress5(SCP_bin[27])<<7 ) + ( SCP_compress5(SCP_bin[28])<<2 ) + ( SCP_compress5(SCP_bin[29])>>3 ); 
+             SCP_outputLCR[21] = ( SCP_compress5(SCP_bin[29])<<5 ) + ( SCP_compress5(SCP_bin[30]) );
+         
+             Science_TMframe::making_frame(1,'L',SCP_outputLCR);
+        }
+        else{
+            
+            SCP_outputLCR[0] = (RTC_TIME>>5)&(0xff);                //first 13 bits for time tagging
+            SCP_outputLCR[1] = (RTC_TIME<<3)&(0xff);
+            SCP_outputHCR[2] = 0x40;
+            SCP_outputHCR[2] += ( SCP_compress6h(SCP_bin[0])<<6 ) ;
+            SCP_outputHCR[3] = ( SCP_compress6h(SCP_bin[1])<<2 ) + ( SCP_compress6h(SCP_bin[2])>>4) ;
+            SCP_outputHCR[4] = ( SCP_compress6h(SCP_bin[2])<<4 ) + ( SCP_compress6h(SCP_bin[3])>>2) ;
+            SCP_outputHCR[5] = ( SCP_compress6h(SCP_bin[3])<<6 ) + ( SCP_compress6h(SCP_bin[4])) ;
+            SCP_outputHCR[6] = ( SCP_compress6h(SCP_bin[5])<<2 ) + ( SCP_compress6h(SCP_bin[6])>>4) ;
+            SCP_outputHCR[7] = ( SCP_compress6h(SCP_bin[6])<<4 ) + ( SCP_compress6h(SCP_bin[7])>>2) ;
+            SCP_outputHCR[8] = ( SCP_compress6h(SCP_bin[7])<<6 ) + ( SCP_compress6h(SCP_bin[8])) ;
+            SCP_outputHCR[9] = ( SCP_compress6h(SCP_bin[9])<<2 ) + ( SCP_compress6h(SCP_bin[10])>>4) ;
+            SCP_outputHCR[10] = ( SCP_compress6h(SCP_bin[10])<<4 ) + ( SCP_compress6h(SCP_bin[11])>>2);
+            SCP_outputHCR[11] = ( SCP_compress6h(SCP_bin[11])<<6 ) + ( SCP_compress6h(SCP_bin[12])) ;
+            SCP_outputHCR[12] = ( SCP_compress6h(SCP_bin[13])<<2 ) + ( SCP_compress6h(SCP_bin[14])>>4)  ;
+            SCP_outputHCR[13] = ( SCP_compress6h(SCP_bin[14])<<4 ) + ( SCP_compress6h(SCP_bin[15])>>2)  ;
+            SCP_outputHCR[14] = ( SCP_compress6h(SCP_bin[15])<<6 ) + ( SCP_compress6h(SCP_bin[16]))     ;
+            SCP_outputHCR[15] = ( SCP_compress7h(SCP_bin[17])<<1 ) + ( SCP_compress7h(SCP_bin[18])>>6)  ;
+            SCP_outputHCR[16] = ( SCP_compress7h(SCP_bin[18])<<2 ) + ( SCP_compress7h(SCP_bin[19])>>5)  ;
+            SCP_outputHCR[17] = ( SCP_compress7h(SCP_bin[19])<<3 ) + ( SCP_compress6h(SCP_bin[20])>>3)  ;
+            SCP_outputHCR[18] = ( SCP_compress6h(SCP_bin[20])<<5 ) + ( SCP_compress6h(SCP_bin[21])>>1)  ;
+            SCP_outputHCR[19] = ( SCP_compress6h(SCP_bin[21])<<7 ) + ( SCP_compress6h(SCP_bin[22])<<1) +  ( SCP_compress6h(SCP_bin[23])>>5) ;
+            SCP_outputHCR[20] = ( SCP_compress6h(SCP_bin[23])<<3 ) + ( SCP_compress6h(SCP_bin[24])>>3)  ;
+            SCP_outputHCR[21] = ( SCP_compress6h(SCP_bin[24])<<5 ) + ( SCP_compress6h(SCP_bin[25])>>1)  ;
+            SCP_outputHCR[22] = ( SCP_compress6h(SCP_bin[25])<<7 ) + ( SCP_compress6h(SCP_bin[26])<<1) +  ( SCP_compress6h(SCP_bin[27])>>5) ;
+            SCP_outputHCR[23] = ( SCP_compress6h(SCP_bin[27])<<3 ) + ( SCP_compress6h(SCP_bin[28])>>3)  ;
+            SCP_outputHCR[24] = ( SCP_compress6h(SCP_bin[28])<<5 ) + ( SCP_compress6h(SCP_bin[29])>>1)  ;
+            SCP_outputHCR[25] = ( SCP_compress6h(SCP_bin[29])<<7 ) + ( SCP_compress6h(SCP_bin[30])<<1)  ; //last bit is empty
+        
+        /*  for (i=0;i<26;i++){
+                printf("\nscp[%d] = %d",i,SCP_outputHCR[i]);
+            }*/
+            Science_TMframe::making_frame(1,'H',SCP_outputHCR);
+        }
+    }
+    
+}
+ 
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Tue Dec 01 10:56:10 2015 +0000
+++ b/DefinitionsAndGlobals.h	Mon Dec 14 12:04:01 2015 +0000
@@ -1,6 +1,6 @@
 // **************DEFINITIONS*********************
 // COM_RX
-    #define RX_TIMEOUT_LIMIT 2.0
+    #define RX_TIMEOUT_LIMIT 1.0
     #define COM_RX_UART_TX PTE22
     #define COM_RX_UART_RX PTE23
 
@@ -8,6 +8,8 @@
     #define SPI_MOSI PTE1
     #define SPI_MISO PTE3
     #define SPI_CLK PTE2
+    #define SPI_CS_ADF PTA15
+    #define SPI_CS_SDC PTB1
 
 // TC LIST
     #define TCL_STATE_IDLE 0x00
@@ -86,6 +88,8 @@
 
 // COMMON SPI
 SPI spi( SPI_MOSI, SPI_MISO, SPI_CLK );
+DigitalOut gCS_ADF(SPI_CS_ADF);
+DigitalOut gCS_SDC(SPI_CS_SDC);
 Mutex SPI_mutex;
 
 // TC LIST
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDC_init.h	Mon Dec 14 12:04:01 2015 +0000
@@ -0,0 +1,427 @@
+//Serial PC(USBTX, USBRX);
+//DigitalOut cs_SDCard(PTE22);
+DigitalOut cs_SDCard(PTB1);
+
+#define SD_COMMAND_TIMEOUT 5000
+
+#define SD_DBG             0
+
+#define R1_IDLE_STATE           (1 << 0)
+#define R1_ERASE_RESET          (1 << 1)
+#define R1_ILLEGAL_COMMAND      (1 << 2)
+#define R1_COM_CRC_ERROR        (1 << 3)
+#define R1_ERASE_SEQUENCE_ERROR (1 << 4)
+#define R1_ADDRESS_ERROR        (1 << 5)
+#define R1_PARAMETER_ERROR      (1 << 6)
+//SPI spi(PTA16, PTA17, PTA15); // mosi, miso, sclk
+
+
+int initialise_card();
+int initialise_card_v1();
+int initialise_card_v2();
+int disk_initialize();
+//int disk_write(const uint8_t *, uint64_t);
+
+int disk_erase(int,int);
+
+uint64_t sd_sectors();
+uint64_t sectors;
+
+int cmd(int, int);
+int cmd58();
+int cmdx(int, int);
+int cmd8();
+int read(uint8_t*, uint32_t );
+int write(const uint8_t*, uint32_t );
+static uint32_t ext_bits(unsigned char *, int , int );
+
+int cdv;
+
+#define SDCARD_FAIL 0
+#define SDCARD_V1   1
+#define SDCARD_V2   2
+#define SDCARD_V2HC 3
+
+int count_bro;
+int i;
+int random[1000];
+
+
+void initialisation_SDCard(){ 
+//*******************************
+
+//    cs_adf =1;
+    
+//**************************
+    
+//    start_block_num =10 ;           // Read from TC
+//    end_block_num =Science_TMframe::SDC_address ;            // Read from TC    
+//    printf("welcome\n");
+    initialise_card();
+    int result= initialise_card();
+//    printf("initialise card result=%d\n",result);
+    disk_initialize();
+}
+
+
+
+
+int initialise_card()
+{
+    // Set to 100kHz for initialisation, and clock card with cs_SDCard = 1
+    spi.frequency(100000);
+    cs_SDCard = 1;
+    for (int i = 0; i < 16; i++) {
+        spi.write(0xFF);
+    }
+
+    // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
+    if (cmd(0, 0) != R1_IDLE_STATE) {
+        debug("No disk, or could not put SD card in to SPI idle state\n");
+        return SDCARD_FAIL;
+    }
+
+// send CMD8 to determine whther it is ver 2.x
+    int r = cmd8();
+    if (r == R1_IDLE_STATE) {
+//        printf("Entering v2 bro\n");
+        return initialise_card_v2();
+
+    } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
+        printf("Entering v1 bro\n");
+        return initialise_card_v1();
+
+    } else {
+        debug("Not in idle state after sending CMD8 (not an SD card?)\n");
+        return SDCARD_FAIL;
+    }
+}
+
+int initialise_card_v1()
+{
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        cmd(55, 0);
+        if (cmd(41, 0) == 0) {
+ //           printf("Yuppie v1 successful\n");
+            cdv = 512;
+            debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
+
+            return SDCARD_V1;
+        }
+    }
+
+    debug("Timeout waiting for v1.x card\n");
+    return SDCARD_FAIL;
+}
+
+
+int initialise_card_v2()
+{
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        wait_ms(50);
+        cmd58();
+        cmd(55, 0);
+        if (cmd(41, 0x40000000) == 0) {
+  //          printf("Yuppie,v2 successful\n");
+            cmd58();
+            debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
+            cdv = 1;
+
+            return SDCARD_V2;
+        }
+    }
+
+    debug("Timeout waiting for v2.x card\n");
+    return SDCARD_FAIL;
+}
+
+int cmd(int cmd, int arg)
+{
+    cs_SDCard = 0;
+
+    // send a command
+    spi.write(0x40 | cmd);
+    spi.write(arg >> 24);
+    spi.write(arg >> 16);
+    spi.write(arg >> 8);
+    spi.write(arg >> 0);
+    spi.write(0x95);
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        int response = spi.write(0xFF);
+        if (!(response & 0x80)) {
+            cs_SDCard = 1;
+            spi.write(0xFF);
+            return response;
+        }
+    }
+    cs_SDCard = 1;
+    spi.write(0xFF);
+    return -1; // timeout
+}
+
+
+int cmd58()
+{
+    cs_SDCard = 0;
+    int arg = 0;
+
+    // send a command
+    spi.write(0x40 | 58);
+    spi.write(arg >> 24);
+    spi.write(arg >> 16);
+    spi.write(arg >> 8);
+    spi.write(arg >> 0);
+    spi.write(0x95);
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        int response = spi.write(0xFF);
+        if (!(response & 0x80)) {
+            int ocr = spi.write(0xFF) << 24;
+            ocr |= spi.write(0xFF) << 16;
+            ocr |= spi.write(0xFF) << 8;
+            ocr |= spi.write(0xFF) << 0;
+            cs_SDCard = 1;
+            spi.write(0xFF);
+            return response;
+        }
+    }
+    cs_SDCard = 1;
+    spi.write(0xFF);
+    return -1; // timeout
+}
+
+
+int cmd8()
+{
+    cs_SDCard = 0;
+
+    // send a command
+    spi.write(0x40 | 8); // CMD8
+    spi.write(0x00);     // reserved
+    spi.write(0x00);     // reserved
+    spi.write(0x01);     // 3.3v
+    spi.write(0xAA);     // check pattern
+    spi.write(0x87);     // crc
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
+        char response[5];
+        response[0] = spi.write(0xFF);
+        if (!(response[0] & 0x80)) {
+            for (int j = 1; j < 5; j++) {
+                response[i] = spi.write(0xFF);
+            }
+            cs_SDCard = 1;
+            spi.write(0xFF);
+            return response[0];
+        }
+    }
+    cs_SDCard = 1;
+    spi.write(0xFF);
+    return -1; // timeout
+}
+
+uint64_t sd_sectors()
+{
+    uint32_t c_size, c_size_mult, read_bl_len;
+    uint32_t block_len, mult, blocknr, capacity;
+    uint32_t hc_c_size;
+    uint64_t blocks;
+
+    // CMD9, Response R2 (R1 byte + 16-byte block read)
+    if (cmdx(9, 0) != 0) {
+        debug("Didn't get a response from the disk\n");
+        return 0;
+    }
+
+    uint8_t csd[16];
+    if (read(csd, 16) != 0) {
+        debug("Couldn't read csd response from disk\n");
+        return 0;
+    }
+
+    // csd_structure : csd[127:126]
+    // c_size        : csd[73:62]
+    // c_size_mult   : csd[49:47]
+    // read_bl_len   : csd[83:80] - the *maximum* read block length
+
+    int csd_structure = ext_bits(csd, 127, 126);
+
+    switch (csd_structure) {
+        case 0:
+            cdv = 512;
+            c_size = ext_bits(csd, 73, 62);
+            c_size_mult = ext_bits(csd, 49, 47);
+            read_bl_len = ext_bits(csd, 83, 80);
+
+            block_len = 1 << read_bl_len;
+            mult = 1 << (c_size_mult + 2);
+            blocknr = (c_size + 1) * mult;
+            capacity = blocknr * block_len;
+            blocks = capacity / 512;
+            debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
+            break;
+
+        case 1:
+            cdv = 1;
+            hc_c_size = ext_bits(csd, 63, 48);
+            blocks = (hc_c_size+1)*1024;
+            debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
+            break;
+
+        default:
+            debug("CSD struct unsupported\r\n");
+            return 0;
+    };
+    return blocks;
+}
+
+int cmdx(int cmd, int arg)
+{
+    cs_SDCard = 0;
+
+    // send a command
+    spi.write(0x40 | cmd);
+    spi.write(arg >> 24);
+    spi.write(arg >> 16);
+    spi.write(arg >> 8);
+    spi.write(arg >> 0);
+    spi.write(0x95);
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        int response = spi.write(0xFF);
+        if (!(response & 0x80)) {
+            return response;
+        }
+    }
+    cs_SDCard = 1;
+    spi.write(0xFF);
+    return -1; // timeout
+}
+
+static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
+{
+    uint32_t bits = 0;
+    uint32_t size = 1 + msb - lsb;
+    for (int i = 0; i < size; i++) {
+        uint32_t position = lsb + i;
+        uint32_t byte = 15 - (position >> 3);
+        uint32_t bit = position & 0x7;
+        uint32_t value = (data[byte] >> bit) & 1;
+        bits |= value << i;
+    }
+    return bits;
+}
+
+int disk_initialize()
+{
+    int i = initialise_card();
+    debug_if(SD_DBG, "init card = %d\n", i);
+    sectors = sd_sectors();
+
+    // Set block length to 512 (CMD16)
+    if (cmd(16, 512) != 0) {
+        debug("Set 512-byte block timed out\n");
+        return 1;
+    } else {
+ //       printf("Hey,block init succesful\n");
+    }
+
+    spi.frequency(1000000); // Set to 1MHz for data transfer
+    return 0;
+}
+
+int disk_write(const uint8_t *buffer, uint64_t block_number)
+
+{
+    // set write address for single block (CMD24)
+    if (cmd(24, block_number * cdv) != 0) {
+        return 1;
+    }
+
+    // send the data block
+    write(buffer, 512);
+    //printf("Written Successfully bro \n");
+    return 0;
+}
+
+int write(const uint8_t*buffer, uint32_t length)
+{
+    cs_SDCard = 0;
+
+    // indicate start of block
+    spi.write(0xFE);
+
+    // write the data
+    for (int i = 0; i < length; i++) {
+        spi.write(buffer[i]);
+    }
+
+    // write the checksum
+    spi.write(0xFF);
+    spi.write(0xFF);
+
+    // check the response token
+    if ((spi.write(0xFF) & 0x1F) != 0x05) {
+        cs_SDCard = 1;
+        spi.write(0xFF);
+        return 1;
+    }
+
+    // wait for write to finish
+    while (spi.write(0xFF) == 0);
+
+    cs_SDCard = 1;
+    spi.write(0xFF);
+    return 0;
+}
+
+int disk_read(uint8_t *buffer, uint64_t block_number)
+{
+    // set read address for single block (CMD17)
+    if (cmd(17, block_number * cdv) != 0) {
+        return 1;
+    }
+
+    // receive the data
+    read(buffer, 512);
+    return 0;
+}
+
+int read(uint8_t *buffer, uint32_t length)
+{
+    cs_SDCard = 0;
+
+    // read until start byte (0xFF)
+    while (spi.write(0xFF) != 0xFE);
+
+    // read data
+    for (int i = 0; i < length; i++) {
+        buffer[i] = spi.write(0xFF);
+    }
+    spi.write(0xFF); // checksum
+    spi.write(0xFF);
+
+    cs_SDCard = 1;
+    spi.write(0xFF);
+    return 0;
+}
+
+int disk_erase(int startBlock, int totalBlocks)
+{
+    if(cmd(32, startBlock * cdv) != 0) {
+        return 1;
+    }
+    if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
+        return 1;
+    }
+    if (cmd(38,0) != 0) {
+        return 1;
+    }
+    
+    return 0; //normal return
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDCread.h	Mon Dec 14 12:04:01 2015 +0000
@@ -0,0 +1,190 @@
+// It takes 276us to read from sd card and write in 112 buffer
+ 
+//~ #include"SDcard.h"
+#define T_frames 30
+unsigned char buffer_112_counter,sent_tm = 0;
+int disk_read(uint8_t *, uint64_t),j;
+bool new_session = true;
+bool last_buffer = false;
+bool first_block = true;
+bool last_block =false;
+bool repeat_T_frame = false;
+unsigned int counter_in_block=0,diff_prev=0;
+unsigned char  EOS_counter = 0;
+unsigned char enable_T_frame = false;
+uint64_t  start_block_num ;           // Read from TC
+uint64_t  end_block_num ;            // Read from TC
+uint64_t  Number_of_blocks_to_read ;
+uint64_t  block_counter = 0;
+unsigned char buffer_512[512], buffer_112[112],counter_in_S_frame=0,counter_in_SCH=0;
+unsigned char make_EOS_flag = true;
+bool reset = false;
+ 
+void differential_encode(unsigned char* ptr, int length){
+    
+        for(int i=0; i<length;i++){
+        
+            unsigned char s = ptr[i] , t;
+            t = s ^ (s >> 1);
+            (diff_prev == 0) ? t=t^0x00 : t=t^0x80 ;
+            diff_prev = int(s & 0x01);
+            ptr[i] = t;
+        }
+        
+    } 
+ 
+void send_tm_from_SD_card(){
+    
+    if(make_EOS_flag == true){
+        make_EOS_flag = false;
+        Number_of_blocks_to_read = end_block_num - start_block_num +1;
+    }
+    
+    
+    for(buffer_112_counter=0;buffer_112_counter<112;buffer_112_counter++){
+      
+ 
+        if(new_session == true){          //check if it is beginnig of session  
+ 
+            buffer_112[buffer_112_counter]=S_frame[counter_in_S_frame++];  // sending S frame 
+            
+            if(counter_in_S_frame==48){
+//                cout<<"new_session_over\n";
+                counter_in_S_frame=0;
+                new_session = false;
+                enable_T_frame = true;
+//              enable_SCH = true;
+            }
+        }
+        else if(enable_T_frame == true){             // To send t frames
+ 
+            if(block_counter != Number_of_blocks_to_read){                  
+                
+                if(first_block){                            // Read first block
+//                    cout<<"brooooooo"<<start_block_num<<"yo"<<endl;   
+                    SPI_mutex.lock();     
+                    disk_read(buffer_512,start_block_num );
+                    SPI_mutex.unlock();
+                    counter_in_block = 0;
+                    block_counter++;
+                    
+                    first_block = false;
+//                    cout<<"first block exit\n";
+//                   cout<<Number_of_blocks_to_read;
+                   
+                   //printf("\n");
+//                    for(int j=0;j<512;j++){
+//                    printf("%02X ",buffer_512[j]);
+//                    }
+//                    printf("\n");
+//                    if(block_counter == Number_of_blocks_to_read){
+//                        last_block = true;
+//                    }
+                    
+                    
+                }
+                if((counter_in_block == 318 )&&(sent_tm<38)){                 // Read next block
+ 
+//                    cout<<"next block exit\n";
+                    SPI_mutex.lock();
+                    disk_read(buffer_512,start_block_num + block_counter );
+                    SPI_mutex.unlock();
+                    counter_in_block = 0;
+                    block_counter++;
+                    if(block_counter == Number_of_blocks_to_read){
+                        last_block = true;
+                    }
+                }
+            }
+                        
+            if(!last_block){  
+              
+ //                 cout<<int(sent_tm)<<" ";
+                if(sent_tm<38){
+                    buffer_112[buffer_112_counter] = buffer_512[counter_in_block++];    // sending T frames
+//                  cout<<int (counter_in_block)<<" ";
+                    if(counter_in_block == 318){
+                       
+                        sent_tm = sent_tm + 2;
+                    }   
+                }
+                else if(sent_tm == (T_frames-1)){
+                    buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159];
+                    if(counter_in_block == 318){
+//                        cout<<"last block exit\n";
+                        sent_tm = T_frames;
+                    }
+                }
+                else if(sent_tm == T_frames){       // Give eos and start new session         
+                    buffer_112[buffer_112_counter]=EoS[EOS_counter++];
+                        if(EOS_counter == 120){
+                            enable_T_frame = false;
+                            new_session = true;
+                            EOS_counter =0;
+                            sent_tm=0;
+                            repeat_T_frame = false;
+//                          cout<<"eos exit\n";
+                        }
+                }
+            }
+            if(last_block){
+//                cout<<"lb";
+                if(sent_tm == (T_frames-1) && (repeat_T_frame == false)){
+                    buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159];
+                    if(counter_in_block == 318){
+//                      cout<<"repeat block exit\n";
+                        sent_tm = T_frames;
+                    }
+                }
+                 if((sent_tm == T_frames) && (repeat_T_frame == false)){       // Give eos and start new session         
+                    buffer_112[buffer_112_counter]=EoS[EOS_counter++];
+                       if(EOS_counter == 120){
+//                        cout<<"eos exit";
+                        enable_T_frame = false;
+                        new_session = true;
+                        EOS_counter =0;
+                        sent_tm=0;
+                    }
+                }
+                else if((sent_tm < (T_frames-1)) && (repeat_T_frame == false)){
+                    buffer_112[buffer_112_counter] = buffer_512[counter_in_block++];    // sending last two T frames    
+                    if(counter_in_block == 318){
+                        sent_tm = sent_tm + 2;
+                        repeat_T_frame= true;
+                    }
+                }
+                else if((sent_tm< T_frames) && (repeat_T_frame == true)){
+                    buffer_112[buffer_112_counter] = buffer_512[(counter_in_block++ )-159];
+                    if(counter_in_block == 477){
+                        sent_tm = sent_tm +1;
+                        counter_in_block = counter_in_block-159;
+                    }
+                }
+                else if((sent_tm == T_frames ) && (repeat_T_frame == true)){       // Give eos and start new session         
+                   
+                    buffer_112[buffer_112_counter]=EoS[EOS_counter++];
+                        if(EOS_counter == 120){
+                        enable_T_frame = false;
+                        new_session = true;
+                        EOS_counter =0;
+//                    printf("yoooo %d",int(sent_tm));    
+                        sent_tm=0;
+                    
+                        last_block = false;
+                        last_buffer = true;
+                        printf("?\r\n");
+                        break;
+                    }
+                }
+                
+            }   
+        
+        }
+                    
+    }
+    differential_encode(buffer_112, 112);
+    if(last_buffer ==true){
+    diff_prev=0;
+    }
+    
+}
\ No newline at end of file
--- a/ThreadsAndFunctions.h	Tue Dec 01 10:56:10 2015 +0000
+++ b/ThreadsAndFunctions.h	Mon Dec 14 12:04:01 2015 +0000
@@ -89,6 +89,7 @@
                         send_l1_ack;
                         EXECUTE_OBOSC_ONLY;
                     }
+                    send_l1_ack;
                     EXECUTE_TC;
                 }
                 else{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adf.h	Mon Dec 14 12:04:01 2015 +0000
@@ -0,0 +1,545 @@
+//without reset feature , with state checks.
+DigitalOut led2(LED_RED);
+InterruptIn IRQ(PTA14);
+Ticker ticker;
+
+bool sent_tmfrom_SDcard;
+bool loop_on;
+bool ADF_off;
+bool buffer_state;
+uint8_t signal = 0x00;
+unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xF4,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,0x7F,0x00,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x10,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00};
+
+int initialise_card();
+int disk_initialize();
+
+#define bbram_write {\
+     SPI_mutex.lock();\
+    gCS_ADF=0;\
+    spi.write(0xB0);\
+    wait_us(300);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    for(int i=0;i<66;i++){\
+        spi.write(bbram_buffer[i]);\
+    }\
+    gCS_ADF=1;\
+    SPI_mutex.unlock();\
+}
+//------------------------------------------------------------------------
+// state checking functions
+//bool assrt_phy_off( int, int, int);
+//bool assrt_phy_on( int,int,int);
+//bool assrt_phy_tx(int,int,int);
+
+#define START_ADDRESS 0x020;
+#define MISO_PIN PTE3
+/**************Defining Counter Limits**************/
+#define THRS 20
+#define STATE_ERR_THRS 20
+#define PHY_OFF_EXEC_TIME 300
+#define PHY_ON_EXEC_TIME 300
+#define PHY_TX_EXEC_TIME 600
+/******DEFINING COMMANDS*********/
+#define CMD_HW_RESET 0xC8
+#define CMD_PHY_ON 0xB1
+#define CMD_PHY_OFF 0xB0
+#define CMD_PHY_TX 0xB5
+#define CMD_CONFIG_DEV 0xBB
+
+#define check_status {\
+    unsigned char stat=0;\
+    gCS_ADF=0;\
+    spi.write(0xFF);\
+    stat = spi.write(0xFF);\
+    gCS_ADF=1;\
+    status = stat;\
+}
+
+// all three arguments are int
+#define assrt_phy_off(return_this) {\
+    int cmd_err_cnt = 0;\
+    int spi_err_cnt = 0;\
+    int state_err_cnt = 0;\
+    for(int i = 0 ; i < 40 ;i++){\
+        check_status;\
+        if(status == 0xB1){\
+            return_this = 0;\
+            break;\
+        }\
+        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){\
+            return_this = 1;\
+            break;\
+        }\
+        else if(state_err_cnt>STATE_ERR_THRS){\
+            return_this = 1;\
+            break;\
+        }\
+        else if( (status & 0xA0) == 0xA0 ){\
+            gCS_ADF=0;\
+            spi.write(CMD_PHY_OFF);\
+            gCS_ADF=1;\
+            wait_us(PHY_OFF_EXEC_TIME);\
+            state_err_cnt++;\
+        }\
+        else if(status&0x80==0x00){\
+            wait_ms(5);\
+            spi_err_cnt++;\
+        }\
+        else {\
+            wait_ms(1);\
+            cmd_err_cnt++;\
+        }\
+    }\
+}
+
+
+//#define assrt_phy_on(cmd_err_cnt, spi_err_cnt, state_err_cnt, return_this){\
+//    status=check_status();\
+//        if((status&0x1F)==0x12){\
+//            return 0;\
+//        }\
+//        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){\
+//            return 1;\
+//        }\
+//        else if(state_err_cnt>STATE_ERR_THRS){\
+//            return 1;\
+//        }\
+//        else if((status&0xA0)==0xA0){\
+//            cs_adf=0;\
+//            spi.write(0xB1);\
+//            cs_adf=1;\
+//            wait_us(PHY_ON_EXEC_TIME);\
+//            return assrt_phy_on(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);\
+//        }\
+//        else if(status&0x80==0x00){\
+//            wait_ms(5);\
+//            //Error: SPI=0 Not ready CMD= Dont care
+//            return assrt_phy_on(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);\
+//        }\
+//        else{\
+//            if(status&0xA0==0x80){\
+//            wait_ms(1);\
+//            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+//            return assrt_phy_on(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);\
+//            }\
+//        }\
+//}
+
+
+
+
+#define initial_adf_check {\
+    spi.write(CMD_PHY_OFF);\
+    int tempReturn = 0;\
+    while( hw_reset_err_cnt < 2 ){\
+        assrt_phy_off( tempReturn);\
+        if( !tempReturn ){\
+            bbram_write;\
+            bbram_flag=1;\
+            break;\
+        }\
+        else{\
+            hardware_reset(0);\
+            hw_reset_err_cnt++;\
+        }\
+    }\
+    assrt_phy_off(tempReturn);\
+    if(!bbram_flag){\
+        bcn_flag=1;\
+     }\
+}
+
+unsigned char status =0;
+unsigned int cmd_err_cnt=0;
+unsigned int state_err_cnt=0;
+unsigned int miso_err_cnt=0;
+unsigned int hw_reset_err_cnt=0;
+bool bcn_flag=0;
+bool bbram_flag=0;
+
+//bool assrt_phy_off(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
+//    status=check_status();
+//        if(status==0xB1){
+//            return 0;
+//        }
+//        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
+//            return 1;//You need to Reset the hardware
+//        }
+//        else if(state_err_cnt>STATE_ERR_THRS){
+//            return 1;//Again reset the hardware
+//        }
+//        else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
+//            cs_adf=0;
+//            spi.write(CMD_PHY_OFF);        //CMD_PHY_OFF=0xB0
+//            cs_adf=1;
+//            wait_us(PHY_OFF_EXEC_TIME);// Typical = 24us  We are giving 300us 
+//            return assrt_phy_off(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
+//        }
+//        else if(status&0x80==0x00){
+//            wait_ms(5);
+//            //Error: SPI=0 Not ready CMD= Dont care
+//            return assrt_phy_off(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
+//        }
+//        else {//if(status&0xA0==0x80){
+//            wait_ms(1);
+//            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+//            return assrt_phy_off(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
+//        }
+//}
+
+//bool assrt_phy_on(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
+//    status=check_status();
+//        if((status&0x1F)==0x12){
+//            return 0;
+//        }
+//        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
+//            return 1;//You need to Reset the hardware
+//        }
+//        else if(state_err_cnt>STATE_ERR_THRS){
+//            return 1;//Again reset the hardware
+//        }
+//        else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
+//            cs_adf=0;
+//            spi.write(0xB1);        //CMD_PHY_OFF
+//            cs_adf=1;
+//            wait_us(PHY_ON_EXEC_TIME);// Typical = 24us  We are giving 300us 
+//            return assrt_phy_on(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
+//        }
+//        else if(status&0x80==0x00){
+//            wait_ms(5);
+//            //Error: SPI=0 Not ready CMD= Dont care
+//            return assrt_phy_on(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
+//        }
+//        else{
+//            if(status&0xA0==0x80){
+//            wait_ms(1);
+//            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+//            return assrt_phy_on(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
+//            }
+//        }
+//}
+
+
+// bool assrt_phy_tx(int cmd_err_cnt,int spi_err_cnt,int state_err_cnt){
+//    status=check_status();
+//        if((status & 0x1F) == 0x14){
+//            return 0;
+//        }
+//        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){
+//            return 1;//You need to Reset the hardware
+//        }
+//        else if(state_err_cnt>STATE_ERR_THRS){
+//            return 1;//Again reset the hardware
+//        }
+//        else if((status&0xA0)==0xA0){  //If Status' first three bit ore 0b1X1 =>SPI ready, Dont care interrupt and CMD Ready.
+//            cs_adf=0;
+//            spi.write(0xB1);        //CMD_PHY_OFF
+//            cs_adf=1;
+//            wait_us(PHY_TX_EXEC_TIME);// Typical = 24us  We are giving 300us 
+//            return assrt_phy_tx(cmd_err_cnt,spi_err_cnt,state_err_cnt+1);
+//        }
+//        else if(status&0x80==0x00){
+//            wait_ms(1);
+//            //Error: SPI=0 Not ready CMD= Dont care
+//            return assrt_phy_tx(cmd_err_cnt,spi_err_cnt+1,state_err_cnt);
+//        }
+//        else {
+//            if(status&0xA0==0x80){
+//            wait_us(50);
+//            //Error: Command Not ready SPI Ready cmd_err_cnt is a global variable
+//            return assrt_phy_tx(cmd_err_cnt+1,spi_err_cnt,state_err_cnt);
+//            }
+//        }
+//}
+
+bool hardware_reset(int bcn_call){
+    for(int i= 0; i < 20 ; i++){
+        gCS_ADF=0;
+        spi.write(CMD_HW_RESET);
+        gCS_ADF=1;
+        wait_ms(2);// Typically 1 ms
+        int count=0;
+        int temp_return = 0;
+        while(count<10 && miso_err_cnt<10){      
+            if(MISO_PIN){
+                assrt_phy_off(temp_return);
+                if(!temp_return){
+                    return 0;
+                }
+                count++;
+            }
+            else{
+                wait_us(50);
+                miso_err_cnt++;
+            }
+        }
+    }
+    return 1;
+}
+
+//bool hardware_reset(int bcn_call){
+//    if (bcn_call>20){//Worst Case 20seconds will be lost !
+//        return 1;
+//    }
+//    int count=0;
+//    cs_adf=0;
+//    spi.write(CMD_HW_RESET);
+//    cs_adf=1;
+//    wait_ms(2);// Typically 1 ms
+//    while(count<10 && miso_err_cnt<10){      
+//        if(MISO_PIN){
+//            int temp_return;
+//            assrt_phy_off(0,0,0,temp_return);
+//            if(!temp_return){
+//                break;
+//            }
+//            count++;
+//        }
+//        else{
+//            wait_us(50);
+//            miso_err_cnt++;
+//        }
+//    }
+//    if(count==10 ||miso_err_cnt==10){
+//        return hardware_reset(bcn_call+1);  
+//    }
+//    else
+//        return 0;
+//    
+//}
+
+
+
+
+//void initial_adf_check(){
+//        spi.write(CMD_PHY_OFF); //0xB0
+//        while(hw_reset_err_cnt<2){
+//        
+//            if(!assrt_phy_off(0,0,0)){  //assrt_phy_off () returns 0 if state is PHY_OFF , returns 1 if couldn't go to PHY_OFF
+//                bbram_write();
+//                bbram_flag=1;
+//                break;
+//            }
+//            else{
+//                hardware_reset(0);  // Asserts Hardware for 20sec(20times). PHY_OFF for 20,000 times
+//                hw_reset_err_cnt++;
+//            }
+//        }
+//        assrt_phy_off(0,0,0);// We actually do not need this but make sure "we do not need this"
+//        if(!bbram_flag){
+//        //Switch to beacon
+//            bcn_flag=1;
+//         }
+//}
+
+//for reseting the transmission call assert function after b5 and b1. after b1 assert_phi_on and after b5 assert_phi_tx. 
+//----------------------------------------------------------------------------
+
+# define initiate {\
+    SPI_mutex.lock();\
+    gCS_ADF=0;\
+    spi.write(0xFF);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0x08);\
+    spi.write(0x14);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0x08);\
+    spi.write(0x15);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0x09);\
+    spi.write(0x24);\
+    spi.write(0x20);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0x09);\
+    spi.write(0x37);\
+    spi.write(0xE0);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0x09);\
+    spi.write(0x36);\
+    spi.write(0x70);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0x09);\
+    spi.write(0x39);\
+    spi.write(0x10);\
+    gCS_ADF=1;\
+    SPI_mutex.unlock();\
+}
+
+
+#define write_data {\
+    SPI_mutex.lock();\
+    gCS_ADF=0;\
+    spi.write(0x0B);\
+    spi.write(0x36);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    if(buffer_state){\
+        spi.write(0x18);\
+        spi.write(0x20);\
+        for(unsigned char i=0; i<112;i++){\
+            spi.write(buffer_112[i]);\
+        }\
+    }\
+    else{\
+        spi.write(0x18);\
+        spi.write(0x90);\
+        for(unsigned char i=0; i<112;i++){\
+            spi.write(buffer_112[i]);\
+        }\
+    }\
+    gCS_ADF=1;\
+    SPI_mutex.unlock();\
+    buffer_state = !buffer_state;\
+    if(last_buffer){\
+        ADF_off = true;\
+        gPC.puts("adf_off\r\n");\
+    }\
+}
+ 
+
+void check(){   
+    if(IRQ){
+        gCOM_MNG_TMTC_THREAD->signal_set(signal);
+    }
+}
+  
+
+#define send_data {\
+    SPI_mutex.lock();\
+    gCS_ADF=0;\
+    spi.write(0xBB);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0xFF);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    SPI_mutex.unlock();\
+    if(sent_tmfrom_SDcard){\
+        send_tm_from_SD_card();\
+    }else{\
+        snd_tm.transmit_data(buffer_112,&last_buffer);\
+    }\
+    write_data;\
+    if(sent_tmfrom_SDcard){\
+        send_tm_from_SD_card();\
+    }else{\
+        snd_tm.transmit_data(buffer_112,&last_buffer);\
+    }\
+    write_data;\
+    if(sent_tmfrom_SDcard){\
+        send_tm_from_SD_card();\
+    }else{\
+        snd_tm.transmit_data(buffer_112,&last_buffer);\
+    }\
+    SPI_mutex.lock();\
+    gCS_ADF=0;\
+    spi.write(0xB1);\
+    gCS_ADF=1;\
+    wait_us(300);\
+    gCS_ADF=0;\
+    spi.write(0xFF);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    gCS_ADF=0;\
+    spi.write(0xB5);\
+    gCS_ADF=1;\
+    wait_us(300);\
+    gCS_ADF=0;\
+    spi.write(0xFF);\
+    spi.write(0xFF);\
+    gCS_ADF=1;\
+    SPI_mutex.unlock();\
+    ticker.attach_us(&check,32000);\
+}
+    
+    
+    
+#define adf_SND_SDCard {\
+    buffer_state = true;\
+    last_buffer = false;\
+    loop_on = true;\
+    ADF_off = false;\
+    sent_tmfrom_SDcard = true;\
+    signal = COM_MNG_TMTC_SIGNAL_ADF_SD;\
+    start_block_num = starting_add;\
+    end_block_num = ending_add;\
+    initial_adf_check;\
+    initiate;\
+    send_data;\
+    while(loop_on){\
+        led2=!led2;\
+        Thread::signal_wait(COM_MNG_TMTC_SIGNAL_ADF_SD);\
+        if(ADF_off){\
+            SPI_mutex.lock();\
+            ticker.detach();\
+            gCS_ADF=0;\
+            spi.write(0xB1);\
+            gCS_ADF=1;\
+            SPI_mutex.unlock();\
+            gPC.puts("transmission done\r\n");\
+            loop_on = false;\
+        }else{\
+            write_data;\
+            if(!last_buffer)\
+            send_tm_from_SD_card();\
+        }\
+    }\
+}
+    
+void read_TC(Base_tc* TC_ptr){
+    gPC.puts("Inside sd card sending\r\n");
+    unsigned char service_subtype = 0;
+    uint64_t starting_add  = 0, ending_add = 0;
+    service_subtype = (TC_ptr->TC_string[2])&0x0f;
+    starting_add =  (TC_ptr->TC_string[5]) +  ( (TC_ptr->TC_string[4])<<8 ) + ( (TC_ptr->TC_string[3]) <<16);
+    ending_add =  (TC_ptr->TC_string[8]) +  ( (TC_ptr->TC_string[7])<<8 ) + ( (TC_ptr->TC_string[6]) <<16);
+    starting_add = 10; // for now
+    ending_add = 20;
+//    adf_SND_SDCard(starting_add , ending_add);
+    gPC.puts("sending from sd card\r\n");
+    adf_SND_SDCard;
+}
+    
+
+void adf_not_SDcard(){
+    buffer_state = true;
+    last_buffer = false;
+    loop_on = true;
+    ADF_off = false;
+    sent_tmfrom_SDcard = false;
+   
+    signal = COM_MNG_TMTC_SIGNAL_ADF_NSD;
+    initial_adf_check;
+    initiate;
+    send_data;
+
+    while(loop_on){
+        led2=!led2;
+        Thread::signal_wait(COM_MNG_TMTC_SIGNAL_ADF_NSD);
+        if(ADF_off){
+            SPI_mutex.lock();
+            ticker.detach();
+//            wait_ms(35); 
+            gCS_ADF=0;
+            spi.write(0xB1);
+            gCS_ADF=1;
+            SPI_mutex.unlock();
+            loop_on = false;
+        }else{   
+            write_data;
+            snd_tm.transmit_data(buffer_112,&last_buffer);
+        }
+    }
+}
+ 
\ No newline at end of file
--- a/main.cpp	Tue Dec 01 10:56:10 2015 +0000
+++ b/main.cpp	Mon Dec 14 12:04:01 2015 +0000
@@ -1,9 +1,16 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "mbed_debug.h"
 
 #include "Structures.h"
 #include "DefinitionsAndGlobals.h"
 #include "crc.h"
+#include "COM_SND_TM_functions.h"
+#include "COM_SND_TM.h"
+#include "SDCread.h"
+#include "adf.h"
+#include "Compression.h"
+#include "SDC_init.h"
 #include "COM_RCV_TC.h"
 #include "COM_MNG_TMTC.h"
 #include "ThreadsAndFunctions.h"