Team Fox / Mbed 2 deprecated BAE_QM_MAR9

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of workinQM_5thJan_azad by Team Fox

Revision:
39:670133e7ffd8
Parent:
33:76f2b8735501
Child:
40:c2538d97e78b
Child:
42:413cd8c60cfc
Child:
45:b5bd48ffbb67
Child:
47:d59ba66229ce
--- a/main.cpp	Mon Jul 04 04:29:59 2016 +0000
+++ b/main.cpp	Tue Jul 05 13:44:15 2016 +0000
@@ -6,9 +6,12 @@
 #include "BCN.h"
 #include "TCTM.h"
 #define tm_len 135
-#define tc_len 11
+#define tc_len 134
 #define batt_heat_low 20
 #define print 1
+#define PRINT2 1
+#define baby 1
+#define baby2 1
 
 #define DISABLE_WDOG 0;
 
@@ -25,7 +28,7 @@
     kick_WDOG();
 }
 
-DigitalOut time_wdog(PIN68);// for determining the time between code
+DigitalInOut time_wdog(PIN68);// for determining the time between code
 
 //**********************************************GLOBAL RTOS TIMER*********************************************************//
 RtosTimer *BAE_STANDBY_STATUS_TIMER;
@@ -49,7 +52,7 @@
     *min = time/60;    
 }
 
-extern DigitalOut BTRY_HTR_ENABLE;
+extern DigitalInOut BTRY_HTR_ENABLE;
 
 uint8_t HTR_CYCLE_COUNTS=0;         //Count of heater cycles
 uint8_t HTR_CYCLE_START_DLY=0;      //EPS_HTR_DLY_TIMER timer duration in minutes
@@ -172,7 +175,7 @@
 extern void FCTN_BCN_FEN(void const *args);
 extern void LONG_BCN_DATA(uint8_t* );
 extern uint16_t gencrc16_for_me();//for calculating the crc of baehk data before sendingkk as including crc causes problem
-extern DigitalOut BTRY_HTR_ENABLE;
+extern DigitalInOut BTRY_HTR_ENABLE;
 
 
 Timeout timeout_bcn;
@@ -194,7 +197,7 @@
 extern uint8_t EPS_BAT_TEMP_HIGH;
 extern uint8_t EPS_BAT_TEMP_DEFAULT;
 float EPS_BTRY_TMP_AVG;
-extern DigitalOut BTRY_HTR_ENABLE;
+extern DigitalInOut BTRY_HTR_ENABLE;
 
 //extern void FCTN_BATTERYGAUGE_MAIN(float*Battery_parameters);
 
@@ -204,7 +207,7 @@
 //CDMS
 //eps cdms fault
 uint8_t CDMS_SW_STATUS;
-DigitalOut CDMS_OC_FAULT(PIN79);
+DigitalInOut CDMS_OC_FAULT(PIN79);
 bool CDMS_SW_ENABLE;
 int CDMS_FAULT_COUNTER = 0;
 
@@ -221,7 +224,7 @@
 //uint16_t BCN_TX_MAIN_COUNTER = 0;
 uint8_t BCN_LONG_MSG_TYPE = 1;
 
-DigitalOut BCN_TX_OC_FAULT(PIN80);
+DigitalInOut BCN_TX_OC_FAULT(PIN80);
 int BCN_TX_FAULT_COUNTER;   
 
 //ACS
@@ -229,23 +232,23 @@
 
 //eps hw faults
 //uint8_t ACS_TR_Z_SW_STATUS;
-DigitalOut ACS_TR_Z_ENABLE(PIN40);
-DigitalOut ACS_TR_Z_OC_FAULT(PIN91);
-DigitalOut ACS_TR_Z_FAULT(PIN89);            //Driver IC fault
+DigitalInOut ACS_TR_Z_ENABLE(PIN40);
+DigitalInOut ACS_TR_Z_OC_FAULT(PIN91);
+DigitalInOut ACS_TR_Z_FAULT(PIN89);            //Driver IC fault
 int ACS_TR_Z_FAULT_COUNTER = 0;
 
 //uint8_t ACS_TR_XY_SW_STATUS;
