cdms_update

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of CDMS_SD_MNG_OVERDRIVE by saikiran cholleti

Files at this revision

API Documentation at this revision

Comitter:
pradeepvk2208
Date:
Thu Jan 21 14:46:28 2016 +0000
Parent:
0:bcbd76c86cde
Commit message:
cdms_updated

Changed in this revision

CDMS_HK.cpp Show annotated file Show diff for this revision Revisions of this file
CDMS_HK.h Show annotated file Show diff for this revision Revisions of this file
Flags.h Show annotated file Show diff for this revision Revisions of this file
FreescaleIAP.lib Show annotated file Show diff for this revision Revisions of this file
TMTC.cpp Show annotated file Show diff for this revision Revisions of this file
cdms_sd.cpp 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.cpp 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
main.h Show annotated file Show diff for this revision Revisions of this file
main_funcs.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
sd_flags.h Show annotated file Show diff for this revision Revisions of this file
diff -r bcbd76c86cde -r ad3b8a8032e2 CDMS_HK.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CDMS_HK.cpp	Thu Jan 21 14:46:28 2016 +0000
@@ -0,0 +1,164 @@
+#include "Flags.h"
+#include "CDMS_HK.h"
+#include "mbed.h"
+#include "cdms_rtc.h"
+#include "i2c.h"
+#include "Flash.h"
+#include "cdms_sd.h"
+#include "pinconfig.h"
+
+
+Serial hk_cdms(USBTX, USBRX);
+//DigitalOut Select_Lines_C[]={PIN85,PIN84,PIN39,PIN38};  //to mux on IF board,from LSB(Select_Lines_C[3]) to MSB(Select_Lines_C[0])
+DigitalOut Select_Lines_C[]={D7,D6,D5,D4};
+AnalogIn Temperature_voltage_Input(PIN53);  //output from IF mux
+//AnalogIn Sensor_Input(PIN53);    //output from temperature sensor on CDMS
+
+
+void FCTN_CDMS_HK_MAIN()
+
+{
+    CDMS_HK_STATUS=(CDMS_HK_STATUS)|(HK_MAIN_STATUS);
+    
+    hk_cdms.printf(" \r\nCDMS HK entered Verify COM RX RSSI >0.4 \r\n");
+    
+    int HK_I2C;
+    char BAE_HK[73];
+    FCTN_I2C_READ(BAE_HK,73);
+    printf("BAE_HK=%s",BAE_HK);
+    //FCTN_I2C_READ(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));
+    }**/
+    
+       // printf("BAE HK is %s",BAE_HK);
+        uint64_t time=FCTN_CDMS_RD_RTC();
+        char tree[61];
+        hk_cdms.printf("i am done\r\n");
+        uint8_t* data;
+        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");
+       data=(uint8_t*)tree;
+        
+        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("let me know the truth\r\n");
+}
+
+
+
+int quantiz(float l_start,float l_step,float l_x)       // accepts min and measured values and step->quantises on a scale 0-15..(4 bit quantisation)
+{
+    int l_y = (l_x - l_start)/l_step;
+        
+    if(l_y <= 0)
+            l_y = 0;
+            
+    if(l_y >= 15)
+            l_y = 15;
+            
+    return l_y;
+} 
+ 
+Sensor_Data Sensor; 
+Sensor_Data_Quantised Sensor_Quantised;
+ 
+char* FCTN_CDMS_HK()
+{
+    char CDMS_HK_DATA[8];
+    float l_resistance_thermistor;  //declaration of variables
+    float l_voltage_thermistor;
+    float l_Payload_voltage;
+    int l_Loop_Iterator = 0;
+    int l_Select_Line_Iterator = 3;
+ 
+    for(l_Loop_Iterator = 0; l_Loop_Iterator < 16; l_Loop_Iterator++)
+    {
+ 
+        l_voltage_thermistor = Temperature_voltage_Input.read()*3.3;       //voltage across thermistor
+        l_resistance_thermistor = 24000*l_voltage_thermistor/(3.3 - l_voltage_thermistor);           //resistance of thermistor
+        l_Payload_voltage = Temperature_voltage_Input.read()*3.3;
+               
+        Sensor.l_Temperature_thermistor[(l_Loop_Iterator)] = 0.00000004*l_resistance_thermistor*l_resistance_thermistor - 0.0039*l_resistance_thermistor + 347.97;
+        Sensor.l_Temperature_sensor[l_Loop_Iterator] = (-90.7*3.3*Temperature_voltage_Input.read() + 190.1543);  
+              
+        if(l_Loop_Iterator % 2 == 0) 
+        {
+             if(l_Loop_Iterator < 4)
+             Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] = quantiz(tstart_thermistor,tstep_thermistor,Sensor.l_Temperature_thermistor[(l_Loop_Iterator)]);
+               
+             else  
+             {
+                if(4 < l_Loop_Iterator < 14)
+                Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] <<4 + quantiz(tstart,tstep,Sensor.l_Payload_voltage[l_Loop_Iterator]); 
+                
+                else
+                Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] = quantiz(tstart,tstep,Sensor.l_Temperature_sensor[l_Loop_Iterator]); 
+             }
+        }
+                                              
+        else
+        {
+            
+            if(l_Loop_Iterator < 4)
+            Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Temperature_thermistor[(l_Loop_Iterator)/2] <<4 + quantiz(tstart_thermistor,tstep_thermistor,Sensor.l_Temperature_thermistor[l_Loop_Iterator]);
+            
+            else
+            {
+                if(4 < l_Loop_Iterator < 14)
+                Sensor_Quantised.l_Payload_voltage[(l_Loop_Iterator)/2] = quantiz(tstart,tstep,Sensor.l_Payload_voltage[l_Loop_Iterator]);
+                
+                else
+                Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] = Sensor_Quantised.l_Temperature_sensor[(l_Loop_Iterator)/2] <<4 + quantiz(tstart,tstep,Sensor.l_Temperature_sensor[l_Loop_Iterator]); 
+        
+            }
+        }
+    
+       
+          
+       // The following lines are used to iterate the select lines from 0 to 15
+       for(l_Select_Line_Iterator = 3;l_Select_Line_Iterator >= 0;l_Select_Line_Iterator--)
+       {
+            if(Select_Lines_C[l_Select_Line_Iterator] == 0)
+            {
+                Select_Lines_C[l_Select_Line_Iterator] = 1;
+                break;
+            }
+            else Select_Lines_C[l_Select_Line_Iterator] = 0;
+            printf("%d\n",l_Select_Line_Iterator);
+       }
+      
+        wait_us(10.0); //  A delay of 10 microseconds between each sensor output. Can be changed.
+    }
+     printf("vol %f temp %f",Temperature_voltage_Input.read()*3.3,-90.7*3.3*Temperature_voltage_Input.read() + 190.1543 );
+     printf("CDMS_HK Data is Temperature_sensor=%s, l_Temperature_thermistor=%s, Payload_voltage=%s ",Sensor_Quantised.l_Temperature_sensor,Sensor_Quantised.l_Temperature_thermistor,Sensor_Quantised.l_Payload_voltage);
+     strcpy (CDMS_HK_DATA,Sensor_Quantised.l_Temperature_sensor);
+     strcat (CDMS_HK_DATA,Sensor_Quantised.l_Temperature_thermistor);
+     strcat (CDMS_HK_DATA,Sensor_Quantised.l_Payload_voltage);
+     return(CDMS_HK_DATA);
+}
+
+
+
diff -r bcbd76c86cde -r ad3b8a8032e2 CDMS_HK.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CDMS_HK.h	Thu Jan 21 14:46:28 2016 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+
+void FCTN_CDMS_HK_MAIN();
+char* FCTN_CDMS_HK();
+extern uint8_t CDMS_HK_STATUS;
+extern uint8_t Power_level;
+extern uint8_t Att_level;
+extern char CDMS_HK[8];
+
+#define tstart -40
+#define tstep 8
+#define tstep_thermistor 8
+#define tstart_thermistor -40
+
+ 
+typedef struct Sensor_Data
+{
+    
+    float l_Temperature_sensor[2];
+    float l_Temperature_thermistor[4]; //read by thermistors on solar panels
+    float l_Payload_voltage[9]; //for payload analog signals
+ 
+} Sensor_Data;
+ 
+typedef struct Sensor_Data_Quantised
+{
+    char l_Temperature_sensor[1];
+    char l_Temperature_thermistor[2];
+    char l_Payload_voltage[5];
+    
+} Sensor_Data_Quantised;
\ No newline at end of file
diff -r bcbd76c86cde -r ad3b8a8032e2 Flags.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flags.h	Thu Jan 21 14:46:28 2016 +0000
@@ -0,0 +1,41 @@
+/*HK*/
+#define HK_INIT_STATUS 0X01
+#define HK_MAIN_STATUS 0X02
+#define HK_BAE_DISBLED 0X04
+#define HK_BAE_ERR_I2C 0X08
+/*End*/
+
+/*PL_MAIN*/
+#define PL_MAIN_STATUS 0x00
+#define PL_LOW_POWER 0x01
+#define PL_OFF 0x02
+#define PL_ERR_I2C 0x04
+#define PL_STANDBY 0x08
+#define PL_HIBERNATE 0x10
+#define PL_SCIENCE 0x20
+#define PL_INVALID_STATE 0x40
+/*End*/
+
+
+/*PL_SCIENCE*/
+#define PL_SCIENCE_STATUS                         0x0000000000000001
+#define PL_FRAME_SIZE_CORRECT                     0x0000000000000002
+#define TIME_ELAPSED_LAST_SRP_INTERVAL_HIGH       0x0000000000000008
+#define TIME_ELAPSED_LAST_SRP_INTERVAL_LOW        0x0000000000000010
+/*End*/
+
+/*TC_TM*/
+#define CDMS_TC_TM_STATUS                         0x0000000000000001
+#define BAE_TC_STATUS                             0x0000000000000002
+#define PL_TC_STATUS                              0x0000000000000004 
+#define CDMS_TC_STATUS                            0x0000000000000008 
+#define TC_I2C_BAE_STATUS                         0x0000000000000010
+#define TC_I2C_PL_STATUS                          0x0000000000000020
+#define TC_BAE_I2C_ERR                            0x0000000000000040
+#define TC_PL_I2C_ERR                             0x0000000000000080   
+#define TM_STATUS                                 0x0000000000000100
+#define TX_TM_ERROR                               0x0000000000000200 
+/*End*/   
+
+
+      
\ No newline at end of file
diff -r bcbd76c86cde -r ad3b8a8032e2 FreescaleIAP.lib
--- a/FreescaleIAP.lib	Wed Dec 16 09:06:59 2015 +0000
+++ b/FreescaleIAP.lib	Thu Jan 21 14:46:28 2016 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/Sissors/code/FreescaleIAP/#474d231b2f35
+http://developer.mbed.org/users/Sissors/code/FreescaleIAP/#6749f7702fa5
diff -r bcbd76c86cde -r ad3b8a8032e2 TMTC.cpp
--- a/TMTC.cpp	Wed Dec 16 09:06:59 2015 +0000
+++ b/TMTC.cpp	Thu Jan 21 14:46:28 2016 +0000
@@ -7,6 +7,7 @@
 #include "cdms_sd.h"
 #include "Flash.h"
 
