Compression code changed and RLY_TMTC pointers managed

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Files at this revision

API Documentation at this revision

Comitter:
chaithanyarss
Date:
Sat Jul 02 10:48:30 2016 +0000
Parent:
206:fba4aeebf004
Child:
208:d14b1c783736
Commit message:
Except Flash everything else.

Changed in this revision

CDMS_HK.h Show annotated file Show diff for this revision Revisions of this file
CDMS_PL.h Show annotated file Show diff for this revision Revisions of this file
COM_MNG_TMTC.h Show annotated file Show diff for this revision Revisions of this file
Compression.h Show annotated file Show diff for this revision Revisions of this file
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
FMS_all.h Show annotated file Show diff for this revision Revisions of this file
FMS_functions.h Show diff for this revision Revisions of this file
Reset_functions.h Show annotated file Show diff for this revision Revisions of this file
cdms_rtc.h Show annotated file Show diff for this revision Revisions of this file
cdms_sd.h Show annotated file Show diff for this revision Revisions of this file
i2c.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	Thu Jun 30 14:00:33 2016 +0000
+++ b/CDMS_HK.h	Sat Jul 02 10:48:30 2016 +0000
@@ -1,19 +1,23 @@
-#include "cdms_rtc.h"
-#include "pinconfig.h"
-#include "reset_functions.h"
-#include "DefinitionsAndGlobals.h"
-#include "COM_SND_TM_functions.h"
-#include "COM_SND_TM_functions.h"
+
+//CDMS HK
+    #define COMRX_ALIVE 0xFF
+    #define COMRX_DEAD 0x00
+    #define DEVICE_ENABLED 0x00
+    #define DEVICE_POWERED 0x01
+    #define DEVICE_OC_FAULT 0x02
+    #define DEVICE_DISABLED 0x03
 
 void FCTN_CDMS_HK_MAIN();
 void FCTN_CDMS_HK();
 void VERIFY_COMRX();
 void VERIFY_RTC();
 void CDMS_HK_SD();
-void CDMS_HW_FAULTS();
+void HANDLE_HW_FAULTS();
 void HANDLE_HW_FAULT_SD();
 void HANDLE_HW_FAULT_BAE();
 void HANDLE_HW_FAULT_PL();
+void FUNC_CDMS_GPIO_STATUS();
+void minMaxHkData();
 void COLLECT_CDMS_RAM();
 
 Serial hk_cdms(USBTX, USBRX);
@@ -22,26 +26,6 @@
 AnalogIn CDMS_temp_sensor(PIN53);
 AnalogIn COMRX_RSSI_volatge(PIN70);
 
-DigitalIn SD_OC_FAULT (PIN90);
-DigitalOut SD_SW_EN_DS (PIN97);
-DigitalOut BAE_SW_EN_DS (PIN47);
-DigitalOut PL_SW_EN_DS (PIN73);
-
-//GPIO PINS
-DigitalIn V_A_PGOOD (PIN88);
-DigitalIn V_B_PGOOD_1 (PIN6);
-DigitalIn V_B_PGOOD_2 (PIN7);
-DigitalIn V_C_PGOOD (PIN54);
-DigitalIn COMRX_OC_FAULT (PIN68);
-DigitalIn COMTX_OC_FAULT (PIN69);
-DigitalIn BAE_OC_FAULT (PIN92);
-DigitalIn PL_GPIO_1_STATUS (PIN71);
-DigitalIn PL_GPIO_2_STATUS (PIN81);
-DigitalIn PL_GPIO_3_STATUS (PIN80);
-DigitalIn PL_BEE_SW_OC_FAULT (PIN4);
-DigitalIn PL_EPS_LATCH_SW_OC_FAULT ();
-DigitalIn V_C_EN_STATUS ();
-DigitalIn V_D_EN_STATUS ();
 
 DigitalOut SelectLinec3 (PIN79); // MSB of Select Lines
 DigitalOut SelectLinec2 (PIN78);
@@ -51,7 +35,6 @@
 void FCTN_CDMS_HK_MAIN(void const *args)
 {
 
-  uint8_t CDMS_HEALTH_DATA[128] = {0};
   unsigned char CDMS_HK_FRAME[134] = {0};
   char BAE_HK[128] = {0};
   uint8_t convoluted_CDMS_HK[270];
@@ -79,13 +62,13 @@
   }
   CDMS_quant[16]= (uint8_t)RSSI_volatge;
   CDMS_quant[17]= (uint8_t)quant_data.CDMS_temp_quant;
-  
+  minMaxHkData();
   
   uint64_t time = FCTN_CDMS_RD_RTC();             //Reading Time from RTC 
   time = time>>7;
   uint32_t HK_time = (uint32_t)time;
   for(int i = 0;i<4;i++)
-  CDMS_HEALTH_DATA[i] = HK_time[i];
+    CDMS_HEALTH_DATA[i] = HK_time >> i;
 
   // Here: Have to FIT flash data.
   for(int i = 0;i<19;i++)                         //Collecting Data from Temp sensors
@@ -124,9 +107,10 @@
 
 /*---------------------------------- BAE HK --------------------------------------------*/
   
+  
   BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134);
   if(BAE_HK_I2C == 0){
-    TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;
+    TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
     hk_cdms.printf("Bae hk data received");
   }
   else
@@ -135,7 +119,7 @@
       BAE_HK[i] = 0;
   }
   for(int i = 0;i<4;i++)
-  BAE_HK[i] = HK_time[i];
+    BAE_HK[i] = HK_time >> i;
   BAE_HK_FRAME[0] = 0x28;
   BAE_HK_FRAME[1] = FSC_CURRENT[5]+1;
   BAE_HK_FRAME[2] = (FSC_CURRENT[5]+1) >> 8;
@@ -153,6 +137,17 @@
   BAE_HEALTH_FINAL[i] = interleave_BAE_HK[i];
   SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5);
   hk_cdms.printf("BAE hk succesfully completed\r\n");
