Repository for CDMS code

Dependencies:   SimpleDMA mbed-rtos mbed eeprom

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Files at this revision

API Documentation at this revision

Comitter:
chaithanyarss
Date:
Wed Jun 29 13:59:21 2016 +0000
Parent:
180:ccc21cc163f8
Child:
188:5f75962ae7ea
Commit message:
SD card finalized code

Changed in this revision

CDMS_HK.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
COM_POWER_ON_TX.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
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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CDMS_HK.h	Wed May 25 15:28:32 2016 +0000
+++ b/CDMS_HK.h	Wed Jun 29 13:59:21 2016 +0000
@@ -1,17 +1,46 @@
-//#include "cdms_rtc.h"
+#include "cdms_rtc.h"
 #include "pinconfig.h"
+#include "reset_functions.h"
+#include "DefinitionsAndGlobals.h"
+#include "COM_SND_TM_functions.h"
 
 void FCTN_CDMS_HK_MAIN();
 void FCTN_CDMS_HK();
-//extern uint8_t CDMS_HK_STATUS;
-//extern uint8_t Power_level;
-//extern uint8_t Att_level;
-//extern char CDMS_HK[8];
+void VERIFY_COMRX();
+void VERIFY_RTC();
+void CDMS_HK_SD();
+void CDMS_HW_FAULTS();
+void HANDLE_HW_FAULT_SD();
+void HANDLE_HW_FAULT_BAE();
+void HANDLE_HW_FAULT_PL();
+void COLLECT_CDMS_RAM();
 
 Serial hk_cdms(USBTX, USBRX);
 
-AnalogIn TempInput(PIN27); // Input from Current Multiplexer
+AnalogIn TempInput(PIN27);          // Input from Current Multiplexer
 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);
@@ -20,75 +49,61 @@
 
 void FCTN_CDMS_HK_MAIN(void const *args)
 {
-//    CDMS_HK_STATUS=(CDMS_HK_STATUS)|(HK_MAIN_STATUS);
-    FCTN_CDMS_HK;
-    hk_cdms.printf(" \r\nCDMS HK entered Verify COM RX RSSI >0.4 \r\n");
-   
-    int HK_I2C;
-    char BAE_HK[74];
-    FCTN_I2C_READ(BAE_HK,74);
-    printf("BAE_HK=%s",BAE_HK);
-    /*if(HK_I2C==0)
-    {
-        if(Power_level!=0)
-        {
-            printf("Update Power_level to 0 in Flash");
-        }
-        Att_level=0;
-        CDMS_HK_STATUS=(CDMS_HK_STATUS)|(HK_BAE_ERR_I2C);
-        CDMS_HK_STATUS=(CDMS_HK_STATUS)&(~(HK_MAIN_STATUS));
-    }**/
-        uint8_t CDMS_quant[17];
-        // printf("BAE HK is %s",BAE_HK);
-        for(int i=0;i<16;i++)
-        {
-           CDMS_quant[i]= (uint8_t)quant_data.temp_quant[i];
-        }
-        CDMS_quant[16]= (uint8_t)quant_data.CDMS_temp_quant;
-        uint64_t time=0x00;//replace with rtc function
-        char tree[61];
-        hk_cdms.printf("i am done\r\n");
-        uint8_t data[512];
-        for(int i=0;i<512;i++)
-        {
-            data[i]=0x00;
-        }
-        tree[0]=(char)(time>>(56))&(0xFF);
-        tree[1]=(char)(time>>(48))&(0xFF);
-        tree[2]=(char)(time>>(40))&(0xFF);
-        tree[3]=(char)(time>>(32))&(0xFF);
-        tree[4]=(char)(time>>(24))&(0xFF);
-        tree[5]=(char)(time>>(16))&(0xFF);
-        tree[6]=(char)(time>>(8))&(0xFF);
-        tree[7]=(char)(time)&(0xFF);
-        for(int i=0;i<73;i++)
-        {
-            tree[i+8]=BAE_HK[i];
-        }
-        printf("Hope u r alive \r\n");
-        for(int i=0;i<81;i++)
-        {
-            data[i]=(uint8_t)tree[i];
-        }
-        for(int i=81;i<98;i++)
-        {
-            data[i]=CDMS_quant[i];
-        }
-        for(int i=0;i<512;i++)
-        {
-            printf("%d",data[i]);
-        }
-        //uint8_t fsc=FCTN_SD_MNGR(0x3);
-        //printf("FSC where SD is written is %d\r\n",fsc);
-        //int a=SD_WRITE(data,0x00000012,0x3);
-        //printf("Result of writing is %d \r\n",a);
-        //BCN long frame ???
-        //CDMS_HK_STATUS=(CDMS_HK_STATUS)&(~(HK_MAIN_STATUS));
-        hk_cdms.printf("CDMS executed succesfully\r\n");
+  CDMS_HK_MAIN_STATUS = 0x01;
+  CDMS_HK_MAIN_COUNTER++;
+
+  FCTN_CDMS_HK();
+  RSSI_volatge = COMRX_RSSI_volatge.read() * 3.3;
+  VERIFY_COMRX();
+  VERIFY_RTC();
+  HANDLE_HW_FAULTS();
+  FUNC_CDMS_GPIO_STATUS(); //yet to be done
+  COLLECT_CDMS_RAM();
+
+  uint8_t CDMS_quant[19];
+  for(int i=0;i<16;i++)
+  {
+     CDMS_quant[i]= (uint8_t)quant_data.temp_quant[i];
+  }
+  CDMS_quant[16]= (uint8_t)RSSI_volatge;
+  CDMS_quant[17]= (uint8_t)quant_data.CDMS_temp_quant;
+  
+
+  char BAE_HK[74];
+  BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,74);
+  if(BAE_HK_I2C == 0)
+    hk_cdms.printf("Bae hk data received");
+  else
+  {
+    hk_cdms.printf("Bae hk data NOT received");
+    for(int i = 0;i<74;i++)
+    BAE_HK = 0;
+  }
+  
+  for(int i=0;i<512;i++)
+  {
+      CDMS_HEALTH_DATA[i]=0x00;
+  }
+  
+  uint64_t time = FCTN_CDMS_RD_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];
+  // Here: Have to FIT flash data.
+  for(int i = 0;i<19;i++)
+  CDMS_HEALTH_DATA[i+24] = CDMS_quant[i];
+  
+  
+  for(int i=0;i<512;i++)
+  {
+      printf("%d",CDMS_HEALTH_DATA[i]);
+  }
+  CDMS_HK_FSC = FCTN_SD_MNGR(0x04);
+  BAE_HK_FSC = FCTN_SD_MNGR(0x05);
+  hk_cdms.printf("CDMS executed succesfully\r\n");
 }
 
