CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Files at this revision

API Documentation at this revision

Comitter:
ee12b079
Date:
Wed Jul 20 09:58:59 2016 +0000
Parent:
267:783c248a6207
Child:
269:72529b247333
Child:
271:24c50959b0a4
Commit message:
Testing with SBC

Changed in this revision

CDMS_HK.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
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/CDMS_HK.h	Sat Jul 16 16:04:14 2016 +0000
+++ b/CDMS_HK.h	Wed Jul 20 09:58:59 2016 +0000
@@ -37,6 +37,7 @@
 
 void FCTN_CDMS_HK_MAIN(void const *args)
 {
+    uint8_t sd_stat = 0;
     while(1)
     {
     gHK_THREAD->signal_wait(HK_SIGNAL);
@@ -97,7 +98,12 @@
     for(int i=0; i<288; i++)
         CDMS_HEALTH_FINAL[i] = interleave_CDMS_HK[i];
 
-    //SD_WRITE(CDMS_HEALTH_FINAL,FSC_CURRENT[4]+1,4);
+    sd_stat = SD_WRITE(CDMS_HEALTH_FINAL,FSC_CURRENT[4]+1,4);
+    if(sd_stat)
+    {
+        gPC.puts("sd write failure");
+        break;
+        }
     gPC.printf("Completed CDMS HK\t");
 
     /*---------------------------------- BAE HK --------------------------------------------*/
@@ -128,7 +134,12 @@
             interleave(convoluted_BAE_HK +135, interleave_BAE_HK + 144);
             for(int i=0; i<288; i++)
                 BAE_HEALTH_FINAL[i] = interleave_BAE_HK[i];
-            //SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5);
+            sd_stat = SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5);
+            if(sd_stat)
+    {
+        gPC.puts("sd write failure");
+        break;
+        }
         }
         
     } else {
--- a/Compression.h	Sat Jul 16 16:04:14 2016 +0000
+++ b/Compression.h	Wed Jul 20 09:58:59 2016 +0000
@@ -14,6 +14,10 @@
 //using namespace std;
 
 //reading functions 2byte, 4byte
+#define srpz 1
+#define scpz 1
+#define scp_last 1
+#define srp_last 1
 
 uint16_t read_2byte(uint8_t* ptr)
 {
@@ -101,14 +105,15 @@
 //give the pointer of 6 second data to this function
 void srp(uint8_t * head)
 {
-    gPC.printf("\n\rEntered srp");
+    uint8_t sd_stat = 0;
+    gPC.printf("\n\rEntered srp function");
     debug_cntr = 0;
     sci_time = FCTN_CDMS_RD_RTC();
     if(time_prev_scp==0){
         time_prev_scp = sci_time;
     }    
     FCTN_SD_MNGR();                                                             ///changed recently
-    for (int i = 0; i < 56 ; i++)
+    for (int i = 0; i < 52 ; i++)
     {
         scp_bin[i] = 0;
     }
@@ -116,16 +121,23 @@
     srp_mode = head[2]&0x1;
     for(uint8_t counter = 0 ; counter < 60 ; counter++)
     {       
+        #if srpz
+        //gPC.printf("\n\n\rSRP count:%d",counter+1);
+        #endif
         /*-------------------- ------------populating sfp_bin and scp_bin[] starts here(also tag AT or BT)-----------------------*/
         at = 0;     pzf = 1;    ezf = 1;
         if(srp_mode == 0)                                   //calibrated mode 
         {
+            #if srpz
+            //gPC.printf("\n\rSRP - Calibrated Mode");
+            #endif
             for(int i=0; i<48 ; i++)                        //first 48 bins or SRP are of 16 bits size
             {
                 sfp_bin[i] = read_2byte(ptr + i*2);
                 scp_bin[i] += sfp_bin[i];
                 if(sfp_bin[i]>sfp_threshold_m0[i])
                 {
+                    //gPC.printf("\n\rSFP above threshold index:%d",i);
                     at = 1;
                 }
                 if(i<17)
@@ -143,31 +155,41 @@
             {
                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
                 scp_bin[i+48] += sfp_bin[i+48];
-                if(sfp_bin[i+48]>sfp_threshold_m0[i+48])
+                if(sfp_bin[i+48]>sfp_threshold_m0[i+48]){
+                    //gPC.printf("\n\rSP above threshold index: %d",i+48);
                     at = 1;
+                }
             }
         }
         else if(srp_mode == 1)                              //scattered mode
         {
+            #if srpz
+            //gPC.printf("\n\rSRP - Scatter Plot Mode");
+            #endif
             for(int i = 0; i <48; i++)                      //first 48 bins of SRP are of 16 bit size
             {
                 sfp_bin[i]  = read_2byte(ptr+2*i);
                 scp_bin[i] += sfp_bin[i];
-                if(sfp_bin[i] > sfp_threshold_m1[i])
+                //gPC.printf("\n\rsfp_bin[%d]= %u\t\tscp_bin[%d]= %u",i,sfp_bin[i],i,scp_bin[i]);
+                if(sfp_bin[i] > sfp_threshold_m1[i]){
+                    //gPC.printf("\n\rSP above threshold index: %d",i);
                     at = 1;
+                }
             }
             for(int i = 0; i < 4 ; i++)                     //next 4 bins are of 32 bit size
             {
                 ///sfp_bin[i+48] = read_4byte( (ptr+64) + 4*i );
                 sfp_bin[i+48] = read_4byte( (ptr+96) + 4*i );
                 scp_bin[i+48] += sfp_bin[i+48];
-                if(sfp_bin[i+48] > sfp_threshold_m1[i+48])
+                if(sfp_bin[i+48] > sfp_threshold_m1[i+48]){
+                    //gPC.printf("\n\rSP above threshold index: %d",i+48);
                     at = 1;
+                }
             }
         }
         /*---------------------------populating sfp_bin[] and scp_bin[] from srp ends here(also tagging AT or BT)------------------*/
 
-        ptr = ptr + 112;                                    //moving the pointer to next srp packet in the DMA buffer
+        //ptr = ptr + 112;                                    //moving the pointer to next srp packet in the DMA buffer
         
         /*------------------------------------------forming a science fine packet starts here----------------------------------------
         First we fill packet header, then fill packet data and finally assign its length
@@ -178,6 +200,9 @@
         {
             if(at == 0)
             {
+                #if srpz
+                //gPC.printf("\n\rSFP Below Threshold");
+                #endif
                 pointer = sfp; debug_cntr = 0;
                 space = adjust(4, attitude,pointer,8);      pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
@@ -215,6 +240,9 @@
                 }
             }                                                                                           //below thershold ends here.
             if(at == 1){
+                #if srpz
+                //gPC.printf("\n\rSFP Above Threshold");
+                #endif
                 pointer = sfp + 6; debug_cntr = 6;space = 8;
                 sfp[0] = (sci_time>>27)&0xff;   sfp[1] = (sci_time>>19)&0xff;   sfp[2] = (sci_time>>11)&0xff;   sfp[3] = (sci_time>>3)&0xff;
                 sfp[4] = ((sci_time&0x07)<<5) + ((attitude&0x0f)<<1) + (counter>>5);
@@ -242,6 +270,9 @@
 
                 if(pzf == 0){
                     //cout<<"proton bins ";
+                    #if srpz
+                    //gPC.printf("\n\rProton bins present in SRP");
+                    #endif
                     for(uint8_t i = 0; i<17 ; i++){
                         space = adjust(2, ((compress(sfp_bin[i],8,2))>>8) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
                         space = adjust(8, compress(sfp_bin[i],8,2) ,pointer,space);             pointer += space>>4;    debug_cntr += space>>4;
@@ -250,6 +281,9 @@
                 }
                 if(ezf == 0){
                     //cout<<"electron bins ";
+                    #if srpz
+                    //gPC.printf("\n\rElectron bins present in SRP");
+                    #endif
                     for(int i = 0; i<5 ; i++){
                         space = adjust(2,((compress(sfp_bin[18+i],8,2))>>8),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
                         space = adjust(8, compress(sfp_bin[18+i],8,2),pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
@@ -278,6 +312,9 @@
         {
             if(at == 0)
             {
+                #if srpz
+                //gPC.printf("\n\rSFP below threshold");
+                #endif
                 pointer = sfp; debug_cntr = 0;  space = 8;
                 space = adjust(4, attitude,pointer,8);      pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(6, counter,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
@@ -294,6 +331,10 @@
             }
             if(at == 1)
             {
+                
+                #if srpz                
+                //gPC.printf("\n\rSFP below threshold");
+                #endif
                 pointer = sfp; debug_cntr = 0;  space = 8;
                 space = adjust(3, sci_time>>32 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, sci_time>>24 ,pointer,space); pointer += space>>4;    debug_cntr += space>>4;
@@ -324,11 +365,17 @@
             }
         }
         length = (length%8==0)?(length/8):(length/8)+1;                         //converting length to mulitple of 8
+        #if srpz
+        //gPC.printf("\n\rLength of SFP packet in bytes = %d",length);
+        #endif
         /*----------------------------------------forming a science packet ends here-----------------------------------------------*/
 
         /*-----------------------------Inserting the above packet(sfp) into the TM frame(s) starts here----------------------------*/
         if(id==1){
             if(position_tm_frame[id]>130){
+                #if srpz
+                //gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame.");
+                #endif
                 while(position_tm_frame[id]<132){
                     frames[id][position_tm_frame[id]] = 0;
                     position_tm_frame[id]++;
@@ -337,6 +384,9 @@
         }
         else if(id==2){
             if(position_tm_frame[id]>126){
+                #if srpz
+                //gPC.printf("\n\rSkipping the current frame.Inserting sfp packet into the next frame.");
+                #endif
                 while(position_tm_frame[id]<132){
                     frames[id][position_tm_frame[id]] = 0;
                     position_tm_frame[id]++;
@@ -350,6 +400,9 @@
         {            
             if(position_tm_frame[id]==frames[id][6-id]){
                 if(id==1){
+                    #if srpz
+                    //gPC.printf("\n\rAdding Time to TM frame");
+                    #endif
                     frames[id][6]  = (uint8_t)((sci_time>>32)&0x07);
                     frames[id][7]  = (uint8_t)((sci_time>>24)&0xff);
                     frames[id][8]  = (uint8_t)((sci_time>>16)&0xff);
@@ -365,11 +418,16 @@
             if(position_tm_frame[id] == 132)
             {
                 /*-----------the current frame is completely filled and we fill frame header to write into SD card---------*/
+                #if srpz
+                //gPC.printf("\n\rSFP_TM frame full. Length of current packet = %d",j);
+                //gPC.printf("\n\rFrame Header: %d",frames[id][6-id]);
+                //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]);
+                #endif
                 pointer = frames[id];
                 if(id == 1){    //below threshold
                     space = adjust(1,0,pointer,8);
                     space = adjust(4,3,pointer,space);
-                    FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit
+                    FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
                     frames[id][1] = (FSC_science>>24)&0xff;
                     frames[id][2] = (FSC_science>>16)&0xff;
                     frames[id][3] = (FSC_science>>8)&0xff;
@@ -387,7 +445,14 @@
                 temp_crc = crc16_gen(frames[id],132);
                 frames[id][132] = temp_crc>>8;
                 frames[id][133] = temp_crc & 0xff;
-
+                
+                #if srpz
+                gPC.printf("\n\rPrinting SFP_TM\n\r{");                
+                for(uint8_t z=0;z<134;z++){
+                    gPC.printf("0x%02X,",frames[id][z]);
+                }
+                gPC.printf("}\n\r");
+                #endif
                 /*------------------current TM frame completely filled-----------------------*/
                 
                 exor(frames[id]);
@@ -396,18 +461,61 @@
                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
                 if(id == 1)
                 {
-                    SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
+                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,3);     //sd_write will return ack later, for now not included
                 }
                 else if (id == 2)
                 {
-                    SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
+                    sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,2);     //sd_write will return ack later, for now not included
                 }
+                if(sd_stat)
+                {
+                    gPC.puts("sd write fail");
+                    }
                 position_tm_frame[id] = position_tm_starting[id];
-                frames[id][6-id] = ((length-1) - j)+position_tm_starting[id];
+                frames[id][6-id] = (length - j)+position_tm_starting[id];
             }
         }
         /*------------------------------finished inserting the sfp packet into the TM frame(s)-------------------------------------*/
-    }                                                                                           // for loop bracket which runs 60 times    
+    }                                                                               // for loop bracket which runs 60 times    
+    #if srpz_last
+    //gPC.printf("\n\rPrinting the last SFP_TM frame");
+    //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]);
+    while(position_tm_frame[id]<132){
+        frames[id][position_tm_frame[id]] = 0;
+        position_tm_frame[id]++;
+    }
+    if(position_tm_frame[id]==132){
+        //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][6-id]]);
+        pointer = frames[id];
+        if(id == 1){    //below threshold
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,3,pointer,space);
+            FSC_science = FSC_CURRENT[3];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
+            frames[id][1] = (FSC_science>>24)&0xff;
+            frames[id][2] = (FSC_science>>16)&0xff;
+            frames[id][3] = (FSC_science>>8)&0xff;
+            frames[id][4] = FSC_science&0xff;                    
+        }
+        else if(id == 2)    //above threshold
+        {
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,2,pointer,space);                    
+            FSC_science = FSC_CURRENT[2];                                               ///to be used as this
+            frames[id][1] = (FSC_science>>16)&0xff;
+            frames[id][2] = (FSC_science>>8)&0xff;
+            frames[id][3] = FSC_science&0xff;
+        }
+        temp_crc = crc16_gen(frames[id],132);
+        frames[id][132] = temp_crc>>8;
+        frames[id][133] = temp_crc & 0xff;        
+        
+        gPC.printf("\n\rPrinting SFP_TM\n\r{");
+        for(uint8_t z=0;z<134;z++){
+            gPC.printf("0x%02X,",frames[id][z]);
+        }
+        gPC.printf("}\n\r");
+    }
+    #endif
     at = 0;     pzf = 1;    ezf = 1;
     srp_mode = head[2]&0x1;
     uint8_t compression_option = (head[2]>>1)&0x3;
@@ -421,6 +529,7 @@
             {
                 if(scp_bin[i] > scp_threshold_m0[i])
                 {
+                    //gPC.printf("\n\rSCP above threshold index: %d",i);
                     at = 1;
                     break;
                 }
@@ -428,17 +537,25 @@
         }
         else if(compression_option == 1)
         {
-            if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
+            if(scp_bin[44] > scp_threshold_m0_1[0]){//gPC.printf("\n\rSCP above threshold index: 44"); 
+            at=1;}
             for(int i=0; i<4 ;i++)
             {
                 if(scp_bin[48+i] > scp_threshold_m0_1[i+1])
                 {
+                    //gPC.printf("\n\rSCP above threshold index: %d",i+48);
                     at = 1;
                     break;
                 }
             }
-            if(scp_bin[17] > scp_threshold_m0_1[5]) at=1;
-            if(scp_bin[23] > scp_threshold_m0_1[6]) at=1;
+            if(scp_bin[17] > scp_threshold_m0_1[5]){ 
+                //gPC.printf("\n\rSP above threshold index: 17");
+                at=1;
+            }
+            if(scp_bin[23] > scp_threshold_m0_1[6]){ 
+                //gPC.printf("\n\rSP above threshold index: 23");
+                at=1;
+            }
 
             proton_scp_sum = 0; electron_scp_sum = 0;
             for(int i=0;i<17;i++)
@@ -449,12 +566,15 @@
             {
                 electron_scp_sum += scp_bin[18+i];
             }
-            if(proton_scp_sum > scp_threshold_m0_1[7]) at=1;
-            if(electron_scp_sum > scp_threshold_m0_1[8]) at=1;
+            if(proton_scp_sum > scp_threshold_m0_1[7]){ //gPC.printf("\n\rProton_scp_sum above threshold");
+            at=1;}
+            if(electron_scp_sum > scp_threshold_m0_1[8]){//gPC.printf("\n\rElectron_scp_sum above threshold");
+             at=1;}
         }
         else if(compression_option == 2)
         {
-            if(scp_bin[50] > scp_sfp_threshold_m0_2[0]) at=1;
+            if(scp_bin[50] > scp_sfp_threshold_m0_2[0]){//gPC.printf("\n\rSCP above threshold index:50"); 
+            at=1;}
             proton_scp_sum = 0; electron_scp_sum = 0;
             for(int i=0;i<17;i++)
             {
@@ -464,16 +584,19 @@
             {
                 electron_scp_sum += scp_bin[18+i];
             }
-            if(proton_scp_sum > scp_sfp_threshold_m0_2[1]) at=1;
-            if(electron_scp_sum > scp_sfp_threshold_m0_2[2]) at=1;
+            if(proton_scp_sum > scp_sfp_threshold_m0_2[1]){//gPC.printf("\n\rProton_SCP_sum above threshold"); 
+            at=1;}
+            if(electron_scp_sum > scp_sfp_threshold_m0_2[2]){//gPC.printf("\n\rElectron above threshold");
+             at=1;}
         }
     }
     else if(srp_mode ==1)
     {
-        if(compression_option==0){ 
+        if(compression_option==0){
             for(int i=0; i<48; i++)
             {
                 if(scp_bin[i] > scp_threshold_m1[i]){
+                    //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]);
                     at = 1;
                     break;
                 }
@@ -481,13 +604,17 @@
             for(int i=48; i<52; i++)
             {
                 if(scp_bin[i] > scp_threshold_m1[i]){
+                    //gPC.printf("\nSCP above threshold index:%d, Value:%d",i,scp_bin[i]);
                     at = 1;
                     break;
                 }
             }
         }
-        else if(compression_option==2){
-            if(scp_bin[50]>scp_sfp_threshold_m0_2[0])   at=1;
+        else if(compression_option==2){            
+            if(scp_bin[50]>scp_sfp_threshold_m0_2[0]){
+                //gPC.printf("\nSCP above threshold index:50, Value:%",scp_bin[50]);
+                at=1;
+            }
         }
     }
     /*----------------------------Tagging the scp packet formed from these 60 sfp's as AT or BT ends here--------------------------*/
@@ -645,7 +772,7 @@
     beacon_cntr++;
     /*--------------------------------------------------beacon packet ends here--------------------------------------------------------*/
 
-    gPC.printf("scp\r\n");
+    //gPC.printf("\n\rSCP count: %d");
     uint8_t packet_pp,time_diff;
     /*------------------------------------------Forming a science packet(scp) starts here----------------------------------------------*/
 
@@ -679,9 +806,15 @@
         space = adjust(8, sfp_at_counter,pointer,space);            pointer += space>>4;    debug_cntr += space>>4;
     }
     if(srp_mode == 0 && compression_option == 0){       //normal callibrated mode
+        #if scpz
+        //gPC.printf("\n\rSCP - Normal calibrated mode");
+        #endif
         space = adjust(1, pzf,pointer,space);                       pointer += space>>4;    debug_cntr += space>>4;
         space = adjust(1, ezf,pointer,space);                       pointer += space>>4;    debug_cntr += space>>4;
         if(at == 0 ){
+            #if scpz
+            //gPC.printf("\n\rBelow threshold");
+            #endif
             for(int i = 0; i<8 ;i++){
                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -713,6 +846,9 @@
             }
         }// below threshold ends here
         if(at == 1){
+            #if scpz
+            //gPC.printf("\n\rAbove threshold");
+            #endif
             for(int i = 0; i<8 ;i++){
                 space = adjust(7,compress(scp_bin[24+i],4,3) ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -745,7 +881,13 @@
         }   //above thresholds ends
     }   //srp_mode == 0 ends
     if(srp_mode == 1){
+        #if scpz
+        //gPC.printf("\n\rSCP - Scatter plot mode");
+        #endif
         if(at == 0){
+            #if scpz
+            //gPC.printf("\n\rBelow Threshold");
+            #endif
             for(int i=0; i<48; i++){
                 space = adjust(6, ((compress(scp_bin[i],3,3))&0xff) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -754,6 +896,9 @@
             }
         }
         if(at == 1){
+            #if scpz
+            //gPC.printf("\n\rAbove threshold");
+            #endif
             for(int i=0; i<48; i++){
                 space = adjust(7, (compress((scp_bin[i]/2),4,3))>>8 ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
             }
@@ -764,9 +909,15 @@
         }
     }// scp mode 1 end
     if( srp_mode == 0 && compression_option == 1 ){ //scp data conservation mode
+        #if scpz
+        //gPC.printf("\n\rSCP - Calibrated data conservation mode");
+        #endif
         space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
         space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
         if(at == 0){
+            #if scpz
+            //gPC.printf("\n\rBelow threshold");
+            #endif
             space = adjust(6, (compress(scp_bin[44],3,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
             for(int i=0; i<4; i++){
                 space = adjust(7, (compress(scp_bin[48+i],4,3)) ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
@@ -782,6 +933,9 @@
                 space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
             }
         }else if(at == 1){
+            #if scpz
+            //gPC.printf("\n\rAbove threshold");
+            #endif
             space = adjust(1, (compress(scp_bin[44],6,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
             space = adjust(8, (compress(scp_bin[44],6,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
             for(int i=0; i<4; i++){
@@ -806,9 +960,15 @@
     {
         if(srp_mode==0)
         {
+            #if scpz
+            //gPC.printf("\n\rSCP - calibrated extreme data conservation mode");
+            #endif
             space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             if(at==0){
+                #if scpz
+                //gPC.printf("\n\rBelow threshold");
+                #endif
                 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 if(pzf==0){
                     space = adjust(4, (compress(proton_scp_sum,10,2))>>8 ,pointer,space);   pointer += space>>4;    debug_cntr += space>>4;
@@ -819,6 +979,9 @@
                     space = adjust(8, (compress(electron_scp_sum,10,2)) ,pointer,space);    pointer += space>>4;    debug_cntr += space>>4;
                 }
             }else if(at==1){
+                #if scpz
+                //gPC.printf("\n\rAbove threshold");
+                #endif
                 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space);              pointer += space>>4;    debug_cntr += space>>4;
                 if(pzf==0){
@@ -833,10 +996,16 @@
         }
         else if(srp_mode==1)
         {
+            #if scpz
+            //gPC.printf("\n\rSCP - Scatter plot, extreme data conservation mode");
+            #endif
             pzf = 0;    ezf = 0;    //because proton energy bins are not available in scatter plot mode and hence in the packet we send dummy data(which is nothng but the maximum value which fits into that particular compression scheme)
             space = adjust(1, pzf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             space = adjust(1, ezf,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
             if(at==0){
+                #if scpz
+                //gPC.printf("\n\rBelow threshold");
+                #endif
                 space = adjust(7, (compress(scp_bin[50],4,3)) ,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
                 // below 4 lines - filling the proton ane electron energy bins with zeros(junk value) since they're not available in srp
                 space = adjust(4, (compress(0,10,2))>>8 ,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
@@ -845,6 +1014,9 @@
                 space = adjust(8, (compress(0,10,2)) ,pointer,space);           pointer += space>>4;    debug_cntr += space>>4;
             }
             else if(at==1){
+                #if scpz
+                //gPC.printf("\n\rAbove threshold");
+                #endif
                 space = adjust(4, (compress(scp_bin[50],9,3))>>8 ,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(8, (compress(scp_bin[50],9,3)) ,pointer,space);          pointer += space>>4;    debug_cntr += space>>4;
                 space = adjust(1, (compress(0,6,3))>>8 ,pointer,space);                 pointer += space>>4;    debug_cntr += space>>4;
@@ -909,7 +1081,14 @@
         }
     }
     length = (length%8==0)?(length/8):(length/8)+1;
-    if(position_tm_frame[id]>129){
+    #if scpz
+    //gPC.printf("\n\rSCP packet length = %d",length);
+    #endif
+    if(position_tm_frame[id]>129){        
+        #if scpz
+        //gPC.printf("\n\rSkipping the current TM frame. Inserting SCP into the next frame");
+        //gPC.printf("\n\rposition_tm_frame = %d",position_tm_frame[id]);
+        #endif
         while(position_tm_frame[id]<132){
             frames[id][position_tm_frame[id]] = 0;
             position_tm_frame[id]++;
@@ -921,6 +1100,9 @@
     for(int j= 0; j < length ;)
     {        
         if(position_tm_frame[id]==frames[id][4]){
+            #if scpz
+            //gPC.printf("\n\rInserting Time into SCP_TM");
+            #endif
             frames[id][5] = (uint8_t)((sci_time>>29)&0x3f);
             frames[id][6] = (uint8_t)((sci_time>>21)&0xff);
             frames[id][7] = (uint8_t)((sci_time>>13)&0x3f);
@@ -932,6 +1114,9 @@
         }
         if(position_tm_frame[id] == 132)                                                //space full in the frame bro
         {
+            #if scpz
+            //gPC.printf("\n\rSCP_TM frame full. Length of current SCP packet = %d",j);
+            #endif
             pointer = frames[id];
             space = adjust(1,0,pointer,8);
             space = adjust(4,1,pointer,space);
@@ -942,15 +1127,67 @@
             temp_crc = crc16_gen(frames[id],132);
             frames[id][132] = temp_crc>>8;
             frames[id][133] = temp_crc & 0xff;
+            #if scpz
+            //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]);
+            gPC.printf("\n\rPrinting SCP_TM\n\r{");
+            for(uint8_t z=0;z<134;z++){
+                gPC.printf("0x%02X,",frames[id][z]);
+            }
+            gPC.printf("}\n\r");
+            #endif
             exor(frames[id]);
             convolution(frames[id]);
             interleave(TM_convoluted_data,TM_interleave_data);
             interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
 
-            SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
-
+            sd_stat = SD_WRITE(TM_interleave_data,FSC_science+1,1);         //sd_write returns ack, for now not included
+            if(sd_stat)
+                {
+                    gPC.puts("sd write fail");
+                    }
+            
             position_tm_frame[id] = position_tm_starting[id];
-            frames[id][4] = ((length-1) - j)+position_tm_starting[id];
+            frames[id][4] = (length - j)+position_tm_starting[id];
         }
     }
+    #if scpz_last
+    //gPC.printf("\n\rPrinting the last SFP_TM frame");
+    //gPC.printf("\n\n\rPosition of tm_pointer: %d",position_tm_frame[id]);
+    while(position_tm_frame[id]<132){
+        frames[id][position_tm_frame[id]] = 0;
+        position_tm_frame[id]++;
+    }
+    if(position_tm_frame[id]==132){
+        //gPC.printf("\n\rFirst head pointer contents: 0x%02X",frames[id][frames[id][4]]);
+        pointer = frames[id];
+        if(id == 1){    //below threshold
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,3,pointer,space);
+            FSC_science = FSC_CURRENT[1];                               ///to be used as this, but FSC_CURRENT[] is 32 bit                    
+            frames[id][1] = (FSC_science>>24)&0xff;
+            frames[id][2] = (FSC_science>>16)&0xff;
+            frames[id][3] = (FSC_science>>8)&0xff;
+            frames[id][4] = FSC_science&0xff;                    
+        }
+        else if(id == 2)    //above threshold
+        {
+            space = adjust(1,0,pointer,8);
+            space = adjust(4,2,pointer,space);                    
+            FSC_science = FSC_CURRENT[2];                                               ///to be used as this
+            frames[id][1] = (FSC_science>>16)&0xff;
+            frames[id][2] = (FSC_science>>8)&0xff;
+            frames[id][3] = FSC_science&0xff;
+        }
+        temp_crc = crc16_gen(frames[id],132);
+        frames[id][132] = temp_crc>>8;
+        frames[id][133] = temp_crc & 0xff;
+        
+        
+        gPC.printf("\n\rPrinting SCP_TM\n\r{");
+        for(uint8_t z=0;z<134;z++){
+            gPC.printf("0x%02X,",frames[id][z]);
+        }
+        gPC.printf("}\n\r");
+    }
+    #endif    
 }
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Sat Jul 16 16:04:14 2016 +0000
+++ b/DefinitionsAndGlobals.h	Wed Jul 20 09:58:59 2016 +0000
@@ -1,15 +1,15 @@
 // **************DEFINITIONS*********************
 
-#define bypass_adf 1
+#define bypass_adf 0
 #define dma 0
 
 // COM_RX
     #define RX_TIMEOUT_LIMIT 0.5
-    //#define COM_RX_UART_TX PTE20    // For bypassing RX1M (SET BAUD RATE 1200) #define COM_RX_UART_TX USBTX
-    //#define COM_RX_UART_RX PTE21    // For bypassing RX1M (SET BAUD RATE 1200) #define COM_RX_UART_RX USBRX
+    #define COM_RX_UART_TX PTE20    // For bypassing RX1M (SET BAUD RATE 1200) #define COM_RX_UART_TX USBTX
+    #define COM_RX_UART_RX PTE21    // For bypassing RX1M (SET BAUD RATE 1200) #define COM_RX_UART_RX USBRX
     
-    #define COM_RX_UART_TX USBTX
-    #define COM_RX_UART_RX USBRX
+    //#define COM_RX_UART_TX USBTX
+    //#define COM_RX_UART_RX USBRX
     
 // COMMON SPI
     #define SPI_MOSI PTE1
@@ -157,7 +157,7 @@
 
 //RF RELAY
     #define RF_RELAY_CNTRL_TX PTB7
-    #define RF_RELAY_CNTRL_BCN PTB12 
+    #define RF_RELAY_CNTRL_BCN PTB17 
     
 //CDMS HK
 #define HK_SIGNAL 0x05
--- a/ThreadsAndFunctions.h	Sat Jul 16 16:04:14 2016 +0000
+++ b/ThreadsAndFunctions.h	Wed Jul 20 09:58:59 2016 +0000
@@ -157,7 +157,7 @@
         }
         else if( gFLAGS & NEW_TC_RECEIVED ){
             gPC.puts("NEW TC RECEIVED\r\n");
-            //Thread::wait(10000);
+            Thread::wait(10000);
             gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
             gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;
 
--- a/adf.h	Sat Jul 16 16:04:14 2016 +0000
+++ b/adf.h	Wed Jul 20 09:58:59 2016 +0000
@@ -7,29 +7,38 @@
 bool buffer_state;
 bool finish_write_data;
 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};
+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,0x20,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xE0,0x00,0x10,0x04,0x00,0x00,0x00,0x00,0x00};
+bool data_irq_err=0;
+bool data_err= false;
+unsigned char temp_byte=0x00;
+unsigned char data_err1[112];
+unsigned char data_err_cnt=0;
+unsigned int byte_count =0;
+#define DATA_ERR_THRS 20
+bool rolling_buffer_settings_error=true;
+bool bbram_write_success=true;
+int bbram_err_cnt=0;
+int rbp_err_cnt=0;
+bool reset_flag=false;
+#define RBP_THRS 4
+bool quit_configuration=false;
+bool power_reset_flag=false;
+unsigned int power_reset_count=0;
+bool Configuration_done=false;
+#define HW_THRS 2
  
 //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 TRANSMIT_LEN_1 0xFF
+#define TRANSMIT_LEN_2 0xFF
+/***/
+Timer T;
  
 #define START_ADDRESS 0x020;
 #define MISO_PIN PTE3
@@ -45,7 +54,105 @@
 #define CMD_PHY_OFF 0xB0
 #define CMD_PHY_TX 0xB5
 #define CMD_CONFIG_DEV 0xBB
+/**STATES**/
+//=======================
+#define PHY_OFF 0xB1
+#define PHY_ON 0xB2
+#define PHY_TX 0xB4
+#define BUSY 0x00
+//===================================================
+
+unsigned int Adf_data_counter=0;
+unsigned char status =0;
+unsigned int cmd_err_cnt=0;
+unsigned int data_length;
+unsigned int state_err_cnt=0;
+unsigned int miso_err_cnt=0;
+unsigned int hw_reset_err_cnt=0;
+unsigned int counter =0;
+bool temp_return = 0;
+bool bcn_flag=0;
+bool bbram_flag=0;
+
+bool stop_transmission=false;
+
+#define reset_flags {\
+finish_write_data = false;\
+buffer_state = true;\
+last_buffer = false;\
+loop_on = true;\
+ADF_off = false;\
+buffer_state = true;\
+loop_on = true;\
+ADF_off = false;\
+sent_tmfrom_SDcard = false;\
+Adf_data_counter=0;\
+status =0;\
+cmd_err_cnt=0;\
+data_length;\
+state_err_cnt=0;\
+miso_err_cnt=0;\
+hw_reset_err_cnt=0;\
+counter =0;\
+bcn_flag=0;\
+bbram_flag=0;\
+stop_transmission=false;\
+    }
+
+
+int err_arr[64];
+bool bbram_err=false;
+int err_idx=-1;
+int err[64];
+#define bbram_check gCS_ADF=0;\
+    for(int i=0;i<64;i++){\
+        err_arr[i]=0;\
+        }\
+    for(int i=0;i<64;i++){\
+        err[i]=0;\
+        }\
+    bbram_err=false;\
+    gCS_ADF=0;\
+    spi.write(0x39);\
+    spi.write(0x00);\
+    spi.write(0xFF);\
+    for(int i=0;i<64;i++){\
+        err_arr[i]=spi.write(0xFF);\
+        if(err_arr[i]!=bbram_buffer[i+2]){\
+            err[i]=1;\
+            bbram_err=true;\
+            }\
+    }\
+    gCS_ADF=1;\
+    if(!bbram_err)\
+        gPC.printf("BBRAM verified \r\n");\
+    else\
+        gPC.printf("BBRAM error \r\n");\
  
+
+bool tx_loop=1;
+
+#define PRNT_ONCE {\
+    gPC.printf("%d %d lol_ what?\r\n",segment_len,EOS_len);\
+    }
+ 
+#define bbram_write {\
+    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;\
+}
+//------------------------------------------------------------------------
+// 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 check_status {\
     unsigned char stat=0;\
     gCS_ADF=0;\
@@ -54,97 +161,106 @@
     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++){\
+
+/*CMD_VAR*/
+bool cmd_bit=true;
+bool cmd_err_flag=false;
+unsigned char command = 0x00;
+#define CMD(command) {\
+    cmd_err_cnt=0;\
+    cmd_err_flag=false;\
+    while(cmd_err_cnt<3) {\
         check_status;\
-        if(status == 0xB1){\
-            return_this = 0;\
+        cmd_bit=status&0x20;\
+        if(cmd_bit) {\
+            gCS_ADF=0;\
+            spi.write(command);\
+            gCS_ADF=1;\
             break;\
-        }\
-        else if(cmd_err_cnt>THRS||spi_err_cnt>THRS){\
-            return_this = 1;\
-            break;\
+        } else {\
+            wait_us(5);\
+            cmd_err_cnt++;\
         }\
-        else if(state_err_cnt>STATE_ERR_THRS){\
-            return_this = 1;\
-            break;\
+    }\
+    if(cmd_err_cnt==3) {\
+        cmd_err_flag=true;\
+    }\
+}
+// all three arguments are int\
+#define assrt_phy_off {\
+    int state_err_cnt = 0;\
+    CMD(CMD_PHY_OFF);\
+    if(cmd_err_flag){\
+        temp_return=1;\
         }\
-        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++;\
+    else{\
+        for(int i = 0 ; i < 40 ;i++){\
+            CMD(CMD_PHY_OFF);\
+            check_status;\
+            if(status == PHY_OFF){\
+                temp_return = 0;\
+            break;\
+            }\
+            else if(state_err_cnt>THRS){\
+                temp_return = 1;\
+            break;\
+            }\
+            else {\
+                wait_ms(1);\
+            }\
         }\
     }\
 }
- 
- 
-#define initial_adf_check {\
-    spi.write(CMD_PHY_OFF);\
-    int tempReturn = 0;\
-    bool flag = false;\
-    while( hw_reset_err_cnt < 2 ){\
-        assrt_phy_off( tempReturn);\
-        if( !tempReturn ){\
-            bbram_write;\
-            bbram_flag=1;\
-            flag = true;\
-            break;\
-        }\
-        else{\
-            hardware_reset(0);\
-            hw_reset_err_cnt++;\
-            gPC.puts("Resetting hardware\r\n");\
-        }\
-    }\
-    if( flag == false ){\
-        gPC.puts("Seems to be SPI problem\r\n");\
-    }\
-    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 hardware_reset(int bcn_call){
-    for(int i= 0; i < 20 ; i++){
+
+
+bool hardware_reset(int bcn_call)
+{
+    for(int i= 0; i < 2 ; 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);
+        temp_return = 0;
+        while(count<10 && miso_err_cnt<10) {
+            if(MISO_PIN) {
+//                assrt_phy_off;
+/*asseert_phyoff Starts**/
+{\
+    int state_err_cnt = 0;\
+    CMD(CMD_PHY_OFF);\
+    if(cmd_err_flag){\
+        temp_return=1;\
+        }\
+    else{\
+        for(int i = 0 ; i < 40 ;i++){\
+            CMD(CMD_PHY_OFF);\
+            check_status;\
+            if(status == PHY_OFF){\
+                temp_return = 0;\
+            break;\
+            }\
+            else if(state_err_cnt>THRS){\
+                temp_return = 1;\
+            break;\
+            }\
+            else {\
+                wait_ms(1);\
+            }\
+        }\
+    }\
+}\
+/*Assert_phy_off_ends*/
+
+
+
+
+
                 if(!temp_return){
                     return 0;
                 }
                 count++;
-            }
-            else{
+            } else {
                 wait_us(50);
                 miso_err_cnt++;
             }
@@ -152,46 +268,158 @@
     }
     return 1;
 }
- 
-//for reseting the transmission call assert function after b5 and b1. after b1 assert_phi_on and after b5 assert_phi_tx. 
+//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();\
+unsigned char temp;
+bool reg_err;
+int reg_err_cnt;
+#define REG_ERR_THRS 5
+#define reg_check(addr1,addr2,reg_val){\
+ gCS_ADF=0;\
+        reg_err=false;\
+        spi.write(addr1);\
+        spi.write(addr2);\
+        spi.write(0xFF);\
+        temp = spi.write(0xFF);\
+        if(temp==reg_val)\
+            reg_err = false;\
+        else\
+            reg_err=true;\
+    gCS_ADF=1;\
+}
+
+#define initiate {\
     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;\
+    reg_err=true;\
+    for(reg_err_cnt=0;reg_err_cnt<REG_ERR_THRS && reg_err;reg_err_cnt++){\
+        gCS_ADF=0;\
+        spi.write(0x08);\
+        spi.write(0x14);\
+        spi.write(TRANSMIT_LEN_1);\
+        gCS_ADF=1;\
+        reg_check(0x28,0x14,TRANSMIT_LEN_1);\
+    }\
+    if(reg_err)\
+        gPC.printf("Reg_err_ignored %x \r\n",(int)temp);\
+    else\
+        gPC.printf("reg written successful %x \r\n",(int)temp);\
+    reg_err=true;\
+    for(reg_err_cnt=0;reg_err_cnt<REG_ERR_THRS && reg_err;reg_err_cnt++){\
+        gCS_ADF=0;\
+        spi.write(0x08);\
+        spi.write(0x15);\
+        spi.write(TRANSMIT_LEN_2);\
+        gCS_ADF=1;\
+        reg_check(0x28,0x15,TRANSMIT_LEN_2);\
+    }\
+    if(reg_err)\
+        gPC.printf("Reg_err_ignored %x \r\n",(int)temp);\
+    else\
+        gPC.printf("reg written successful %x \r\n",(int)temp);\
+}
+
+#define write_data {\
+    counter++;\
     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);\
+        spi.write(0x0B);\
+        spi.write(0x36);\
+        spi.write(0xFF);\
     gCS_ADF=1;\
-    gCS_ADF=0;\
-    spi.write(0x09);\
-    spi.write(0x37);\
-    spi.write(0xE0);\
+    data_err_cnt=0;\
+    data_err=true;\
+    while(data_err && (data_err_cnt<DATA_ERR_THRS)){\
+        gCS_ADF=0;\
+        if(buffer_state){\
+            spi.write(0x18);\
+            spi.write(0x20);\
+            for(unsigned char i=0; i<112;i++){\
+                if(bypass_adf)\
+                    gPC.putc(buffer_112[i]);\
+                else\
+                    spi.write(buffer_112[i]);\
+                    /*gPC.printf("0x%X,",spi.write(buffer_112[i]));*/\
+            }\
+        }\
+        else{\
+            spi.write(0x18);\
+            spi.write(0x90);\
+            for(unsigned char i=0; i<112;i++){\
+                if(bypass_adf)\
+                    gPC.putc(buffer_112[i]);\
+                else\
+                    spi.write(buffer_112[i]);\
+                /*gPC.printf("0x%X,",spi.write(buffer_112[i]));*/\
+            }\
+        }\
+        gCS_ADF=1;\
+        wait_us(5);\
+        data_err=false;\
+        gCS_ADF=0;\
+        spi.write(0x38);\
+        if(buffer_state){\
+            spi.write(0x20);\
+            }\
+        else{\
+            spi.write(0x90);\
+            }\
+        spi.write(0xFF);\
+        for(unsigned char i=0; i<112;i++){\
+            temp_byte=spi.write(0xFF);\
+            if(buffer_112[i]!=temp_byte){\
+                data_err1[i]=1;\
+                data_err=true;\
+                }\
+        }\
     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();*/\
+    if(data_err==false){\
+    buffer_state = !buffer_state;\
+    }\
+    data_err_cnt++;\
+    if(last_buffer){\
+        finish_write_data = true;\
+           /*gPC.puts("adf_off\r\n");*/\
+        }\
+    }\
+}
+
+#define check {\
+    check_status;\
+    /*gPC.printf("I 0x%X\r\n",(int)status);*/\
+    if(IRQ || bypass_adf){\
+        /*gPC.printf("det\r\n");*/\
+        if(!ADF_off) {\
+            if(finish_write_data) {\
+                write_data;\
+                ADF_off=true;\
+            } else {\
+                write_data;\
+                if(sent_tmfrom_SDcard)\
+                send_tm_from_SD_card_fun();\
+                else snd_tm.transmit_data(buffer_112,&last_buffer);\
+            }\
+        } else {\
+            wait_ms(20);\
+            gCS_ADF=0;\
+            spi.write(0xB1);\
+            gCS_ADF=1;\
+            gPC.puts("transmission done\r\n");\
+            loop_on=false;\
+        }\
+    }\
+    else{\
+        if(T.read_us()>98000){\
+                data_irq_err=true;\
+                CMD(CMD_PHY_ON);\
+                gPC.printf("Data_error_detected");\
+                }\        
+        }\
+}
+
+#define send_data {\
     gCS_ADF=0;\
     spi.write(0xBB);\
     gCS_ADF=1;\
@@ -199,58 +427,7 @@
     spi.write(0xFF);\
     spi.write(0xFF);\
     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++){\
-            if(bypass_adf)\
-                gPC.putc(buffer_112[i]);\
-            else\
-                spi.write(buffer_112[i]);\
-            /*gPC.printf("%02X",buffer_112[i])*/;\
-        }\
-    }\
-    else{\
-        spi.write(0x18);\
-        spi.write(0x90);\
-        for(unsigned char i=0; i<112;i++){\
-            if(bypass_adf)\
-                gPC.putc(buffer_112[i]);\
-            else\
-                spi.write(buffer_112[i]);\
-            /*gPC.printf("%02X",buffer_112[i])*/;\
-        }\
-    }\
-    gCS_ADF=1;\
-    SPI_mutex.unlock();\
-    buffer_state = !buffer_state;\
-    if(last_buffer){\
-        finish_write_data = true;\
-        /*gPC.puts("adf_off\r\n");*/\
-    }\
-}
- 
-/* 
-void check(){   
-    if(IRQ){
-        gCOM_MNG_TMTC_THREAD->signal_set(signal);
-    }
-}*/
-  
- 
-#define send_data {\
-if(sent_tmfrom_SDcard){\
+    if(sent_tmfrom_SDcard){\
         send_tm_from_SD_card_fun();\
     }else{\
         snd_tm.transmit_data(buffer_112,&last_buffer);\
@@ -267,7 +444,6 @@
     }else{\
         snd_tm.transmit_data(buffer_112,&last_buffer);\
     }\
-    SPI_mutex.lock();\
     gCS_ADF=0;\
     spi.write(0xB1);\
     gCS_ADF=1;\
@@ -284,54 +460,114 @@
     spi.write(0xFF);\
     spi.write(0xFF);\
     gCS_ADF=1;\
-    SPI_mutex.unlock();\
-    /*ticker.attach_us(&check,32000);*/\
 }
- 
+  
+#define initial_adf_check {\
+    bool Configuration_Done=false;\
+    quit_configuration=false;\
+    bool reset_flag1 = 0;\
+    bool flag = false;\
+    while((hw_reset_err_cnt<HW_THRS)&&(quit_configuration==false)){\
+        while((bbram_err_cnt<2)&&(quit_configuration==false)){\
+            /*assrt_phy_off;*/\
+            /*Assrt_phy_off Begin*/\
+            {\
+    int state_err_cnt = 0;\
+    CMD(CMD_PHY_OFF);\
+    if(cmd_err_flag){\
+        temp_return=1;\
+        }\
+    else{\
+        for(int i = 0 ; i < 40 ;i++){\
+            CMD(CMD_PHY_OFF);\
+            check_status;\
+            if(status == PHY_OFF){\
+                temp_return = 0;\
+            break;\
+            }\
+            else if(state_err_cnt>THRS){\
+                temp_return = 1;\
+            break;\
+            }\
+            else {\
+                wait_ms(1);\
+            }\
+        }\
+    }\
+}\
+            /*Assrt_phy_off end*/\
+            reset_flag1=temp_return;\
+            if(!reset_flag1){\
+                bbram_write;\
+                while((rbp_err_cnt<RBP_THRS)&&(quit_configuration==false)){\
+                    bbram_check;\
+                    if(bbram_err==0){\
+                        bbram_write_success=true;\
+                        bbram_err_cnt=0;\
+                        initiate;\
+                        if(reg_err){\
+                            rbp_err_cnt++;\
+                            }\
+                        else{\
+                            rbp_err_cnt=0;\
+                            gPC.printf("NO Reg err\r\n");\
+                            CMD(CMD_CONFIG_DEV);\
+                            if(cmd_err_flag){\
+                                reset_flag=1;\
+                                gPC.printf("CMD ERR\r\n");\
+                            }\
+                            else{\
+                                reset_flag=0;\
+                                gPC.printf("NO CMD ERR CONFIG_DONE\r\n");\
+                                quit_configuration=true;\
+                            }\
+                        }\
+                    }\
+                    else{\
+                        bbram_write_success=false;\
+                        bbram_err_cnt++;\
+                        break;\
+                    }\
+                }\
+            }\
+            else{\
+                break;\
+            }\
+        }\
+        if(reset_flag1){\
+            hardware_reset(0);\
+            hw_reset_err_cnt++;\
+            gPC.puts("Resetting hardware\r\n");\
+        }\
+    }\
+    if(hw_reset_err_cnt==HW_THRS){\
+        power_reset_flag=1;\
+        power_reset_count++;\
+        }\
+    else{\
+        Configuration_Done=true;\
+        gPC.printf("Configuration_Done\n");\
+        }\
+}
+
 #define configure_adf {\
-    finish_write_data = false;\
-    buffer_state = true;\
-    last_buffer = false;\
-    loop_on = true;\
-    ADF_off = false;\
+    reset_flags;\
     initial_adf_check;\
-    gPC.puts("initial adf check\r\n");\
-    initiate;\
-    gPC.puts("adf configured\r\n");\
-    /*gLEDR = !gLEDR;*/\
+    gPC.puts("Config_part done\r\n");\
 }
  
 #define transmit_adf {\
     configure_adf;\
-    if(sent_tmfrom_SDcard)\
-    signal = COM_MNG_TMTC_SIGNAL_ADF_SD;\
-    else signal = COM_MNG_TMTC_SIGNAL_ADF_NSD;\
     send_data;\
+    CMD(CMD_PHY_TX);\
+    wait_us(2000);\
     while(loop_on){\
-        wait_ms(COM_TX_TICKER_LIMIT);\
-        if(IRQ || bypass_adf){\
-            if(finish_write_data){\
-                if(ADF_off){\
-                    SPI_mutex.lock();\
-                    gCS_ADF=0;\
-                    spi.write(0xB1);\
-                    gCS_ADF=1;\
-                    SPI_mutex.unlock();\
-                    loop_on = false;\
-                    gPC.puts("Transmission done\r\n");\
-                    gLEDR = 1;\
-                }\
-                else{\
-                    ADF_off = true;\
-                }\
-            }else{\
-                gLEDG = !gLEDG;\
-                write_data;\
-                if(sent_tmfrom_SDcard)\
-                send_tm_from_SD_card_fun();\
-                else snd_tm.transmit_data(buffer_112,&last_buffer);\
-            }\
+        wait_us(20);\
+        check;\
+        if(data_irq_err){\
+        break;\
         }\
     }\
     /*gPC.puts("after while loop\r\n");*/\
-}
\ No newline at end of file
+}
+
--- a/main.cpp	Sat Jul 16 16:04:14 2016 +0000
+++ b/main.cpp	Wed Jul 20 09:58:59 2016 +0000
@@ -35,108 +35,6 @@
 #include "ThreadsAndFunctions.h"
 #include "TEST_PL.h"
 
-#define PL_TC(tm_ptr){\
-    Base_tc *beacon_tc = new Long_tc;\
-    beacon_tc->next_TC = NULL;\
-    PUTshort_or_long(beacon_tc,LONG_TC_CODE);\
-    PUTcrc_pass(beacon_tc,0x1);\
-    PUTexec_status(beacon_tc,0);\
-    beacon_tc->TC_string[0] = 0x01;\
-    beacon_tc->TC_string[1] = 0x60;\
-    beacon_tc->TC_string[2] = 0x81;\
-    beacon_tc->TC_string[3] = 0x21;\
-    beacon_tc->TC_string[4] = 0x00;\
-    beacon_tc->TC_string[5] = 0;\
-    beacon_tc->TC_string[6] = 0;\
-    beacon_tc->TC_string[7] = 0;\
-    beacon_tc->TC_string[8] = 0;\
-    uint16_t crc16 = crc16_gen(beacon_tc->TC_string, 9);\
-    beacon_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
-    beacon_tc->TC_string[10] = (crc16 & 0x00FF);\
-    for(int i = 11; i<135;i++) beacon_tc->TC_string[i] = 0;\
-    tm_ptr =  FCTN_CDMS_RLY_TMTC(beacon_tc);\
-    delete PL_TC;\
-    Base_tm *temp;\
-    temp = tm_ptr;\
-    while(tm_ptr!=NULL)\
-    {\
-        temp = temp->next_TM;\
-        delete tm_ptr;\
-        tm_ptr = temp;\
-    }\
-}
-#define OBSRS_TC(tm_ptr){\
-    Base_tc *beacon_tc = new Short_tc;\
-    beacon_tc->next_TC = NULL;\
-    PUTshort_or_long(beacon_tc,SHORT_TC_CODE);\
-    PUTcrc_pass(beacon_tc,0x1);\
-    PUTexec_status(beacon_tc,0);\
-    beacon_tc->TC_string[0] = 0x01;\
-    beacon_tc->TC_string[1] = 0x20;\
-    beacon_tc->TC_string[2] = 0xf2;\
-    beacon_tc->TC_string[3] = 0x00;\
-    beacon_tc->TC_string[4] = 0x00;\
-    beacon_tc->TC_string[5] = 0;\
-    beacon_tc->TC_string[6] = 2;\
-    beacon_tc->TC_string[7] = 0;\
-    beacon_tc->TC_string[8] = 2;\
-    uint16_t crc16 = crc16_gen(beacon_tc->TC_string, 9);\
-    beacon_tc->TC_string[9]  = (crc16 & 0xFF00)>>8;\
-    beacon_tc->TC_string[10] = (crc16 & 0x00FF);\
-    Base_tm *tm_ptr_head = NULL;\
-                            get_call_sign(tm_ptr_head);\
-                            Base_tm *put_tm_here = NULL;\
-                            /*execute_obsrs(current_TC, put_tm_here)*/\
-                            /*read_TC(current_TC);*/\
-                            execute_OBSRS_TC(beacon_tc);\
-                            gPC.puts("enter_adf\r\n");\
-                            transmit_adf;\
-                            gPC.puts("exit_adf\r\n");\
-}
-
-#define BAE_tc(tm_ptr) {\
-    Base_tc *bae_tc = new  Long_tc;\
-    bae_tc->next_TC = NULL;\
-    PUTshort_or_long(bae_tc,0);\
-    PUTcrc_pass(bae_tc,0x1);\
-    PUTexec_status(bae_tc,0);\
-    bae_tc->TC_string[0] = 0x01;\
-    bae_tc->TC_string[1] = 0x71;\
-    bae_tc->TC_string[2] = 0x81;\
-    bae_tc->TC_string[3] = 0x32;\
-    bae_tc->TC_string[4] = 0;\
-    bae_tc->TC_string[5] = 0;\
-    bae_tc->TC_string[6] = 0;\
-    bae_tc->TC_string[7] = 0;\
-    bae_tc->TC_string[8] = 0;\
-    uint16_t crc16 = crc16_gen(bae_tc->TC_string, 9);\
-    bae_tc->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
-    bae_tc->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
-    for(uint8_t i=11;i<135;i++)\
-        bae_tc->TC_string[i] = 0;\
-    tm_ptr =  FCTN_CDMS_RLY_TMTC(bae_tc);\
-    delete bae_tc;\
-    Base_tm *temp;\
-    temp = tm_ptr;\
-    while(tm_ptr!=NULL)\
-    {\
-        temp = temp->next_TM;\
-        delete tm_ptr;\
-        tm_ptr = temp;\
-    }\
-}
-
-void BAE_TC();
-void BAE_TC()
-{
-    Base_tm *tm_ptr = new Long_tm;     
-    BAE_tc(tm_ptr);    
-}
-
-uint8_t test_srp[]= {
-                    
-                    };
-
 int main()
 {    
     CDMS_I2C_GPIO = 0; 
@@ -149,7 +47,7 @@
     gRX_COUNT = 0;
     RX1M.attach(&rx_read, Serial::RxIrq);
     
-    gPC.baud(9600);//changed for bypassing COM
+    gPC.baud(1200);//changed for bypassing COM
     
     // COMMON SPI
     spi.format(8,0);
@@ -160,12 +58,12 @@
     gCS_RTC = 1;
     gCS_ADF = 1;
     
-    //FCTN_CDMS_INIT_RTC();/* rtc initialization*/
-    //FCTN_CDMS_SD_INIT();/* sd card initialization*/
+    FCTN_CDMS_INIT_RTC();/* rtc initialization*/
+    FCTN_CDMS_SD_INIT();/* sd card initialization*/
     
     
-    //uint8_t test[512] = {0};
-    //disk_write(test,7000); //to be used only just before launch
+    uint8_t test[512] = {0};
+    disk_write(test,7000); //to be used only just before launch
     
     #if DEBUG
     gPC.puts("welcome to mng_tmtc\r\n");
@@ -178,14 +76,14 @@
     // COM_POWER_ON_TX();
     
     // COM_MNG_TMTC THREAD
-    /*gCOM_MNG_TMTC_THREAD = new Thread(COM_MNG_TMTC_FUN);
+    gCOM_MNG_TMTC_THREAD = new Thread(COM_MNG_TMTC_FUN);
     gCOM_MNG_TMTC_THREAD->set_priority(osPriorityHigh);
     
     gHK_THREAD = new Thread(FCTN_CDMS_HK_MAIN);
     gHK_THREAD->set_priority(osPriorityAboveNormal);
     
     gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
-    gSCIENCE_THREAD->set_priority(osPriorityAboveNormal);*/
+    gSCIENCE_THREAD->set_priority(osPriorityAboveNormal);
     
     #if DEBUG
     gPC.puts("completed allocating threads\r\n");
@@ -194,14 +92,14 @@
     master.frequency(400000);
     
     /*HK_counter = new RtosTimer(hk_isr, osTimerPeriodic,(void * )NULL);
-    HK_counter->start(10000);
+    HK_counter->start(10000);*/
     
     sys_reset_cdms_timer = new RtosTimer(sys_pwr_reset, osTimerPeriodic, (void * )NULL);
-    sys_reset_cdms_timer->start(cdms_reset_timeout);*/
+    sys_reset_cdms_timer->start(cdms_reset_timeout);
 
     /*PL_wo_dma = new RtosTimer(payload_isr_fun_dma, osTimerPeriodic,(void * )NULL);
     PL_wo_dma->start(6000);*/
-    //gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
+    gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
     
     /*Timeout flipper;
     flipper.attach(BAE_TC,31);*/