hi

Dependencies:   FreescaleIAP mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
cholletisaik777
Date:
Wed Dec 16 09:06:59 2015 +0000
Commit message:
nnb

Changed in this revision

Flash.cpp Show annotated file Show diff for this revision Revisions of this file
Flash.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
TMTC.h Show annotated file Show diff for this revision Revisions of this file
cdms_rtc.cpp Show annotated file Show diff for this revision Revisions of this file
cdms_rtc.h Show annotated file Show diff for this revision Revisions of this file
cdms_sd.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
crc.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_funcs.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib 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
pinconfig.h Show annotated file Show diff for this revision Revisions of this file
structure.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r bcbd76c86cde Flash.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flash.cpp	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,31 @@
+#include "mbed.h"
+#include "FreescaleIAP.h"
+#include "Flash.h"
+
+int strt_add = flash_size() - (4*SECTOR_SIZE);  
+uint32_t flasharray[256+(3*1024)];
+char *nativeflash = (char*)strt_add;
+
+/*Writing to the Flash*/
+void FCTN_CDMS_WR_FLASH(uint16_t j,uint32_t block)
+{
+    for(int64_t i=0;i<(256+(3*1024));i++)
+    {
+        flasharray[i]=nativeflash[i];
+    }
+    flasharray[j]=block;
+    erase_sector(strt_add);
+    program_flash(strt_add, (char*)&flasharray,4*(256+(1024*3)));
+}
+/*End*/
+
+/*Reading from Flash*/
+uint32_t FCTN_CDMS_RD_FLASH(uint16_t j)
+{
+    for(int64_t i=0;i<(256+(3*1024));i++)
+    {
+        flasharray[i]=nativeflash[i];
+    }
+    return flasharray[j];
+}
+/*End*/
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde Flash.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Flash.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,2 @@
+void FCTN_CDMS_WR_FLASH(uint16_t j,uint32_t block);
+uint32_t FCTN_CDMS_RD_FLASH(uint16_t j);
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde FreescaleIAP.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FreescaleIAP.lib	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Sissors/code/FreescaleIAP/#474d231b2f35
diff -r 000000000000 -r bcbd76c86cde TMTC.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMTC.cpp	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,889 @@
+#include "structure.h"
+#include "i2c.h"
+#include "pinconfig.h"
+#include "crc.h"
+#include "TMTC.h"
+#include "cdms_rtc.h"
+#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
+
+uint8_t received = 0;
+uint16_t mid1;
+uint16_t mid2;
+uint16_t Nbytes;
+uint32_t Data;
+uint16_t nbytes;
+uint8_t flash_counter = 0;
+uint16_t crc16;
+uint16_t mid;
+uint32_t block;
+
+Base_tm *FCTN_CDMS_RLY_TMTC(Base_tc *tc)
+{
+    printf("\rTC execution in progress\r\n");
+    Base_tm *tm = new Long_tm;
+    Base_tm *tm_pointer = tm;
+    received = 0;
+    switch(GETapid(tc))
+    {
+        case 1: //apid=01 implies it corresponds to bae
+        {
+            printf("Telecommand is for BAE\r\n");
+            printf("Sending TC to BAE...\r\n"); //interrupt to be sent to the bae
+            FCTN_I2C_WRITE((char*)tc->TC_string);
+            while(1)
+            {
+                wait(1); //TimeOut instead of wait
+                if(tm_status_4m_slv == 1)
+                {
+                    printf("receiving...\r\n");
+                    FCTN_I2C_READ((char*)tm_pointer->TM_string);
+                    printf("%s", tm_pointer->TM_string);
+                    received+=1;
+                    tm_pointer = tm_pointer->next_TM;
+                    break; //only for testing purpose
+                }
+                else
+                {
+                    break;
+                }
+            }
+            if(received >= 1 )
+            {
+                printf("Telemetry is received from BAE\r\n");
+                Base_tm *tm_print = tm;
+                for(uint8_t i=0;i<received;i++)
+                {   
+                    printf("%s", tm_print->TM_string);
+                    tm_print = tm_print->next_TM; //for check
+                }
+                received  = 0;
+                return tm;
+            }
+            else
+            {
+                printf("Telemetry is not received from BAE\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;
+        }
+        case 2:
+        {
+            printf("Telecommand is for CDMS\r\n"); //apid = 10 corresponds to cdms
+            switch(GETservice_type(tc))
+            {
+                case 6:
+                {
+                    printf("service:MMS\r\n");
+                    switch(GETservice_subtype(tc))
+                    {
+                        case 1:
+                        {
+                            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)
+                                {
+                                    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++)
+                                    {
+                                        tm_pointer->TM_string[3+i] = data[i+128*flash_counter];
+                                    }
+                                    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[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[4+i] = data[i+128*flash_counter];
+                                    }
+                                    for(uint8_t i=nbytes;i<128;i++)
+                                    {
+                                        tm_pointer->TM_string[4+i] = 0;
+                                    }
+                                    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);
+                                }
+                                return tm;
+                            }
+                            else if (GETpid(tc)==0x22)
+                            {
+                                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)
+                                {
+                                    tm_pointer = new Short_tm;
+                                    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<8;i++)
+                                    {
+                                        tm_pointer->TM_string[3+i] = data[i+8*flash_counter];
+                                    }
+                                    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;
+                            }
+                            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 
+                            {
+                                printf("INVALID TC\r\n"); //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++)
+                                {
+                                    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 5:
+                        {
+                            printf("WRITE ON MEMORY\r\n");
+                            switch(GETpid(tc))
+                            {
+                                case 0:
+                                {
+                                    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++)
+                                    {
+                                        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 1:
+                                {
+                                    printf("WR_S_RAM\r\r\n"); //RAM variables to be written is to be decided
+                                    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 16:
+                                {
+                                    printf("WR_L_FLASH\r\n");
+                                    mid = ((uint16_t)tc->TC_string[4]);
+                                    for(uint8_t i=0;i<32;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;
+                                    }
+                                    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:
+                                {
+                                    printf("WR_L_RAM\r\r\n"); //RAM variables to be written is to be decided
+                                    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"); //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;
+                        }
+                        // 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++)
+                                {
+                                    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))
+                                    {
+                                        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;
+                                        }
+                                    }
+                                }
+                                else if((GETpid(tc)&0xF0)==0x10)
+                                {
+                                    printf("PWR_SWCH_ON\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[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;
+                                }
+                                else if((GETpid(tc)&0xF0)==0x20)
+                                {
+                                    printf("PWR_SWCH_OFF\r\n"); // call PWR_SWCH_OFF 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[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;
+                                }
+                                else if((GETpid(tc)&0xF0)==0x20) 
+                                {
+                                    printf("PWR_RST\r\n"); // call PWR_RST 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[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;
+                                }
+                                else if((GETpid(tc)&0xF0)==0xE0)
+                                {
+                                    printf("RD_RTC\r\n"); //call RD_RTC
+                                    tm_pointer = new Short_tm;
+                                    tm_pointer->TM_string[0] = 0xD0;
+                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc);
+                                    tm_pointer->TM_string[2] = 0x01;
+                                    uint64_t time = FCTN_CDMS_RD_RTC();
+                                    tm_pointer->TM_string[3] = (uint8_t)((time&0xFF00000000000000)>>56);
+                                    tm_pointer->TM_string[4] = (uint8_t)((time&0x00FF000000000000)>>48);
+                                    tm_pointer->TM_string[5] = (uint8_t)((time&0x0000FF0000000000)>>40);
+                                    tm_pointer->TM_string[6] = (uint8_t)((time&0x000000FF00000000)>>32);
+                                    tm_pointer->TM_string[7] = (uint8_t)((time&0x00000000FF000000)>>24);
+                                    tm_pointer->TM_string[8] = (uint8_t)((time&0x0000000000FF0000)>>16);
+                                    tm_pointer->TM_string[9] = (uint8_t)((time&0x000000000000FF00)>>8);
+                                    tm_pointer->TM_string[10] = (uint8_t)(time&0x00000000000000FF);
+                                    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)&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");
+                                    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;
+                                }
+                            }
+                            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
+                    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("Telecommand is for PL\r\n");
+            // printf("Informing PL about TC using I2C\r\n"); //interrupt to be sent to the bae
+            // FCTN_I2C_WRITE((char*)tc->TC_string);
+            // printf("sent the Telecommand to BAE\r\n"); //packet along with crc will be sent when bae is ready
+            // wait(1); //wait time is to be optimised
+            // if(tm_status_4m_slv == 1)
+            // {
+            //     FCTN_I2C_READ((char*)tm->TM_string);
+            //     received = 1;
+            // }
+            // if(received == 1 )
+            // {
+            //     printf("Telemetry is received from BAE\r\n");
+            //     printf("%s", tm->TM_string);
+            //     received  = 0;
+            // } // similar to BAE functions
+            break;
+        }
+        default: //invalid TC
+        {
+            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;
+        }
+     }
+}
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde TMTC.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMTC.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,4 @@
+#define ACKCODE 0xFF
+#define TimeOut 0xFFFF
+
+Base_tm *FCTN_CDMS_RLY_TMTC(Base_tc * tc);
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde cdms_rtc.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cdms_rtc.cpp	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,154 @@
+#include "mbed.h"
+#include "cdms_rtc.h"
+
+SPI rtc_spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)                                                       
+DigitalOut rtc_cs(PTE29);         //Slave Select pin 
+
+void FCTN_CDMS_INIT_RTC()
+{
+    rtc_cs=1;
+    rtc_spi.format(8,0);
+    rtc_spi.frequency(1000000);
+    //clearing the halt bit 
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x8C);
+    rtc_spi.write(0x00);
+     
+    //clearing the OF bit 
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x8F);
+    rtc_spi.write(0x00);
+     
+    //century bits
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x80|0x03);
+    rtc_spi.write(0x00);
+ 
+    //Kick starting the oscillator 
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x81); //register address with write flag
+    rtc_spi.write(0x80);//enabling stop bit in the seconds register
+ 
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x81);
+    rtc_spi.write(0x00);//disabling the stop bit to restart the oscillator 
+ 
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x80); 
+    rtc_spi.write(0x01); // set milliseconds value to 00
+    rtc_cs=1;
+ 
+    rtc_cs=0;
+    rtc_spi.write(0x81); 
+    rtc_spi.write(0x01); //set seconds value to 00
+    rtc_cs=1;
+ 
+    rtc_cs=0;
+    rtc_spi.write(0x82); 
+    rtc_spi.write(0x01);//set minutes value to 00
+    rtc_cs=1;
+ 
+    rtc_cs=0;
+    rtc_spi.write(0x83); 
+    rtc_spi.write(0x23); //set the hours to 01
+    rtc_cs=1;
+    
+    rtc_cs=0;
+    rtc_spi.write(0x84); 
+    rtc_spi.write(0x01); //set day of the week to 01
+    rtc_cs=1;
+    
+    rtc_cs=0;
+    rtc_spi.write(0x85); 
+    rtc_spi.write(0x31); //set date of the month to 01
+    rtc_cs=1;
+    
+    rtc_cs=0;
+    rtc_spi.write(0x86); 
+    rtc_spi.write(0x12); //set month to 01
+    rtc_cs=1;
+    
+    rtc_cs=0;
+    rtc_spi.write(0x87); 
+    rtc_spi.write(0x01); //set year to 00(2000)
+    rtc_cs=1;
+    printf("\n\r rtc initalised \n");
+}
+ 
+uint64_t FCTN_CDMS_RD_RTC()
+{    
+    uint8_t response;
+    printf("\n\r Entered rtc\n");
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x00); //reading milliseconds register
+    response = rtc_spi.write(0x00); // read the value by sending dummy byte
+    uint8_t centiseconds =  (uint8_t(response&0xF0)>>4)*10+uint8_t(response&0x0F)*1;
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x01); //reading seconds register
+    response =rtc_spi.write(0x01);
+    uint8_t seconds =  ((response&0x70)>>4)*10+(response&0x0F)*1;
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x02); //reading minutes register
+    response =rtc_spi.write(0x01);
+    uint8_t minutes =  ((response&0xF0)>>4)*10+(response&0x0F)*1;
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x03); //reading hours register
+    response=rtc_spi.write(0x01);
+    uint8_t hours =  ((response&0x30)>>4)*10+(response&0x0F)*1;
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x04); //reading day's  register
+    uint8_t day =rtc_spi.write(0x01);
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x05); //reading date register
+    response =rtc_spi.write(0x01);
+    uint8_t date =  ((response&0x30)>>4)*10+(response&0x0F)*1;
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x06); //reading month registe
+    response =rtc_spi.write(0x01);
+    uint8_t month =  ((response&0x10)>>4)*10+(response&0x0F)*1;
+    
+    rtc_cs=1;
+    rtc_cs=0;
+    rtc_spi.write(0x07); //reading year's registe
+    response =rtc_spi.write(0x01);
+    uint8_t year =  ((response&0xF0)>>4)*10+(response&0x0F)*1;
+    rtc_cs=1;
+    //sprintf(Time_stamp,"%02d%02d%02d%02d%02d%02d%02d%02d",year, month, date, day, hours, minutes, seconds, milliseconds );
+    uint8_t Time_stamp[8] = {year, month, date, day, hours, minutes, seconds, centiseconds};
+    for(int i= 0; i<8;i++)
+        printf("%d\t",Time_stamp[i]);
+    printf("read\r\n");
+    uint64_t time;
+    time = 0;
+    time = time|(((uint64_t)(centiseconds&0x7F)));
+    time = time|(((uint64_t)(seconds&0x3F))<<7);
+    time = time|(((uint64_t)(minutes&0x3F))<<13);
+    time = time|(((uint64_t)(hours&0x1F))<<19);
+    time = time|(((uint64_t)(day&0x1F))<<24);
+    time = time|(((uint64_t)(month&0x07))<<29);
+    time = time|(((uint64_t)(year&0x03))<<33);
+    time = (time&0x00000007FFFFFFFF);
+    //printf("\n\r%x%x", (int)(time >> 32), (int)(time));
+    printf("\n\r0x%016llx\n\r", time);
+   return time;
+}
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde cdms_rtc.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cdms_rtc.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,2 @@
+void FCTN_CDMS_INIT_RTC();
+uint64_t FCTN_CDMS_RD_RTC();
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde cdms_sd.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cdms_sd.cpp	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,382 @@
+#include "cdms_sd.h"
+
+SPI spi_sd(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
+DigitalOut cs_sd(PTE22);
+
+Serial sd1(USBTX,USBRX);
+
+int cdv;
+uint64_t sd_sectors();
+uint64_t sectors;
+
+void FCTN_SD_MNGR()
+{
+    /*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*/
+       
+}
+int initialise_card()
+{
+    // Set to 100kHz for initialisation, and clock card with cs_sd = 1
+    spi_sd.frequency(100000);
+    cs_sd = 1;
+    for (int i = 0; i < 16; i++) {
+        spi_sd.write(0xFF);
+    }
+
+    // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
+    if (cmd(0, 0) != R1_IDLE_STATE) {
+        debug("No disk, or could not put SD card in to spi_sd idle state\r\n");
+        return SDCARD_FAIL;
+    }
+
+// send CMD8 to determine whther it is ver 2.x
+    int r = cmd8();
+    if (r == R1_IDLE_STATE) {
+        printf("\rEntering v2\r\n");
+        return initialise_card_v2();
+
+    } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
+        printf("\rEntering v1\r\n");
+        return initialise_card_v1();
+
+    } else {
+        debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n");
+        return SDCARD_FAIL;
+    }
+}
+
+int initialise_card_v1()
+{
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        cmd(55, 0);
+        if (cmd(41, 0) == 0) {
+            printf("\rv1 initialization successfull\r\n");
+            cdv = 512;
+            debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
+
+            return SDCARD_V1;
+        }
+    }
+
+    debug("\rTimeout waiting for v1.x card\r\n");
+    return SDCARD_FAIL;
+}
+
+
+int initialise_card_v2()
+{
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        wait_ms(50);
+        cmd58();
+        cmd(55, 0);
+        if (cmd(41, 0x40000000) == 0) {
+            printf("\rv2 initialization successfull\r\n");
+            cmd58();
+            debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
+            cdv = 1;
+
+            return SDCARD_V2;
+        }
+    }
+
+    debug("\rTimeout waiting for v2.x card\r\n");
+    return SDCARD_FAIL;
+}
+
+int cmd(int cmd, int arg)
+{
+    cs_sd = 0;
+
+    // send a command
+    spi_sd.write(0x40 | cmd);
+    spi_sd.write(arg >> 24);
+    spi_sd.write(arg >> 16);
+    spi_sd.write(arg >> 8);
+    spi_sd.write(arg >> 0);
+    spi_sd.write(0x95);
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        int response = spi_sd.write(0xFF);
+        if (!(response & 0x80)) {
+            cs_sd = 1;
+            spi_sd.write(0xFF);
+            return response;
+        }
+    }
+    cs_sd = 1;
+    spi_sd.write(0xFF);
+    return -1; // timeout
+}
+
+
+int cmd58()
+{
+    cs_sd = 0;
+    int arg = 0;
+
+    // send a command
+    spi_sd.write(0x40 | 58);
+    spi_sd.write(arg >> 24);
+    spi_sd.write(arg >> 16);
+    spi_sd.write(arg >> 8);
+    spi_sd.write(arg >> 0);
+    spi_sd.write(0x95);
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        int response = spi_sd.write(0xFF);
+        if (!(response & 0x80)) {
+            int ocr = spi_sd.write(0xFF) << 24;
+            ocr |= spi_sd.write(0xFF) << 16;
+            ocr |= spi_sd.write(0xFF) << 8;
+            ocr |= spi_sd.write(0xFF) << 0;
+            cs_sd = 1;
+            spi_sd.write(0xFF);
+            return response;
+        }
+    }
+    cs_sd = 1;
+    spi_sd.write(0xFF);
+    return -1; // timeout
+}
+
+
+int cmd8()
+{
+    cs_sd = 0;
+
+    // send a command
+    spi_sd.write(0x40 | 8); // CMD8
+    spi_sd.write(0x00);     // reserved
+    spi_sd.write(0x00);     // reserved
+    spi_sd.write(0x01);     // 3.3v
+    spi_sd.write(0xAA);     // check pattern
+    spi_sd.write(0x87);     // crc
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
+        char response[5];
+        response[0] = spi_sd.write(0xFF);
+        if (!(response[0] & 0x80)) {
+            for (int j = 1; j < 5; j++) {
+                response[i] = spi_sd.write(0xFF);
+            }
+            cs_sd = 1;
+            spi_sd.write(0xFF);
+            return response[0];
+        }
+    }
+    cs_sd = 1;
+    spi_sd.write(0xFF);
+    return -1; // timeout
+}
+
+uint64_t sd_sectors()
+{
+    uint32_t c_size, c_size_mult, read_bl_len;
+    uint32_t block_len, mult, blocknr, capacity;
+    uint32_t hc_c_size;
+    uint64_t blocks;
+
+    // CMD9, Response R2 (R1 byte + 16-byte block read)
+    if (cmdx(9, 0) != 0) {
+        debug("\rDidn't get a response from the disk\n");
+        return 0;
+    }
+
+    uint8_t cs_sdd[16];
+    if (read(cs_sdd, 16) != 0) {
+        debug("\rCouldn't read cs_sdd response from disk\n");
+        return 0;
+    }
+
+    // cs_sdd_structure : cs_sdd[127:126]
+    // c_size        : cs_sdd[73:62]
+    // c_size_mult   : cs_sdd[49:47]
+    // read_bl_len   : cs_sdd[83:80] - the *maximum* read block length
+
+    int cs_sdd_structure = ext_bits(cs_sdd, 127, 126);
+
+    switch (cs_sdd_structure) {
+        case 0:
+            cdv = 512;
+            c_size = ext_bits(cs_sdd, 73, 62);
+            c_size_mult = ext_bits(cs_sdd, 49, 47);
+            read_bl_len = ext_bits(cs_sdd, 83, 80);
+
+            block_len = 1 << read_bl_len;
+            mult = 1 << (c_size_mult + 2);
+            blocknr = (c_size + 1) * mult;
+            capacity = blocknr * block_len;
+            blocks = capacity / 512;
+            debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
+            break;
+
+        case 1:
+            cdv = 1;
+            hc_c_size = ext_bits(cs_sdd, 63, 48);
+            blocks = (hc_c_size+1)*1024;
+            debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
+            break;
+
+        default:
+            debug("cs_sdD struct unsupported\r\n");
+            return 0;
+    };
+    return blocks;
+}
+
+int cmdx(int cmd, int arg)
+{
+    cs_sd = 0;
+
+    // send a command
+    spi_sd.write(0x40 | cmd);
+    spi_sd.write(arg >> 24);
+    spi_sd.write(arg >> 16);
+    spi_sd.write(arg >> 8);
+    spi_sd.write(arg >> 0);
+    spi_sd.write(0x95);
+
+    // wait for the repsonse (response[7] == 0)
+    for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
+        int response = spi_sd.write(0xFF);
+        if (!(response & 0x80)) {
+            return response;
+        }
+    }
+    cs_sd = 1;
+    spi_sd.write(0xFF);
+    return -1; // timeout
+}
+
+static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
+{
+    uint32_t bits = 0;
+    uint32_t size = 1 + msb - lsb;
+    for (int i = 0; i < size; i++) {
+        uint32_t position = lsb + i;
+        uint32_t byte = 15 - (position >> 3);
+        uint32_t bit = position & 0x7;
+        uint32_t value = (data[byte] >> bit) & 1;
+        bits |= value << i;
+    }
+    return bits;
+}
+
+int disk_initialize()
+{
+    int i = initialise_card();
+    debug_if(SD_DBG, "init card = %d\n", i);
+    sectors = sd_sectors();
+
+    // Set block length to 512 (CMD16)
+    if (cmd(16, 512) != 0) {
+        debug("\rSet 512-byte block timed out\r\n");
+        return 1;
+    } else {
+        printf("\rDisk initialization successfull\r\n");
+    }
+
+    spi_sd.frequency(1000000); // Set to 1MHz for data transfer
+    return 0;
+}
+
+int disk_write(const uint8_t *buffer, uint64_t block_number)
+
+{
+    // set write address for single block (CMD24)
+    if (cmd(24, block_number * cdv) != 0) {
+        return 1;
+    }
+
+    // send the data block
+    write(buffer, 512);
+    //printf("Written Successfully bro \n");
+    return 0;
+}
+
+int write(const uint8_t*buffer, uint32_t length)
+{
+    cs_sd = 0;
+
+    // indicate start of block
+    spi_sd.write(0xFE);
+
+    // write the data
+    for (int i = 0; i < length; i++) {
+        spi_sd.write(buffer[i]);
+    }
+
+    // write the checksum
+    spi_sd.write(0xFF);
+    spi_sd.write(0xFF);
+
+    // check the response token
+    if ((spi_sd.write(0xFF) & 0x1F) != 0x05) {
+        cs_sd = 1;
+        spi_sd.write(0xFF);
+        return 1;
+    }
+
+    // wait for write to finish
+    while (spi_sd.write(0xFF) == 0);
+
+    cs_sd = 1;
+    spi_sd.write(0xFF);
+    return 0;
+}
+
+int disk_read(uint8_t *buffer, uint64_t block_number)
+{
+    // set read address for single block (CMD17)
+    if (cmd(17, block_number * cdv) != 0) {
+        return 1;
+    }
+
+    // receive the data
+    read(buffer, 512);
+    return 0;
+}
+
+int read(uint8_t *buffer, uint32_t length)
+{
+    cs_sd = 0;
+
+    // read until start byte (0xFF)
+    while (spi_sd.write(0xFF) != 0xFE);
+
+    // read data
+    for (int i = 0; i < length; i++) {
+        buffer[i] = spi_sd.write(0xFF);
+    }
+    spi_sd.write(0xFF); // checksum
+    spi_sd.write(0xFF);
+
+    cs_sd = 1;
+    spi_sd.write(0xFF);
+    return 0;
+}
+
+int disk_erase(int startBlock, int totalBlocks)
+{
+    if(cmd(32, startBlock * cdv) != 0) {
+        return 1;
+    }
+    if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
+        return 1;
+    }
+    if (cmd(38,0) != 0) {
+        return 1;
+    }
+    
+    return 0; //normal return
+}
diff -r 000000000000 -r bcbd76c86cde cdms_sd.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cdms_sd.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+#include "mbed_debug.h"
+
+#define SD_COMMAND_TIMEOUT 5000
+
+#define SD_DBG             0
+
+#define R1_IDLE_STATE           (1 << 0)
+#define R1_ERASE_RESET          (1 << 1)
+#define R1_ILLEGAL_COMMAND      (1 << 2)
+#define R1_COM_CRC_ERROR        (1 << 3)
+#define R1_ERASE_SEQUENCE_ERROR (1 << 4)
+#define R1_ADDRESS_ERROR        (1 << 5)
+#define R1_PARAMETER_ERROR      (1 << 6)
+
+int initialise_card();
+int initialise_card_v1();
+int initialise_card_v2();
+int disk_write(const uint8_t *, uint64_t);
+int disk_read(uint8_t *, uint64_t);
+int disk_initialize();
+int disk_erase(int,int);
+
+//void FCTN_CDMS_INIT_SD();
+//int FCTN_CDMS_WR_SD(const uint8_t *, uint64_t);
+//int FCTN_CDMS_RD_SD(uint8_t *, uint64_t);
+void FCTN_SD_MNGR();
+
+
+int cmd(int, int);
+int cmd58();
+int cmdx(int, int);
+int cmd8();
+int read(uint8_t*, uint32_t );
+int write(const uint8_t*, uint32_t );
+static uint32_t ext_bits(unsigned char *, int , int );
+
+#define SDCARD_FAIL 0
+#define SDCARD_V1   1
+#define SDCARD_V2   2
+#define SDCARD_V2HC 3
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde crc.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,79 @@
+//EDITS
+//changed the initial remainder from 0x0000 to 0xffff according to the standards
+//made two seperate functions crc16_gen and crc8_gen
+
+#define TOPBIT16 (1 << 15)
+#define TOPBIT8 (1 << 7)
+#define POLYNOMIAL16 0x1021
+#define POLYNOMIAL8 0xEA
+
+namespace CRC{
+    typedef uint16_t crctype16; 
+    crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){
+        crctype16 remainder = 0xffff;
+        int byte;
+        char bit;
+        
+        for( byte = 0 ; byte < nBytes ; byte++ ){
+            /*
+            Bring the data byte by byte
+            each time only one byte is brought
+            0 xor x = x
+            */
+            remainder = remainder ^ ( message[byte] << 8 );
+            
+            for( bit = 8 ; bit > 0 ; bit--){
+                /*
+                for each bit, xor the remainder with polynomial
+                if the MSB is 1
+                */
+                if(remainder & TOPBIT16){
+                    remainder = (remainder << 1) ^ POLYNOMIAL16;
+                    /*
+                    each time the remainder is xor-ed with polynomial, the MSB is made zero
+                    hence the first digit of the remainder is ignored in the loop
+                    */
+                }
+                else{
+                    remainder = (remainder << 1);
+                }
+            }
+        }
+        
+        return remainder;
+    }
+    
+    typedef uint8_t crctype8;
+    crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes){
+        
+        crctype8 remainder = 0xff;
+        
+        for(int byte = 0 ; byte < nBytes ; byte++ ){
+            /*
+            Bring the data byte by byte
+            each time only one byte is brought
+            0 xor x = x
+            */
+            remainder = remainder ^ ( message[byte] );
+            
+            for(int bit = 8 ; bit > 0 ; bit--){
+                /*
+                for each bit, xor the remainder with polynomial
+                if the MSB is 1
+                */
+                if(remainder & TOPBIT8){
+                    remainder = (remainder << 1) ^ POLYNOMIAL8;
+                    /*
+                    each time the remainder is xor-ed with polynomial, the MSB is made zero
+                    hence the first digit of the remainder is ignored in the loop
+                    */
+                }
+                else{
+                    remainder = (remainder << 1);
+                }
+            }
+        }
+        
+        return remainder;
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde i2c.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2c.cpp	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#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);
+DigitalOut led2(PIN95);
+const int addr = 0x20;                                            //slave address 
+char telecommand[tc_len];
+char telemetry[tm_len];
+Timer test;
+Timer t_read;
+bool write_ack = true;
+bool read_ack = true;
+
+int count = 0;
+
+void FCTN_I2C_WRITE(char *data)
+{
+    irpt_2_slv = 1;
+    //t.start();
+    write_ack = master.write(addr|0x00,data,tc_len);
+    //t.stop();
+    //if(write_ack == 0)
+        //pc.printf("\n\r data not sent \n");
+       
+if (write_ack == 1)
+    {
+        led2 = 1;
+        printf("\n\r data not sent \n");
+        led2 = 0;    
+    }
+    irpt_2_slv = 0;
+    //pc.printf("\n\r %d \n",t.read_us());
+    //t.reset();
+}
+
+
+void FCTN_I2C_READ(char *data)
+{
+    irpt_2_slv = 1; 
+    t_read.start();
+    read_ack = master.read(addr|1,data,tm_len);
+    t_read.stop();
+   
+    //if(read_ack == 0)
+      //  printf("\n\rData received from BAE %s \n",data);
+ if (read_ack == 1)
+    {
+        led2 = 1;
+        printf("\n \r data not received \n");
+        led2 = 0;
+    }
+//if(read_ack == 1)
+//pc.printf("\n \r data not received \n");
+
+    irpt_2_slv = 0;
+    //printf("\n\r %d \n",t.read_us());
+    //t.reset();
+}
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde i2c.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2c.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,5 @@
+void FCTN_I2C_READ(char*);
+void FCTN_I2C_WRITE(char*);
+#define tm_len 134
+#define tc_len 135
+#define tc_test_len 135
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,135 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "pinconfig.h"
+#include "cdms_sd.h"
+#include "structure.h"
+#include "i2c.h"
+#include "cdms_rtc.h"
+#include "main_funcs.h"
+#include "TMTC.h"
+
+Serial pc(USBTX,USBRX);
+
+Thread *ptr_t_sc_data;
+Thread *ptr_t_tmtc;
+
+InterruptIn pl_sc_data(PIN81);
+
+/* only for sd testing*/
+uint8_t write_to_sd[512];
+uint8_t read_from_sd[512];
+/*end*/
+
+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();
+}
+
+void BAE_HK()
+{
+    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;
+        for(int i=4;i < 135 ; i++)
+            tc_test->TC_string[i] = 43; 
+        printf("%s", tc_test->TC_string);
+        tm_test = FCTN_CDMS_RLY_TMTC(tc_test);
+        printf("%s", tm_test->TM_string);
+//        for(int i =0;i<134;i++)\
+//        {
+//            printf("ack");
+//            printf("%c", tm_test->TM_string[i]);
+//        }
+        //FUNC_CDMS_RLY_TM();
+    }
+}
+
+void T_PL_RCV_SC_DATA(void const *args)
+{
+    while(1)
+    {
+        Thread::signal_wait(0x1);
+        FCTN_PL_RCV_SC_DATA();
+        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++)
+        {
+            printf("%d",read_from_sd[i]);
+        }
+    }
+}
+
+void ISR_PL_RCV_SC_DATA()
+{
+    ptr_t_sc_data->signal_set(0x1);
+}
+
+void FCTN_PL_RCV_SC_DATA()
+{
+    printf("\n\rFCTN_PL_RCV_SC_DATA Function Executed\r\n");
+}
+
+void FCTN_COM_COMP_SC_DATA()
+{
+    printf("\n\rFCTN_COM_COMP_SC_DATA Function Executed\r\n");
+}
+
+void FCTN_CDMS_WR_SD()
+{
+    printf("\n\rFCTN_CDMS_WR_SD Function Executed\r\n");
+}
+
+int main()
+{
+    printf("\n\r CDMS Activated \r\n");
+    initialise_card();
+    disk_initialize();
+    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);
+    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);
+}
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde main_funcs.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main_funcs.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,5 @@
+void BAE_HK();
+void CDMS_HK();
+void PL_MODE();
+void FCTN_PL_RCV_SC_DATA();
+void FCTN_COM_COMP_SC_DATA();
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#c825593ece39
diff -r 000000000000 -r bcbd76c86cde mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde pinconfig.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pinconfig.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,101 @@
+// 100 LQFP format pin assignment
+#define PIN1 PTE0
+#define PIN2 PTE1
+#define PIN3 PTE2
+#define PIN4 PTE3
+#define PIN5 PTE4
+#define PIN6 PTE5
+#define PIN7 PTE6
+//#define 8 
+//#define 9 
+//#define 10 
+//#define 11 
+//#define 12 
+//#define 13 
+#define PIN14 PTE16
+#define PIN15 PTE17
+#define PIN16 PTE18
+#define PIN17 PTE19
+#define PIN18 PTE20
+#define PIN19 PTE21
+#define PIN20 PTE22
+#define PIN21 PTE23
+//#define 22 
+//#define 23 
+//#define 24 
+//#define 25 
+#define PIN26 PTE29
+#define PIN27 PTE30
+#define PIN28 PTE31
+//#define 29 
+//#define 30 
+#define PIN31 PTE24
+#define PIN32 PTE25
+#define PIN33 PTE26
+#define PIN34 PTA0
+#define PIN35 PTA1
+#define PIN36 PTA2
+#define PIN37 PTA3
+#define PIN38 PTA4
+#define PIN39 PTA5
+#define PIN40 PTA6
+#define PIN41 PTA7
+#define PIN42 PTA12
+#define PIN43 PTA13
+#define PIN44 PTA14
+#define PIN45 PTA15
+#define PIN46 PTA16
+#define PIN47 PTA17
+//#define 48 
+//#define 49 
+#define PIN50 PTA18
+#define PIN51 PTA19
+#define PIN52 PTA20
+#define PIN53 PTB0
+#define PIN54 PTB1
+#define PIN55 PTB2
+#define PIN56 PTB3
+#define PIN57 PTB7
+#define PIN58 PTB8
+#define PIN59 PTB9
+#define PIN60 PTB10
+#define PIN61 PTB11
+#define PIN62 PTB16
+#define PIN63 PTB17
+#define PIN64 PTB18
+#define PIN65 PTB19
+#define PIN66 PTB20
+#define PIN67 PTB21
+#define PIN68 PTB22
+#define PIN69 PTB23
+#define PIN70 PTC0
+#define PIN71 PTC1
+#define PIN72 PTC2
+#define PIN73 PTC3
+//#define 74 
+//#define 75 
+#define PIN76 PTC20
+#define PIN77 PTC21
+#define PIN78 PTC22
+#define PIN79 PTC23
+#define PIN80 PTC4
+#define PIN81 PTC5
+#define PIN82 PTC6
+#define PIN83 PTC7
+#define PIN84 PTC8
+#define PIN85 PTC9
+#define PIN86 PTC10
+#define PIN87 PTC11
+#define PIN88 PTC12
+#define PIN89 PTC13
+#define PIN90 PTC16
+#define PIN91 PTC17
+#define PIN92 PTC18
+#define PIN93 PTD0
+#define PIN94 PTD1
+#define PIN95 PTD2
+#define PIN96 PTD3
+#define PIN97 PTD4
+#define PIN98 PTD5
+#define PIN99 PTD6
+#define PIN100 PTD7
\ No newline at end of file
diff -r 000000000000 -r bcbd76c86cde structure.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/structure.h	Wed Dec 16 09:06:59 2015 +0000
@@ -0,0 +1,401 @@
+#include "mbed.h"
+//SIZE
+#define TC_SHORT_SIZE 11
+#define TC_LONG_SIZE 135
+#define TM_LONG_SIZE 134
+#define TM_SHORT_SIZE 13
+
+//TELECOMMAND:
+
+// exec_status:
+// 0: unexecuted
+// 1: successfully executed
+// 2: Execution Failed
+// 3: Disabled
+// 4: Marked For retry
+
+//MASKS
+#define SHORT_LONG_TC_MASK 0x10
+#define CRC_MASK 0x08
+#define EXEC_STATUS_MASK 0x07
+
+//USE ONLY THE BELOW MACROS TO MODIFY 'flags' VARIABLE
+//x should be a Base_tc pointer
+#define GETshort_or_long_tc(x) ( ( (x->flags) & SHORT_LONG_TC_MASK ) >> 4 )
+#define GETcrc_pass(x) ( ( (x->flags) & CRC_MASK ) >> 3 )
+#define GETabort_on_nack(x) ( ( (x->TC_string[1]) & 0x08 ) >> 3 )
+#define GETapid(x) ( ( (x->TC_string[1]) & 0xC0 ) >> 6 )
+#define GETexec_status(x) ( (x->flags) & EXEC_STATUS_MASK )
+#define GETpacket_seq_count(x) (x->TC_string[0])
+#define GETservice_type(x) ( (x->TC_string[2]) & 0xF0 )
+#define GETservice_subtype(x) ( (x->TC_string[2]) & 0x0F )
+#define GETpid(x) (x->TC_string[3])
+
+//x should be a Base_tc pointer
+//y should be a 16-bit number with relevant data in LSB
+//use in a seperate line with ; at the end: similar to a function
+#define PUTshort_or_long(x,y) x->flags = ( (x->flags) & ~(SHORT_LONG_TC_MASK)) | ( (y << 4) & SHORT_LONG_TC_MASK )
+#define PUTcrc_pass(x,y) x->flags = ( (x->flags) & ~(CRC_MASK)) | ( (y << 3) & CRC_MASK)
+#define PUTexec_status(x,y) x->flags = ( (x->flags) & ~(EXEC_STATUS_MASK)) | ( y & EXEC_STATUS_MASK)
+
+//PARENT TELECOMMAND CLASS 
+class Base_tc{
+public:
+    uint8_t flags;
+    uint8_t *TC_string;
+    Base_tc *next_TC;
+    
+    virtual ~Base_tc(){}
+};
+
+//DERIVED CLASS - SHORT TC
+class Short_tc : public Base_tc{
+private:
+    uint8_t fix_str[TC_SHORT_SIZE];
+public:
+    Short_tc(){
+        TC_string = fix_str;
+        flags = 0;
+    }
+    
+    ~Short_tc(){}
+};
+
+//DERIVED CLASS - LONG TC
+class Long_tc : public Base_tc{
+private:
+    uint8_t fix_str[TC_LONG_SIZE];
+public:
+    Long_tc(){
+        TC_string = fix_str;
+        flags = 0;
+    }
+    
+    ~Long_tc(){}
+};
+
+// TELEMETRY:
+// MASKS
+#define SHORT_LONG_TM_MASK 0x10
+#define TMID_MASK 0x0F
+
+//x should be 'fields' variable defined in the Base_tm
+#define GETshort_or_long_tm(x) ((x & SHORT_LONG_TM_MASK) >> 4)
+#define GETtmid(x) (x & TMID_MASK)
+
+//x should be 'fields' variable defines in the Base_tm
+//y should be an 8-bit number with relevent data at LSB
+#define PUTshort_or_long_tm(x,y) x = (x & ~(SHORT_LONG_TM_MASK)) | ((y<<4) & SHORT_LONG_TM_MASK)
+#define PUTtmid(x,y) x = (x & ~(TMID_MASK)) | (y & TMID_MASK)
+
+// PARENT TELEMETRY CLASS
+class Base_tm{
+public:
+    uint8_t fields;
+    uint8_t *TM_string;
+    Base_tm *next_TM;
+    
+    virtual ~Base_tm(){}
+};
+
+// DERIVED CLASS : Long tm [type 0]
+// type 0
+class Long_tm : public Base_tm{
+private:
+    uint8_t fix_str[TM_LONG_SIZE];
+public:
+    Long_tm(){
+        TM_string = fix_str;
+        // type 0
+        fields = 0;
+    }
+    
+    ~Long_tm(){}
+};
+
+// DERIVED CLASS : Short tm [type 1]
+// type 1
+class Short_tm : public Base_tm{
+private:
+    uint8_t fix_str[TM_SHORT_SIZE];
+public:
+    Short_tm(){
+        TM_string = fix_str;
+        // type 1
+        fields = 0x10;
+    }
+    
+    ~Short_tm(){}
+};
+
+//////////////////////////////END
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+/*typedef struct TC_list{
+    // received from the RCV_TC
+    unsigned char *TC_string;
+    bool short_or_long; //'true' for short
+    bool crc_pass;
+ 
+    // updated info - updated in MNG_TC
+    unsigned char packet_seq_count;
+    unsigned char apid;
+    bool abort_on_nack;
+    bool enabled;
+//    bool valid_execution;
+    unsigned char exec_status;
+ 
+    struct TC_list *next_TC;
+    
+    ~TC_list(){}
+}TC_list;
+ 
+typedef struct TM_list{
+ 
+    unsigned char *TM_string;
+    // bool short_or_long; // true for short
+    // pass while calling the function
+    unsigned char tmid;
+    struct TM_list *next_TM;
+    
+    ~TM_list(){}
+}TM_List;*/
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+////PARENT CLASS
+//class Base_tc{
+//public:
+//    uint16_t fields;
+//    uint8_t *TC_string;
+//    Base_tc *next_node;
+    
+//    short = 0, long = 1
+//    bool GETshort_or_long(void){
+//        return (fields & SHORT_LONG_TC_MASK);
+//    }
+//    void PUTshort_or_long(bool input){
+//        if(input){
+//            fields |= SHORT_LONG_TC_MASK;
+//        }
+//        else{
+//            fields &= ~(SHORT_LONG_TC_MASK);
+//        }
+//    }
+//    
+//    bool inline GETcrc_pass(){
+//        return (fields & CRC_MASK);
+//    }
+//    void inline PUTcrc_pass(bool input){
+//        if(input){
+//            fields |= CRC_MASK;
+//        }
+//        else{
+//            fields &= ~(CRC_MASK);
+//        }
+//    }
+//        
+//    bool inline GETabort_on_nack(){
+//        return (fields & ABORT_ON_NACK_MASK);
+//    }
+//    void inline PUTabort_on_nack(bool input){
+//        if(input){
+//            fields |= ABORT_ON_NACK_MASK;
+//        }
+//        else{
+//            fields &= ~(ABORT_ON_NACK_MASK);
+//        }
+//    }
+//    
+//    uint8_t inline GETapid(){
+//        uint16_t temp = fields & APID_MASK;
+//        temp = temp >> 10;
+//        return (temp & 0xFF);
+//    }
+//    void inline PUTapid(uint8_t input){
+//        uint16_t temp = input;
+//        temp = temp << 10;
+//        fields &= ~(APID_MASK);
+//        fields |= (temp & APID_MASK);
+//    }
+//    
+//    uint8_t inline GETexec_status(){
+//        uint16_t temp = fields & EXEC_STATUS_MASK;
+//        temp = temp >> 8;
+//        return (temp & 0xFF);
+//    }
+//    void inline PUTexec_status(uint8_t input){
+//        uint16_t temp = input;
+//        temp = temp << 8;
+//        fields &= ~(EXEC_STATUS_MASK);
+//        fields |= (temp & EXEC_STATUS_MASK);
+//    }
+//    
+//    uint8_t inline GETpacket_seq_count(){
+//        uint16_t temp = fields & PACKET_SEQ_COUNT_MASK;
+//        return (temp & 0xFF);
+//    }
+//    void inline PUTpacket_seq_count(uint8_t input){
+//        uint16_t temp = input;
+//        fields &= ~(PACKET_SEQ_COUNT_MASK);
+//        fields |= (temp & PACKET_SEQ_COUNT_MASK);
+//    }
+//    
+////    update everything other than short_or_long, and crc_pass from TC_string
+//    void update_fields(){
+////        abort on nack
+//        uint8_t temp = TC_string[1];
+//        uint16_t t16 = 0;
+//        if(temp & 0x10){
+//            fields |= ABORT_ON_NACK_MASK;
+//        }
+//        else{
+//            fields &= ~(ABORT_ON_NACK_MASK);
+//        }
+//        
+//        // apid
+//        t16 = temp;
+//        t16 = t16 << 4;
+//        fields &= ~(APID_MASK);
+//        fields |= (t16 & APID_MASK);
+//        
+//        // exec_status : default value of exec status
+//        fields &= ~(EXEC_STATUS_MASK);
+//        
+//        // packet seq count
+//        temp = TC_string[0];
+//        t16 = temp;
+//        fields &= ~(PACKET_SEQ_COUNT_MASK);
+//        fields |= (t16 & PACKET_SEQ_COUNT_MASK);
+//    }
+//    
+//    virtual ~Base_tc(){}
+//};
+
+////DERIVED CLASS - SHORT TC
+//class Short_tc : public Base_tc{
+//private:
+//    uint8_t fix_str[TC_SHORT_SIZE];
+//public:
+//    Short_tc(){
+//        TC_string = fix_str;
+//        fields = 0;
+//    }
+//    
+//    ~Short_tc(){}
+//};
+//
+////DERIVED CLASS - LONG TC
+//class Long_tc : public Base_tc{
+//private:
+//    uint8_t fix_str[TC_LONG_SIZE];
+//public:
+//    Long_tc(){
+//        TC_string = fix_str;
+//        fields = 0;
+//    }
+//    
+//    ~Long_tc(){}
+//};
+
+//// TELEMETRY CLASS :
+//
+//// MASKS
+//#define SHORT_LONG_TM_MASK 0x10
+//#define TMID_MASK 0x0F
+//
+//// PARENT CLASS
+//class Base_tm{
+//protected:
+//    uint8_t fields;
+//public:
+//    uint8_t *TM_string;
+//    Base_tm *next_node;
+//    
+    // short = 0, long = 1
+//    bool GETshort_or_long(){
+//        return (fields & SHORT_LONG_TM_MASK);
+//    }
+//    void PUTshort_or_long(bool input){
+//        if(input){
+//            fields |= SHORT_LONG_TM_MASK;
+//        }
+//        else{
+//            fields &= ~(SHORT_LONG_TM_MASK);
+//        }
+//    }
+//    
+//    uint8_t GETtmid(){
+//        return (fields & TMID_MASK);
+//    }
+//    void PUTtmid(uint8_t input){
+//        fields &= ~(TMID_MASK);
+//        fields |= (input & TMID_MASK);
+//    }
+//    
+//    virtual ~Base_tm(){}
+//};
+
+
+//// DERIVED CLASS : Long tc [type 0]
+//// type 0
+//class Long_tm : public Base_tm{
+//private:
+//    uint8_t fix_str[TM_LONG_SIZE];
+//public:
+//    Long_tm(){
+//        TM_string = fix_str;
+//        // type 0
+//        fields = 0;
+//    }
+//    
+//    ~Long_tm(){}
+//};
+//
+//// DERIVED CLASS : Short tc [type 1]
+//// type 1
+//class Short_tm : public Base_tm{
+//private:
+//    uint8_t fix_str[TM_SHORT_SIZE];
+//public:
+//    Short_tm(){
+//        TM_string = fix_str;
+//        // type 1
+//        fields = 0x10;
+//    }
+//    
+//    ~Short_tm(){}
+//};
\ No newline at end of file