-
-
 int quantiz(float start,float step,float x)
 {
     int y=(x-start)/step;
@@ -97,24 +112,18 @@
     return y;
 }
 
-
-
-void saveMin(char x,char y){
-    if(y<x){
-        x=y;
-    }
-
+void saveMin(char x,char y)
+{
+    return (y<x)?y:x;
 }
-void saveMax(char x,char y){
-    if (y>x)
-    {
-       x=y;
-    }
+
+void saveMax(char x,char y)
+{
+    return (y>x)?y:x;
 }
-void minMaxHkData(){
-   
-  
-   
+
+void minMaxHkData()
+{  
     if(firstCount==true){
         for (int i = 0; i < 16; ++i){   
         min_max_data.temp_min[i] = quant_data.temp_quant[i];
@@ -123,20 +132,16 @@
 
         min_max_data.CDMS_temp_min=quant_data.CDMS_temp_quant;
         min_max_data.CDMS_temp_max=quant_data.CDMS_temp_quant;
-   
-      
     }
     else {
         for (int i = 0; i < 16; ++i)
         {
-            saveMin(min_max_data.temp_min[i],quant_data.temp_quant[i]);
-            saveMax(min_max_data.temp_max[i],quant_data.temp_quant[i]);
+          min_max_data.temp_min[i] = saveMin(min_max_data.temp_min[i],quant_data.temp_quant[i]);
+          min_max_data.temp_max[i] = saveMax(min_max_data.temp_max[i],quant_data.temp_quant[i]);
         }
                
-        saveMin(min_max_data.CDMS_temp_min,quant_data.CDMS_temp_quant);
-        saveMax(min_max_data.CDMS_temp_max,quant_data.CDMS_temp_quant);
-           
-       
+        min_max_data.CDMS_temp_min = saveMin(min_max_data.CDMS_temp_min,quant_data.CDMS_temp_quant);
+        min_max_data.CDMS_temp_max = saveMax(min_max_data.CDMS_temp_max,quant_data.CDMS_temp_quant);
     }  
     firstCount=false;
 }
@@ -160,11 +165,11 @@
             SelectLinec1=!(SelectLinec1);
         if(Iteration%4==3)
             SelectLinec2=!(SelectLinec2);
-        if(Iteration%8==7)
+        if(Iteration%8==7)            [0]
              SelectLinec3=!(SelectLinec3);
     }
 
-actual_data.CDMS_temp_actual=(-90.7*3.3*CDMS_temp_sensor.read())+190.1543;
+    actual_data.CDMS_temp_actual=(-90.7*3.3*CDMS_temp_sensor.read())+190.1543;
    
     for(Iteration=0;Iteration<16;Iteration++){
 
@@ -200,4 +205,203 @@
     quant_data.CDMS_temp_quant=quantiz(tstart,tstep,actual_data.CDMS_temp_actual);
 
     minMaxHkData();
+}
+
+void FUNC_CDMS_GPIO_STATUS()       //Polls the status of Input GPIO PINS
+{
+  //V_A_PGOOD //TRZ EN
+  GPIO_STATUS=(V_A_PGOOD)?(GPIO_STATUS)||((uint16_t)(0x1<<15)):(GPIO_STATUS)&(~((uint16_t)(0x1<<15)));
+  //V_B_PGOOD_1 //3V3BPGOOD //$
+  GPIO_STATUS=(V_B_PGOOD_1)?(GPIO_STATUS)||((uint16_t)(0x1<<14)):(GPIO_STATUS)&(~((uint16_t)(0x1<<14)));
+  //V_B_PGOOD_2 //3V3BEN //$
+  GPIO_STATUS=(V_B_PGOOD_2)?(GPIO_STATUS)||((uint16_t)(0x1<<13)):(GPIO_STATUS)&(~((uint16_t)(0x1<<13)));
+  //V_C_PGOOD //3V3CPGOOD //$
+  GPIO_STATUS=(V_C_PGOOD)?(GPIO_STATUS)||((uint16_t)(0x1<<12)):(GPIO_STATUS)&(~((uint16_t)(0x1<<12)));
+  //COMRX_OC_FAULT //$
+  GPIO_STATUS=(COMRX_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<11)):(GPIO_STATUS)&(~((uint16_t)(0x1<<11)));
+  // COMTX_OC_FAULT //$
+  GPIO_STATUS=(COMTX_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<10)):(GPIO_STATUS)&(~((uint16_t)(0x1<<10)));
+  //BAE_OC_FAULT //$
+  GPIO_STATUS=(BAE_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<9)):(GPIO_STATUS)&(~((uint16_t)(0x1<<9)));
+  //PL_GPIO_1_STATUS //$
+  GPIO_STATUS=(PL_GPIO_1_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<8)):(GPIO_STATUS)&(~((uint16_t)(0x1<<8)));
+  //PL_GPIO_2_STATUS //$
+  GPIO_STATUS=(PL_GPIO_2_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<7)):(GPIO_STATUS)&(~((uint16_t)(0x1<<7)));
+  //PL_GPIO_3_STATUS //$
+  GPIO_STATUS=(PL_GPIO_3_STATUS)?(GPIO_STATUS)||((uint16_t)(0x1<<6)):(GPIO_STATUS)&(~((uint16_t)(0x1<<6)));
+  //PL_BEE_SW_OC_FAULT //to be verified
+  GPIO_STATUS=(PL_BEE_SW_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<5)):(GPIO_STATUS)&(~((uint16_t)(0x1<<5)));
+  //PL_EPS_LATCH_SW_OC_FAULT // to be verified
+  GPIO_STATUS=(PL_EPS_LATCH_SW_OC_FAULT)?(GPIO_STATUS)||((uint16_t)(0x1<<4)):(GPIO_STATUS)&(~((uint16_t)(0x1<<4)));
+
+}
+
+void VERIFY_COMRX()
+{
+  //COMRX_OC_FAULT //$
+  if(PIN68==0 && RSSI_volatge > 0.4)
+    COMRX_STATUS = COMRX_ALIVE;
+  else
+  {
+    RESET_COMRX();
+    COMRX_RESET_COUNTER++;
+    if(PIN68==0 && RSSI_volatge > 0.4)
+      COMRX_STATUS = COMRX_ALIVE;
+    else
+      COMRX_STATUS = COMRX_DEAD;
+  }
+}
+
+void VERIFY_RTC(){
+
+  if(RTC_STATUS = 0x00)
+  {
+    SPI_mutex.lock();
+    gCS_RTC=1; 
+    gCS_RTC=0;
+    spi.write(0x0F);
+    if(spi.write(0x00) & 0x04 == 0x04)
+    {
+      RTC_STATUS = Not_started;
+      RESET_RTC();
+      RTC_FAULTCOUNT++;
+    }
+    gCS_RTC=1; 
+    SPI_mutex.unlock();
+  }
+}
+
+void HANDLE_HW_FAULTS()
+{
+  HANDLE_HW_FAULT_SD();
+  HANDLE_HW_FAULT_BAE();
+  HANDLE_HW_FAULT_PL()
+}
+
+void HANDLE_HW_FAULT_SD()
+{
+  if(SD_STATUS != DEVICE_DISABLED)
+  {
+    if(SD_STATUS = DEVICE_OC_FAULT)
+      SD_SW_EN_DS = 0; //powering on SD
+
+    if(SD_OC_FAULT == 0) 
+    {
+      SD_SW_EN_DS = 1; //switching off SD card
+     
+      SD_FAULTCOUNT++;
+      SD_STATUS = (SD_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
+    }
+    else
+    {
+      SD_STATUS = DEVICE_POWERED;
+      SD_FAULTCOUNT = 0;
+    }
+  }
+}
+
+void HANDLE_HW_FAULT_BAE()
+{
+  if(BAE_STATUS != DEVICE_DISABLED)
+  {
+    if(BAE_STATUS = DEVICE_OC_FAULT)
+        BAE_SW_EN_DS = 1; //Power ON BAE
+
+    if(BAE_OC_FAULT == 0)  // If OC Fault
+    {
+      BAE_SW_EN_DS = 0; //Switch OFF BAE
+      BAE_FAULTCOUNT++;
+      BAE_STATUS = (BAE_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
+    }
+    else
+    {
+      BAE_STATUS = DEVICE_POWERED;
+      BAE_FAULTCOUNT = 0;
+    }
+  }
+}
+
+void HANDLE_HW_FAULT_PL()
+{
+  if(PL_STATUS != DEVICE_DISABLED)
+  {
+    if(PL_STATUS = DEVICE_OC_FAULT)
+      PL_SW_EN_DS = 1; //Power ON PL
+
+    if(PL_BEE_SW_OC_FAULT == 1)  // if OC Fault
+    {
+      PL_SW_EN_DS = 0;  // switching OFF PL
+      PL_FAULTCOUNT++;
+      PL_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
+    }
+    else
+    {
+      if(PL_STATUS == DEVICE_OC_FAULT)
+        PL_SW_EN_DS = 0; //Switching OFF PL
+      PL_STATUS = DEVICE_ENABLED;
+      PL_FAULTCOUNT = 0;
+    }
+  }
+  
+void COLLECT_CDMS_RAM()
+{ 
+  /*--------------------Current FSC's---------------------*/\
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+43] = CURRENT_FSC_HK_ARCH_BAE >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+47] = CURRENT_FSC_HK_ARCH_CDMS >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+51] = CURRENT_FSC_SFF_BT >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+55] = CURRENT_FSC_SFF_AT >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+59] = CURRENT_FSC_SCF >> (i*8);
+
+  /*---------------------Latest Time----------------------*/
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+63] = TIME_LATEST_SPI_SPEED >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+67] = TIME_LATEST_SD_RD >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+71] = TIME_LATEST_SD_WR >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+75] = TIME_LATEST_I2C_SPEED >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+79] = TIME_LATEST_I2C_BAE >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+83] = TIME_LATEST_RTC >> (i*8);
+  for(int i=0;i<2;i++)
+    CDMS_HEALTH_DATA[i+87] = COMRX_RESET_COUNTER >> (i*8);
+  for(int i=0;i<2;i++)
+    CDMS_HEALTH_DATA[i+89] = PL_RCV_SC_DATA_COUNTER >> (i*8);
+  for(int i=0;i<2;i++)
+    CDMS_HEALTH_DATA[i+91] = PL_MAIN_COUNTER >> (i*8);
+  for(int i=0;i<2;i++)
+    CDMS_HEALTH_DATA[i+93] = CDMS_HK_MAIN_COUNTER >> (i*8);
+  for(int i=0;i<2;i++)
+    CDMS_HEALTH_DATA[i+95] = CDMS_I2C_ERR_BAE_COUNTER >> (i*8);
+  for(int i=0;i<2;i++)
+    CDMS_HEALTH_DATA[i+97] = CDMS_I2C_ERR_SPEED_COUNTER >> (i*8);
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | CDMS_STANDBY_PL << 7;
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_INIT_STATUS << 6) & 0x40);
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_HK_MAIN_STATUS << 5) & 0x20);
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_HK_STATUS << 3) & 0x18);
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((COMRX_STATUS << 2) & 0x04);
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_RTC_BL << 1) & 0x02);
+  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] & 0xFE;
+  
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | PL_RCV_SC_DATA_STATUS << 7;
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_SESSION << 6) & 0x40);
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_RX << 5) & 0x20);
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((RF_SW_STATUS << 4) & 0x10);
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_TX << 3) & 0x08);
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_TX_STATUS << 2) & 0x04);
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_MNG_TMTC << 1) & 0x02);
+  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | (CDMS_STANDBY_HK & 0x01);
+
+  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | PL_INIT_STATUS << 7;
+  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | ((PL_MAIN_STATUS << 6) & 0x40);
+  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | ((PL_LOW_POWER << 5) & 0x20);
+  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | ((PL_STATE << 3) & 0x18);
+  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | (PL_STATUS & 0x07);
 }