+
 DigitalIn tm_status_4m_slv(PIN39); //I2C interrupt to CDMS from BAE
 //DigitalIn tm_status_4m_pl(PIN61); //I2C interrupt to CDMS from PL
 
@@ -14,7 +15,7 @@
 uint16_t mid1;
 uint16_t mid2;
 uint16_t Nbytes;
-uint32_t Data;
+uint32_t Data[2];
 uint16_t nbytes;
 uint8_t flash_counter = 0;
 uint16_t crc16;
@@ -40,8 +41,8 @@
                 if(tm_status_4m_slv == 1)
                 {
                     printf("receiving...\r\n");
-                    FCTN_I2C_READ((char*)tm_pointer->TM_string);
-                    printf("%s", tm_pointer->TM_string);
+                    FCTN_I2C_READ((char*)tm_pointer->TM_string,135);
+                    //printf("%s", tm_pointer->TM_string);
                     received+=1;
                     tm_pointer = tm_pointer->next_TM;
                     break; //only for testing purpose
@@ -88,158 +89,97 @@
             printf("Telecommand is for CDMS\r\n"); //apid = 10 corresponds to cdms
             switch(GETservice_type(tc))
             {
-                case 6:
+                case 0x60:
                 {
-                    printf("service:MMS\r\n");
-                    switch(GETservice_subtype(tc))
-                    {
-                        case 1:
+                        printf("service:MMS\r\n");
+                        switch(GETservice_subtype(tc))
                         {
-                            printf("sub_service:Read from Memory\r\n");
-                            if(GETpid(tc) == 2)
-                            {                                  
-                                printf("pid:RD_L_FLASH\r\n");
-                                mid1 = ((((uint16_t)tc->TC_string[5])<<8)|(uint16_t)(tc->TC_string[6]));
-                                mid2 = ((((uint16_t)tc->TC_string[7])<<8)|(uint16_t)(tc->TC_string[8]));
-                                Nbytes = (((mid2-mid1)*32)/8)+4;
-                                uint8_t data[4*Nbytes];
-                                for(uint8_t i=0;i<mid2-mid1+1;i++)
-                                {
-                                    Data = FCTN_CDMS_RD_FLASH(mid1+i);
-                                    data[4*i] = (uint8_t)((Data&0xFF000000)>>24);
-                                    data[4*i+1] = (uint8_t)((Data&0xFF000000)>>16);
-                                    data[4*i+2] = (uint8_t)((Data&0xFF000000)>>8);
-                                    data[4*i+3] = (uint8_t)(Data&0xFF000000);
-                                }
-                                nbytes = Nbytes;
-                                flash_counter = 0;
-                                while(nbytes>=8)
-                                {
+                            case 0x1:
+                            {
+                            printf("sub_service:Read from RAM_Memory\r\n");
+                            mid=(uint16_t)(tc->TC_string[3]<<4)+(uint16_t)(tc->TC_string[4]);
+                            
+                            
+                                                              
+                                    Data[0] = FCTN_CDMS_RD_FLASH(0);
+                                    Data[1] = FCTN_CDMS_RD_FLASH(1);
                                     tm_pointer = new Long_tm;
                                     tm_pointer->TM_string[0] = 0x30;
                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
                                     tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
-                                    for(uint8_t i=0;i<8;i++)
+                                    for(uint8_t i=0;i<4;i++)
                                     {
-                                        tm_pointer->TM_string[3+i] = data[i+128*flash_counter];
+                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[0]>>(8*i))&0x00FF);
+                                    }
+                                    for(uint8_t i=4;i<8;i++)
+                                    {
+                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[1]>>(8*i))&0x00FF);
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,9);
-                                    tm_pointer->TM_string[9] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[10] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer = tm_pointer->next_TM;
-                                    nbytes-=128;
-                                    flash_counter++;
-                                }
-                                if(nbytes>0)
-                                {
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                return tm;
+                            }
+                            
+                            case 0x2:
+                            {
+                            printf("sub_service:Read from Flash_Memory\r\n");
+                            mid=(uint16_t)(tc->TC_string[3]<<4)+(uint16_t)(tc->TC_string[4]);
+                            
+                              
+                                
+                                
+                                    Data[0] = FCTN_CDMS_RD_FLASH(0);
+                                    Data[1] = FCTN_CDMS_RD_FLASH(1);
+                                    
+                                
+                              
+                                    tm_pointer = new Long_tm;
                                     tm_pointer->TM_string[0] = 0x30;
                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = (uint8_t)((flash_counter&0xFF00)>>8);
-                                    tm_pointer->TM_string[3] = (uint8_t)(flash_counter&0x00FF);
-                                    for(uint8_t i=0;i<nbytes;i++)
+                                    tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
+                                    for(uint8_t i=0;i<4;i++)
                                     {
-                                        tm_pointer->TM_string[4+i] = data[i+128*flash_counter];
+                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[0]>>(8*i))&0x00FF);
                                     }
-                                    for(uint8_t i=nbytes;i<128;i++)
+                                    for(uint8_t i=4;i<8;i++)
                                     {
-                                        tm_pointer->TM_string[4+i] = 0;
+                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[1]>>(8*i))&0x00FF);
                                     }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,132);