+  
+  /*----------------------------------Beacon message--------------------------------------*/
+  unsigned char SC_HK_LBM_0[135];
+  SC_HK_LBM_0[0] = 0;       // Sending long beacon msg as telecommand with Packet sequence count 0x00
+  // Add HK bits
+  
+  // Add SC bits
+  crc = crc16_gen(SC_HK_LBM_0,133);
+  SC_HK_LBM_0[132] = crc;
+  SC_HK_LBM_0[133] = crc >> 8;
+  FCTN_I2C_WRITE((char *)SC_HK_LBM_0,135);
 }
 
 int quantiz(float start,float step,float x)
@@ -163,12 +158,12 @@
     return y;
 }
 
-void saveMin(char x,char y)
+char saveMin(char x,char y)
 {
     return (y<x)?y:x;
 }
 
-void saveMax(char x,char y)
+char saveMax(char x,char y)
 {
     return (y>x)?y:x;
 }
@@ -216,7 +211,7 @@
             SelectLinec1=!(SelectLinec1);
         if(Iteration%4==3)
             SelectLinec2=!(SelectLinec2);
-        if(Iteration%8==7)            [0]
+        if(Iteration%8==7)
              SelectLinec3=!(SelectLinec3);
     }
 
@@ -291,7 +286,9 @@
 {
   //COMRX_OC_FAULT //$
   if(PIN68==0 && RSSI_volatge > 0.4)
+  {
     COMRX_STATUS = COMRX_ALIVE;
+  }
   else
   {
     RESET_COMRX();
@@ -305,7 +302,7 @@
 
 void VERIFY_RTC(){
 
-  if(RTC_STATUS = 0x00)
+  if(RTC_STATUS == 0x00)
   {
     SPI_mutex.lock();
     gCS_RTC=1; 
@@ -313,7 +310,7 @@
     spi.write(0x0F);
     if(spi.write(0x00) & 0x04 == 0x04)
     {
-      RTC_STATUS = Not_started;
+      RTC_STATUS = 0x00;
       RESET_RTC();
       RTC_FAULTCOUNT++;
     }
@@ -326,14 +323,14 @@
 {
   HANDLE_HW_FAULT_SD();
   HANDLE_HW_FAULT_BAE();
-  HANDLE_HW_FAULT_PL()
+  HANDLE_HW_FAULT_PL();
 }
 
 void HANDLE_HW_FAULT_SD()
 {
   if(SD_STATUS != DEVICE_DISABLED)
   {
-    if(SD_STATUS = DEVICE_OC_FAULT)
+    if(SD_STATUS == DEVICE_OC_FAULT)
       SD_SW_EN_DS = 0; //powering on SD
 
     if(SD_OC_FAULT == 0) 
@@ -341,7 +338,7 @@
       SD_SW_EN_DS = 1; //switching off SD card
      
       SD_FAULTCOUNT++;
-      SD_STATUS = (SD_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
+      SD_STATUS = (SD_FAULTCOUNT == 3) ? DEVICE_DISABLED :DEVICE_OC_FAULT;
     }
     else
     {
@@ -355,7 +352,7 @@
 {
   if(BAE_STATUS != DEVICE_DISABLED)
   {
-    if(BAE_STATUS = DEVICE_OC_FAULT)
+    if(BAE_STATUS == DEVICE_OC_FAULT)
         BAE_SW_EN_DS = 0; //Power ON BAE
 
     if(BAE_OC_FAULT == 0)  // If OC Fault
@@ -376,7 +373,7 @@
 {
   if(PL_STATUS != DEVICE_DISABLED)
   {
-    if(PL_STATUS = DEVICE_OC_FAULT)
+    if(PL_STATUS == DEVICE_OC_FAULT)
       PL_SW_EN_DS = 0; //Power ON PL
 
     if(PL_BEE_SW_OC_FAULT == 0)  // if OC Fault
@@ -393,10 +390,11 @@
       PL_FAULTCOUNT = 0;
     }
   }
+}
   
 void COLLECT_CDMS_RAM()
 { 
-  /*--------------------Current FSC's---------------------*/\
+  /*--------------------Current FSC's---------------------*/
   for(int i=0;i<4;i++)
     CDMS_HEALTH_DATA[i+43] = FSC_LAST[5] >> (i*8);
   for(int i=0;i<4;i++)
--- a/CDMS_PL.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/CDMS_PL.h	Sat Jul 02 10:48:30 2016 +0000
@@ -2,9 +2,8 @@
 uint8_t pl_main_flag=0;
 uint8_t PL_PREV_STATE=0x00;
 uint8_t POWER_LEVEL = 3;                                //Would be present in HK data extracted 
-extern uint32_t pl_block[192]={0};
+uint32_t pl_block[192]={0};
 uint32_t pl_time;
-uint16_t PL_MAIN_COUNTER=0;
 uint32_t TIME_LATEST_PL=0;
 uint8_t i;
 uint8_t PL_BEE_SW_STATUS=0;
--- a/COM_MNG_TMTC.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/COM_MNG_TMTC.h	Sat Jul 02 10:48:30 2016 +0000
@@ -356,6 +356,7 @@
     {
         case 1: //apid=01 implies it corresponds to bae
         {
+            BAE_I2C_mutex.lock();
             gPC.printf("Telecommand is for BAE\r\n");
             gPC.printf("Sending TC to BAE...\r\n"); //interrupt to be sent to the bae
             y = FCTN_I2C_WRITE((char*)tc_ptr->TC_string,TC_LONG_SIZE);                         // Check for SHORT and LONG TC and then send
@@ -399,6 +400,7 @@
                     return tm_ptr_short;
                 }
             }
+            BAE_I2C_mutex.unlock();
         }
 
         case 2: // APID = 2 corresponds to CDMS
@@ -701,6 +703,13 @@
                             ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                                                        
                         }
+                        else if(GETpid(tc_ptr)==0x38)
+                        {
+                            //gPC.printf()
+                            
+                            CDMS_INTERNAL_RESET();
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                        }
                         else if(GETpid(tc_ptr)==0x40)
                         {                         
                             //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
@@ -740,7 +749,13 @@
                             tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
                             tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
                             tm_pointer->next_TM = NULL;                 
-                            return tm_ptr;                                                     //incomplete
+                            return tm_ptr; */                                                    //incomplete
+                        }
+                        else if(GETpid(tc_ptr)==0xF0)
+                        {
+                            uint8_t statusbits[64];
+                            CDMS_RD_SD_HK(statusbits);
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
                         }                               
                         else 
                         {
@@ -755,7 +770,7 @@
                         ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
                         return tm_ptr_short;
                     }                                    
-                }*/
+                }
                 case 0x70:
                 {
                     gPC.printf("\n\n\rService:PAYLOAD MANAGEMENT SERVICE"); 
--- a/Compression.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/Compression.h	Sat Jul 02 10:48:30 2016 +0000
@@ -1,19 +1,20 @@
-unsigned int read_2byte(unsigned char* ptr){
-    unsigned int output = (unsigned int) *(ptr+1);
-    output += ( (unsigned int)(*ptr) ) << 8;
+uint8_t read_2byte(uint8_t* ptr){
+    uint8_t output = (uint8_t ) *(ptr+1);
+    output += ( (uint8_t )(*ptr) ) << 8;
     return output;
 }
     
-unsigned int read_4byte(unsigned char* ptr){
-    unsigned int output = (unsigned int) *(ptr+3);
-    output += (unsigned int)*(ptr+2)<<8;
-    output += (unsigned int)*(ptr+1)<<16;
-    output += (unsigned int)*(ptr)<<24;
+uint8_t read_4byte(uint8_t* ptr){
+    uint8_t output = (uint8_t ) *(ptr+3);
+    output += (uint8_t )*(ptr+2)<<8;
+    output += (uint8_t )*(ptr+1)<<16;
+    output += (uint8_t )*(ptr)<<24;
     return output;
 }
 
-int adjust(int size, unsigned int data, unsigned char* ptr , int space){
+int adjust(int size, uint8_t data, uint8_t* ptr , int space){
     space = space&0x0f;
+    if(space == 8)  *ptr = 0;
     data = data&((1<<size)-1);
     if(space >= size){
         *ptr += data<<(space-size);
@@ -42,32 +43,45 @@
     }
 }
 
+//--------------------------------------------------------beacon
+struct beacon_packet{
+    unsigned char data[14];
+    beacon_packet *next;
+    int len;
+};
+
+int beacon_cntr = 0;
+bool beacon_flag = true;
+beacon_packet *head_beacon, *tail_beacon;
+//--------------------------------------------------------beacon
+
 
 
 //variable declerations
-unsigned char srp_mode , at , pzf , ezf ,sfp[65] ,scp[55];; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
+uint8_t srp_mode , at , pzf , ezf ,sfp[65] ,scp[55],beacon_at;; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
 unsigned int sfp_bin[52] , scp_bin[52];         //storing the bin values.
 unsigned int sfp_threshold_m0[52] = {8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128,4032,4032,124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,252,252,252,252,252}; 
-const unsigned int scp_threshold_m0[52] = {245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760,114688,114688,4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,4032,4032,4032,4032};
+const int scp_threshold_m0[52] = {245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760,114688,114688,4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,4032,4032,4032,4032};
 unsigned int scp_threshold_m0_1[9]={114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for mode 0..ie callibrated mode
-const unsigned int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
-unsigned char frames[3][134] = {0};
-unsigned char position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
-unsigned char id;   //sf = 0,sc-at = 1,sc-bt = 2;
-unsigned char TM_interleave_data[512] , TM_convoluted_data[270] = {0};
+const int sfp_threshold_m1[2] ={252,8128},scp_threshold_m1[2]={7680,245760};
+uint8_t frames[3][134] = {0};
+uint8_t position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
+uint8_t id;   //sf = 0,sc-at = 1,sc-bt = 2;
+uint8_t TM_interleave_data[512] , TM_convoluted_data[270] = {0};
 int proton_scp_sum,electron_scp_sum,length, temp_crc,attitude = 0,FSC_science = 1,debug_cntr = 0, size,space;   
-unsigned char *ptr ,* pointer;
-long long int sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
+uint8_t *ptr ,* pointer;
+uint64_t sci_time = 0x221000000;       //call some function and get the time or get in the starting, what ever akshay says
 int test_cntr = 0;
+unsigned int beacon_threshold[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472};  //fill it bro
 
     Convolution ConvObj;
-    void convolution (unsigned char * ptr){
+    void convolution (uint8_t * ptr){
         ConvObj.convolutionEncode(ptr, TM_convoluted_data);
         ConvObj.convolutionEncode(ptr + 67, TM_convoluted_data + 135);
     }
 
 //give the pointer of 6 second data to this function
-void srp(unsigned char * head){
+void srp(uint8_t * head){
     //sci_time = FCTN_CDMS_RD_RTC();
     sci_time = 0;
     gPC.puts("enter srp");
@@ -79,6 +93,9 @@
     srp_mode = head[2]&0x1;
     at = 0;     //above threshold is false
     for(int counter = 0 ; counter < 60 ; counter++){
+        //gPC.puts("Y");
+//        gPC.printf("counter = %d\n",counter);
+//        cout<<"counter = "<<counter<<endl;
         pzf = 1;    ezf = 1;
         if(srp_mode == 0){      //calibrated mode
             for(int i=0; i<48 ; i++){
@@ -295,9 +312,10 @@
                 }else if(id == 2){
                     space = adjust(1,0,pointer,8);
                     space = adjust(4,2,pointer,space);
-//                    FSC_science = FCTN_SD_MNGR(2);
+                    FCTN_SD_MNGR();
+                    FSC_science = FSC_CURRENT[2];
                     
-                    //gPC.printf("SID = 2, FSC = %02X\r\n", FSC_science);
+ //                   gPC.printf("SID = 2, FSC = %02X\r\n", FSC_science);
                     frames[id][1] = (FSC_science>>16)&0xff;
                     frames[id][2] = (FSC_science>>8)&0xff;
                     frames[id][3] = FSC_science&0xff;
@@ -305,24 +323,30 @@
                 temp_crc = crc16_gen(frames[id],132);
                 frames[id][132] = temp_crc>>8;
                 frames[id][133] = temp_crc & 0xff;
-                if(test_cntr++ == 0){
-                    gPC.puts("frame start\r\n");
-                    for(int i = 0; i<134; i++){
-                        gPC.printf("%02X",frames[id][i]);
-                    }    
-                    gPC.puts("frame ends\r\n");
-                }
+              //  if(test_cntr++ == 0){
+                    //gPC.puts("frame start\r\n");
+//                    for(int i = 0; i<134; i++){
+//                        gPC.printf("%02X",frames[id][i]);
+//                    }    
+                   // gPC.puts("frame ends\r\n");
+//                }
+                   
+                
                 exor(frames[id]);
                 convolution(frames[id]);
                 interleave(TM_convoluted_data,TM_interleave_data);
                 interleave(TM_convoluted_data+ 135,TM_interleave_data + 144);
-                if(id == 1)
+                
+                if(id == 1){
                     SD_WRITE(TM_interleave_data,FSC_science,3);
-                else if (id == 2)
+                   }
+                else if (id == 2){
                     SD_WRITE(TM_interleave_data,FSC_science,2);
+                  }
                 FSC_science++;      //remove this after testing
                 position_tm_frame[id] = position_tm_starting[id];
                 frames[id][6-id] = (length-1) - j;      // first head pointer.
+                
             }
         }
         
@@ -331,7 +355,7 @@
     //---------------below is scp --------------**************************************************************************************************
     at = 0;     pzf = 1;    ezf = 1;
     srp_mode = head[2]&0x1;
-    unsigned char compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
+    uint8_t compression_option = (head[2]>>1)&0x3;    //--------------------------------------------------------------------------------------
     if(srp_mode ==0){
         if(compression_option == 0){
             for(int i=0; i<52 ;i++){
@@ -340,6 +364,27 @@
                     break;
                 }
             }
+            
+            //---------------------------------------------***********************************************************----------------------------beacon
+            beacon_at = 0;
+            if(scp_bin[44]>beacon_threshold[0])     beacon_at = 1;
+            if(scp_bin[48]>beacon_threshold[1])     beacon_at = 1;
+            if(scp_bin[49]>beacon_threshold[2])     beacon_at = 1;
+            if(scp_bin[50]>beacon_threshold[3])     beacon_at = 1;
+            if(scp_bin[51]>beacon_threshold[4])     beacon_at = 1;
+            if(scp_bin[17]>beacon_threshold[5])     beacon_at = 1;
+            if(scp_bin[23]>beacon_threshold[6])     beacon_at = 1;
+            proton_scp_sum = 0; electron_scp_sum = 0;
+            for(int i=0;i<17;i++){
+                proton_scp_sum += scp_bin[i];
+            }
+            for(int i=0; i<5; i++){
+                electron_scp_sum += scp_bin[18+i];
+            }
+            if(proton_scp_sum > beacon_threshold[7]) beacon_at =1;
+            if(electron_scp_sum > beacon_threshold[8]) beacon_at =1;
+            //--------------------------------------------*******************************************************************----------------------beacon
+            
         }
         else if(compression_option == 1){
             if(scp_bin[44] > scp_threshold_m0_1[0]) at=1;
@@ -386,10 +431,80 @@
             ezf = 0;
     }
     
+        //------------------------------------------------------------------------beacon
+//  struct beacon_packet{
+//  unsigned char data[12];
+//  beacon_packet *next;
+//  };
+//
+//  int beacon_cntr = 0;
+//  bool beacon_flag = true;
+//  beacon_packet *head_beacon, *tail_beacon;
+
+    if(beacon_cntr == 0){
+        head_beacon = tail_beacon = new beacon_packet;
+        tail_beacon->next = NULL;
+    }
+    else{
+        tail_beacon->next = new beacon_packet;
+        tail_beacon = tail_beacon->next;
+        tail_beacon->next = NULL;
+    }
+    pointer = tail_beacon->data;        debug_cntr = 0;     space = 8;
+    space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(5,(sci_time>>8)&0x1f,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(8, sci_time,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+    space = adjust(4, (attitude)&0xf,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+    if(beacon_at == 0){
+        tail_beacon->len = 12;
+        space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(6, compress(sfp_bin[44],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[48],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[49],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[50],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(7, compress(sfp_bin[51],4,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(6, compress(sfp_bin[17],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(6, compress(sfp_bin[23],3,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(proton_scp_sum ,10,2)>>8,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(8, compress(proton_scp_sum ,10,2),pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(electron_scp_sum ,10,2)>>8,pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(8, compress(electron_scp_sum ,10,2),pointer,space);      pointer += space>>4;    debug_cntr += space>>4;
+//      cout<<"for beacon space = "<<(space&0x0f)<<" counter = "<<debug_cntr;
+        for(int temp_i = 12; temp_i<14; temp_i++){
+            *pointer = 0;
+            pointer = pointer + 1;
+        }
+    }
+    else{
+        tail_beacon->len = 14;
+        space = adjust(1, 0,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(sfp_bin[44],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[44],6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[48],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[48],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[49],9,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[49],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[50],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;                 space = adjust(8, compress(sfp_bin[50],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(4, compress(sfp_bin[51],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;                 space = adjust(8, compress(sfp_bin[51],9,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(sfp_bin[17],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[17],6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(sfp_bin[23],6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;             space = adjust(8, compress(sfp_bin[23],6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(proton_scp_sum ,6,3)>>8,pointer,space);      pointer += space>>4;    debug_cntr += space>>4;         space = adjust(8, compress(proton_scp_sum ,6,3),pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
+        space = adjust(1, compress(electron_scp_sum ,6,3)>>8,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;     space = adjust(8, compress(electron_scp_sum ,6,3),pointer,space);       pointer += space>>4;    debug_cntr += space>>4;
+//        cout<<"for beacon space = "<<(space&0x0f)<<" counter = "<<debug_cntr;
+    }
+    beacon_cntr++;
+    if(beacon_cntr ==6){
+//        cout<<"i deleted it"<<endl;
+        beacon_packet  *temp_bec;
+        temp_bec = head_beacon;
+        head_beacon = head_beacon->next;
+        delete(temp_bec);
+        beacon_cntr--;
+    }
+
+    //------------------------------------------------------------------------beacon
+    
     gPC.puts("scp\r\n");
     
     pointer = scp;      debug_cntr = 0;     space = 8;
-    unsigned char packet_pp = 1;    //where to get this packet present bit----------------------------------------------------------------------------------
+    uint8_t packet_pp = 1;    //where to get this packet present bit----------------------------------------------------------------------------------
     int sfp_at_counter; //----------------------------------------------------------------------------------------------------------------------------------
     space = adjust(1, packet_pp,pointer,space);     pointer += space>>4;    debug_cntr += space>>4;
     space = adjust(5, (sci_time>>8)&0x1f,pointer,space);        pointer += space>>4;    debug_cntr += space>>4;
@@ -545,7 +660,6 @@
             if(ezf == 0)
                 length += 40;
         }
-            
     }else if(srp_mode == 0 && compression_option == 1){     //data conservation mode
         if(at == 0){
             length = 94;
--- a/DefinitionsAndGlobals.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/DefinitionsAndGlobals.h	Sat Jul 02 10:48:30 2016 +0000
@@ -145,14 +145,7 @@
 //RF RELAY
     #define RF_RELAY_CNTRL_TX PTA7
     #define RF_RELAY_CNTRL_BCN PTA12 
-    
-//CDMS HK
-    #define COMRX_ALIVE 0xFF;
-    #define COMRX_DEAD 0x00;
-    #define DEVICE_ENABLED 0x00;
-    #define DEVICE_POWERED 0x01;
-    #define DEVICE_OC_FAULT 0x02;
-    #define DEVICE_DISABLED 0x03;
+
     
 // ****************GLOBAL VARIABLES******************
 // DEBUG
@@ -177,6 +170,9 @@
 DigitalOut gCS_RTC(SPI_CS_RTC);
 Mutex SPI_mutex;
 
+//I2C
+Mutex BAE_I2C_mutex;
+
 //RF Relay
 DigitalOut RF_SW_CNTRL_BCN(RF_RELAY_CNTRL_BCN);
 DigitalOut RF_SW_CNTRL_TX(RF_RELAY_CNTRL_TX);
@@ -220,6 +216,7 @@
 const uint8_t gCALL_SIGN_STRING[TM_SHORT_SIZE] = {0xE0, 0x00, 0x00, 0x00, 0x56, 0x55, 0x32, 0x4E, 0x43, 0x46, 0x00, 0xAC, 0x11};
 
 // CDMS HK
+uint8_t CDMS_STANDBY;
 uint8_t CDMS_HEALTH_DATA[128];
 uint8_t CDMS_RAM[59];
 uint16_t GPIO_STATUS;
@@ -241,9 +238,32 @@
 CDMS_HK_min_max min_max_data;
 bool firstCount=true;
 
+//GPIO PINS
+DigitalIn V_A_PGOOD (PIN88);
+DigitalIn V_B_PGOOD_1 (PIN6);
+DigitalIn V_B_PGOOD_2 (PIN7);
+DigitalIn V_C_PGOOD (PIN54);
+DigitalIn COMRX_OC_FAULT (PIN68);
+DigitalIn COMTX_OC_FAULT (PIN69);
+DigitalIn BAE_OC_FAULT (PIN92);
+DigitalIn PL_GPIO_1_STATUS (PIN71);
+DigitalIn PL_GPIO_2_STATUS (PIN81);
+DigitalIn PL_GPIO_3_STATUS (PIN80);
+DigitalIn PL_BEE_SW_OC_FAULT (PIN4);
+DigitalIn PL_EPS_LATCH_SW_OC_FAULT ();
+DigitalIn V_C_EN_STATUS ();
+DigitalIn V_D_EN_STATUS ();
+
+
+DigitalIn SD_OC_FAULT (PIN90);
+DigitalOut SD_SW_EN_DS (PIN97);
+DigitalOut BAE_SW_EN_DS (PIN47);
+DigitalOut PL_SW_EN_DS (PIN73);
+
 //SD HK
 uint32_t FSC_CURRENT[6];
 uint32_t FSC_LAST[6];
+uint8_t SD_RD_ERROR;
 
 //Time's
 uint32_t TIME_LATEST_RTC;
@@ -259,4 +279,22 @@
 uint16_t CDMS_HK_MAIN_COUNTER;
 uint16_t COMRX_RESET_COUNTER;
 uint16_t PL_MAIN_COUNTER;
-uint16_t PL_RCV_SC_DATA_COUNTER;
\ No newline at end of file
+uint16_t PL_RCV_SC_DATA_COUNTER;
+
+//CDMS RAM parameters
+uint8_t PL_INIT_STATUS;
+uint8_t PL_MAIN_STATUS;
+uint8_t PL_LOW_POWER;
+uint8_t PL_STATE;
+uint8_t PL_RCV_SC_DATA_STATUS;
+uint8_t COM_SESSION;
+uint8_t COM_RX;
+uint8_t RF_SW_STATUS;
+uint8_t COM_TX;
+uint8_t COM_TX_STATUS;
+uint8_t COM_MNG_TMTC;
+uint8_t CDMS_STANDBY_HK;
+uint8_t CDMS_STANDBY_PL;
+uint8_t CDMS_INIT_STATUS;
+uint8_t CDMS_HK_STATUS;
+uint8_t CDMS_RTC_BL;
\ No newline at end of file
--- a/FMS_all.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/FMS_all.h	Sat Jul 02 10:48:30 2016 +0000
@@ -19,33 +19,35 @@
 void RST_BAE();
 void RST_PL_BEE();
 void RST_HK_COUNTER();
+uint8_t CDMS_RD_SD_HK(uint8_t *);
+void CDMS_INTERNAL_RESET();
 
-uint8_t P_PL_INIT()
+void P_PL_INIT()
 {
     
 }
 
-uint8_t P_PL_MAIN()
+void P_PL_MAIN()
 {
     
 }
 
-uint8_t P_COM_INIT()
+void P_COM_INIT()
 {
     
 }
 
-uint8_t P_CDMS_HK_MAIN()
+void P_CDMS_HK_MAIN()
 {
-    FCTN_CDMS_HK_MAIN();
+    //FCTN_CDMS_HK_MAIN();
 }
 
-uint8_t P_PL_RCV_SC_DATA()
+void P_PL_RCV_SC_DATA()
 {
-    
+
 }
 
-uint8_t P_CDMS_INIT_SD()
+void P_CDMS_INIT_SD()
 {
     FCTN_CDMS_SD_INIT();
 }
@@ -62,22 +64,22 @@
 
 void SW_ON_BAE()
 {
-
+    BAE_SW_EN_DS = 0; //Power ON BAE
 }
 
 void SW_OFF_BAE()
 {
-
+    BAE_SW_EN_DS = 1; //Switch OFF BAE
 }
 
 void SW_ON_PL_BEE()
 {
-
+    PL_SW_EN_DS = 0; //Power ON PL
 }
 
 void SW_OFF_PL_BEE()
 {
-    
+      PL_SW_EN_DS = 1;  // switching OFF PL    
 }
 
 void SW_ON_PL_EPS()
@@ -92,7 +94,7 @@
 
 void SW_ON_V_A_EN()
 {
-
+    
 }
 
 void SW_OFF_V_A_EN()
@@ -102,20 +104,53 @@
 
 void RST_SD()
 {
-    
+    SD_SW_EN_DS = 1; //switching off SD card
+    wait_ms(10);
+    SD_SW_EN_DS = 0;
+    FCTN_CDMS_SD_INIT();
 }
 
 void RST_BAE()
 {
-    
+    BAE_SW_EN_DS = 1; //Switch OFF BAE
+    wait_ms(10);
+    BAE_SW_EN_DS = 0; //Switch ON BAE
 }
 
 void RST_PL_BEE()
 {
-    
+    PL_SW_EN_DS = 1;  // switching OFF PL
+    wait_ms(10);
+    PL_SW_EN_DS = 0;  // Switch ON PL
 }
 
 void RST_HK_COUNTER()
 {
+    firstCount = true;
+}
+
+uint8_t CDMS_RD_SD_HK(uint8_t *sd_statusbits)
+{
+    uint8_t p;
+    uint8_t ACK;
+    if(SD_SW_EN_DS != DEVICE_POWERED)
+    {
+        ACK = 0x89;
+        SD_RD_ERROR = 1;
+        //Generate TM
+    }
+    else
+    {
+        p = disk_read_statusbits(sd_statusbits);
+        if(p == 0)
+            ACK = 0xA0;
+        else
+            ACK = 0x89;
+    }
+    return ACK;
+}
+
+void CDMS_INTERNAL_RESET()
+{
     
-}
+}
\ No newline at end of file
--- a/FMS_functions.h	Thu Jun 30 14:00:33 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,187 +0,0 @@
-#include "CDMS_HK.h"
-
-uint8_t ACK_CODE;
-
-void CDMS_standby_prcs();
-void CDMS_run_prcs();
-void CDMS_rst_pwr();
-void CDMS_swch_pwr_on();
-void CDMS_swch_pwr_off();
-void CDMS_rst_hk_counter();
-void CDMS_rd_rtc();
-void CDMS_mng_rtc();
-void CDMS_rd_sd_hk();
-
-extern uint8_t PL_STANDBY_STATE;
-extern bool STANDBY_PL;
-extern uint8_t CDMS_HK_STANDBY_STATE;
-extern bool STANDBY_CDMS_HK;
-
-extern bool SD_RD_ERROR;
-
-extern uint8_t BAE_STATUS;
-extern uint8_t PL_STATUS;
-extern uint8_t SD_STATUS;
-
-// These are defined in CDMS_HK.h Please check
-#define DEVICE_ENABLED 0x00;
-#define DEVICE_POWERED 0x01;
-#define DEVICE_OC_FAULT 0x02;
-#define DEVICE_DISABLED 0x03;
-DigitalOut SD_SW_EN_DS (PIN97);
-DigitalOut BAE_SW_EN_DS (PIN47);
-
-
-void CDMS_standby_prcs()    // FID = 0x40
-{
-    if(PL_STANDBY_STATE = 0x00)
-        STANDBY_PL = 0;
-    if(PL_STANDBY_STATE = 0x01)
-        STANDBY_PL = 1;
-    if(CDMS_HK_STANDBY_STATE = 0x00)
-        STANDBY_CDMS_HK = 0;
-    if(CDMS_HK_STANDBY_STATE = 0x01)
-        STANDBY_CDMS_HK = 1;
-    if(RCV_SC_STANDBY_STATE = 0x00)
-        RCV_SC = 0;
-    if(RCV_SC_STANDBY_STATE = 0x01)
-        RCV_SC = 1;
-    ACK_CODE = 0xC0;
-    // Generate the TM
-}
-
-void CDMS_run_prcs()    // FID = 0x0_
-{
-    
-}
-
-void CDMS_rst_pwr(uint8_t FID)      // FID = 0x3_
-{
-    switch(FID){
-        case 0x31 : //power reset SD
-            SD_SW_EN_DS = 0; //switching OFF
-            wait_ms(10);
-            SD_SW_EN_DS = 1; //switching ON
-            ACK_CODE = 0xA0;
-            // Generate TM
-    }
-}
-
-void CDMS_swch_pwr_on(uint8_t FID)      // FID = 0x1_
-{
-    if(FID = 0x14)
-    {
-        PL_STATUS = DEVICE_ENABLED;
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-    else if(FID = 0x13)
-    {
-        BAE_STATUS = DEVICE_POWERED;
-        BAE_SW_EN_DS = 1; //Power ON BAE
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-    else if(FID = 0x11)
-    {
-        SD_STATUS = DEVICE_POWERED;
-        SD_SW_EN_DS = 1; //Power ON SD
-        FCTN_CDMS_SD_INIT();
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-    else
-    {
-        ACK_CODE = 0x02;
-        // Generate TM
-    }
-}
-
-void CDMS_swch_pwr_off(uint8_t FID) // FID = 0x2_
-{
-        if(FID = 0x24)
-    {
-        PL_SW_EN_DS = 0; //switching OFF PL
-        PL_STATUS = DEVICE_DISABLED;
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-    else if(FID = 0x23)
-    {
-        BAE_SW_EN_DS = 0; //Switching OFF BAE
-        BAE_STATUS = DEVICE_DISABLED;
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-    else if(FID = 0x21)
-    {
-        SD_SW_EN_DS = 0; //Switching OFF SD
-        SD_STATUS = DEVICE_DISABLED;
-        FCTN_CDMS_SD_INIT();
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-    else
-    {
-        ACK_CODE = 0x02;
-        // Generate TM
-    }
-}
-
-void CDMS_rst_hk_counter()      // FID = 0x41
-{
-    
-}
-
-void CDMS_rd_rtc()      // FID = 0xF1
-{
-    uint8_t time = FCTN_CDMS_RD_RTC();
-    // Check if time is read properly
-    if( /*if error in reading*/ )
-    {
-        ACK_CODE = 0x88;
-        // Generate TM
-    }
-    else if( /*if Time is read properly*/ )
-    {
-        ACK_CODE = 0xA0;
-        // Generate TM
-    }
-}
-
-void CDMS_mng_rtc(uint64_t time)
-{
-    
-}
-
-void CDMS_rd_sd_hk()
-{
-    uint8_t sd_statusbits[64];
-    uint8_t *p;
-    int i;
-    if(SD_SW_EN_DS != DEVICE_POWERED)
-    {
-        ACK_CODE = 0x89;
-        SD_RD_ERROR = 1;
-        //Generate TM
-    }
-    else
-    {
-        p = disk_read_statusbits();
-        if(p != NULL)
-        {
-            for(i = 0; i<64; i++)
-                sd_statusbits[i] = *(p + i);
-            SD_RD_ERROR = 0;
-            ACK_CODE = 0xA0;
-            // Generate TM
-        }
-        else
-        {
-            ACK_CODE = 0x89;
-            SD_RD_ERROR = 1;
-            //Generate TM
-        }
-    }
-}
-
--- a/Reset_functions.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/Reset_functions.h	Sat Jul 02 10:48:30 2016 +0000
@@ -1,4 +1,7 @@
-DigitalOut pwr_comrx (PIN72)
+DigitalOut pwr_comrx (PIN72);
+
+void RESET_COMRX();
+void RESET_RTC();
 
 void RESET_COMRX()
 {
--- a/cdms_rtc.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/cdms_rtc.h	Sat Jul 02 10:48:30 2016 +0000
@@ -26,12 +26,12 @@
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x81); //register address with write flag
-    spi.write(0x80);//enabling stop bit in the seconds register
+    spi.write(0x80); //enabling stop bit in the seconds register
 
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x81);
-    spi.write(0x00);//disabling the stop bit to restart the oscillator
+    spi.write(0x00); //disabling the stop bit to restart the oscillator
 
 
     gCS_RTC=1;
@@ -87,7 +87,7 @@
     gCS_RTC=0;
     spi.write(0x0F);
     response = (spi.write(0x00))&0x04;
-
+    
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x00);                //reading centiseconds register
@@ -129,4 +129,5 @@
 
     SPI_mutex.unlock();
     return time;
-}
\ No newline at end of file
+}
+
--- a/cdms_sd.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/cdms_sd.h	Sat Jul 02 10:48:30 2016 +0000
@@ -1,4 +1,3 @@
-#include "cdms_rtc.h"
 //SPI spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
 //DigitalOut cs_sd(PTE22);
 
@@ -42,6 +41,7 @@
 int disk_write(const uint8_t *, uint64_t);
 int disk_read(uint8_t *, uint64_t);
 int disk_erase(int,int);
+int disk_read_statusbits(uint8_t *);
 
 void FCTN_SD_MNG();
 int INCREMENT_SD_LIB(uint8_t);
@@ -61,7 +61,6 @@
 #define SDCARD_V1   1
 #define SDCARD_V2   2
 #define SDCARD_V2HC 3
-DigitalOut SD_SW_EN_DS (PIN97);
 
 int cdv;
 uint64_t sd_sectors();
@@ -131,6 +130,7 @@
     {
         fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
         start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
+        fsc++;
         buffer[0]=(uint8_t) (fsc>>24 & 0xFF);
         buffer[1]=(uint8_t) (fsc>>16 & 0xFF);
         buffer[2]=(uint8_t) (fsc>>8 & 0xFF);
@@ -143,12 +143,20 @@
             buffer[6]=(uint8_t) (start_fsc>>8 & 0xFF);
             buffer[7]=(uint8_t) (start_fsc & 0xFF);
         }
-        return disk_write(buffer,SD_MNG_SECT);
+        
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[1] = fsc;
+            FSC_LAST[1] = start_fsc;
+            return i;
+        } 
     }
     if(sid==0x02)
     {
         fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
         start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
+        fsc++;
         buffer[8]=(uint8_t) (fsc>>24 & 0xFF);
         buffer[9]=(uint8_t) (fsc>>16 & 0xFF);
         buffer[10]=(uint8_t) (fsc>>8 & 0xFF);
@@ -161,12 +169,19 @@
             buffer[14]=(uint8_t) (start_fsc>>8 & 0xFF);
             buffer[15]=(uint8_t) (start_fsc & 0xFF);
         }
-        return disk_write(buffer,SD_MNG_SECT);
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[2] = fsc;
+            FSC_LAST[2] = start_fsc;
+            return i;
+        }
     }
     if(sid==0x03)
     {
         fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
         start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
+        fsc++;
         buffer[16]=(uint8_t) (fsc>>24 & 0xFF);
         buffer[17]=(uint8_t) (fsc>>16 & 0xFF);
         buffer[18]=(uint8_t) (fsc>>8 & 0xFF);
@@ -179,12 +194,19 @@
             buffer[22]=(uint8_t) (start_fsc>>8 & 0xFF);
             buffer[23]=(uint8_t) (start_fsc & 0xFF);
         }
-        return disk_write(buffer,SD_MNG_SECT);
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[3] = fsc;
+            FSC_LAST[3] = start_fsc;
+            return i;
+        }
     }
      if(sid==0x04)
     {
         fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27];
         start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31];
+        fsc++;
         buffer[24]=(uint8_t) (fsc>>24 & 0xFF);
         buffer[25]=(uint8_t) (fsc>>16 & 0xFF);
         buffer[26]=(uint8_t) (fsc>>8 & 0xFF);
@@ -197,12 +219,19 @@
             buffer[30]=(uint8_t) (start_fsc>>8 & 0xFF);
             buffer[31]=(uint8_t) (start_fsc & 0xFF);
         }
-        return disk_write(buffer,SD_MNG_SECT);
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[4] = fsc;
+            FSC_LAST[4] = start_fsc;
+            return i;
+        }
     }
      if(sid==0x05)
     {
         fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35];
         start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39];
+        fsc++;
         buffer[32]=(uint8_t) (fsc>>24 & 0xFF);
         buffer[33]=(uint8_t) (fsc>>16 & 0xFF);
         buffer[34]=(uint8_t) (fsc>>8 & 0xFF);
@@ -215,7 +244,13 @@
             buffer[38]=(uint8_t) (start_fsc>>8 & 0xFF);
             buffer[39]=(uint8_t) (start_fsc & 0xFF);
         }
-        return disk_write(buffer,SD_MNG_SECT);
+        i = disk_write(buffer,SD_MNG_SECT);
+        if(i == 0)
+        {
+            FSC_CURRENT[5] = fsc;
+            FSC_LAST[5] = start_fsc;
+            return i;
+        }
     }
     return -1;
 }
@@ -653,6 +688,7 @@
 {
     // set read address for single block (CMD17)
     if (cmd(17, block_number * cdv) != 0) {
+        SD_RD_ERROR = 1;
         return 1;
     }
 
@@ -695,4 +731,15 @@
     }
     
     return 0; //normal return
+}
+
+int disk_read_statusbits(uint8_t *buffer)
+{
+    if (cmd(17, 0) != 0) {
+          SD_RD_ERROR = 1;
+        return -1;
+    }
+
+    // receive the data
+    return read(buffer,64);
 }
\ No newline at end of file
--- a/i2c.h	Thu Jun 30 14:00:33 2016 +0000
+++ b/i2c.h	Sat Jul 02 10:48:30 2016 +0000
@@ -28,7 +28,7 @@
     PORTE->PCR[0] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
     Thread::wait(1);                //Wait for all I2C registers to be updates to their their values
 }
-bool FCTN_I2C_READ_PL(char *data,int length)
+bool FCTN_I2C_READ_PL(char *data,int length)       // Returns 0 for success
 {    
     PL_I2C_GPIO = 1;
     read_ack = master.read(addr_pl|1,data,length);
@@ -62,7 +62,7 @@
     return read_ack;
     
 }
-bool FCTN_I2C_WRITE_PL(char *data2,uint8_t tc_len2)
+bool FCTN_I2C_WRITE_PL(char *data2,uint8_t tc_len2)       // Returns 0 for success
 {
     write_ack = master.write(addr_pl|0x00,data2,tc_len2);//address to be defined in payload    
     Thread::wait(1);    //As per the tests Thread::wait is not required on master side but its safe to give 1ms
@@ -93,8 +93,8 @@
     i2c_count = 0;
     return write_ack;
 }
-bool FCTN_I2C_READ(char *data,int length)
-{    
+bool FCTN_I2C_READ(char *data,int length)       // Returns 0 for success
+{   
     CDMS_I2C_GPIO = 1;
     read_ack = master.read(addr_bae|1,data,length);
     Thread::wait(1);        //as per tests Thread::wait not required on master side. But its safe to give 1ms 
@@ -127,7 +127,7 @@
     return read_ack;
 }
 
-bool FCTN_I2C_WRITE(char *data,int tc_len2)
+bool FCTN_I2C_WRITE(char *data,int tc_len2)       // Returns 0 for success
 {    
     CDMS_I2C_GPIO = 1;
     write_ack = master.write(addr_bae|0x00,data,tc_len2);    
--- a/main.cpp	Thu Jun 30 14:00:33 2016 +0000
+++ b/main.cpp	Sat Jul 02 10:48:30 2016 +0000
@@ -9,9 +9,10 @@
 #include "rtos.h"
 #include "mbed_debug.h"
 
+#include "pinconfig.h"
 #include "Structures.h"
-#include "pinconfig.h"
 #include "DefinitionsAndGlobals.h"
+#include "cdms_rtc.h"
 void rx_read();
 #include "crc.h"
 #include "i2c.h"
@@ -19,16 +20,20 @@
 #include "COM_SND_TM.h"
 #include "cdms_sd.h"
 #include "common_functions.h"
+#include "Reset_functions.h"
 #include "CDMS_HK.h"
 #include "OBSRS.h"
 #include "adf.h"
 #include "COM_RCV_TC.h"
+#include "CDMS_PL.h"
+#include "FMS_all.h"
 #include "COM_MNG_TMTC.h"
 #include "COM_POWER_OFF_TX.h"
 #include "COM_POWER_ON_TX.h"
 #include "Compression.h"
 #include "ThreadsAndFunctions.h"
 
+
 //void set_sig(){gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);}
 int main()
 {
@@ -70,7 +75,7 @@
     gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
    // gPC.puts("step one complete\r\n");
     gSCIENCE_THREAD->set_priority(osPriorityBelowNormal);
-    gHK_THREAD = new Thread(HK_);
+    //gHK_THREAD = new Thread(HK_);
     if (DEBUG)
         gPC.puts("competed allocating threads\r\n");