\ No newline at end of file
--- a/COM_MNG_TMTC.h	Wed May 25 15:28:32 2016 +0000
+++ b/COM_MNG_TMTC.h	Wed Jun 29 13:59:21 2016 +0000
@@ -1372,7 +1372,7 @@
             gPC.printf("completed or session timed out: %x\r\n", gMASTER_STATE);\
         /*COMPLETED EXECUTION OF TC*/\
         gMASTER_STATE = TCL_STATE_COMPLETED;\
-        COM_POWER_OFF_TX;\
+        /*COM_POWER_OFF_TX;*/\
         reset_all;\
         /*PENDING : ENABLE THREADS*/\
         gPAY_SPI.bulkRead_start();\
--- a/COM_POWER_ON_TX.h	Wed May 25 15:28:32 2016 +0000
+++ b/COM_POWER_ON_TX.h	Wed Jun 29 13:59:21 2016 +0000
@@ -282,8 +282,8 @@
         }
         get_tc_list(power_on_tm->next_TM, 0x00);
         /*Call Sign, ACK_L1, TC_LIST*/
-        snd_tm.head_pointer(power_on_tm_head);
-        transmit_adf;
+        //snd_tm.head_pointer(power_on_tm_head);
+        //transmit_adf;
         uint8_t transmissionPass = 0xFF;
         /*PENDING: get acknowledgement of transmission*/
         /*deleting the telemetry*/