-                                    tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                                }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,9);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
                                 return tm;
-                            }
-                            else if (GETpid(tc)==0x22)
+                            
+               }
+                            
+                            case 0x5:
                             {
-                                printf("RD_S_FLASH\r\n");
-                                mid1 = ((((uint16_t)tc->TC_string[5])<<8)|(uint16_t)(tc->TC_string[6]));
-                                mid2 = ((((uint16_t)tc->TC_string[7])<<8)|(uint16_t)(tc->TC_string[8]));
-                                Nbytes = (((mid2-mid1)*32)/8)+4;
-                                uint8_t data[4*Nbytes];
-                                for(uint8_t i=0;i<mid2-mid1+1;i++)
-                                {
-                                    Data = FCTN_CDMS_RD_FLASH(mid1+i);
-                                    data[4*i] = (uint8_t)((Data&0xFF000000)>>24);
-                                    data[4*i+1] = (uint8_t)((Data&0xFF000000)>>16);
-                                    data[4*i+2] = (uint8_t)((Data&0xFF000000)>>8);
-                                    data[4*i+3] = (uint8_t)(Data&0xFF000000);
-                                }
-                                nbytes = Nbytes;
-                                flash_counter = 0;
-                                while(nbytes>=8)
-                                {
+                            printf("WRITE ON FLASH_MEMORY\r\n");
+                            
+                                    mid=(uint16_t)(tc->TC_string[3]<<4)+(uint16_t)(tc->TC_string[4]);
+                                    
+                                    block = (((uint32_t)(tc->TC_string[5])<<24)|((uint32_t)(tc->TC_string[5])<<16)|((uint32_t)(tc->TC_string[5])<<8)|((uint32_t)(tc->TC_string[5])));
+                                    FCTN_CDMS_WR_FLASH(mid,block);
                                     tm_pointer = new Short_tm;
-                                    tm_pointer->TM_string[0] = 0xC0;
+                                    tm_pointer->TM_string[0] = 0xB0;
                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
-                                    for(uint8_t i=0;i<8;i++)
+                                    tm_pointer->TM_string[2] = 0x01;
+                                    tm_pointer->TM_string[3] = ACKCODE;  //ackcode to be decided
+                                    tm_pointer->TM_string[4] = 0x01;
+                                    for(uint8_t i=0;i<6;i++)
                                     {
-                                        tm_pointer->TM_string[3+i] = data[i+8*flash_counter];
+                                        tm_pointer->TM_string[i+5] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer = tm_pointer->next_TM;
-                                    nbytes-=8;
-                                    flash_counter++;
-                                }
-                                if(nbytes>0)
-                                {
-                                    tm_pointer->TM_string[0] = 0xC0;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
-                                    for(uint8_t i=0;i<nbytes;i++)
-                                    {
-                                        tm_pointer->TM_string[3+i] = data[i+8*flash_counter];
-                                    }
-                                    for(uint8_t i=nbytes;i<128;i++)
-                                    {
-                                        tm_pointer->TM_string[3+i] = 0;
-                                    }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                    tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>2);
-                                    tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                                }
-                                return tm;
+                                    return tm;
+                                    break;
                             }
-                            else if (GETpid(tc)==0x42)
-                            {
-                                printf("RD_L_RAM\r\r\n"); //to be discussed which RAM variables are to read;
-                                tm_pointer = new Short_tm; //for now sending ACKL234
-                                tm_pointer->TM_string[0] = 0xB0;
-                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                tm_pointer->TM_string[2] = 0x01;
-                                tm_pointer->TM_string[3] = ACKCODE; 
-                                tm_pointer->TM_string[4] = 0x01;
-                                for(uint8_t i=0;i<6;i++)
-                                {
-                                    tm_pointer->TM_string[i+5] = 0;
-                                }
-                                crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                return tm;
-                            }
-                            else if (GETpid(tc)==0x62)
-                            {
-                                printf("RD_S_RAM\r\r\n");  //to be discussed which RAM variables are to read;
-                                tm_pointer = new Short_tm; //for now sending ACKL234
-                                tm_pointer->TM_string[0] = 0xB0;
-                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                tm_pointer->TM_string[2] = 0x01;
-                                tm_pointer->TM_string[3] = ACKCODE; 
-                                tm_pointer->TM_string[4] = 0x01;
-                                for(uint8_t i=0;i<6;i++)
-                                {
-                                    tm_pointer->TM_string[i+5] = 0;
-                                }
-                                crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                return tm;
-                            }
-                            else 
+                           
+                         
+                            default: 
                             {
                                 printf("INVALID TC\r\n"); //Send Invalid TC Telemetry
                                 tm_pointer = new Short_tm;
@@ -258,27 +198,28 @@
                                 return tm;                               
                             }
                             break;