-DigitalOut ACS_TR_XY_ENABLE(PIN71);
-DigitalOut ACS_TR_XY_OC_FAULT(PIN72);
-DigitalOut ACS_TR_XY_FAULT(PIN83);            //Driver IC fault
+DigitalInOut ACS_TR_XY_ENABLE(PIN71);
+DigitalInOut ACS_TR_XY_OC_FAULT(PIN72);
+DigitalInOut ACS_TR_XY_FAULT(PIN83);            //Driver IC fault
 int ACS_TR_XY_FAULT_COUNTER = 0;
 
 //bool ACS_ATS1_ENABLE;
-DigitalOut ACS_ATS1_OC_FAULT(PIN97);
+DigitalInOut ACS_ATS1_OC_FAULT(PIN97);
 int ACS_ATS1_FAULT_COUNTER = 0;
 
 //bool ACS_ATS2_ENABLE;
-DigitalOut ACS_ATS2_OC_FAULT(PIN5);
+DigitalInOut ACS_ATS2_OC_FAULT(PIN5);
 int ACS_ATS2_FAULT_COUNTER;
 
 //EPS
@@ -300,8 +303,8 @@
 extern uint8_t ACS_DEMAG_TIME_DELAY;
 extern uint16_t ACS_Z_FIXED_MOMENT;
 
-int write_ack = 1;
-int read_ack = 1;
+int write_ack;
+int read_ack;
 char telecommand[tc_len];
 
 
@@ -311,8 +314,8 @@
 bool if2check = 0;
 
 //ASSIGNING PINS//
-DigitalOut ATS1_SW_ENABLE(PTC0); // enable of att sens2 switch
-DigitalOut ATS2_SW_ENABLE(PTC16); // enable of att sens switch
+DigitalInOut ATS1_SW_ENABLE(PTC0); // enable of att sens2 switch
+DigitalInOut ATS2_SW_ENABLE(PTC16); // enable of att sens switch
 InterruptIn irpt_4m_mstr(PIN38);                                      //I2c interrupt from CDMS
 DigitalOut irpt_2_mstr(PIN4);                                        //I2C interrupt to CDMS
 I2CSlave slave (PIN1,PIN2);///pin1 pin2
@@ -325,16 +328,16 @@
 
 //........faults
 //Polled Faults
-DigitalIn pf1(PIN5);//Attitude Sensor 1 OC bar fault signal
-DigitalIn pf2(PIN97);//Attitude Sensor 2 OC bar fault signal
-DigitalIn pf3(PIN83);//Fault Bar for TRXY driver
+DigitalInOut pf1(PIN5);//Attitude Sensor 1 OC bar fault signal
+DigitalInOut pf2(PIN97);//Attitude Sensor 2 OC bar fault signal
+DigitalInOut pf3(PIN83);//Fault Bar for TRXY driver
  
-DigitalOut TRXY_SW(PIN71);  //TR XY Switch
-DigitalOut DRV_Z_EN(PIN88);    //Sleep pin of driver z
-DigitalOut TRZ_SW(PIN40);  //TR Z Switch
+DigitalInOut TRXY_SW(PIN71);  //TR XY Switch
+DigitalInOut DRV_Z_EN(PIN88);    //Sleep pin of driver z
+DigitalInOut TRZ_SW(PIN40);  //TR Z Switch
 DigitalOut CDMS_RESET(PIN7,1); // CDMS RESET
-DigitalOut BCN_SW(PIN14);      //Beacon switch
-DigitalOut DRV_XY_EN(PIN82);
+DigitalInOut BCN_SW(PIN14);      //Beacon switch
+DigitalInOut DRV_XY_EN(PIN82);
 
 
 