@@ -369,5 +369,5 @@
             }
         }
     }
-    /*gPC.puts("COMPLETED COM_POWER_ON_TX\r\n");*/
+    gPC.puts("COMPLETED COM_POWER_ON_TX\r\n");
 }
\ No newline at end of file
--- a/DefinitionsAndGlobals.h	Wed May 25 15:28:32 2016 +0000
+++ b/DefinitionsAndGlobals.h	Wed Jun 29 13:59:21 2016 +0000
@@ -11,11 +11,11 @@
     #define COM_RX_UART_RX USBRX
     
 // COMMON SPI
-    #define SPI_MOSI PTE1
-    #define SPI_MISO PTE3
-    #define SPI_CLK PTE2
+    #define SPI_MOSI PTD6
+    #define SPI_MISO PTD7
+    #define SPI_CLK PTD5
     #define SPI_CS_ADF PTA15
-    #define SPI_CS_SDC PIN20
+    #define SPI_CS_SDC PTD4
     #define SPI_CS_RTC PIN26
     
 // COM_TX
@@ -137,6 +137,14 @@
     #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
 Serial gPC( USBTX, USBRX );
@@ -203,7 +211,31 @@
 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_HEALTH_DATA[134];
+uint8_t CDMS_RAM[59];
+uint16_t GPIO_STATUS;
+uint8_t CDMS_HK_MAIN_STATUS;  
+uint16_t CDMS_HK_MAIN_COUNTER;  
+uint8_t COMRX_STATUS;
+uint16_t COMRX_RESET_COUNTER;
+uint8_t RTC_FAULTCOUNT;
+uint16_t SD_FAULTCOUNT;
+uint8_t SD_STATUS;
+uint8_t BAE_FAULTCOUNT;
+uint8_t BAE_STATUS;
+uint8_t PL_STATUS;
+uint16_t PL_FAULTCOUNT;
+bool BAE_HK_I2C;
+uint8_t RTC_STATUS;
+uint32_t CDMS_HK_FSC;
+uint32_t BAE_HK_FSC;
+float RSSI_volatge;
+
 CDMS_HK_actual actual_data;
 CDMS_HK_quant quant_data;
 CDMS_HK_min_max min_max_data;