-                        }
-                        case 5:
-                        {
-                            printf("WRITE ON MEMORY\r\n");
-                            switch(GETpid(tc))
+                        
+                    }
+                       
+                      
+                       
+                    case 0x8:
+                    {
+                        printf("service:FUNCTION MANAGEMENT SERVICE\r\n"); 
+                        if(GETservice_subtype(tc)==0x1)
                             {
-                                case 0:
+                                
+                              if(GETpid(tc)==0x01)
                                 {
-                                    printf("WR_S_FLASH\r\r\n");
-                                    mid = ((uint16_t)tc->TC_string[4]);
-                                    block = (((uint32_t)(tc->TC_string[5])<<24)|((uint32_t)(tc->TC_string[5])<<16)|((uint32_t)(tc->TC_string[5])<<8)|((uint32_t)(tc->TC_string[5])));
-                                    FCTN_CDMS_WR_FLASH(mid,block);
-                                    tm_pointer = new Short_tm;
-                                    tm_pointer->TM_string[0] = 0xB0;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE;  //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    printf("TC_PL_INIT\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -286,18 +227,36 @@
                                     return tm;
                                     break;
                                 }
-                                case 1:
+                                
+                                else if(GETpid(tc)==0x02)
                                 {
-                                    printf("WR_S_RAM\r\r\n"); //RAM variables to be written is to be decided
+                                    printf("TC_PL_MAIN\r\n"); // call PWR_SWCH_ON function
                                     tm_pointer = new Short_tm; 
                                     tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                               else if(GETpid(tc)==0x03)
+                                {
+                                    printf("TC_COM_INIT\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -305,44 +264,36 @@
                                     return tm;
                                     break;
                                 }
-                                case 16:
+                                else if(GETpid(tc)==0x04)
                                 {
-                                    printf("WR_L_FLASH\r\n");
-                                    mid = ((uint16_t)tc->TC_string[4]);
-                                    for(uint8_t i=0;i<32;i++)
+                                    printf("TC_CDMS_HK_MAIN\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        block = (((uint32_t)(tc->TC_string[5])<<24)|((uint32_t)(tc->TC_string[5])<<16)|((uint32_t)(tc->TC_string[5])<<8)|((uint32_t)(tc->TC_string[5])));
-                                        FCTN_CDMS_WR_FLASH(mid+i,block);
-                                    }
-                                    tm_pointer = new Short_tm;
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
-                                    {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
                                     return tm;
                                     break;
-                                    break;
+                                   
                                 }
-                                case 17:
+                                else if(GETpid(tc)==0x11)
                                 {
-                                    printf("WR_L_RAM\r\r\n"); //RAM variables to be written is to be decided
+                                    printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
                                     tm_pointer = new Short_tm; 
                                     tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -350,18 +301,71 @@
                                     return tm;
                                     break;
                                 }
-                                default:
+                                else if(GETpid(tc)==0x12)
                                 {
-                                    printf("INVALID TC"); //send invalid TC TM
+                                    printf("TC_SW_ON_RTC\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x13)
+                                {
+                                    printf("TC_SW_ON_BAE\r\n"); // call PWR_SWCH_ON function
                                     tm_pointer = new Short_tm; 
                                     tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x14)
+                                {
+                                    printf("TC_SW_ON_PL_DL\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                 else if(GETpid(tc)==0x15)
+                                {
+                                    printf("TC_SW_ON_PL_AG_HV\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -369,254 +373,108 @@
                                     return tm;
                                     break;
                                 }
-                            }
-                            break;
-                        }
-                        // case 6:
-                        // {
-                        //     printf("WRITE FROM ONE MEMORY TO ANOTHER\r\r\n");
-                        //     switch(appdata[0])
-                        //     {
-                        //         case 0:
-                        //         {
-                        //             printf("WR_S_FLASH\r\r\n");
-                        //             telemetry_type2[0]=0XB0;
-                        //             telemetry_type2[1]=tc_pac_seq_cnt_tm;
-                        //             telemetry_type2[2]=tm_pac_seq_cnt_tm;
-                        //             telemetry_type2[3]=ackcode_tm;
-                        //             for(int i=4;i<11;i++)
-                        //             {
-                        //                 telemetry_type2[i]=0X00;
-                        //             }
-                        //             break;
-                        //         }
-                        //         case 1:
-                        //         {
-                        //             printf("WR_S_RAM\r\r\n");
-                        //             telemetry_type2[0]=0XB0;
-                        //             telemetry_type2[1]=tc_pac_seq_cnt_tm;
-                        //             telemetry_type2[2]=tm_pac_seq_cnt_tm;
-                        //             telemetry_type2[3]=ackcode_tm;
-                        //             for(int i=4;i<11;i++)
-                        //             {
-                        //                 telemetry_type2[i]=0X00;
-                        //             }
-                        //             break;
-                        //         }
-                        //         case 16:
-                        //         {
-                        //             printf("WR_L_FLASH\r\r\n");
-                        //             telemetry_type2[0]=0XB0;
-                        //             telemetry_type2[1]=tc_pac_seq_cnt_tm;
-                        //             telemetry_type2[2]=tm_pac_seq_cnt_tm;
-                        //             telemetry_type2[3]=ackcode_tm;
-                        //             for(int i=4;i<11;i++)
-                        //             {
-                        //                 telemetry_type2[i]=0X00;
-                        //             }
-                        //             break;
-                        //         }
-                        //         case 17:
-                        //         {
-                        //             printf("WR_L_RAM\r\r\n");
-                        //             telemetry_type2[0]=0XB0;
-                        //             telemetry_type2[1]=tc_pac_seq_cnt_tm;
-                        //             telemetry_type2[2]=tm_pac_seq_cnt_tm;
-                        //             telemetry_type2[3]=ackcode_tm;
-                        //             for(int i=4;i<11;i++)
-                        //             {
-                        //                 telemetry_type2[i]=0X00;
-                        //             }
-                        //             break;
-                        //         }
-                        //         default:
-                        //         {
-                        //             printf("INVALID TC"); //send invalid TC TM
-                        //             break;
-                        //         }
-                        //     }
-                        //     break;
-                        // }
-                        case 9:
-                        {
-                            printf("SEND CRC OF FLASH MEMORY\r\n");
-                            if(GETpid(tc)==2)
-                            {
-                                printf("GET_FLASH_CRC\r\n");
-                                mid1 = ((((uint16_t)tc->TC_string[5])<<8)|(uint16_t)(tc->TC_string[6]));
-                                mid2 = ((((uint16_t)tc->TC_string[7])<<8)|(uint16_t)(tc->TC_string[8]));
-                                Nbytes = (((mid2-mid1)*32)/8)+4;
-                                uint8_t data[4*Nbytes];
-                                uint8_t flash_counter = 0;
-                                tm_pointer = new Long_tm;
-                                tm_pointer->TM_string[0] = 0x80;
-                                tm_pointer->TM_string[1] = 0x00; //spare
-                                tm_pointer->TM_string[2] = GETpacket_seq_count(tc);
-                                tm_pointer->TM_string[3] = (uint8_t)(flash_counter);
-                                for(uint8_t i=0;i<mid2-mid1+1;i++)
-                                {
-                                    Data = FCTN_CDMS_RD_FLASH(mid1+i);
-                                    data[4*i] = (uint8_t)((Data&0xFF000000)>>24);
-                                    data[4*i+1] = (uint8_t)((Data&0xFF000000)>>16);
-                                    data[4*i+2] = (uint8_t)((Data&0xFF000000)>>8);
-                                    data[4*i+3] = (uint8_t)(Data&0xFF000000);
-                                }
-                                nbytes = Nbytes;
-                                while(nbytes>=32)
-                                {
-                                    crc16 = CRC::crc16_gen((data+32*flash_counter),32);
-                                    tm_pointer->TM_string[11+2*flash_counter] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12+2*flash_counter] = (uint8_t)(crc16&0x00FF);
-                                    nbytes-=32;
-                                    flash_counter++;
-                                }
-                                if(nbytes>0)
-                                {
-                                    crc16 = CRC::crc16_gen((data+32*flash_counter),nbytes);
-                                    tm_pointer->TM_string[11+2*flash_counter] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12+2*flash_counter] = (uint8_t)(crc16&0x00FF);
-                                }
-                                return tm;
-                            }
-                            else 
-                            {
-                                printf("INVALID TC"); //Send Invalid TC Telemetry
-                                tm_pointer = new Short_tm; 
-                                tm_pointer->TM_string[0] = 0xB0; 
-                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                tm_pointer->TM_string[2] = 0x01;
-                                tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                tm_pointer->TM_string[4] = 0x01;
-                                for(uint8_t i=0;i<6;i++)
+                                 else if(GETpid(tc)==0x16)
                                 {
-                                    tm_pointer->TM_string[i+5] = 0;
-                                }
-                                crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                return tm;
-                            }
-                            break;
-                        }
-                        case 8:
-                        {
-                            printf("service:FUNCTION MANAGEMENT SERVICE\r\n"); 
-                            if(GETservice_subtype(tc)==1)
-                            {
-                                if((GETpid(tc)&0xF0)==0x00)
-                                {
-                                    printf("RUN_PRCS\r\n");
-                                    switch(GETpid(tc))
+                                    printf("TC_SW_ON_V_A_EN\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        case 1:
-                                        {
-                                            printf("P_PL_INIT\r\n"); //call P_PL_INIT using CDMS_RUN_PRCS
-                                            tm_pointer = new Short_tm; 
-                                            tm_pointer->TM_string[0] = 0xB0; 
-                                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                            tm_pointer->TM_string[2] = 0x01;
-                                            tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                            tm_pointer->TM_string[4] = 0x01;
-                                            for(uint8_t i=0;i<6;i++)
-                                            {
-                                                tm_pointer->TM_string[i+5] = 0;
-                                            }
-                                            crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                            return tm;
-                                            break;
-                                        }
-                                        case 2:
-                                        {
-                                            printf("P_PL_MNGR\r\n"); //call P_PL_MNGR using CDMS_RUN_PRCS
-                                            tm_pointer = new Short_tm; 
-                                            tm_pointer->TM_string[0] = 0xB0; 
-                                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                            tm_pointer->TM_string[2] = 0x01;
-                                            tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                            tm_pointer->TM_string[4] = 0x01;
-                                            for(uint8_t i=0;i<6;i++)
-                                            {
-                                                tm_pointer->TM_string[i+5] = 0;
-                                            }
-                                            crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                            return tm;
-                                            break;
-                                        }
-                                        case 3:
-                                        {
-                                            printf("P_COM_INIT\r\n"); //call P_COM_INIT using CDMS_RUN_PRCS
-                                            tm_pointer = new Short_tm; 
-                                            tm_pointer->TM_string[0] = 0xB0; 
-                                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                            tm_pointer->TM_string[2] = 0x01;
-                                            tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                            tm_pointer->TM_string[4] = 0x01;
-                                            for(uint8_t i=0;i<6;i++)
-                                            {
-                                                tm_pointer->TM_string[i+5] = 0;
-                                            }
-                                            crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                            return tm;
-                                            break;
-                                        }
-                                        case 4:
-                                        {
-                                            printf("P_CDMS_HK_MNGR\r\n"); //call P_CDMS_HK_MNGR using CDMS_RUN_PRCS
-                                            tm_pointer = new Short_tm; 
-                                            tm_pointer->TM_string[0] = 0xB0; 
-                                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                            tm_pointer->TM_string[2] = 0x01;
-                                            tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                            tm_pointer->TM_string[4] = 0x01;
-                                            for(uint8_t i=0;i<6;i++)
-                                            {
-                                                tm_pointer->TM_string[i+5] = 0;
-                                            }
-                                            crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                            return tm;
-                                            break;
-                                        }
-                                        default:
-                                        {
-                                            printf("INVALID TC\r\n");
-                                            tm_pointer = new Short_tm; 
-                                            tm_pointer->TM_string[0] = 0xB0; 
-                                            tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                            tm_pointer->TM_string[2] = 0x01;
-                                            tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                            tm_pointer->TM_string[4] = 0x01;
-                                            for(uint8_t i=0;i<6;i++)
-                                            {
-                                                tm_pointer->TM_string[i+5] = 0;
-                                            }
-                                            crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                            return tm;
-                                            break;
-                                        }
+                                        tm_pointer->TM_string[i] = 0;
                                     }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                   
                                 }
-                                else if((GETpid(tc)&0xF0)==0x10)
+                                else if(GETpid(tc)==0x21)
                                 {
-                                    printf("PWR_SWCH_ON\r\n"); // call PWR_SWCH_ON function
+                                    printf("TC_SW_OFF_SD\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x22)
+                                {
+                                    printf("TC_SW_OFF_RTC\r\n"); // call PWR_SWCH_ON function
                                     tm_pointer = new Short_tm; 
                                     tm_pointer->TM_string[0] = 0xB0; 
                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x23)
+                                {
+                                    printf("TC_SW_OFF_BAE\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x24)
+                                {
+                                    printf("TC_SW_OFF_PL_DL\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                 else if(GETpid(tc)==0x25)
+                                {
+                                    printf("TC_SW_OFF_PL_AG_HV\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -624,18 +482,53 @@
                                     return tm;
                                     break;
                                 }
-                                else if((GETpid(tc)&0xF0)==0x20)
+                                 else if(GETpid(tc)==0x26)
                                 {
-                                    printf("PWR_SWCH_OFF\r\n"); // call PWR_SWCH_OFF function
+                                    printf("TC_SW_OFF_V_A_EN\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x31)
+                                {
+                                    printf("TC_RST_SD\r\n"); // call PWR_SWCH_ON function
                                     tm_pointer = new Short_tm; 
                                     tm_pointer->TM_string[0] = 0xB0; 
                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0x32)
+                                {
+                                    printf("TC_RST_RTC\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -643,18 +536,17 @@
                                     return tm;
                                     break;
                                 }
-                                else if((GETpid(tc)&0xF0)==0x20) 
+                                else if(GETpid(tc)==0x33)
                                 {
-                                    printf("PWR_RST\r\n"); // call PWR_RST function
+                                    printf("TC_RST_BAE\r\n"); // call PWR_SWCH_ON function
                                     tm_pointer = new Short_tm; 
                                     tm_pointer->TM_string[0] = 0xB0; 
                                     tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
                                     {
-                                        tm_pointer->TM_string[i+5] = 0;
+                                        tm_pointer->TM_string[i] = 0;
                                     }
                                     crc16 = CRC::crc16_gen(tm->TM_string,11);
                                     tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
@@ -662,7 +554,44 @@
                                     return tm;
                                     break;
                                 }
-                                else if((GETpid(tc)&0xF0)==0xE0)
+                                else if(GETpid(tc)==0x34)
+                                {
+                                    printf("TC_RST_PL_DL\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                
+                                }
+                                else if(GETpid(tc)==0xC1)
+                                {
+                                    printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
+                                    tm_pointer = new Short_tm; 
+                                    tm_pointer->TM_string[0] = 0xB0; 
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
+                                    tm_pointer->TM_string[2] = ACKCODE;
+                                    
+                                    for(uint8_t i=3;i<11;i++)
+                                    {
+                                        tm_pointer->TM_string[i] = 0;
+                                    }
+                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
+                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                                    return tm;
+                                    break;
+                                }
+                                else if(GETpid(tc)==0xF1)
                                 {
                                     printf("RD_RTC\r\n"); //call RD_RTC
                                     tm_pointer = new Short_tm;
@@ -683,22 +612,7 @@
                                     tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
                                     return tm;
                                 }
-                                else if((GETpid(tc)&0xF0)==0xF0)
-                                {
-                                    printf("SD_MNGR\r\n"); //call SD_MNGR function and send required information in FMS TM
-                                    tm_pointer = new Short_tm; // for now sending ACK L_234
-                                    tm_pointer->TM_string[0] = 0xD0;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0; // for now filling with zeroes instead of SD_MNGR Data
-                                    }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    return tm;
-                                }
+                                
                                 else 
                                 {
                                     printf("INVALID TC\r\n");
@@ -718,113 +632,12 @@
                                     return tm;
                                 }
                             }
-                            default:
-                            {
-                                printf("INVALID TC\r\n");
-                                tm_pointer = new Short_tm; 
-                                tm_pointer->TM_string[0] = 0xB0; 
-                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                tm_pointer->TM_string[2] = 0x01;
-                                tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                tm_pointer->TM_string[4] = 0x01;
-                                for(uint8_t i=0;i<6;i++)
-                                {
-                                    tm_pointer->TM_string[i+5] = 0;
-                                }
-                                crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                return tm;
-                                break;
-                            }
-                            break;
+                           
                         }
-                        case 7:
-                        {
-                            printf("service:PAYLOAD MANAGEMENT SERVICE\r\r\n"); //TM is ACKL234
-                            switch(GETservice_subtype(tc))
-                            {
-                                case 1:
-                                {
-                                    printf("INSERT PL SCHEDULE\r\n"); //should be called PL_MAIN 
-                                    tm_pointer = new Short_tm;  // for now sending dummy ACK L_234
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
-                                    {
-                                        tm_pointer->TM_string[i+5] = 0;
-                                    }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    return tm;
-                                    break;
-                                }
-                                case 2:
-                                {
-                                    printf("DELETE PL SCHEDULE\r\n"); //Should be called PL_MAIN 
-                                    tm_pointer = new Short_tm; // for now sending dummy ACK L_234
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
-                                    {
-                                        tm_pointer->TM_string[i+5] = 0;
-                                    }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    return tm;
-                                    break;
-                                }
-                                case 3:
-                                {
-                                    printf("REPORT PL SCHEDULE\r\n"); //Data is taken by calling PL_MAIN
-                                    tm_pointer = new Long_tm; //for now sending dummy PMS
-                                    tm_pointer->TM_string[0] = 0x48;
-                                    tm_pointer->TM_string[1] = 0x00; //spare 8 bits 
-                                    tm_pointer->TM_string[2] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[3] = 0x01;
-                                    for(uint8_t i=4;i<132;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0; // for now filling with zeros instead of PL_SCHEDULE
-                                    }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                    tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                                    return tm;
-                                    break;
-                                }
-                                default:
-                                {
-                                    printf("INVALID TC"); //send invalid TC TM
-                                    tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
-                                    {
-                                        tm_pointer->TM_string[i+5] = 0;
-                                    }
-                                    crc16 = CRC::crc16_gen(tm->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    return tm;
-                                    break;
-                                }
-                            }
-                            break;
-                        }
-                    }
-                    break;
-                }
+                       
+                    
+                  
+                
                 default:
                 {
                     printf("INVALID TC"); //send invalid TC TM
@@ -866,6 +679,7 @@
             // } // similar to BAE functions
             break;
         }
+       
         default: //invalid TC
         {
             printf("INVALID TC\r\n");
@@ -884,6 +698,9 @@
             tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
             return tm;
             break;
-        }
-     }
+       }
+
+     
+}
+}
 }
\ No newline at end of file
diff -r bcbd76c86cde -r ad3b8a8032e2 cdms_sd.cpp
--- a/cdms_sd.cpp	Wed Dec 16 09:06:59 2015 +0000
+++ b/cdms_sd.cpp	Thu Jan 21 14:46:28 2016 +0000
@@ -1,5 +1,7 @@
 #include "cdms_sd.h"
 
+
+
 SPI spi_sd(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
 DigitalOut cs_sd(PTE22);
 
@@ -9,22 +11,189 @@
 uint64_t sd_sectors();
 uint64_t sectors;
 
-void FCTN_SD_MNGR()
+void FCTN_CDMS_SD_INIT()
+{
+   
+    initialise_card();
+    disk_initialize();
+}
+
+    
+
+uint32_t  FCTN_SD_MNGR(uint8_t sid)
+
+{
+   
+    uint32_t SD_MNG_SECT=7000;
+    
+    uint32_t fsc;
+    uint8_t buffer[512];
+    int b=disk_read(buffer, SD_MNG_SECT);
+    if(sid==0x0)
+    {
+        
+        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);
+    }
+    if(sid==0x1)
+    {
+        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);
+    }
+    if(sid==0x2)
+    {
+        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);
+    }
+     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)
+    {
+        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);
+    }
+    return fsc;
+}
+
+
+int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid)
 {
-    /*Size of block of SD card for 2GB = 512B, 4 , 8 GB SD card. We will prefer 8 GB.
-    SD allocation. Assuming 8GB
-    SCP: 600 MB -122880
-    SFF-AT: 2 GB -4194304
-    SFF-BT: 5 GB -10485760
-    HK-ARCH:100 MB -204800
-    LOG: 50MB -102400
-    SD card management: 50MB - 102400*/
-       
+   
+    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)
+    {
+        block_number=SD_SCP_FIRST+fsc;
+        printf("write_block_number=%d\r\n",block_number);
+        result= disk_write(buffer,block_number);
+        return result;
+    }
+   if(sid==0x1)
+    {
+        block_number=SD_SFF_AT_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);
+        return result;
+    }
+     if(sid==0x3)
+    {
+        block_number=SD_HK_ARCH_FIRST+fsc;
+        sd1.printf("Block number is %d \r\n",block_number);
+        result= disk_write(buffer,block_number);
+        return result;
+    }
+     if(sid==0x4)
+    {
+        block_number=LOG_FIRST +fsc;
+        result= disk_write(buffer,block_number);
+        return result;
+    }
+    return 1;
 }
+
+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)
+    {
+        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)
+    {
+        block_number=SD_SFF_AT_FIRST + fsc;
+        result= disk_read(buffer,block_number);
+    }
+    else if(sid==0x2)
+    {
+        block_number=SD_SFF_BT_FIRST + fsc;
+        result= disk_read(buffer,block_number);
+    }
+    else if(sid==0x3)
+    {
+        block_number=SD_HK_ARCH_FIRST + fsc;
+        result= disk_read(buffer,block_number);
+    }
+    else if(sid==0x4)
+    {
+        block_number=LOG_FIRST +fsc;
+        result= disk_read(buffer,block_number);
+    }
+    else
+    {
+        return 1;
+    }
+    return result;
+}
+
+
 int initialise_card()
 {
     // Set to 100kHz for initialisation, and clock card with cs_sd = 1
-    spi_sd.frequency(100000);
+    spi_sd.frequency(100000);           // changed on 31 12 2015 to 1 MHz 
     cs_sd = 1;
     for (int i = 0; i < 16; i++) {
         spi_sd.write(0xFF);
diff -r bcbd76c86cde -r ad3b8a8032e2 cdms_sd.h
--- a/cdms_sd.h	Wed Dec 16 09:06:59 2015 +0000
+++ b/cdms_sd.h	Thu Jan 21 14:46:28 2016 +0000
@@ -13,6 +13,14 @@
 #define R1_ADDRESS_ERROR        (1 << 5)
 #define R1_PARAMETER_ERROR      (1 << 6)
 
+
+
+#define SD_MAX_CYCLES 10000
+
+extern uint8_t SD_INIT_FLAGS;
+
+
+
 int initialise_card();
 int initialise_card_v1();
 int initialise_card_v2();
@@ -24,9 +32,7 @@
 //void FCTN_CDMS_INIT_SD();
 //int FCTN_CDMS_WR_SD(const uint8_t *, uint64_t);
 //int FCTN_CDMS_RD_SD(uint8_t *, uint64_t);
-void FCTN_SD_MNGR();
-
-
+uint32_t FCTN_SD_MNGR(uint8_t);
 int cmd(int, int);
 int cmd58();
 int cmdx(int, int);
@@ -34,7 +40,9 @@
 int read(uint8_t*, uint32_t );
 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 SD_READ(uint8_t*,uint32_t,uint8_t);
 #define SDCARD_FAIL 0
 #define SDCARD_V1   1
 #define SDCARD_V2   2
diff -r bcbd76c86cde -r ad3b8a8032e2 i2c.cpp
--- a/i2c.cpp	Wed Dec 16 09:06:59 2015 +0000
+++ b/i2c.cpp	Thu Jan 21 14:46:28 2016 +0000
@@ -2,6 +2,7 @@
 #include "pinconfig.h"
 #include "rtos.h"
 #include "i2c.h"
+
 DigitalOut irpt_2_slv(PIN67);                                        //I2C interrupt to CDMS
 I2C master(PIN32,PIN31);
 DigitalOut led1(PIN46);
@@ -37,11 +38,11 @@
 }
 
 
-void FCTN_I2C_READ(char *data)
+void FCTN_I2C_READ(char *data,int length)
 {
     irpt_2_slv = 1; 
     t_read.start();
-    read_ack = master.read(addr|1,data,tm_len);
+    read_ack = master.read(addr|1,data,length);
     t_read.stop();
    
     //if(read_ack == 0)
diff -r bcbd76c86cde -r ad3b8a8032e2 i2c.h
--- a/i2c.h	Wed Dec 16 09:06:59 2015 +0000
+++ b/i2c.h	Thu Jan 21 14:46:28 2016 +0000
@@ -1,4 +1,4 @@
-void FCTN_I2C_READ(char*);
+void FCTN_I2C_READ(char*,int);
 void FCTN_I2C_WRITE(char*);
 #define tm_len 134
 #define tc_len 135
diff -r bcbd76c86cde -r ad3b8a8032e2 main.cpp
--- a/main.cpp	Wed Dec 16 09:06:59 2015 +0000
+++ b/main.cpp	Thu Jan 21 14:46:28 2016 +0000
@@ -6,7 +6,10 @@
 #include "i2c.h"
 #include "cdms_rtc.h"
 #include "main_funcs.h"
+#include "CDMS_HK.h"
 #include "TMTC.h"
+#include "main.h"
+
 
 Serial pc(USBTX,USBRX);
 
@@ -14,6 +17,9 @@
 Thread *ptr_t_tmtc;
 
 InterruptIn pl_sc_data(PIN81);
+InterruptIn cdms_rcv_tc(PIN38);
+extern DigitalOut irpt_2_slv;
+
 
 /* only for sd testing*/
 uint8_t write_to_sd[512];
@@ -23,11 +29,14 @@
 Base_tc *tc_test = new Long_tc;
 Base_tm *tm_test = new Long_tm;
 
+
+
+
 void TSC_HK_BAE_CDMS_PL_MODE(void const *args)
 {
-    BAE_HK();
-    CDMS_HK();
-    PL_MODE();
+    
+    FCTN_CDMS_HK_MAIN();
+   
 }
 
 void BAE_HK()
@@ -35,35 +44,31 @@
     printf("\n\rBAE_HK Function Executed\r\n");
 }
 
-void CDMS_HK()
-{
-    printf("\n\rCDMS_HK Function Executed\r\n");
-    uint64_t time = FCTN_CDMS_RD_RTC();
-    printf("\n\r0x%016llx\n\r", time);
-    disk_write(write_to_sd,4);
-    disk_read(read_from_sd,4);
-    for(int i=0;i<512;i++)
-    {
-        printf("%d",read_from_sd[i]);
-    }
-}
+
 
 void PL_MODE()
 {
     printf("\n\rPL_MODE Function Executed\r\n");
-    ptr_t_tmtc->signal_set(0x3);
+   
 }
 
 void T_CDMS_RLY_TMTC(void const *args)
 {
-    while(1)
-    {
-        Thread::signal_wait(0x3);
-        printf("\rin T_CDMS_RLY_TM\r\n");
-        tc_test->TC_string[0] = 43;
-        tc_test->TC_string[1] = 81;
-        tc_test->TC_string[2] = 97;
-        tc_test->TC_string[3] = 80;
+   
+        
+        printf("\rin T_CDMS_RLY_TMTC\r\n");
+        printf("\r \n Enter a valid TC \n\r");
+        uint8_t read;
+        /* for(int i=0;i <4 ; i++)
+         {
+             scanf("%d",&tc_test->TC_string[i]);
+          }*/
+     
+    tc_test->TC_string[0] = 43;
+    tc_test->TC_string[1] = 0x40;
+    tc_test->TC_string[2] = 0x81;
+    tc_test->TC_string[3] = 0x13;
+        
         for(int i=4;i < 135 ; i++)
             tc_test->TC_string[i] = 43; 
         printf("%s", tc_test->TC_string);
@@ -75,7 +80,7 @@
 //            printf("%c", tm_test->TM_string[i]);
 //        }
         //FUNC_CDMS_RLY_TM();
-    }
+    
 }
 
 void T_PL_RCV_SC_DATA(void const *args)
@@ -83,15 +88,16 @@
     while(1)
     {
         Thread::signal_wait(0x1);
-        FCTN_PL_RCV_SC_DATA();
+        //uint8_t* buff=FCTN_PL_RCV_SC_DATA();
+        uint8_t* buff;
         FCTN_COM_COMP_SC_DATA();
         FCTN_CDMS_RD_RTC();
-        disk_write(write_to_sd,2);
-        disk_read(read_from_sd,2);
-        for(int i=0;i<512;i++)
+        uint8_t fsc=FCTN_SD_MNGR(0x0);
+        SD_WRITE(buff,fsc,0x0);
+        /*for(int i=0;i<512;i++)
         {
             printf("%d",read_from_sd[i]);
-        }
+        }*/
     }
 }
 
@@ -100,36 +106,45 @@
     ptr_t_sc_data->signal_set(0x1);
 }
 