@@ -355,7 +358,42 @@
         for(int j=0;j<8;j++)
             {   
                 FCTN_BAE_WR_FLASH(j,ARR_INITIAL_VAL[j]); 
-            } 
+                ACS_ATS_STATUS  = ARR_INITIAL_VAL[0]>>24;
+                ACS_TR_XY_SW_STATUS = ((uint8_t)(ARR_INITIAL_VAL[0]>>22))&0x03;
+                ACS_TR_Z_SW_STATUS = (ARR_INITIAL_VAL[0]>>20)&0x03;
+                ACS_STATE = (ARR_INITIAL_VAL[0]>>16)&0x0F;
+                //pc.printf("\n\r acs state in starting is %x",ACS_STATE);
+                ACS_DETUMBLING_ALGO_TYPE = (ARR_INITIAL_VAL[0]>>15)&0x01;
+                BCN_TX_SW_STATUS = ((uint8_t)(ARR_INITIAL_VAL[0]>>13))&0x03;
+                BCN_SPND_TX = ((uint8_t)(ARR_INITIAL_VAL[0]>>12))&0x01;
+                BCN_FEN = ((uint8_t)(ARR_INITIAL_VAL[0]>>11))&0x01;
+                BCN_LONG_MSG_TYPE = ((uint8_t)(ARR_INITIAL_VAL[0]>>10))&0x01;
+                EPS_BTRY_HTR_AUTO = ((uint8_t)(ARR_INITIAL_VAL[0]>>9))&0x03;//EPS_BATTERY_HEATER_ENABLE
+                //now one spares in telemetry[5]
+                
+                //updating the reset counter
+                BAE_RESET_COUNTER = ((uint8_t)(ARR_INITIAL_VAL[0]));
+                EPS_SOC_LEVEL_12 = (uint8_t)(ARR_INITIAL_VAL[1]>>24); 
+                EPS_SOC_LEVEL_23 = (uint8_t)(ARR_INITIAL_VAL[1]>>16);
+                ACS_MAG_TIME_DELAY = (uint8_t)(ARR_INITIAL_VAL[1]>>8);
+                ACS_DEMAG_TIME_DELAY = (uint8_t)ARR_INITIAL_VAL[1];
+                        
+                EPS_BAT_TEMP_LOW = (uint8_t)(ARR_INITIAL_VAL[2]>>24);
+                EPS_BAT_TEMP_HIGH = (uint8_t)(ARR_INITIAL_VAL[2]>>16);
+                EPS_BAT_TEMP_DEFAULT = (uint8_t)(ARR_INITIAL_VAL[2]>>8);
+                                                                            
+                ACS_MM_X_COMSN = (uint16_t)(ARR_INITIAL_VAL[3]>>16);                                                                
+                ACS_MM_Y_COMSN = (uint16_t)ARR_INITIAL_VAL[3];
+                                                                            
+                ACS_MG_X_COMSN = (uint16_t)(ARR_INITIAL_VAL[4]>>16);
+                ACS_MG_Y_COMSN = (uint16_t)ARR_INITIAL_VAL[4];
+                                                                        
+                ACS_MM_Z_COMSN = (uint16_t)(ARR_INITIAL_VAL[5]>>16);
+                ACS_MG_Z_COMSN = (uint16_t)ARR_INITIAL_VAL[5];
+                                                                            
+                ACS_Z_FIXED_MOMENT = (uint16_t)(ARR_INITIAL_VAL[6]>>16);//assignvalues 
+                                 
+        } 
     else
        {
         for(int j=0;j<8;j++)
@@ -366,6 +404,7 @@
         ACS_TR_XY_SW_STATUS = ((uint8_t)(read[0]>>22))&0x03;
         ACS_TR_Z_SW_STATUS = (read[0]>>20)&0x03;
         ACS_STATE = (read[0]>>16)&0x0F;
+        //pc.printf("\n\r acs state in starting is %x",ACS_STATE);
         ACS_DETUMBLING_ALGO_TYPE = (read[0]>>15)&0x01;
         BCN_TX_SW_STATUS = ((uint8_t)(read[0]>>13))&0x03;
         BCN_SPND_TX = ((uint8_t)(read[0]>>12))&0x01;
@@ -378,6 +417,7 @@
         BAE_RESET_COUNTER = ((uint8_t)(read[0]))+1;
         read[0]=(read[0]&0xffffff00) | (uint32_t)BAE_RESET_COUNTER;
         FCTN_BAE_WR_FLASH(0,read[0]);
+        pc.printf("\n\r reset counter %d",BAE_RESET_COUNTER);
         
                 
         EPS_SOC_LEVEL_12 = (uint8_t)(read[1]>>24); 
@@ -431,7 +471,7 @@
     I2C1->C1 |= 0x80;  
     PORTE->PCR[1] |= 0x00000004;
     PORTE->PCR[0] |= 0x00000004;    
-    Thread::wait(1);
+    wait_ms(1);
 }
 
 //*******************************************ACS THREAD**************************************************//
@@ -720,179 +760,203 @@
 
 //**************************************************TCTM THREAD*******************************************************************//
 