-bool firstCount=true;
\ No newline at end of file
+bool firstCount=true;
+
+//SD HK
+uint32_t FSC_CURRENT[6];
+uint32_t FSC_LAST[6];
\ No newline at end of file
--- a/ThreadsAndFunctions.h	Wed May 25 15:28:32 2016 +0000
+++ b/ThreadsAndFunctions.h	Wed Jun 29 13:59:21 2016 +0000
@@ -135,6 +135,7 @@
         //gLEDG = 1;
     }
     while(true){
+        COM_POWER_ON_TX();
         Thread::signal_wait( COM_MNG_TMTC_SIGNAL_UART_INT );
         if( gFLAGS & UART_INT_FLAG ){
             gFLAGS = gFLAGS & (~UART_INT_FLAG);
--- a/cdms_rtc.h	Wed May 25 15:28:32 2016 +0000
+++ b/cdms_rtc.h	Wed Jun 29 13:59:21 2016 +0000
@@ -74,79 +74,64 @@
     spi.write(0x87); 
     spi.write(0x01); //set year to 00(2000)
     gCS_RTC=1;
-    gPC.puts("\n\r rtc initalised \n");
+    gPC.puts("\r\nrtc initalised\r\n");
     SPI_mutex.unlock();
 }
  
 uint64_t FCTN_CDMS_RD_RTC()
 {    
-    SPI_mutex.lock();
-    uint8_t response;
-    //printf("\n\r Entered rtc\n");
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x00); //reading milliseconds register
-    response = spi.write(0x00); // read the value by sending dummy byte
-    uint8_t centiseconds =  (uint8_t(response&0xF0)>>4)*10+uint8_t(response&0x0F)*1;
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x01); //reading seconds register
-    response =spi.write(0x01);
-    uint8_t seconds =  ((response&0x70)>>4)*10+(response&0x0F)*1;
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x02); //reading minutes register
-    response =spi.write(0x01);
-    uint8_t minutes =  ((response&0xF0)>>4)*10+(response&0x0F)*1;
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x03); //reading hours register
-    response=spi.write(0x01);
-    uint8_t hours =  ((response&0x30)>>4)*10+(response&0x0F)*1;
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x04); //reading day's  register
-    uint8_t day =spi.write(0x01);
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x05); //reading date register
-    response =spi.write(0x01);
-    uint8_t date =  ((response&0x30)>>4)*10+(response&0x0F)*1;
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x06); //reading month registe
-    response =spi.write(0x01);
-    uint8_t month =  ((response&0x10)>>4)*10+(response&0x0F)*1;
-    
-    gCS_RTC=1;
-    gCS_RTC=0;
-    spi.write(0x07); //reading year's registe
-    response =spi.write(0x01);
-    uint8_t year =  ((response&0xF0)>>4)*10+(response&0x0F)*1;
-    gCS_RTC=1;
-    //sprintf(Time_stamp,"%02d%02d%02d%02d%02d%02d%02d%02d",year, month, date, day, hours, minutes, seconds, milliseconds );
-    uint8_t Time_stamp[8] = {year, month, date, day, hours, minutes, seconds, centiseconds};
-    for(int i= 0; i<8;i++);
-        //printf("%d\t",Time_stamp[i]);
-    //printf("read\r\n");
-    uint64_t time;
-    time = 0;
-    time = time|(((uint64_t)(centiseconds&0x7F)));
-    time = time|(((uint64_t)(seconds&0x3F))<<7);
-    time = time|(((uint64_t)(minutes&0x3F))<<13);
-    time = time|(((uint64_t)(hours&0x1F))<<19);
-    time = time|(((uint64_t)(day&0x1F))<<24);
-    time = time|(((uint64_t)(month&0x07))<<29);
-    time = time|(((uint64_t)(year&0x03))<<33);
-    time = (time&0x00000007FFFFFFFF);
-    //printf("\n\r%x%x", (int)(time >> 32), (int)(time));
-    //printf("\n\r0x%016llx\n\r", time);
-   return time;
-   SPI_mutex.unlock();
+  SPI_mutex.lock();
+  uint8_t response;
+
+  gCS_RTC=1;
+  gCS_RTC=0;
+  spi.write(0x0F);
+  response = (spi.write(0x00))&0x04;
+/*  if(response =! 0x00)
+  {
+    time = 0x0000000000000000;
+    return time;
+  }*/
+
+  gCS_RTC=1;
+  gCS_RTC=0;
+  spi.write(0x00);                //reading centiseconds register
+  response = spi.write(0x00);     // read the value by sending dummy byte
+  uint8_t centiseconds = ((response&0xF0)>>4)*10+(response&0x0F)*1;
+
+  response =spi.write(0x00);
+  uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1;
+
+  response =spi.write(0x00);
+  uint8_t minutes = ((response&0x70)>>4)*10+(response&0x0F)*1;
+
+  response=spi.write(0x00);
+  uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1;
+
+  uint8_t day =spi.write(0x00);
+
+  response =spi.write(0x00);
+  uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1;
+
+  response =spi.write(0x00);
+  uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1;
+
+  response =spi.write(0x00);
+  uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
+  year = (year == 17)?0x00:(year == 18)?0x01:(year == 19)?0x02:(year == 20)?0x03:0x00;
+  gCS_RTC=1;
+
+  uint64_t time;
+  time = 0;
+  time = time|(((uint64_t)(centiseconds&0x7F)));
+  time = time|(((uint64_t)(seconds&0x3F))<<7);
+  time = time|(((uint64_t)(minutes&0x3F))<<13);
+  time = time|(((uint64_t)(hours&0x1F))<<19);
+  time = time|(((uint64_t)(day&0x1F))<<24);
+  time = time|(((uint64_t)(month&0x07))<<29);
+  time = time|(((uint64_t)(year&0x03))<<33);
+  time = (time&0x00000007FFFFFFFF);
+  
+  SPI_mutex.unlock();
+  return time;
 }
\ No newline at end of file
--- a/cdms_sd.h	Wed May 25 15:28:32 2016 +0000
+++ b/cdms_sd.h	Wed Jun 29 13:59:21 2016 +0000
@@ -23,22 +23,29 @@
 
 #define SD_MAX_CYCLES 10000
 