-void FCTN_PL_RCV_SC_DATA()
+/*uint8_t* FCTN_PL_RCV_SC_DATA()
 {
     printf("\n\rFCTN_PL_RCV_SC_DATA Function Executed\r\n");
-}
+    uint8_t buff[512];
+    for(int i=0;i<512;i++)
+        {
+            buff[i]=0x32;
+        }
+    return buff;
+    
+} */
 
 void FCTN_COM_COMP_SC_DATA()
 {
     printf("\n\rFCTN_COM_COMP_SC_DATA Function Executed\r\n");
 }
 
-void FCTN_CDMS_WR_SD()
+
+
+void ISR_TMTC_THREAD()
 {
-    printf("\n\rFCTN_CDMS_WR_SD Function Executed\r\n");
+    ptr_t_tmtc->signal_set(0x3);
 }
+    
 
 int main()
 {
-    printf("\n\r CDMS Activated \r\n");
-    initialise_card();
-    disk_initialize();
+    irpt_2_slv=0;
+    FCTN_CDMS_SD_INIT();
     FCTN_CDMS_INIT_RTC();
-    for(int i=0;i<512;i++)
-    {
-        write_to_sd[i] = i;
-    }     
-    RtosTimer TIMER_HK_ACQ(TSC_HK_BAE_CDMS_PL_MODE,osTimerPeriodic);
-    TIMER_HK_ACQ.start(10000);
+    RtosTimer TIMER_HK_ACQ(T_CDMS_RLY_TMTC,osTimerPeriodic);
+    TIMER_HK_ACQ.start(20000);
     ptr_t_tmtc = new Thread (T_CDMS_RLY_TMTC);
     ptr_t_sc_data = new Thread (T_PL_RCV_SC_DATA);
     pl_sc_data.rise(&ISR_PL_RCV_SC_DATA);
-
-    while(1);
+    cdms_rcv_tc.rise(&ISR_TMTC_THREAD);
+   while(1)
+    {
+        Thread::wait(10);
+        
+    }
 }
