Changes to be made for ATS_Fault logic and ACS_State

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of Japan_BAE_sensorworking_interrupr_reoccuring_copy by Team Fox

Files at this revision

API Documentation at this revision

Comitter:
sakthipriya
Date:
Tue Dec 29 05:14:39 2015 +0000
Parent:
2:c823d84b4cb0
Child:
4:39a4ae8c7ecd
Commit message:
BAE with acs + eps + bcn (conops) + tmtc (without calling actual funtions).; status : working

Changed in this revision

ACS.cpp Show annotated file Show diff for this revision Revisions of this file
EPS.cpp Show annotated file Show diff for this revision Revisions of this file
EPS.h Show annotated file Show diff for this revision Revisions of this file
TCTM.cpp Show annotated file Show diff for this revision Revisions of this file
TCTM.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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ACS.cpp	Fri Dec 25 15:25:44 2015 +0000
+++ b/ACS.cpp	Tue Dec 29 05:14:39 2015 +0000
@@ -199,7 +199,7 @@
 void  FCTN_ACS_INIT()
 {
     ACS_INIT_STATUS = 's';     //set ACS_INIT_STATUS flag
-    FLAG();
+    //FLAG();
     pc_acs.printf("Attitude sensor init called \n \r");
     //FLAG();
     cmd[0]=RESETREQ;
--- a/EPS.cpp	Fri Dec 25 15:25:44 2015 +0000
+++ b/EPS.cpp	Tue Dec 29 05:14:39 2015 +0000
@@ -14,19 +14,35 @@
 
 //......................................Peripheral declarations.........................................................//
 Serial pc_eps(USBTX,USBRX);
+
 I2C m_I2C(PIN85,PIN84);
 DigitalOut TRXY(TRXY_DR_EN);            //active high
 DigitalOut TRZ(TRZ_DR_EN);              //active high
 DigitalOut EN3V3A(ENBL3V3A);
 DigitalOut EN_BTRY_HT(BATT_HEAT);
 //DigitalIn BTRY_HT_OUTPUT(BATT_HEAT_OUTPUT);
-AnalogIn Vbatt_ang(VBATT);
+//AnalogIn Vbatt_ang(VBATT);
+AnalogIn Batt_voltage(PIN20);   //Battery voltage
+
 SPI spi_bt(PIN99,PIN100,PIN98); //MOSI,MISO,SLK
 DigitalOut ssn1(PIN19); //Slave select1
 DigitalOut ssn2(PIN21);//Slave select2
 //DigitalOut PS(PTB0);
 //DigitalOut HS(PTB1);
 
+AnalogIn CurrentInput(PIN54); // Input from Current Multiplexer //PIN54
+AnalogIn VoltageInput(PIN53); // Input from Voltage Multiplexer //PIN53
+AnalogIn BAE_temp_sensor(PIN55); //Input from BAE temp sensor
+
+DigitalOut SelectLinea3 (PIN46); // MSB of Select Lines
+DigitalOut SelectLinea2 (PIN45);
+DigitalOut SelectLinea1 (PIN44);
+DigitalOut SelectLinea0 (PIN43); // LSB of Select Lines
+
+DigitalOut SelectLineb3 (PIN56); // MSB of Select Lines
+DigitalOut SelectLineb2 (PIN57);
+DigitalOut SelectLineb1 (PIN58);
+DigitalOut SelectLineb0 (PIN59); // LSB of Select Lines
 
 //*********************************************************flags********************************************************//
 extern char EPS_INIT_STATUS ;
@@ -43,9 +59,9 @@
 {
     printf("\n\r eps init \n");
     EPS_INIT_STATUS = 's' ;             //set EPS_INIT_STATUS flag
-   // FLAG();
+    // FLAG();
     FCTN_BATTERYGAUGE_INIT();
-    //FCTN_BATTTEMP_INIT();
+    FCTN_BATTTEMP_INIT();
     EN3V3A = 1;                             //enable dc dc converter A  
     char value=alertFlags();
     unsigned short value_u= (short int )value;
@@ -58,7 +74,7 @@
     else
     {
         actual_data.Batt_gauge_actual[1] = soc();
-        actual_data.Batt_voltage_actual = Vbatt_ang.read()*3.3;
+        actual_data.Batt_voltage_actual = Batt_voltage.read()*3.3;
         FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);
         EPS_BATTERY_GAUGE_STATUS = 's';               //set EPS_BATTERY_GAUGE_STATUS
     }
@@ -82,6 +98,119 @@
 
 //...................................................HK...........................................//
 
+
+void FCTN_HK_MAIN()
+{
+    int Iteration=0;
+
+    SelectLinea0=0;
+    SelectLinea1=0;
+    SelectLinea2=0;
+    SelectLinea3=0;
+
+    SelectLineb0=0;
+    SelectLineb1=0;
+    SelectLineb2=0;
+    SelectLineb3=0;
+    
+      //collecting data
+    for(Iteration=0; Iteration<16; Iteration++){
+
+            actual_data.voltage_actual[Iteration]=VoltageInput.read();
+            actual_data.current_actual[Iteration]=CurrentInput.read();
+           
+            SelectLinea0=!(SelectLinea0);
+            if(Iteration%2==1)
+                SelectLinea1=!(SelectLinea1);
+            if(Iteration%4==3)
+                SelectLinea2=!(SelectLinea2);
+            if(Iteration%8==7)
+                SelectLinea3=!(SelectLinea3);
+                int s0,s1,s2,s3;
+            s0=SelectLineb0=SelectLinea0;
+            s1=SelectLineb1=SelectLinea2;
+            s2=SelectLineb2=SelectLinea2;
+            s3=SelectLineb3=SelectLinea3;
+            printf("\n\r  %d %d %d %d", s0,s1,s2,s3);
+
+    }
+      for(Iteration=0; Iteration<16; Iteration++){
+
+        if(Iteration==14)
+            actual_data.voltage_actual[Iteration]= (-90.7*3.3*actual_data.voltage_actual[Iteration])+190.1543;
+        else
+            actual_data.voltage_actual[Iteration]= actual_data.voltage_actual[Iteration]*3.3*5.63;
+    }
+        
+    for(Iteration=0;Iteration<12;Iteration++){
+        if(Iteration<8)
+            actual_data.current_actual[Iteration]= actual_data.current_actual[Iteration]*3.3/(50*rsens);
+        else
+            actual_data.current_actual[Iteration]=actual_data.current_actual[Iteration]*3.3;
+            int resistance;       
+             
+            resistance=24000*actual_data.current_actual[Iteration]/(3.3-actual_data.current_actual[Iteration]);
+            if(actual_data.current_actual[Iteration]>1.47) 
+            {
+                actual_data.current_actual[Iteration]=3694/log(24.032242*resistance);
+            }
+            else{
+                
+                actual_data.current_actual[Iteration]=3365.4/log(7.60573*resistance);
+            }
+    }
+    actual_data.BAE_temp_actual=(-90.7*3.3*actual_data.BAE_temp_actual)+190.1543;
+    
+    actual_data.Batt_voltage_actual=Batt_voltage.read()*3.3*5.63;
+
+    //quantizing data
+    for(Iteration=0; Iteration<16; Iteration++){
+
+        if(Iteration==14)
+            quant_data.voltage_quant[Iteration]=quantiz(tstart,tstep,actual_data.voltage_actual[Iteration]);
+        else
+            quant_data.voltage_quant[Iteration]=quantiz(vstart,vstep,actual_data.voltage_actual[Iteration]);
+        
+    }
+    for(Iteration=0;Iteration<12;Iteration++){
+        if(Iteration<8)
+            quant_data.current_quant[Iteration]=quantiz(cstart,cstep,actual_data.current_actual[Iteration]);
+        else
+            quant_data.current_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.current_actual[Iteration]);
+     }       
+    for(Iteration=0;Iteration<2;Iteration++){
+        
+        quant_data.Batt_temp_quant[Iteration]=quantiz(tstart,tstep,actual_data.Batt_temp_actual[Iteration]);
+    }
+    
+    quant_data.Batt_gauge_quant[0]=quantiz(vcell_start,vcell_step,actual_data.Batt_gauge_actual[0]);
+    quant_data.Batt_gauge_quant[1]=quantiz(soc_start,soc_step,actual_data.Batt_gauge_actual[1]);
+    quant_data.Batt_gauge_quant[2]=quantiz(crate_start,crate_step,actual_data.Batt_gauge_actual[2]);
+    quant_data.Batt_gauge_alerts=actual_data.Batt_gauge_actual[3];
+    
+    quant_data.BAE_temp_quant=quantiz(tstart,tstep,actual_data.BAE_temp_actual);
+    
+    for(Iteration=0;Iteration<3;Iteration++){
+        quant_data.AngularSpeed_quant[Iteration]=quantiz(AngularSpeed_start,AngularSpeed_step,actual_data.AngularSpeed_actual[Iteration]);
+    }
+    
+    for(Iteration=0;Iteration<3;Iteration++){
+        quant_data.Bvalue_quant[Iteration]=actual_data.Bvalue_actual[Iteration];
+    }
+    
+    quant_data.Batt_voltage_quant=quantiz(vstart,vstep,actual_data.Batt_voltage_actual);
+    
+    
+    arch_data.Batt_1_temp=quant_data.Batt_temp_quant[0];
+    arch_data.Batt_2_temp=quant_data.Batt_temp_quant[1];
+    arch_data.EPS_PCB_temp=quant_data.voltage_quant[14];
+    arch_data.Batt_SOC=quant_data.Batt_gauge_quant[1];
+    arch_data.power_mode=actual_data.power_mode;
+    arch_data.Batt_voltage=quant_data.Batt_voltage_quant;    
+    
+    
+}
+
 int quantiz(float start,float step,float x)
 {
     int y=(x-start)/step;
@@ -90,12 +219,99 @@
     return y;
 }
 