-extern uint8_t SD_INIT_FLAGS;
+uint32_t SD_SCP_FIRST=1001;
+uint32_t SD_SCP_LAST=2000;
+uint32_t SD_SFF_AT_FIRST=2001;
+uint32_t SD_SFF_AT_LAST = 3000;
+uint32_t SD_SFF_BT_FIRST =3001;
+uint32_t SD_SFF_BT_LAST=4000;
+uint32_t SD_HK_ARCH_FIRST=4001;
+uint32_t SD_HK_ARCH_LAST= 5000;
+uint32_t LOG_FIRST =5001;
+uint32_t LOG_LAST=6000;
+uint32_t SD_MNG_SECT=7000;
 
-
+extern uint8_t SD_INIT_FLAGS;
 
 int initialise_card();
 int initialise_card_v1();
 int initialise_card_v2();
 int disk_write(const uint8_t *, uint64_t);
 int disk_read(uint8_t *, uint64_t);
-int disk_initialize();
 int disk_erase(int,int);
 
-//void FCTN_CDMS_INIT_SD();
-//int FCTN_CDMS_WR_SD(const uint8_t *, uint64_t);
-//int FCTN_CDMS_RD_SD(uint8_t *, uint64_t);
-uint32_t FCTN_SD_MNGR(uint8_t);
+uint32_t FCTN_SD_MNG(uint8_t);
+int INCREMENT_SD_LIB(uint8_t);
 
 
 int cmd(int, int);
@@ -49,7 +56,7 @@
 int write(const uint8_t*, uint32_t );
 static uint32_t ext_bits(unsigned char *, int , int );
 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
-void FCTN_CDMS_SD_INIT();
+int FCTN_CDMS_SD_INIT();
 int SD_READ(uint8_t*,uint32_t,uint8_t);
 #define SDCARD_FAIL 0
 #define SDCARD_V1   1
@@ -57,145 +64,221 @@
 #define SDCARD_V2HC 3
 
 
-
-
-
-
-
-
-
-
-
 int cdv;
 uint64_t sd_sectors();
 uint64_t sectors;
 
-void FCTN_CDMS_SD_INIT()
-{
-   
-    initialise_card();
-    
-    disk_initialize();
-         
+int FCTN_CDMS_SD_INIT()
+{   
+    int i = initialise_card();
+    debug_if(SD_DBG, "init card = %d\n", i);
+    sectors = sd_sectors();
+
+    // Set block length to 512 (CMD16)
+    if (cmd(16, 512) != 0) {
+        debug("\rSet 512-byte block timed out\r\n");
+        return 1;
+    } else {
+    //printf("\rDisk initialization successfull\r\n");
+    }
+
+    spi.frequency(1000000); // Set to 1MHz for data transfer
+    return 0;
 }
 
     
 
 uint32_t  FCTN_SD_MNGR(uint8_t sid)
-
 {
-   
-    uint32_t SD_MNG_SECT=7000;
-    
     uint32_t fsc;
+    uint32_t start_fsc;
     uint8_t buffer[512];
     int b=disk_read(buffer, SD_MNG_SECT);
-    if(sid==0x0)
+    if(sid==0x01)
     {
-        
         fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
-        uint32_t next_fsc=fsc+1;
-        buffer[0]=(uint8_t) (next_fsc>>24 & 0xFF);
-        buffer[1]=(uint8_t) (next_fsc>>16 & 0xFF);
-        buffer[2]=(uint8_t) (next_fsc>>8 & 0xFF);
-        buffer[3]=(uint8_t) (next_fsc & 0xFF);
-        buffer[511]+=2;
-        disk_write(buffer,SD_MNG_SECT);
+        start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
+        FSC_CURRENT[1] = fsc;
+        FSC_LAST[1] = start_fsc;
+    }
+    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_CURRENT[2] = fsc;
+        FSC_LAST[2] = start_fsc;
+    }
+    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_CURRENT[3] = fsc;
+        FSC_LAST[3] = start_fsc;
+    }
+    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_CURRENT[4] = fsc;
+        FSC_LAST[4] = start_fsc;
     }
-    if(sid==0x1)
+    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_CURRENT[5] = fsc;
+        FSC_LAST[5] = start_fsc;
+    }
+    return fsc;
+}
+
+int INCREMENT_SD_LIB(uint8_t sid)
+{
+    uint32_t fsc;
+    uint32_t start_fsc;
+    uint8_t buffer[512];
+    disk_read(buffer,SD_MNG_SECT);
+    if(sid==0x01)
     {
-        fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
-        uint32_t next_fsc=fsc+1;
-        buffer[4]=(uint8_t) (next_fsc>>24 & 0xFF);
-        buffer[5]=(uint8_t) (next_fsc>>16 & 0xFF);
-        buffer[6]=(uint8_t) (next_fsc>>8 & 0xFF);
-        buffer[7]=(uint8_t) (next_fsc & 0xFF);
-        buffer[511]+=2;
-        disk_write(buffer,SD_MNG_SECT);
+        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];
+        buffer[0]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[1]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[2]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[3]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_SCP_LAST-SD_SCP_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[4]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[5]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[6]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[7]=(uint8_t) (start_fsc & 0xFF);
+        }
+        return disk_write(buffer,SD_MNG_SECT);
     }
-    if(sid==0x2)
+    if(sid==0x02)
     {
         fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
-        uint32_t next_fsc=fsc+1;
-        buffer[8]=(uint8_t) (next_fsc>>24 & 0xFF);
-        buffer[9]=(uint8_t) (next_fsc>>16 & 0xFF);
-        buffer[10]=(uint8_t) (next_fsc>>8 & 0xFF);
-        buffer[11]=(uint8_t) (next_fsc & 0xFF);
-        buffer[511]+=2;
-        disk_write(buffer,SD_MNG_SECT);
+        start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
+        buffer[8]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[9]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[10]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[11]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_SFF_AT_LAST-SD_SFF_AT_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[12]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[13]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[14]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[15]=(uint8_t) (start_fsc & 0xFF);
+        }
+        return disk_write(buffer,SD_MNG_SECT);
     }