\ No newline at end of file
diff -r bcbd76c86cde -r ad3b8a8032e2 main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Thu Jan 21 14:46:28 2016 +0000
@@ -0,0 +1,7 @@
+uint8_t SD_INIT_FLAGS;
+uint8_t CDMS_HK_STATUS;
+uint8_t Power_level;
+uint8_t Att_level;
+char CDMS_HK[8];
+
+
diff -r bcbd76c86cde -r ad3b8a8032e2 main_funcs.h
--- a/main_funcs.h	Wed Dec 16 09:06:59 2015 +0000
+++ b/main_funcs.h	Thu Jan 21 14:46:28 2016 +0000
@@ -1,5 +1,4 @@
 void BAE_HK();
-void CDMS_HK();
 void PL_MODE();
-void FCTN_PL_RCV_SC_DATA();
+//uint8_t* FCTN_PL_RCV_SC_DATA();
 void FCTN_COM_COMP_SC_DATA();
\ No newline at end of file
diff -r bcbd76c86cde -r ad3b8a8032e2 mbed.bld
--- a/mbed.bld	Wed Dec 16 09:06:59 2015 +0000
+++ b/mbed.bld	Thu Jan 21 14:46:28 2016 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c
\ No newline at end of file
diff -r bcbd76c86cde -r ad3b8a8032e2 sd_flags.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sd_flags.h	Thu Jan 21 14:46:28 2016 +0000
@@ -0,0 +1,5 @@
+#include "mbed.h"
+
+#define SD_INIT_STATUS 0X01
+#define SD_INIT_FAIL_STATUS 0X02
+