+//===================================================================
 void T_TC(void const * args)
 {   
     while(1)
-        {            
-            Thread::signal_wait(0x4);
-            //wait_us(200);// se if it can be changed//@Lakshya:It can't be changed :)
-            wait_us(300);
-            BAE_MNG_I2C_STATUS =1 ;
-            I2C_last.reset();
-            I2C_last.start();
-            FLAG();
-            //earlier BAE_I2CRX_STATUS=1;
-            #if print
-                printf("\n\rreached\n");
-            #endif                                             // can be between 38 to 15700
-            if( slave.receive() == 0)
+    {  
+        Thread::signal_wait(0x4);
+        wait_us(300);
+        BAE_MNG_I2C_STATUS =1 ;
+        I2C_last.reset();
+        I2C_last.start();
+        pc.printf("\n\r intrpet");
+        if( slave.receive() == 0)
+        {
+            pdir_ss1=PTE->PDIR;                                         /////////edited
+            irpt_2_mstr = 0;
+            data_send_flag = 'h';                
+            slave.stop();                
+            if(((pdir_ss1 & 0x00000003)!=3))        //check if bus has freezed
+            {                  
+                I2C_busreset();                                         /////////edited
+            }
+            pdir_ss2=PTE->PDIR;
+            pc.printf("\n\rSlave not addressed");
+            pc.printf("\n\rPTE->DIR = 0x%08X",pdir_ss1);                
+            #if PRINT2
+            pc.printf("\n\rPTE->DIR = 0x%08X",pdir_ss2);
+            #endif
+        }
+        else if( slave.receive() == 1)                                     // slave writes to master
+        {
+            BAE_I2C_COUNTER++;                                                      //////////edited
+            if(data_send_flag == 'h') //to be renamed as BAE_I2C_STATUS
+            {
+                irpt_2_mstr =1;                                     //wait till cdms code is changed
+                FCTN_APPEND_HKDATA();
+                uint8_t i2c_count =0;
+                //crc is already being added
+                write_ack=slave.write((char*)BAE_chardata,134);
+                wait_ms(1);    //for correct values of register to be updated
+                pdir_tm1=PTE->PDIR;
+                irpt_2_mstr = 0;                             
+                if(write_ack==0)// wait till cdms code is changed
                 {
-                    irpt_2_mstr = 0;
-                    data_send_flag = 'h';
-                    pc.printf("\n\rSlave not addressed");
-                    pc.printf("\n\rPTE->DIR = 0x%08X",pdir_ss1);
-                    slave.stop();
-                    pdir_ss1=PTE->PDIR;                                         /////////edited
-                    if(((pdir_ss1 & 0x00000003)!=3))        //check if bus has freezed
+                    while(((pdir_tm1 & 0x00000003)!=3)&& i2c_count<10)
+                    {
+                        wait_ms(1);
+                        pdir_tm1=PTE->PDIR;
+                        i2c_count++;
+                    }                    
+                    if(((pdir_tm1 & 0x00000003)==3))
                     {                  
-                        I2C_busreset();                                         /////////edited
+                        pc.printf("\n\rWrite HK success");
+                        data_send_flag = 'h';
+                        irpt_2_mstr = 0;                                //////////edited
                     }
-                    /*pdir_ss2=PTE->PDIR;            
-                    #if PRINT2
-                    pc.printf("\n\rPTE->DIR = 0x%08X",pdir_ss2);
-                    #endif*/
-                    #if print
-                        printf("\n\rnot send\n");
+                    else
+                    {
+                        #if baby
+                        pc.printf("\n\rWrite HK failed");
+                        pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm1);
+                        #endif
+                        wait_ms(20); //should be atleast 7ms for correct operation but fix this value as 20ms
+                        I2C_busreset();
+                        #if baby2
+                        pdir_tm2 = PTE->PDIR;
+                        pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm2);
+                        #endif                   
+                    }
+                }
+                else
+                {                    
+                    #if baby1
+                    pc.printf("\n\rWrite Ack failed(HK)");
+                    pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm1);
+                    #endif
+                    I2C_busreset();
+                    #if baby2
+                    pdir_tm2 = PTE->PDIR;
+                    pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm2);
                     #endif
                 }
-            else if( slave.receive() == 1)                                     // slave writes to master
+                i2c_count=0;
+            }
+            else//data_send_flag = "t" //else if(telecommand[1]&0xC0 == 't')
+            {
+                uint8_t i2c_count =0;
+                write_ack=slave.write((char*)telemetry,134);                        ////////edited(size)
+                wait_ms(1);    //for correct values of register to be updated
+                pdir_tm1=PTE->PDIR;
+                irpt_2_mstr = 0;
+                data_send_flag = 'h';               
+                if(write_ack==0)
+                {
+                    while(((pdir_tm1 & 0x00000003)!=3)&& i2c_count<10)
+                    {
+                        wait_ms(1);
+                        pdir_tm1=PTE->PDIR;
+                        i2c_count++;
+                    }                    
+                    if(((pdir_tm1 & 0x00000003)==3))
+                    {                  
+                        pc.printf("\n\rWrite TM success");
+                    }
+                    else
+                    {
+                        #if baby
+                        pc.printf("\n\rWrite TM failed");
+                        pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm1);
+                        #endif
+                        wait_ms(20); //should be atleast 7ms for correct operation but fix this value as 20ms
+                        I2C_busreset();
+                        #if baby2
+                        pdir_tm2 = PTE->PDIR;
+                        pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm2);
+                        #endif
+                    }
+                } 
+                else
                 {
-                    BAE_I2C_COUNTER++;                                                      //////////edited
-                    if(data_send_flag == 'h') //to be renamed as BAE_I2C_STATUS
-                        {
-                            irpt_2_mstr =1;                                     //wait till cdms code is changed
-                            FCTN_APPEND_HKDATA();
-                            uint8_t i2c_count =0;
-                            //crc is already being added
-                            write_ack=slave.write((char*)BAE_chardata,134);
-                            Thread::wait(1);    //for correct values of register to be updated
-                            pdir_tm1=PTE->PDIR;
-                            irpt_2_mstr = 0;                             
-                            if(write_ack==0)// wait till cdms code is changed
-                                {
-                                    while(((pdir_tm1 & 0x00000003)!=3)&& i2c_count<10)
-                                        {
-                                            Thread::wait(1);
-                                            pdir_tm1=PTE->PDIR;
-                                            i2c_count++;
-                                        }                    
-                                    if(((pdir_tm1 & 0x00000003)==3))
-                                        {                  
-                                            pc.printf("\n\rWrite HK success");
-                                            data_send_flag = 'h';
-                                            //shoulddn't it be here
-                                            irpt_2_mstr = 0;                                //////////edited
-                                        }
-                                    else
-                                        {
-                                            wait_ms(20); //should be atleast 7ms for correct operation but fix this value as 20ms
-                                            I2C_busreset();
-                                        }
-                                    #if print
-                                        pc.printf("\n\rgot interrupt\n");
-                                    #endif
-                                }
-                            else
-                                {
-                                    I2C_busreset();           
-                                }
-                            i2c_count=0;
-                        }
-                    else//data_send_flag = "t" //else if(telecommand[1]&0xC0 == 't')
-                        {
-                            uint8_t i2c_count =0;
-                            write_ack=slave.write((char*)telemetry,134);                        ////////edited(size)
-                            Thread::wait(1);    //for correct values of register to be updated
-                            pdir_tm1=PTE->PDIR;
-                            irpt_2_mstr = 0;
-                            data_send_flag = 'h';               
-                            if(write_ack==0)
-                            {
-                                while(((pdir_tm1 & 0x00000003)!=3)&& i2c_count<10)
-                                    {
-                                        Thread::wait(1);
-                                        pdir_tm1=PTE->PDIR;
-                                        i2c_count++;
-                                    }                    
-                                if(((pdir_tm1 & 0x00000003)==3))
-                                    {                  
-                                        pc.printf("\n\rWrite TM success");
-                                    }
-                                else
-                                    {
-                                        wait_ms(20); //should be atleast 7ms for correct operation but fix this value as 20ms
-                                        I2C_busreset();                       
-                                    }
-                            } 
-                            else
-                                {
-                                    I2C_busreset();                   
-                                }
-                            i2c_count=0;                  
-                        }
-                       
+                    #if baby
+                    pc.printf("\n\rWrite Ack failed");
+                    pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm1);
+                    #endif
+                    I2C_busreset();
+                    #if baby2
+                    pdir_tm2 = PTE->PDIR;
+                    pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm2);                   
+                    #endif
                 }