-     if(sid==0x3)
-    {
-        fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
-        uint32_t next_fsc=fsc+1;
-        buffer[12]=(uint8_t) (next_fsc>>24 & 0xFF);
-        buffer[13]=(uint8_t) (next_fsc>>16 & 0xFF);
-        buffer[14]=(uint8_t) (next_fsc>>8 & 0xFF);
-        buffer[15]=(uint8_t) (next_fsc & 0xFF);
-        buffer[511]+=2;
-        disk_write(buffer,SD_MNG_SECT);
-    }
-     if(sid==0x4)
+    if(sid==0x03)
     {
         fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
-        uint32_t next_fsc=fsc+1;
-        buffer[16]=(uint8_t) (next_fsc>>24 & 0xFF);
-        buffer[17]=(uint8_t) (next_fsc>>16 & 0xFF);
-        buffer[18]=(uint8_t) (next_fsc>>8 & 0xFF);
-        buffer[19]=(uint8_t) (next_fsc & 0xFF);
-        buffer[511]+=2;
-        disk_write(buffer,SD_MNG_SECT);
+        start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
+        buffer[16]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[17]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[18]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[19]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_SFF_BT_LAST-SD_SFF_BT_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[20]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[21]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[22]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[23]=(uint8_t) (start_fsc & 0xFF);
+        }
+        return disk_write(buffer,SD_MNG_SECT);
     }
-    return fsc;
+     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];
+        buffer[24]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[25]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[26]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[27]=(uint8_t) (fsc & 0xFF);
+        if(fsc > SD_HK_ARCH_LAST-SD_HK_ARCH_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[28]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[29]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[30]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[31]=(uint8_t) (start_fsc & 0xFF);
+        }
+        return disk_write(buffer,SD_MNG_SECT);
+    }
+     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];
+        buffer[32]=(uint8_t) (fsc>>24 & 0xFF);
+        buffer[33]=(uint8_t) (fsc>>16 & 0xFF);
+        buffer[34]=(uint8_t) (fsc>>8 & 0xFF);
+        buffer[35]=(uint8_t) (fsc & 0xFF);
+        if(fsc > LOG_LAST-LOG_FIRST+1)
+        {
+            start_fsc = start_fsc+1;
+            buffer[36]=(uint8_t) (start_fsc>>24 & 0xFF);
+            buffer[37]=(uint8_t) (start_fsc>>16 & 0xFF);
+            buffer[38]=(uint8_t) (start_fsc>>8 & 0xFF);
+            buffer[39]=(uint8_t) (start_fsc & 0xFF);
+        }
+        return disk_write(buffer,SD_MNG_SECT);
+    }
+    return -1;
 }
 
 
 int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid)
 {
-   
-    uint32_t SD_SCP_FIRST=1001;
-    uint32_t SD_SCP_LAST=2000;
-    uint32_t SD_SFF_AT_FIRST=2001;
-    uint32_t SD_SFF_AT_LAST = 3000;
-    uint32_t SD_SFF_BT_FIRST =3001;
-    uint32_t SD_SFF_BT_LAST=4000;
-    uint32_t SD_HK_ARCH_FIRST=4001;
-    uint32_t SD_HK_ARCH_LAST= 5000;
-    uint32_t LOG_FIRST =5001;
-    uint32_t LOG_LAST=6000;
-    uint32_t SD_MNG_SECT=7000;
     uint32_t block_number;
-    int result;
-    if(sid==0x0)
+    int result = 10;
+    if(sid==0x01)
     {
+        //block_number=SD_SCP_FIRST+(fsc%(SD_SCP_LAST-SD_SCP_FIRST+1))-1;
         block_number=SD_SCP_FIRST+fsc;
-//        printf("write_block_number=%d\r\n",block_number);
         result= disk_write(buffer,block_number);
+        if(result == 0)
+            INCREMENT_SD_LIB(sid);
+        return result;
+    }
+   if(sid==0x02)
+    {
+        //block_number= SD_SFF_AT_FIRST+(fsc%(SD_SFF_AT_LAST - SD_SFF_AT_FIRST+1))-1;
+        block_number= SD_SFF_AT_FIRST+fsc;
+        result= disk_write(buffer,block_number);
+        if(result == 0)
+            INCREMENT_SD_LIB(sid);
         return result;
     }
-   if(sid==0x1)
+     if(sid==0x03)
     {
-        block_number=SD_SFF_AT_FIRST + fsc;
+        //block_number= SD_SFF_BT_FIRST +(fsc%(SD_SFF_BT_LAST - SD_SFF_BT_FIRST +1))-1;
+        block_number= SD_SFF_BT_FIRST +fsc;
         result= disk_write(buffer,block_number);
-        return result;
-    }
-     if(sid==0x2)
-    {
-        block_number=SD_SFF_BT_FIRST + fsc;
-        result= disk_write(buffer,block_number);
+        if(result == 0)
+            INCREMENT_SD_LIB(sid);
         return result;
     }
-     if(sid==0x3)
+     if(sid==0x04)
     {
-        block_number=SD_HK_ARCH_FIRST+fsc;
-//        sd1.printf("Block number is %d \r\n",block_number);
+        //block_number=SD_HK_ARCH_FIRST +(fsc%(SD_HK_ARCH_LAST - SD_HK_ARCH_FIRST +1))-1;
+        block_number=SD_HK_ARCH_FIRST +fsc;
         result= disk_write(buffer,block_number);
+        if(result == 0)
+            INCREMENT_SD_LIB(sid);
         return result;
     }
-     if(sid==0x4)
+     if(sid==0x05)
     {
-        block_number=LOG_FIRST +fsc;
+        //block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
+        block_number= LOG_FIRST +fsc;
         result= disk_write(buffer,block_number);
+        if(result == 0)
+            INCREMENT_SD_LIB(sid);
         return result;
     }
     return 1;
@@ -203,42 +286,29 @@
 
 int SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
 {
-   
-    uint32_t SD_SCP_FIRST=1001;
-    uint32_t SD_SCP_LAST=2000;
-    uint32_t SD_SFF_AT_FIRST=2001;
-    uint32_t SD_SFF_AT_LAST = 3000;
-    uint32_t SD_SFF_BT_FIRST =3001;
-    uint32_t SD_SFF_BT_LAST=4000;
-    uint32_t SD_HK_ARCH_FIRST=4001;
-    uint32_t SD_HK_ARCH_LAST= 5000;
-    uint32_t LOG_FIRST =5001;
-    uint32_t LOG_LAST=6000;
-    uint32_t SD_MNG_SECT=7000;
     uint32_t block_number;
     int result;
-    if(sid==0x0)
+    if(sid==0x01)
     {
         block_number=SD_SCP_FIRST + fsc;
-//        sd1.printf("read_block_number=%d\r\n",block_number);
         result= disk_read(buffer,block_number);
     }
-   else if(sid==0x1)
+   else if(sid==0x02)
     {
         block_number=SD_SFF_AT_FIRST + fsc;
         result= disk_read(buffer,block_number);
     }
-    else if(sid==0x2)
+    else if(sid==0x03)
     {
         block_number=SD_SFF_BT_FIRST + fsc;
         result= disk_read(buffer,block_number);
     }
-    else if(sid==0x3)
+    else if(sid==0x04)
     {
         block_number=SD_HK_ARCH_FIRST + fsc;
         result= disk_read(buffer,block_number);
     }
-    else if(sid==0x4)
+    else if(sid==0x05)
     {
         block_number=LOG_FIRST +fsc;
         result= disk_read(buffer,block_number);
@@ -259,21 +329,25 @@
     for (int i = 0; i < 16; i++) {
         spi.write(0xFF);
     }
-
+    uint8_t R1_response = cmd(0,0);
+    gPC.printf("0x%02X",R1_response);
     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
-    if (cmd(0, 0) != R1_IDLE_STATE) {
+    if (R1_response != R1_IDLE_STATE) {
         debug("No disk, or could not put SD card in to spi idle state\r\n");
         return SDCARD_FAIL;
     }
+    else
+        gPC.puts("SD Card is in IDLE state\n\r");    
 
-// send CMD8 to determine whther it is ver 2.x
+    // send CMD8 to determine whther it is ver 2.x
     int r = cmd8();
     if (r == R1_IDLE_STATE) {
-//        printf("\rEntering v2\r\n");
-        return initialise_card_v2();
+        gPC.puts("Entering V2\r");
+        int q =  initialise_card_v2();
+        return q;
 
     } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
-//        printf("\rEntering v1\r\n");
+        gPC.puts("Entering V1");
         return initialise_card_v1();
 
     } else {
@@ -312,7 +386,6 @@
             cmd58();
             debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
             cdv = 1;
-
             return SDCARD_V2;
         }
     }
@@ -486,7 +559,7 @@
     }
     cs_sd = 1;
     spi.write(0xFF);
-    return -1; // timeout
+    return 1; // timeout
 }
 
 static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