-void HK_main()
-{
-    
-    
+bool firstCount=true;  // goes to EPS init
+
+
+void saveMin(char x,char y){
+    if(y<x){
+        x=y;
+    }
+
+}
+void saveMax(char x,char y){
+    if (y>x)
+    {
+       x=y;
+    }
 }
 
+
+void minMaxHkData(){
+    if(firstCount==true){
+        for (int i = 0; i < 16; ++i){    
+        bae_HK_minmax.voltage_min[i] = quant_data.voltage_quant[i];
+        bae_HK_minmax.voltage_max[i] = quant_data.voltage_quant[i];
+        }
+        for (int i = 0; i < 12; ++i){    
+        bae_HK_minmax.current_min[i] = quant_data.current_quant[i];
+        bae_HK_minmax.current_max[i] = quant_data.current_quant[i];   
+        }
+
+        for (int i = 0; i < 2; ++i){    
+        bae_HK_minmax.Batt_temp_min[i] = quant_data.Batt_temp_quant[i];
+        bae_HK_minmax.Batt_temp_max[i] = quant_data.Batt_temp_quant[i];
+        }    
+        for (int i = 0; i < 3; ++i){    
+        bae_HK_minmax.Batt_gauge_min[i] = quant_data.Batt_gauge_quant[i];
+        bae_HK_minmax.Batt_gauge_max[i] = quant_data.Batt_gauge_quant[i];
+        }
+        for (int i = 0; i < 3; ++i){    
+        bae_HK_minmax.AngularSpeed_min[i] = quant_data.AngularSpeed_quant[i];
+        bae_HK_minmax.AngularSpeed_max[i] = quant_data.AngularSpeed_quant[i];
+        }
+        for (int i = 0; i < 3; ++i){    
+        bae_HK_minmax.Bvalue_min[i] = quant_data.Bvalue_quant[i];
+        bae_HK_minmax.Bvalue_max[i] = quant_data.Bvalue_quant[i];
+        }
+        bae_HK_minmax.BAE_temp_min=quant_data.BAE_temp_quant;
+        bae_HK_minmax.BAE_temp_max=quant_data.BAE_temp_quant;
+        bae_HK_minmax.Batt_voltage_min=quant_data.Batt_voltage_quant;
+        bae_HK_minmax.Batt_voltage_max=quant_data.Batt_voltage_quant;
+          
+    } 
+    else {
+        for (int i = 0; i < 16; ++i)
+        {
+            saveMin(bae_HK_minmax.voltage_min[i],quant_data.voltage_quant[i]);
+            saveMax(bae_HK_minmax.voltage_max[i],quant_data.voltage_quant[i]);
+        }
+        for (int i = 0; i < 12; ++i)
+        {
+            saveMin(bae_HK_minmax.current_min[i],quant_data.current_quant[i]);
+            saveMax(bae_HK_minmax.current_max[i],quant_data.current_quant[i]);
+        }
+        
+        for (int i = 0; i < 2; ++i)
+        {
+            saveMin(bae_HK_minmax.Batt_temp_min[i],quant_data.Batt_temp_quant[i]);
+            saveMax(bae_HK_minmax.Batt_temp_max[i],quant_data.Batt_temp_quant[i]);
+        }
+        for (int i = 0; i < 3; ++i)
+        {
+            saveMin(bae_HK_minmax.Batt_gauge_min[i], quant_data.Batt_gauge_quant[i]);
+            saveMax(bae_HK_minmax.Batt_gauge_max[i], quant_data.Batt_gauge_quant[i]);
+        }
+        for (int i = 0; i < 3; ++i)
+        {
+            saveMin(bae_HK_minmax.AngularSpeed_min[i], quant_data.AngularSpeed_quant[i]);
+            saveMax(bae_HK_minmax.AngularSpeed_max[i], quant_data.AngularSpeed_quant[i]);
+        }
+        for (int i = 0; i < 3; ++i)
+        {
+            saveMin(bae_HK_minmax.Bvalue_min[i], quant_data.Bvalue_quant[i]);
+            saveMax(bae_HK_minmax.Bvalue_max[i], quant_data.Bvalue_quant[i]);
+        }
+        saveMin(bae_HK_minmax.BAE_temp_min,quant_data.BAE_temp_quant);
+        saveMax(bae_HK_minmax.BAE_temp_max,quant_data.BAE_temp_quant);
+        saveMin(bae_HK_minmax.Batt_voltage_min,quant_data.Batt_voltage_quant);
+        saveMin(bae_HK_minmax.Batt_voltage_max,quant_data.Batt_voltage_quant);        
+          
+        
+    }   
+    firstCount=false;
+}
+
+
 //............................................BATTERY GAUGE......................................//
 void FCTN_BATTERYGAUGE_INIT()
 {
@@ -112,7 +328,7 @@
 {
         printf("\n\r battery gauge \n");
 
-        float temp=25;    //=Battery_temp  (from temp sensor on battery board)       //value of battery temperature in C currently given a dummy value. Should be updated everytime.
+        float temp=30;    //=Battery_temp  (from temp sensor on battery board)       //value of battery temperature in C currently given a dummy value. Should be updated everytime.
         tempCompensation(temp);
     
         
@@ -150,21 +366,9 @@
         return (buff[0] << 8) | buff[1];
     }
  
- unsigned short read_soc(char reg , bool ack = true)
-    {
-         
-         //Create a temporary buffer
-        char buff[2];
+
  
-        //Select the register
-        m_I2C.write(m_ADDR, &reg, 1, true);
- 
-        //Read the 16-bit register
-        ack = m_I2C.read(m_ADDR, buff, 2);
- 
-        //Return the combined 16-bit value
-        return (buff[0] << 8) | buff[1];
-    }
+
     
     void write(char reg, unsigned short data)
     {
@@ -377,15 +581,24 @@
     // Get the current state of charge measurement of the MAX17049 as a float
     float soc()
     {
-        //unsigned short value;
+      
+         //Create a temporary buffer
         char buff[2];
-        bool ack;
-        //Read the 16-bit raw SOC value
-        unsigned short value = read_soc(REG_SOC, ack);
+         int ack = 1;
+        //Select the register
+        char reg = REG_SOC;         // cannot pass the hash defined values directly
+        m_I2C.write(m_ADDR, &reg, 1, true);
+        
+ 
+        //Read the 16-bit register
+       
+        ack = m_I2C.read(m_ADDR, buff, 2);
+            
+        printf("\n\r acknow %d", ack);
  
         //Return SOC in percent
         if(ack == 0)
-        return value * 0.00390625;
+        return ((buff[0] << 8) | buff[1]) * 0.00390625;
         else 
         return 200;
     }
--- a/EPS.h	Fri Dec 25 15:25:44 2015 +0000
+++ b/EPS.h	Tue Dec 29 05:14:39 2015 +0000
@@ -39,14 +39,14 @@
 #define TRXY_DR_EN PIN82                  //torque rod driver enable
 #define TRZ_DR_EN  PIN88  
 #define ENBL3V3A PIN33
-#define VBATT PIN20
+//#define VBATT PIN20
 #define BATT_HEAT PIN96
 //#define BATT_HEAT_OUTPUT 
 void FCTN_EPS_INIT();
 
 void FCTN_EPS_POWERMODE(float soc) ;
 
-void HK_main();
+void FCTN_HK_MAIN();
 int quantiz(float start,float step,float x);
 
 void saveMin();
@@ -81,7 +81,7 @@
 void vResetThresholdSet();
 
 void FCTN_BATTTEMP_INIT();
-void FCTN_BATT_TEMP_SENSOR_MAIN();
+void FCTN_BATT_TEMP_SENSOR_MAIN(float*);
 
 typedef struct BAE_HK_actual{
     float voltage_actual[16];
--- a/TCTM.cpp	Fri Dec 25 15:25:44 2015 +0000
+++ b/TCTM.cpp	Tue Dec 29 05:14:39 2015 +0000
@@ -1,432 +1,591 @@
 #include "mbed.h"
 #include "TCTM.h"
-
-uint8_t telemetry_type2[13];
-uint8_t telemetry_type1[130];      //two types of telemetry possible
-uint8_t long_memory_block_tm[128],crc_tm[2];   //  tmlmb
-uint8_t frmseqcnt_fileheadpointer_tm,multi_obsrs_tm_packets[127]; // tm obsrs
-uint8_t tc_liservice_type_packet1_tm[9],tc_liservice_type_packet2_tm[9]; // tm tc_liservice_type 
-uint8_t crc1_tm[2],crc2_tm[2];  // tm flash_crc
-uint8_t tmid1_spr_tm=0xA0,tc_exec_tm,tc_service_typeatus_tm,pac_seq_cnt_of_tc_tm[8]; // tm ack_l1
-uint8_t tmid2_spr_tm=0xB0,tc_pac_seq_cnt_tm,tm_pac_seq_cnt_tm,ackcode_tm; // tm ack_l234
-uint8_t tmid3_spr_tm=0xC0,small_memory_block_tm[7]; // tm smb
-uint8_t tmid4_spr_tm=0xD0,func_mngmt_service_tm[8]; // tm func_mngmt_service_tm
-uint8_t apid_size,pac_seq_cnt,apid,long_or_short=1,service_type,sub_service_type; //All variable used TC 
-uint8_t sd_data[512],sd_read_data[512];
-int TM_SD_BLOCK_NUMBER = 1;uint8_t rtc_tc_data[8];
+#include "crc.h"
 
 
-void FCTN_TC_DECODE(uint8_t* TC_Packet)
-
-{                   
-                  // uint8_t long_or_short;
-                   // uint8_t TC_Packet[long_or_short*124+11]; 
-                   uint8_t service_type,sub_service_type,ackcode_tm,func_mngmt_service_tm[8],crc_tm[2];;
-                   service_type=(TC_Packet[2]&0xF0)>>4;
-                   sub_service_type=(TC_Packet[2]&0x0F);
-                   switch(service_type){
-                        case 6:printf("memory management service\n");
-                               switch(sub_service_type){
-                                    case 1:printf("READ FROM MEMORY\n");
-                                           if(TC_Packet[3]==2){             //TC_Packet[3] is pid                     
-                                            printf("RD_L_FyoyouyLASH1\n");
-                                            //P_BAE_RD_FLASH
-
-                                            uint8_t long_memory_block_tm[128];
-                                            uint8_t TM_packet[130];
-                                              for(int i=0;i<128;i++){
-                                                TM_packet[i]=long_memory_block_tm[i-1];
-                                              }
-                                              for(int i=128;i<130;i++){
-                                                //FCTN_CRC(long_memory_block_tm)
-                                                TM_packet[i]=crc_tm[i-128];
-                                              }
-                                           }
-                                           else if ((TC_Packet[3]&0xF0)==0x10){
-                                            printf("RD_L_FLASH2\n");
-                                            //P_BAE_RD_FLASH
-
-                                            uint8_t long_memory_block_tm[128];
-                                            uint8_t TM_packet[130];
-                                              for(int i=0;i<128;i++){
-                                                TM_packet[i]=long_memory_block_tm[i-1];
-                                              }
-                                              for(int i=128;i<130;i++){
-                                                TM_packet[i]=crc_tm[i-128];
-                                              }
-                                           }
-                                           else if (TC_Packet[3]==0x22){
-                                            printf("RD_S_FLASH1\n");
-                                            //P_BAE_RD_FLASH
-
-                                            uint8_t tmid_spr_tm=0xC0,tc_psc_tm,tm_psc_tm,small_memory_block_tm[8];
-                                            uint8_t TM_packet[13];
-                                            TM_packet[0]=tmid_spr_tm;
-                                            TM_packet[1]=tc_psc_tm;
-                                            TM_packet[2]=tm_psc_tm;
-                                            TM_packet[3]=ackcode_tm;
-                                            for(int i=3;i<11;i++){
-                                                  TM_packet[i]=small_memory_block_tm[i-3];
-                                                }
-                                              for(int i=11;i<13;i++){
-                                                  TM_packet[i]=crc_tm[i-11];
-                                                }
-                                           }
-                                           else if ((TC_Packet[3]&0xF0)==0x30){
-                                            printf("RD_S_FLASH2\n");
-                                            //P_BAE_RD_FLASH
-
-                                            uint8_t tmid_spr_tm=0xC0,tc_psc_tm,tm_psc_tm,small_memory_block_tm[8];
-                                            uint8_t TM_packet[13];
-                                            TM_packet[0]=tmid_spr_tm;
-                                            TM_packet[1]=tc_psc_tm;
-                                            TM_packet[2]=tm_psc_tm;
-                                            TM_packet[3]=ackcode_tm;
-                                            for(int i=3;i<11;i++){
-                                                  TM_packet[i]=small_memory_block_tm[i-3];
-                                                }
-                                              for(int i=11;i<13;i++){
-                                                  TM_packet[i]=crc_tm[i-11];
-                                                }
-                                           }
-                                           else if (TC_Packet[3]==0x42){
-                                            printf("RD_L_RAM1\n");
-                                            //P_BAE_RD_RAM
-
-                                            uint8_t long_memory_block_tm[128];
-                                            uint8_t TM_packet[130];
-                                              for(int i=0;i<128;i++){
-                                                TM_packet[i]=long_memory_block_tm[i-1];
-                                              }
-                                              for(int i=128;i<130;i++){
-                                                TM_packet[i]=crc_tm[i-128];
-                                              }
-                                           }
-                                           else if ((TC_Packet[3]&0xF0)==0x50){
-                                            printf("RD_L_RAM2\n");
-                                            ////P_BAE_RD_RAM
-                                            uint8_t long_memory_block_tm[128];
-                                            uint8_t TM_packet[130];
-                                              for(int i=0;i<128;i++){
-                                                TM_packet[i]=long_memory_block_tm[i-1];
-                                              }
-                                              for(int i=128;i<130;i++){
-                                                TM_packet[i]=crc_tm[i-128];
-                                              }
-                                           }
-                                           else if (TC_Packet[3]==0x62){
-                                            printf("RD_S_RAM\n");
-                                            //P_BAE_RD_RAM
-
-                                            uint8_t tmid_spr_tm=0xC0,tc_psc_tm,tm_psc_tm,small_memory_block_tm[8];
-                                            uint8_t TM_packet[13];
-                                            TM_packet[0]=tmid_spr_tm;
-                                            TM_packet[1]=tc_psc_tm;
-                                            TM_packet[2]=tm_psc_tm;
-                                            TM_packet[3]=ackcode_tm;
-                                            for(int i=3;i<11;i++){
-                                                  TM_packet[i]=small_memory_block_tm[i-3];
-                                                }
-                                              for(int i=11;i<13;i++){
-                                                  TM_packet[i]=crc_tm[i-11];
-                                                }
-                                           }
-                                           else if ((TC_Packet[3]&0xF0)==0x70){
-                                            printf("RD_S_RAM2\n");
-                                            //P_BAE_RD_RAM
-
-                                            uint8_t tmid_spr_tm=0xC0,tc_psc_tm,tm_psc_tm,small_memory_block_tm[8];
-                                            uint8_t TM_packet[13];
-                                            TM_packet[0]=tmid_spr_tm;
-                                            TM_packet[1]=tc_psc_tm;
-                                            TM_packet[2]=tm_psc_tm;
-                                            TM_packet[3]=ackcode_tm;
-                                            for(int i=3;i<11;i++){
-                                                  TM_packet[i]=small_memory_block_tm[i-3];
-                                                }
-                                              for(int i=11;i<13;i++){
-                                                  TM_packet[i]=crc_tm[i-11];
-                                                }
-                                           }
-                    else {
-                        printf("INVALID TC");
-                        //Send Invalid TC Telemetry
-                    }
-                                    break;
-                                    case 5:printf("WRITE ON MEMORY\n");
-                                           switch(TC_Packet[3]){
-                                                case 0:printf("WR_S_FLASH\n");
-                                                     //P_BAE_WR_FLASH
-                                                       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\n");
-                                                     //P_BAE_WR_RAM
-                                                      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\n");
-                                                    //P_BAE_WR_FLASH
-                                                      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\n");
-                                                      //P_BAE_WR_RAM
-                                                        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;
-                                           }
-                                    break;
-                                    case 6:printf("WRITE FROM ONE MEMORY TO ANOTHER\n");
-                                           switch(TC_Packet[3]){
-                                                case 0:printf("WR_S_FLASH\n");
-                                                    //P_BAE_WR_FLASH
-                                                     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;
-                                                }
+uint8_t* FCTN_BAE_TM_TC (uint8_t* tc)
 
-                                                break;
-                                                case 1:printf("WR_S_RAM\n");
-                                                      //P_BAE_WR_RAM
-                                                        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\n");
-                                                    //P_BAE_WR_FLASH
-                                                      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\n");
-                                                     //P_BAE_WR_RAM
-                                                    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;
-                                           }
-                                    break;
-                                    case 9:printf("SEND CRC OF FLASH MEMORY\n");
-                                           if(TC_Packet[3]==2){
-                                            printf("GET_FLASH_CRC\n");
-                                            //P_BAE_GET_FLASH_CRC
-
-                                            uint8_t crc1_tm[2],crc2_tm[2];
-                                            uint8_t TM_packet[130];
-                                          for(int i=0;i<2;i++){
-                                                TM_packet[i]=crc1_tm[i];
-                                              }
-                                            for(int i=2;i<4;i++){
-                                                TM_packet[i]=crc2_tm[i-2];
-                                              }
-                                            for(int i=128;i<130;i++){
-                                                TM_packet[i]=crc_tm[i-128];
-                                              }
-                                           }
-                    else {
-                        printf("INVALID TC");
-                        //Send Invalid TC Telemetry
+{
+    uint8_t service_type=(tc[2]&0xF0);
+    uint8_t* tm;
+    uint16_t crc16;
+   
+    
+    switch(service_type)
+    {
+        case 0x60:
+        {
+            printf("Memory Management Service\r\n");
+            uint8_t service_subtype=(tc[2]&0x0F);
+            
+            switch(service_subtype)
+            {
+                case 0x01:
+                {
+                    printf("Read from Flash\r\n");
+                }
+                case 0x02:
+                {
+                    printf("Read from RAM\r\n");
+                }
+                case 0x05:
+                {
+                    printf("Write on Flash\r\n");
+                }
+                default:
+                {
+                    printf("Invalid TC");
+                    //ACK_L234_TM
+                    tm[0]=0xB0;
+                    tm[1]=tc[0];
+                    tm[2]=ACK_CODE;
+                    for(uint8_t i=3;i<11;i++)
+                    {
+                        tm[i]=0x00;
                     }
-                                    break;
-                    default:{printf("INVALID TC");
-                            //send invalid TC TM
-                             break;}
-                                }
-                        break;
-                        case 8:printf("FUNCTION MANAGEMENT SERVICE\n");
-                               if(sub_service_type==1){
-                                if((TC_Packet[3]&0xF0)==0x00){
-                                    printf("RUN_PRCS\n");
-                                    switch(TC_Packet[3]){
-                                        case 1:printf("P_EPS_INIT\n");
-                                        //FCTN_EPS_INIT
-                                          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 2:printf("P_EPS_MAIN\n");
-                                        //FCTN_EPS_MAIN
-                                          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 3:printf("P_ACS_INIT\n");
-                                        //FCTN_ACS_INIT
-                                         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 4:printf("P_ACS_ACQ_DATA\n");
-                                        //P_ACS_ACQ_DATA
-                                            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 5:printf("P_ACS_MAIN\n");
-                                        //P_ACS_MAIN
-                                          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 6:printf("P_BCN_INIT\n");
-                                        //FCTN_BCN_INIT
-                                          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 7:printf("P_BCN_MAIN\n");
-                                        //FCTN_BCN_MAIN
-                                          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;}
-                                    }
-                                }
-                                else if((TC_Packet[3]&0xF0)==0x10){
-                                    printf("PWR_SWCH\n");
-                                    //P_BAE_SWCH_PWR
-
-                                }
-                                else if((TC_Packet[3]&0xF0)==0x20){
-                                    printf("PWR_Rservice_type\n");
-                                    //P_BAE_RST_PWR
-
-                                }
-                                else if((TC_Packet[3]&0xF0)==0xE0){
-                                    printf("COMSN_ACS_ALGO\n");
-                                    //P_BAE_COMSN_ACS_ALGO
-
-                                    uint8_t tmid_spr_tm=0xD0,tc_psc_tm,tm_psc_tm,ackcode_tm;
-                                    uint8_t TM_packet[13];
-                                    TM_packet[0]=tmid_spr_tm;
-                                    TM_packet[1]=tc_psc_tm;
-                                    TM_packet[2]=tm_psc_tm;
-                                    TM_packet[3]=ackcode_tm;
-                                    for(int i=3;i<11;i++){
-                                          TM_packet[i]=func_mngmt_service_tm[i-3];
-                                        }
-                                      for(int i=11;i<13;i++){
-                                          TM_packet[i]=crc_tm[i-11];
-                                        }
-                                }
-                                else if((TC_Packet[3]&0xF0)==0xF0){
-                                    printf("COMSN_ACS_HW\n");
-                                    //P_BAE_COMSN_ACS_HW
-
-                                    uint8_t tmid_spr_tm=0xD0,tc_psc_tm,tm_psc_tm,ackcode_tm;
-                                    uint8_t TM_packet[13];
-                                    TM_packet[0]=tmid_spr_tm;
-                                    TM_packet[1]=tc_psc_tm;
-                                    TM_packet[2]=tm_psc_tm;
-                                    TM_packet[3]=ackcode_tm;
-                                    for(int i=3;i<11;i++){
-                                          TM_packet[i]=func_mngmt_service_tm[i-3];
-                                        }
-                                      for(int i=11;i<13;i++){
-                                          TM_packet[i]=crc_tm[i-11];
-                                        }
-                                }
-                else {
-                        printf("INVALID TC");
-                        //Send Invalid TC Telemetry
+                    crc16 = CRC::crc16_gen(tm,11);
+                    tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                    tm[12] = (uint8_t)(crc16&0x00FF);
+                    for(uint8_t i=13;i<134;i++)
+                    {
+                        tm[i]=0x00;
                     }
-                               }
-                else {
-                        printf("INVALID TC");
-                        //Send Invalid TC Telemetry
-                    }
-                        break;
-                         default:{printf("INVALID TC");
-                            //send invalid TC TM
-                             break;}
-                    }
-            
+                    return tm;
+                }
+            }
         }
         
-/************************************************************************************************************************************/        
-//FUNCTIONS EXECUTED BY TELECOMMAND
-//MEMORY MANAGEMENT SERVICE
-//PID 0X02
-
-
+        case 0x80:
+        {
+            printf("Function Management Service\r\n");
+            uint8_t service_subtype=(tc[2]&0x0F);
+            
+            switch(service_subtype)
+            {
+                case 0x01:
+                {
+                    printf("FMS Activated\r\n");
+                    
+                    uint8_t pid=tc[3];
+                    switch(pid)
+                    {
+                        case 0x01:
+                        {
+                            printf("Run P_EPS_INIT\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                            
+                        }
+                        case 0x02:
+                        {
+                            printf("Run P_EPS_MAIN\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x03:
+                        {
+                            printf("Run P_ACS_INIT\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x04:
+                        {
+                            printf("Run P_ACS_ACQ_DATA\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x05:
+                        {
+                            printf("Run P_ACS_MAIN\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x06:
+                        {
+                            printf("Run P_BCN_INIT\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x07:
+                        {
+                            printf("Run P_BCN_TX_MAIN\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x11:
+                        {
+                            printf("SW_ON_ACS_ATS1_SW_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x12:
+                        {
+                            printf("SW_ON_ACS_ATS2_SW_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x13:
+                        {
+                            printf("SW_ON_ACS_TR_XY_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x14:
+                        {
+                            printf("SW_ON_ACS_TR_Z_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x15:
+                        {
+                            printf("SW_ON_BCN_TX_SW_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x21:
+                        {
+                            printf("SW_OFF_ACS_ATS1_SW_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x22:
+                        {
+                            printf("SW_OFF_ACS_ATS2_SW_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x23:
+                        {
+                            printf("SW_OFF_ACS_TR_XY_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x24:
+                        {
+                            printf("SW_OFF_ACS_TR_Z_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x25:
+                        {
+                            printf("SW_OFF_BCN_TX_SW_ENABLE\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x31:
+                        {
+                            printf("ACS_ATS_SW_RESET\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x32:
+                        {
+                            printf("BCN_SW_RESET\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0x33:
+                        {
+                            printf("BAE_RESET\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                         case 0x34:
+                        {
+                            printf("CDMS_SW_RESET\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        case 0xE0:
+                        {
+                            printf("CMSN_ACS_ALGO\r\n");
+                            //FMS_TM
+                            tm[0]=0xF0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<132;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,132);
+                            tm[132] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[133] = (uint8_t)(crc16&0x00FF);
+                            return tm;
+                        }
+                        case 0xE1:
+                        {
+                            printf("CMSN_ACS_HW\r\n");
+                            //FMS_TM
+                            tm[0]=0xF0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<132;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,132);
+                            tm[132] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[133] = (uint8_t)(crc16&0x00FF);
+                            return tm;
+                        }
+                        case 0xC1:
+                        {
+                            printf("Reset HK_Counter\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                        default:
+                        {   
+                            printf("Invalid TC\r\n");
+                            //ACK_L234_TM
+                            tm[0]=0xB0;
+                            tm[1]=tc[0];
+                            tm[2]=ACK_CODE;
+                            for(uint8_t i=3;i<11;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            crc16 = CRC::crc16_gen(tm,11);
+                            tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm[12] = (uint8_t)(crc16&0x00FF);
+                            for(uint8_t i=13;i<134;i++)
+                            {
+                                tm[i]=0x00;
+                            }
+                            return tm;
+                        }
+                    }
+                    
+                default:
+                {   
+                    printf("Invalid TC\r\n");
+                    //ACK_L234_TM
+                    tm[0]=0xB0;
+                    tm[1]=tc[0];
+                    tm[2]=ACK_CODE;
+                    for(uint8_t i=3;i<11;i++)
+                    {
+                        tm[i]=0x00;
+                    }
+                    crc16 = CRC::crc16_gen(tm,11);
+                    tm[11] = (uint8_t)((crc16&0xFF00)>>8);
+                    tm[12] = (uint8_t)(crc16&0x00FF);
+                    for(uint8_t i=13;i<134;i++)
+                    {
+                        tm[i]=0x00;
+                    }
+                    return tm;
+                }
+                }
+            }
+        }
+    }
+}            
\ No newline at end of file
--- a/TCTM.h	Fri Dec 25 15:25:44 2015 +0000
+++ b/TCTM.h	Tue Dec 29 05:14:39 2015 +0000
@@ -1,1 +1,3 @@
-void FCTN_TC_DECODE(uint8_t*);
\ No newline at end of file
+#define ACK_CODE 0x02;
+
+uint8_t* FCTN_BAE_TM_TC(uint8_t*);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/crc.h	Tue Dec 29 05:14:39 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
--- a/main.cpp	Fri Dec 25 15:25:44 2015 +0000
+++ b/main.cpp	Tue Dec 29 05:14:39 2015 +0000
@@ -59,7 +59,7 @@
 bool write_ack = 1;
 bool read_ack = 1;
 char telecommand[tc_len];
-char telemetry[tm_len];
+char* telemetry;
 char data_send_flag = 'h'; 
 
 //*****************************************************Assigning pins******************************************************//
@@ -77,19 +77,20 @@
 
 
 /*****************************************************************Threads USed***********************************************************************************/
-Thread *ptr_t_acs;
-Thread *ptr_t_eps;
-Thread *ptr_t_bcn;
+
 Thread *ptr_t_i2c;
 
 /*********************************************************FCTN HEADERS***********************************************************************************/
 
 void FCTN_ISR_I2C();
 void FCTN_TM();
+void F_ACS();
+void F_EPS();
+void F_BCN();
 
 //*******************************************ACS THREAD**************************************************//
 
-void T_ACS(void const *args)
+void F_ACS()
 {
     float b1[3]={-23.376,-37.56,14.739}, omega1[3]={-1.52,2.746,0.7629}, moment1[3]= {1.0498,-1.0535,1.3246};
     //b1[3] = {22, 22,10};
@@ -97,10 +98,9 @@
     // gpo1 = 0;  // att sens2 switch is disabled
     // gpo2 = 0; // att sens switch is disabled
      
-    while(1)
-    {
+    
         
-    Thread::signal_wait(0x1);  
+    //Thread::signal_wait(0x1);  
     ACS_MAIN_STATUS = 's'; //set ACS_MAIN_STATUS flag 
     PWM1 = 0;                     //clear pwm pins
     PWM2 = 0;                     //clear pwm pins
@@ -231,21 +231,34 @@
 
   
     ACS_MAIN_STATUS = 'c'; //clear ACS_MAIN_STATUS flag 
-    }//while ends
-    
-    
+        
 }
 //***************************************************EPS THREAD***********************************************//
 
-void T_EPS(void const *args)
+void F_EPS()
 {
-    while(1)
-    {
-        Thread::signal_wait(0x2);  
+    
         pc.printf("\n\rEntered EPS   %f\n",t_start.read());
         EPS_MAIN_STATUS = 's'; // Set EPS main status
-        //FCTN_READ_HK();
-        //FCTN_APPEND_HKDATA();
+        FCTN_BATT_TEMP_SENSOR_MAIN(actual_data.Batt_temp_actual);
+          pc.printf("\n\r Battery temperature %f %f" ,actual_data.Batt_temp_actual[0], actual_data.Batt_temp_actual[1]);
+          EPS_BATTERY_TEMP_STATUS = 's';          //set EPS_BATTERY_TEMP_STATUS
+          if(EPS_BATTERY_HEAT_ENABLE == 'e')
+          {
+              if((actual_data.Batt_temp_actual[0] < batt_heat_low) & (actual_data.Batt_temp_actual[1] < batt_heat_low)) // to confirm
+              {
+                  batt_heat = 1;    //turn on battery heater
+              }
+              else
+              {
+                  batt_heat = 0;     //turn off battery heater
+              }
+              
+           } 
+          else if(EPS_BATTERY_HEAT_ENABLE == 'd')
+          {
+                EPS_STATUS = 1;//EPS_STATUS = EPS_BATTERY_HEATER_DISABLED
+          }
         FCTN_BATTERYGAUGE_MAIN(actual_data.Batt_gauge_actual);
         if (actual_data.Batt_gauge_actual[1] == 200)   //data not received
         {
@@ -258,52 +271,33 @@
           FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);            //updating power level 
           EPS_BATTERY_GAUGE_STATUS = 's';           //set EPS_BATTERY_GAUGE_STATUS
         }
-       /* if( Temperature data received)
-        {
-          FCTN_BATT_TEMP_SENSOR_MAIN();
-          EPS_BATTERY_TEMP_STATUS = 's';          //set EPS_BATTERY_TEMP_STATUS
-          if(EPS_BATTERY_HEAT_ENABLE = 'e')
-          {
-              if(actual_data.Batt_temp_actual[0] < batt_heat_low)
-              {
-                  batt_heat = 1;    //turn on battery heater
-              }
-              else
-              {
-                  batt_heat = 0;     //turn off battery heater
-              }
-              
-           } 
-          else if(EPS_BATTERY_HEAT_ENABLE = 'd)
-          {
-              EPS_STATUS = EPS_BATTERY_HEATER_DISABLED;
-          } 
+       // if( Temperature data received)
+        //{
+          
           
-        }
-        else
-        {
-          Set battery temp to XX  
-          EPS_BATTERY_TEMP_STATUS = 'c';          //clear EPS_BATTERY_TEMP_STATUS
-          EPS_STATUS = EPS_ERR_BATTERY_TEMP;
-        }
+      //  }
+//        else
+//        {
+//          Set battery temp to XX  
+//          EPS_BATTERY_TEMP_STATUS = 'c';          //clear EPS_BATTERY_TEMP_STATUS
+//          EPS_STATUS = EPS_ERR_BATTERY_TEMP;
+//        }
+        FCTN_HK_MAIN();
+        //FCTN_APPEND_HKDATA();
+        //minMaxHkData();
           
-        EPS_MAIN_STATUS = 'c'; // clear EPS main status */
-          
-    }
-    
+        EPS_MAIN_STATUS = 'c'; // clear EPS main status 
+
 }
 
 //**************************************************BCN THREAD*******************************************************************//
 
-void T_BCN(void const *args)
+void F_BCN()
 {
-    while(1)
-    {
-          Thread::signal_wait(0x3);  
+  
           pc.printf("\n\rEntered BCN   %f\n",t_start.read());
           
           P_BCN_TX_MAIN();
-    }
     
 }
 
@@ -319,15 +313,19 @@
             slave.stop();     
         else if( slave.receive() == 1)                                     // slave writes to master
         {
-            write_ack=slave.write(telemetry,tm_len);
+            write_ack=slave.write((char*)telemetry,tm_len);
         }
         else if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
         {
             read_ack=slave.read(telecommand,tc_len);
             pc.printf("\n\rTELECOMMAND received from CDMS is %s \n",telecommand);
             pc.printf("\n\r Executing Telecommand \n"); 
-            FCTN_TC_DECODE((uint8_t*) telecommand);
+           // FCTN_TC_DECODE((uint8_t*) telecommand);
+           uint8_t* temp = FCTN_BAE_TM_TC((uint8_t*) telecommand);
+           telemetry = (char*)temp;
             FCTN_TM();
+            // for(int i = 0; i<134; i++)
+            //pc.printf("%c", telemetry[i]);
         } 
        
     } 
@@ -363,17 +361,16 @@
     }
     if(schedcount%1==0)
     {
-        //ptr_t_acs -> signal_set(0x1);
+       F_ACS();
     }
     
     if(schedcount%2==0)
     {
-        ptr_t_eps -> signal_set(0x2);
-        
+        F_EPS();
     }
     if(schedcount%3==0)
     { 
-        //ptr_t_bcn -> signal_set(0x3);
+       // F_BCN();
     }
     schedcount++;
     printf("\n\r exited scheduler");
@@ -446,6 +443,8 @@
         BAE_STATUS &= 0xFFFDFFFF;               //clear EPS_BATTERY_GAUGE_STATUS
     else if(EPS_BATTERY_GAUGE_STATUS == 's')
         BAE_STATUS |= 0x00020000;               //set EPS_BATTERY_GAUGE_STATUS
+        
+    
 
    
     pc.printf("\n\r BAE status %x BAE ENABLE %x ",BAE_STATUS,BAE_ENABLE);
@@ -454,9 +453,17 @@
 void FCTN_BAE_INIT()
 {
     printf("\n\r Initialising BAE ");
-   // FCTN_ACS_INIT();
+    //..........intial status....//
+    ACS_STATE = '4';
+    ACS_ATS_ENABLE = 'e';
+    ACS_DATA_ACQ_ENABLE = 'e';
+    EPS_BATTERY_HEAT_ENABLE = 'e';
+    //............................//
+    FCTN_ACS_INIT();
     FCTN_EPS_INIT();
     //P_BCN_INIT();
+
+    
     FLAG();
 }
 
@@ -472,28 +479,21 @@
         while(t_rfsilence.read() < RF_SILENCE_TIME); 
     }               
     */
-    ACS_STATE = '4';
+    
     //ACS_INIT_STATUS = 'c';
     //ACS_DATA_ACQ_STATUS = 'c';
     gpo1 = 0;
     FLAG();
     FCTN_BAE_INIT();
-    ACS_ATS_ENABLE = 'e';
-    ACS_DATA_ACQ_ENABLE = 'e';
+    
     
     //...i2c..
-    strcpy(telemetry,"This is telemetry THis is sample telemetry. ffffffffffffffffffffffffffffff  end");
+    //strcpy(telemetry,"This is telemetry THis is sample telemetry. ffffffffffffffffffffffffffffff  end");
     slave.address(addr);
     irpt_2_mstr = 0;
     
     ptr_t_i2c = new Thread(T_TC);
     ptr_t_i2c->set_priority(osPriorityHigh);
-    ptr_t_acs = new Thread(T_ACS);      
-    ptr_t_acs->set_priority(osPriorityAboveNormal);
-    ptr_t_eps = new Thread(T_EPS);      
-    ptr_t_eps->set_priority(osPriorityAboveNormal);
-    ptr_t_bcn = new Thread(T_BCN);      
-    ptr_t_bcn->set_priority(osPriorityAboveNormal);
     
     irpt_4m_mstr.enable_irq();
     irpt_4m_mstr.rise(&FCTN_I2C_ISR);
@@ -505,4 +505,5 @@
     gpo1 = 0;  // att sens2 switch is enabled
     //FCTN_BAE_INIT();
     while(1);                                                   //required to prevent main from terminating
-}
\ No newline at end of file
+}
+