-            else if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
+                i2c_count=0;                  
+            }                   
+        }
+        else if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
+        {
+            BAE_I2C_COUNTER++;
+            uint8_t i2c_count = 0;            
+            read_ack=slave.read((char *)telecommand,135);    //read() function returns acknowledgement
+            wait_ms(1);
+            pdir_tc1=PTE->PDIR;            
+            if(read_ack==0)     //read() says it was successful
+            {
+                while(((pdir_tc1 & 0x00000003)!=3)&& i2c_count<10)//checking if SDA and SCL lines are logic 0 and not more than 10 times(10ms)
+                {
+                    wait_ms(1);
+                    pdir_tc1=PTE->PDIR;
+                    i2c_count++;
+                }                    
+                if(((pdir_tc1 & 0x00000003)==3))
+                {
+                    pc.printf("\n\n\rRead TC success");
+                    if(telecommand[0] == 0x00)                          /////////////edited
+                        FCTN_CDMS_HK_TC((uint8_t*) telecommand);        /////////////edited
+                    else                   
+                    {
+                        FCTN_BAE_TM_TC((uint8_t*) telecommand);
+                        data_send_flag = 't';
+                        irpt_2_mstr = 1;
+                    }
+                }
+                else     //either or both of SDA and SCL lines low
                 {
-                    BAE_I2C_COUNTER++;
-                    uint8_t i2c_count = 0;            
-                    read_ack=slave.read((char *)telecommand,11);    //read() function returns acknowledgement
-                    Thread::wait(1);
-                    pdir_tc1=PTE->PDIR;            
-                    if(read_ack==0)     //read() says it was successful
-                        {
-                            while(((pdir_tc1 & 0x00000003)!=3)&& i2c_count<10)//checking if SDA and SCL lines are logic 0 and not more than 10 times(10ms)
-                                {
-                                    Thread::wait(1);
-                                    pdir_tc1=PTE->PDIR;
-                                    i2c_count++;
-                                }                    
-                            if(((pdir_tc1 & 0x00000003)==3))
-                                {
-                                    pc.printf("\n\n\rRead TC success");   
-                                    //if(telecommand[1]&0xC0 == 0x00)
-                                    if(telecommand[0] == 0x00)                          /////////////edited
-                                        FCTN_CDMS_HK_TC((uint8_t*) telecommand);        /////////////edited
-                                    //FCTN_TC_DECODE((uint8_t*) telecommand);
-                                    else                   
-                                        {
-                                            FCTN_BAE_TM_TC((uint8_t*) telecommand);
-                                            //telemetry = (char*)temp;                  
-                                            data_send_flag = 't';
-                                            //pc.printf("\n\r Telemetry Generation \n");
-                                            irpt_2_mstr = 1;
-                                        }
-                                }
-                           else     //either or both of SDA and SCL lines low
-                                {
-                                    I2C_busreset();
-                                    // data_send_flag = 'i';
-                                    gen_I_TM();
-                                    data_send_flag = 't';
-                                    //shouldn't it be here
-                                    irpt_2_mstr = 1;                                    /////////////added
-                                }
-                        }
-                    else //read() says it was not successful
-                        {
-                            I2C_busreset();
-                            //data_send_flag = 'i';   //'i' = invalid
-                            gen_I_TM();
-                            data_send_flag = 't';  
-                            irpt_2_mstr = 1;                                            /////////////added
-                        }
-                    i2c_count = 0;
-                    #if print
-                        pc.printf("\n\r time taken %d",t_tc.read_us());
-                        for(int i = 0; i<134; i++)
-                            pc.printf("%c", telemetry[i]);
+                    #if baby
+                    pc.printf("\n\rRead TC failed");
+                    pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm1);
+                    #endif
+                    I2C_busreset();
+                    #if baby2
+                    pdir_tm2 = PTE->PDIR;                    
+                    pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm2);
                     #endif
-                }     
-            //was commented here now in txt file main_i2ccomment on desktp
-            BAE_MNG_I2C_STATUS=0;
-            FLAG();
-        }
+                    gen_I_TM();
+                    data_send_flag = 't';
+                    irpt_2_mstr = 1;                                    /////////////added
+                }
+            }
+            else //read() says it was not successful
+            {                
+                #if baby
+                pc.printf("\n\rRead Ack failed");
+                pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm1);                
+                #endif
+                I2C_busreset();
+                #if baby2
+                pdir_tm2 = PTE->PDIR;
+                pc.printf("\n\rPTE->PDIR = 0x%08X",pdir_tm2);
+                #endif
+                //pc.printf("\n\r no data case");
+                gen_I_TM();
+                data_send_flag = 't';  
+                irpt_2_mstr = 1;                                            /////////////added
+            }
+            i2c_count = 0;
+        }     
+        BAE_MNG_I2C_STATUS=0;
+    }
 }
+
 void FCTN_TM()
 { 
     data_send_flag = 't';