@@ -505,20 +578,7 @@
 
 int disk_initialize()
 {
-    int i = initialise_card();
-    debug_if(SD_DBG, "init card = %d\n", i);
-    sectors = sd_sectors();
 
-    // Set block length to 512 (CMD16)
-    if (cmd(16, 512) != 0) {
-        debug("\rSet 512-byte block timed out\r\n");
-        return 1;
-    } else {
-//        printf("\rDisk initialization successfull\r\n");
-    }
-
-    spi.frequency(1000000); // Set to 1MHz for data transfer
-    return 0;
 }
 
 int disk_write(const uint8_t *buffer, uint64_t block_number)
--- a/main.cpp	Wed May 25 15:28:32 2016 +0000
+++ b/main.cpp	Wed Jun 29 13:59:21 2016 +0000
@@ -29,6 +29,8 @@
 #include "Compression.h"
 #include "ThreadsAndFunctions.h"
 
+DigitalOut SD_SW_EN_DS (PIN97);
+
 //void set_sig(){gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);}
 int main()
 {
@@ -44,7 +46,7 @@
 //    gRX_CURRENT_PTR = gRX_CURRENT_DATA_NODE->values;
     RX1M.attach(&rx_read, Serial::RxIrq);
     
-    gPC.baud(1200);
+    gPC.baud(9600);
     
     // COMMON SPI
     spi.format(8,0);
@@ -55,11 +57,30 @@
     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];
+    uint8_t data[512];
+    for(int i=0;i<512;i++)
+        data[i] = i%100;
+    for(int i=0;i<512;i++)
+        test[i] = 1;
+    disk_write(test,7000);
+    disk_read(test,7000);
+    uint32_t fsc = FCTN_SD_MNGR(3);
+    if(SD_WRITE(data,fsc,3) == 0)
+    {
+        SD_READ(test,fsc,3);
+        for(int i=0;i<15;i++)
+        {
+            gPC.printf("\r0x%02X 0x%02X\n",data[i],test[i]);
+        }
+    }
+    
     
     if (DEBUG)
-        gPC.puts("welcome to mng_tmtc\r\n");
+        gPC.puts("\rwelcome to mng_22tmtc\r\n");
+
         
     // COM_MNG_TMTC THREAD
     gCOM_MNG_TMTC_THREAD = new Thread(COM_MNG_TMTC_FUN);