I2C BAE standalone hardware testing

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_Flowchart_BAE_1 by Team Fox

main.cpp

Committer:
prasanthbj05
Date:
2016-06-24
Revision:
17:8a8024c45dc0
Parent:
16:cc77770d787f
Child:
18:95f0cc565ee3

File content as of revision 17:8a8024c45dc0:

#include "mbed.h"
#include "rtos.h"
#include "pin_config.h"
#include "ACS.h"
#include "EPS.h"
#include "BCN.h"
#include "TCTM.h"

#define tm_len 135
#define tc_len 11
#define batt_heat_low 20
#define PRINT 1

//***************************************************** flags *************************************************************//
uint32_t BAE_STATUS = 0x00000000;
uint32_t BAE_ENABLE = 0xFFFFFFFF;

//i2c//
char data_send_flag = 'h'; 

//.........acs...............//
/* char ACS_INIT_STATUS = 'q';
char ACS_DATA_ACQ_STATUS = 'q';
char ACS_ATS_STATUS = 'q';
char ACS_MAIN_STATUS = 'q';
char ACS_STATUS = 'q';

char ACS_ATS_ENABLE = 'q';
char ACS_DATA_ACQ_ENABLE = 'q';
char ACS_STATE = 'q';*/

uint8_t ACS_INIT_STATUS = 0;
uint8_t ACS_DATA_ACQ_STATUS = 0;
uint8_t ACS_ATS_STATUS = 0x60;
uint8_t ACS_MAIN_STATUS = 0;
uint8_t ACS_STATUS = 0;

uint8_t ACS_ATS_ENABLE = 1;
uint8_t ACS_DATA_ACQ_ENABLE = 1;
uint8_t ACS_STATE = 4;

//.....................eps...................//
//eps init
/*char EPS_INIT_STATUS = 'q';
char EPS_BATTERY_GAUGE_STATUS = 'q';
//eps main
char EPS_MAIN_STATUS = 'q';
char EPS_BATTERY_TEMP_STATUS = 'q';
char EPS_STATUS = 'q';

char EPS_BATTERY_HEAT_ENABLE = 'q';
*/

uint8_t EPS_INIT_STATUS = 0;
uint8_t EPS_BATTERY_GAUGE_STATUS = 0;
//eps main
uint8_t EPS_MAIN_STATUS = 0;
uint8_t EPS_BATTERY_TEMP_STATUS = 0;
uint8_t EPS_STATUS = 7; //invalid status

uint8_t EPS_BATTERY_HEAT_ENABLE = 0;

//.......................global variables..................................................................// new hk structure- everything has to changed based on this
uint8_t BAE_data[74];  
char BAE_chardata[74];     
    

//*************************************Global declarations************************************************//
const int addr = 0x20;                                            //slave address 

Timer t_rfsilence;
Timer t_start;
Timer t_tc;
Timer t_tm;
Timer synch;
Serial pc(USBTX, USBRX);
int power_flag_dummy=2;
float data[6];

extern float moment[3];
extern uint8_t BCN_FEN;
extern BAE_HK_actual actual_data;
extern BAE_HK_quant quant_data;
extern BAE_HK_min_max bae_HK_minmax;
extern BAE_HK_arch arch_data;

int write_ack = 1;
int read_ack = 1;
uint8_t telecommand[11];
extern uint8_t telemetry[135];

bool pf1check = 0;
bool pf2check = 0;
bool if1check = 0;
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

InterruptIn irpt_4m_mstr(PIN38);                                      //I2c interrupt from CDMS
DigitalOut irpt_2_mstr(PIN4);                                        //I2C interrupt to CDMS
I2CSlave slave (PIN1,PIN2);///pin1 pin2
DigitalOut batt_heat(PIN96);

//ATS1_SW_ENABLE = 0;
PwmOut PWM1(PIN93); //x                         //Functions used to generate PWM signal 
PwmOut PWM2(PIN94); //y
PwmOut PWM3(PIN95); //z                         //PWM output comes from pins p6

//........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
 
//Interrupt based faults
//InterruptIn  ir1(PIN73);//Battery Gauge - Alert Bar Signal
InterruptIn  ir2(PIN72);//TRXY Driver TR switch Fault
InterruptIn  ir3(PIN89);//TRZ Driver Fault Bar
InterruptIn  ir4(PIN91);//TRZ Driver TR switch Fault
InterruptIn  ir5(PIN79);//CDMS - Switch Fault
InterruptIn  ir6(PIN80);//Beacon- Switch OC bar
InterruptIn  ir7(PIN42);//Charger IC - Fault Bar


//DigitalOut TRXY_SW_EN(PIN71);  //TR XY Switch
//DigitalOut DRV_Z_SLP(PIN88);    //Sleep pin of driver z
//DigitalOut TRZ_SW(PIN40);  //TR Z Switch
//DigitalOut CDMS_RESET(PIN7); // CDMS RESET
//DigitalOut BCN_SW(PIN14);      //Beacon switch
//DigitalOut DRV_XY_SLP(PIN82);


DigitalOut TRXY_SW(PIN71);  //TR XY Switch
DigitalOut DRV_Z_EN(PIN88);    //Sleep pin of driver z
DigitalOut TRZ_SW(PIN40);  //TR Z Switch
DigitalOut CDMS_RESET(PIN7,1); // CDMS RESET
DigitalOut BCN_SW(PIN14);      //Beacon switch
DigitalOut DRV_XY_EN(PIN82);

/*****************************************************************Threads USed***********************************************************************************/

Thread *ptr_t_i2c;

/*********************************************************FCTN HEADERS***********************************************************************************/

void FCTN_ISR_I2C();
void FCTN_TM();
void F_ACS();
void F_EPS();
void F_BCN();

//*******************************************ACS THREAD**************************************************//
uint8_t iterP1;
uint8_t iterP2;
uint8_t iterI1;
uint8_t iterI2;

 
void F_ACS()
{
    

    //...................//
    
    if(pf1check == 1)
    {
        if(iterP1 >= 3)
        {
            ATS1_SW_ENABLE = 1;  // turn off ats1 permanently
            //FCTN_SWITCH_ATS(0);  // switch on ATS2    
        }
        else    
        {
        ATS1_SW_ENABLE = 0;
        iterP1++;
        }
        pf1check = 0;
    }
    if(pf2check == 1)
    {
        if(iterP1 >= 3)
        {
            ATS2_SW_ENABLE = 1;  // turn off ats2 permanently
            ACS_DATA_ACQ_ENABLE = 0;
            ACS_STATE = 2 ; // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY    
        }
        else    
        {
        ATS2_SW_ENABLE = 0;
        iterP2++;
        }
        pf2check = 0;
    }
     if(if1check == 1)
    {
        if(iterI1 >= 3)
        {
            TRXY_SW = 0;  // turn off TRXY permanently
        }
        else    
        {
         TRXY_SW = 1;   //switch on TRXY
         iterI1++;
        }
    }    
    if(if2check == 1)
    {
        if(iterI2 >= 3)
        {
            TRZ_SW = 0;  // turn off TRZ permanently
            ACS_STATE = 2 ; // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY  
        }
        else    
        {
         TRZ_SW = 1;   //switch on Z
         iterI2++;
        }
    }
    
    //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};
    //omega1[3] = {2.1,3.0,1.5};
    // ATS1_SW_ENABLE = 0;  // att sens2 switch is disabled
    // ATS2_SW_ENABLE = 0; // att sens switch is disabled
     
    
        
    //Thread::signal_wait(0x1);  
    ACS_MAIN_STATUS = 1; //set ACS_MAIN_STATUS flag 
    PWM1 = 0;                     //clear pwm pins
    PWM2 = 0;                     //clear pwm pins
    PWM3 = 0;                     //clear pwm pins
    pc.printf("\n\rEntered ACS   %f\n",t_start.read());
    
    if(ACS_DATA_ACQ_ENABLE == 1)// check if ACS_DATA_ACQ_ENABLE = 1?
    {
    //FLAG();
    FCTN_ATS_DATA_ACQ(); //the angular velocity is stored in the first 3 values and magnetic field values in next 3
    pc.printf("gyro values\n\r"); //printing the angular velocity and magnetic field values
    for(int i=0; i<3; i++) 
    {
        printf("%f\n\r",actual_data.AngularSpeed_actual[i]);
    }
    pc.printf("mag values\n\r");
    for(int i=0; i<3; i++) 
    {
        pc.printf("%f\n\r",actual_data.Bvalue_actual[i]);
    }
      //  for(int i=0;i<3;i++)
//    {
//    omega1[i]= data[i];
//    b1[i] = data[i+3];
//    }
    }//if ACS_DATA_ACQ_ENABLE = 1
     else
    {
        // Z axis actuation is the only final solution,
    }
    if(ACS_STATE == 0)        // check ACS_STATE = ACS_CONTROL_OFF?
    {
          printf("\n\r acs control off\n");
          FLAG();
          ACS_STATUS = 0;                // set ACS_STATUS = ACS_CONTROL_OFF
          PWM1 = 0;                     //clear pwm pins
          PWM2 = 0;                     //clear pwm pins
          PWM3 = 0;                     //clear pwm pins
    }
    else
    {
            if(actual_data.power_mode>1)
            
            {
                if(ACS_STATE == 2)   // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY 
                {
                    FLAG();
                    printf("\n\r z axis moment only\n");
                    ACS_STATUS = 2;                    // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY   
                    //   FCTN_ACS_CNTRLALGO(b1, omega1);
                    moment[0] = 0;
                    moment[1] = 0;
                    moment[2] =1.3;// is a dummy value 
                    FCTN_ACS_GENPWM_MAIN(moment) ; 
                 }
                 else
                {
                if(ACS_STATE == 3) // check ACS_STATE = ACS_DATA_ACQ_FAILURE
                {
                     FLAG();
                     printf("\n\r acs data failure "); 
                     ACS_STATUS = 3;                    // set ACS_STATUS = ACS_DATA_ACQ_FAILURE
                        PWM1 = 0;                     //clear pwm pins
                        PWM2 = 0;                     //clear pwm pins
                        PWM3 = 0;                     //clear pwm pins
                 }
                 else
                 {
                     if(ACS_STATE == 4)       // check ACS_STATE = ACS_NOMINAL_ONLY
                        {
                            FLAG();
                            printf("\n\r nominal");
                            ACS_STATUS = 4;                    // set ACS_STATUS = ACS_NOMINAL_ONLY
                            FCTN_ACS_CNTRLALGO(actual_data.Bvalue_actual,actual_data.AngularSpeed_actual);
                            printf("\n\r moment values returned by control algo \n");
                            for(int i=0; i<3; i++) 
                            {
                                printf("%f\t",moment[i]);
                            }
                            FCTN_ACS_GENPWM_MAIN(moment) ;   
                        }
                        else
                        {
                            if(ACS_STATE == 5)       // check ACS_STATE = ACS_AUTO_CONTROL
                            {
                                FLAG();
                                printf("\n\r auto control");
                                ACS_STATUS = 5;                    // set ACS_STATUS = ACS_AUTO_CONTROL
                                //FCTN_ACS_AUTOCTRL_LOGIC                    // gotta include this code
                            }
                            else
                            {
                                if(ACS_STATE == 6)       // check ACS_STATE = ACS_DETUMBLING_ONLY
                                {
                                    FLAG();
                                    printf("\n\r Entered detumbling \n");
                                    ACS_STATUS = 6;                    // set ACS_STATUS = ACS_DETUMBLING_ONLY  
                                    FCTN_ACS_CNTRLALGO(actual_data.Bvalue_actual,actual_data.AngularSpeed_actual);  // detumbling code has to be included
                                    FCTN_ACS_GENPWM_MAIN(moment) ; 
                                }
                                else
                                {
                                    FLAG();
                                    printf("\n\r invalid state");
                                    ACS_STATUS = 7 ;                    // set ACS_STATUS = INVALID STATE 
                                    PWM1 = 0;                     //clear pwm pins
                                    PWM2 = 0;                     //clear pwm pins
                                    PWM3 = 0;                     //clear pwm pins
                                }//else of invalid 
                            }//else of autocontrol 
                        }//else of nominal
                 }//else of data acg failure
                
                }//else fo z axis moment only
            }//if power >2
            else
            {
                FLAG();
                printf("\n\r low power");
                ACS_STATUS = 1;                    // set ACS_STATUS = ACS_LOW_POWER
                PWM1 = 0;                     //clear pwm pins
                PWM2 = 0;                     //clear pwm pins
                PWM3 = 0;                     //clear pwm pins
            }
    } //else for acs control off
    ACS_MAIN_STATUS = 0; //clear ACS_MAIN_STATUS flag 
        
}
//***************************************************EPS THREAD***********************************************//

void F_EPS()
{
    
        pc.printf("\n\rEntered EPS   %f\n",t_start.read());
        EPS_MAIN_STATUS = 1; // Set EPS main status
        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 = 1;          //set EPS_BATTERY_TEMP_STATUS
          if(EPS_BATTERY_HEAT_ENABLE == 1)
          {
              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 == 0)
          {
                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
        {
          actual_data.power_mode = 1;
          EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS
          
        }
        else
        {
          FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);            //updating power level 
          EPS_BATTERY_GAUGE_STATUS = 1;           //set EPS_BATTERY_GAUGE_STATUS
        }
       // if( Temperature data received)
        //{
          
          
      //  }
//        else
//        {
//          Set battery temp to XX  
//          EPS_BATTERY_TEMP_STATUS = 0;          //clear EPS_BATTERY_TEMP_STATUS
//          EPS_STATUS = EPS_ERR_BATTERY_TEMP;
//        }
        FCTN_HK_MAIN();
       // printf("\n\r here");
        FCTN_APPEND_HKDATA();
        minMaxHkData();
        //printf("\n\r here");  
        EPS_MAIN_STATUS = 0; // clear EPS main status 

}

//**************************************************BCN THREAD*******************************************************************//

void F_BCN()
{
  
          pc.printf("\n\rEntered BCN   %f\n",t_start.read());
          
          FCTN_BCN_TX_MAIN();
    
}

//**************************************************TCTM THREAD*******************************************************************//
/*void I2C_busreset(void)
{
    uint8_t count=0;
    if((PORTE->PCR[1] & PORT_PCR_MUX(6)) && (PORTE->PCR[0] & PORT_PCR_MUX(6)))
    {
        printf("\n\rResetting\n\r");
        I2C1->C1 &= 0x7f;                 // Disable I2C1 bus
        PORTE->PCR[1] = PORT_PCR_MUX(1);  // PTE1 Alt1 (pin)
        PORTE->PCR[0] = PORT_PCR_MUX(1);  // PTE0 Alt1 (pin)
        while(((PTE->PDIR & 0x3) != 3) && (count<10))        // When PTE0 / PTE1 are not 1 : I2C1 bus lock-up
        {
            PTE->PDDR |= 0x3;             // Set PTE1 and PTE0 as a GPIO output so we can bit bang it
            PTE->PDOR |= 0x2;             // Set PTE1 (SCL) pin high;
            wait_ms(1);
            PTE->PDOR |= 0x1;             // Set PTE0 (SDA) pin high;
            wait_ms(1);
            count++;
            wait(1);
        }
        // Reinstate I2C1 bus pins
        PORTE->PCR[1] = PORT_PCR_MUX(6);  // PTE1 Alt6 (SCL)
        PORTE->PCR[0] = PORT_PCR_MUX(6);  // PTE0 Alt6 (SDA)
        I2C1->C1 |= 0x80;                 // Enable I2C1 bus  
        printf("Count: %d\n\r",count);      
        //wait(1);        
    }    
        
}*/
#define PORT_PCR_MUX_MASK2 0x00000700u
void debug()
{
   // uint32_t temp=0x0AF30000;
    //printf("\n\rTemp = %08X",temp);
    //pc.printf("\n\rPORTE->PCR[0] = 0x%08X",PORTE->PCR[0]);
    //pc.printf("\n\rPORTE->PCR[1] = 0x%08X",PORTE->PCR[1]);
    pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);           
}
void debug1()
{
    printf("\n\r Before disabling");
    debug();
    //wait_ms(50);
    I2C1->C1 &= 0x7f;
    printf("\n\r After muxing ");
    PORTE->PCR[1] = PORT_PCR_MUX(1); // PTE24 Alt1 (pin)
    PORTE->PCR[0] = PORT_PCR_MUX(1); // PTE25 Alt1 (pin)
    PTE->PDDR |= 0x3;             // Set PTE1 and PTE0 as a GPIO output so we can bit bang it            
    PTE->PDOR |= 0x2;             // Set PTE1 (SCL) pin high;
    wait_ms(1);
    PTE->PDOR |= 0x1;             // Set PTE0 (SDA) pin high;
    wait_ms(1);
    debug();            
    PORTE->PCR[1] &= (~(PORT_PCR_MUX_MASK2));
    PORTE->PCR[0] &= (~(PORT_PCR_MUX_MASK2));
    PORTE->PCR[1] |= PORT_PCR_MUX(6); // PTE24 Alt6 (pin)
    PORTE->PCR[0] |= PORT_PCR_MUX(6); // PTE25 Alt6 (pin)
    PORTE->PCR[1] |= 0x00000004;
    PORTE->PCR[0] |= 0x00000004;
    I2C1->C1 |= 0x80;                 // Enable I2C1 bus
    printf("\n\r After enabling I2C");
    debug(); 
}
void debug2()
{
    printf("\n\rI2C1->A1    = 0x%02X",I2C1->A1);
    printf("\n\rI2C1->F     = 0x%02X",I2C1->F);
    printf("\n\rI2C1->C1    = 0x%02X",I2C1->C1);
    printf("\n\rI2C1->S     = 0x%02X",I2C1->S);
    printf("\n\rI2C1->D     = 0x%02X",I2C1->D);
    printf("\n\rI2C1->C2    = 0x%02X",I2C1->C2);
    printf("\n\rI2C1->FLT   = 0x%02X",I2C1->FLT);
    printf("\n\rI2C1->RA    = 0x%02X",I2C1->RA);
    printf("\n\rI2C1->SMB   = 0x%02X",I2C1->SMB);
    printf("\n\rI2C1->A2    = 0x%02X",I2C1->A2);
    printf("\n\rI2C1->SLTH  = 0x%02X",I2C1->SLTH);
    printf("\n\rI2C1->SLTL  = 0x%02X\n",I2C1->SLTL);    
}

void debug3()
{
    //pc.printf("\n\r Resetting I2C");
    //debug2();
    //printf("\n\r SIM->SCGC4 = 0x%08X",SIM->SCGC4);
    PORTE->PCR[1] &= 0xfffffffb;
    PORTE->PCR[0] &= 0xfffffffb;
    I2C1->C1 &= 0x7f;
    //wait_ms(2);
    SIM->SCGC4 &= 0xffffff7f;
    //printf("\n\r SIM->SCGC4 = 0x%08X",SIM->SCGC4);
    //wait_ms(10);
    SIM->SCGC4 |= 0x00000080;
    //printf("\n\r SIM->SCGC4 = 0x%08X",SIM->SCGC4);
    //wait_ms(10);
    I2C1->C1 |= 0x80;  
    PORTE->PCR[1] |= 0x00000004;
    PORTE->PCR[0] |= 0x00000004;    
    //wait_ms(10);
    wait_ms(2);
    //printf("\n\r After enabling clock");
    //debug2();
    //printf("\n");
}
void I2C_busreset()
{
    //Thread::wait(1);
    PORTE->PCR[1] &= 0xfffffffb;
    //Thread::wait(1);
    PORTE->PCR[0] &= 0xfffffffb;
    //Thread::wait(1);
    I2C1->C1 &= 0x7f;
    //Thread::wait(1);
    SIM->SCGC4 &= 0xffffff7f;
    //Thread::wait(1);
    SIM->SCGC4 |= 0x00000080;
    //Thread::wait(1);
    I2C1->C1 |= 0x80;  
    //Thread::wait(1);
    PORTE->PCR[1] |= 0x00000004;
    //Thread::wait(1);
    PORTE->PCR[0] |= 0x00000004;    
    Thread::wait(1);
}
uint32_t pdirtc1,pdirtc2,pdirtm1,pdirtm2,pdirss1,pdirss2;
void T_TC(void const * args)
{
      while(1)
      {
        //pc.printf("\n\n\rWaiting");
        Thread::signal_wait(0x4);
        wait_us(300);                                                // can be between 38 to 15700
        if( slave.receive() == 0)        
        {
            //synch.stop();
            //pdirss1=PTE->PDIR;
            irpt_2_mstr = 0;
            data_send_flag = 'h';            
            #if PRINT
            pc.printf("\n\rSlave stop");           
            pc.printf("\n\rPTE->DIR = 0x%08X",pdirss1);
            #endif
            slave.stop();            
            I2C_busreset();
            pdirss2=PTE->PDIR;            
            #if PRINT
            pc.printf("\n\rPTE->DIR = 0x%08X",pdirss2);
            #endif
            //pc.printf("\n\rPTE->DIR = 0x%08X",pdir2);            
        }     
        else if( slave.receive() == 1)                              // slave writes to master
        {
             if(data_send_flag == 'h')
             {
                //FCTN_APPEND_HKDATA();
                // pc.printf("\n\r here");
                write_ack=slave.write(BAE_chardata,74);                
                irpt_2_mstr = 0;
                if(write_ack==0)                
                {
                    irpt_2_mstr = 0;
                    pc.printf("\n\rgot interrupt\n");
                }
            }
            else if(data_send_flag == 't')
            {
                uint8_t i2c_count =0;
                write_ack=slave.write((char*)telemetry,135);                
                Thread::wait(1);
                pdirtm1=PTE->PDIR;
                irpt_2_mstr = 0;
                //data_send_flag = 'h';               
                if(write_ack==0)
                {
                    while(((pdirtm1 & 0x00000003)!=3)&& i2c_count<10)
                    {
                        Thread::wait(1);
                        pdirtm1=PTE->PDIR;
                        i2c_count++;
                    }                    
                    if(((pdirtm1 & 0x00000003)==3))
                    {                  
                        pc.printf("\n\rWrite TM success");
                        //pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                    }
                    else
                    {
                        //Resetting should be done here                        
                        
                        #if PRINT
                        pc.printf("\n\rWrite TM error");                                    
                        pc.printf("\n\rPTE->DIR = 0x%08X",pdirtm1);
                        #endif
                        I2C_busreset();
                        pdirtm2=PTE->PDIR;                        
                        #if PRINT
                        pc.printf("\n\rPTE->DIR = 0x%08X",pdirtm2);
                        #endif
                    }                    
                    //pc.printf("\n\rTM count:%d",i2c_count);
                }
                else
                {                    
                    //Thread::wait(30);
                    #if PRINT
                    pc.printf("\nTM ack failed");
                    pc.printf("\n\rPTE->DIR = 0x%08X",pdirtm1);
                    #endif
                    I2C_busreset();       
                    pdirtm2=PTE->PDIR;
                    #if PRINT                          
                    pc.printf("\n\rPTE->DIR = 0x%08X",pdirtm2);
                    #endif
                    //Thread::wait(10);                   
                    //pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                    //debug3();   
                    //wait_ms(10);                 
                    //pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                }
                i2c_count=0;
            }
            else if(data_send_flag == 'i')
            {
                //to be filled
                data_send_flag = 'h';                
            }
        }
        else if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
        {
            uint8_t i2c_count = 0;            
            synch.stop();            
            read_ack=slave.read((char *)telecommand,11);            
            Thread::wait(1);
            pdirtc1=PTE->PDIR;
            irpt_2_mstr = 1;  
            data_send_flag = 't'; 
            if(read_ack==0)
            {
                while(((pdirtc1 & 0x00000003)!=3)&& i2c_count<10)
                {
                    Thread::wait(1);
                    pdirtc1=PTE->PDIR;
                    i2c_count++;
                }                    
                if(((pdirtc1 & 0x00000003)==3))
                {
                    pc.printf("\n\n\rRead TC success");
                    
                    // FCTN_TC_DECODE((uint8_t*) telecommand);                   
                    //FCTN_BAE_TM_TC((uint8_t*) telecommand);
                    //telemetry = (char*)temp;                  
                    //FCTN_TM();
                    
                    //uint8_t read_ack2 = (uint8_t)(PTE->PDIR & 0x00000003);
                    //printf("\n\rread_ack2 = %d",read_ack2);
                    //if((read_ack ==0)&&(read_ack2==3))           
                    //if(read_ack==0 && ((PTE->PDIR & 0x00000003)==3))
                    //debug2();
                    //pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                    //wait_us(10);
                    //read_ack2 = (uint8_t)(PTE->PDIR & 0x00000003);
                    //tempt.stop();               
                    //data_send_flag = 't';                                 //uncomment later
               }
               else
               {                   
                    #if PRINT
                    pc.printf("\n\n\rRead TC error");
                    pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc1);
                    #endif
                    I2C_busreset();
                    pdirtc2=PTE->PDIR;                    
                    #if PRINT
                    pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc2);
                    #endif
                   //pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
                   //debug3();
                   //printf("\n\rread_ack2 = %d and time = %d",read_ack2,tempt.read_us());
                   //data_send_flag = 'i';
                }
                //pc.printf("\n\rTC count:%d",i2c_count);
            }
            else
            {
                #if PRINT
                pc.printf("\n\n\rTC ack failed");
                pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc1);
                #endif
                I2C_busreset();
                pdirtc2=PTE->PDIR;                
                #if PRINT
                pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc2);
                #endif
                //pc.printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
            }           
            i2c_count = 0;
            //printf("\n\rSize of data : %d",sizeof(telecommand));
            //printf("\n\rGiven Size : %d\n",11);
            //printf("\n\rREAD_ACK : %d",read_ack);
            //t_tc.start();
            //pc.printf("\n\rTELECOMMAND received from CDMS is %s \n",telecommand);
            //pc.printf("\n\r Executing Telecommand \n"); 
            
            //t_tc.stop();
            //printf("\n\r time taken %d",t_tc.read_us());
            //t_tc.reset();
            // for(int i = 0; i<134; i++)
            //pc.printf("%c", telemetry[i]);
        }
        //pc.printf("Time after slave.read() = %d",synch.read_us());
        //synch.reset();
    }
}
void FCTN_TM()
{
    //irpt_2_mstr = 0; 
    data_send_flag = 't';
    pc.printf("\n\r Telemetry Generation \n");
    irpt_2_mstr = 1;   
}


//******************************************************* I2C *******************************************************************//

void FCTN_I2C_ISR()
{
     //synch.start();
     ptr_t_i2c->signal_set(0x4);
}


//***********************************************************FAULTS***************************************************************//
/*void ir1clear()
{
    actual_data.faultIr_status |= 0x01;   // alert
 
}*/


 
void ir2clear()
{
    actual_data.faultIr_status |= 0x02;
    TRXY_SW = 0;   // Switch off TR XY
    if1check = 1;
}
 
void ir3clear()
{
    actual_data.faultIr_status |= 0x04;
    DRV_Z_EN = 0;
    wait_us(1);
    DRV_Z_EN = 1;
 
}
 
void ir4clear()
{
    if2check = 1;
    actual_data.faultIr_status |= 0x08;
    TRZ_SW = 0;
}
 
void ir5clear()
{
    actual_data.faultIr_status |= 0x10;
    CDMS_RESET = 0;
    wait_us(1);
    CDMS_RESET = 1;
}
 
void ir6clear()
{
    actual_data.faultIr_status |= 0x20;
    BCN_SW = 0;
    wait_us(1);
    BCN_SW = 1;
}
 
void ir7clear()
{
    actual_data.faultIr_status |= 0x40;
}
uint8_t iter2=0,iter4 = 0; 



void pollfault()
{   
    
    
 
    if (pf1==0)                // OC_ATS1
    { 
        pf1check=1;
        actual_data.faultPoll_status |=0x01 ;
        ATS1_SW_ENABLE = 1;  // turn off ats1  // to be turned on next cycle in ACS
    }
    else actual_data.faultPoll_status &= 0xFE;
 
    if (pf2==0)
    {   
        pf2check=1;
        actual_data.faultPoll_status |=0x02 ;
        ATS2_SW_ENABLE = 1;  // turn off ats2  // turn on in ACS
    }
    else actual_data.faultPoll_status &= 0xFD;
 
    if (pf3==0)
    {   actual_data.faultPoll_status |=0x04 ;
        DRV_XY_EN = 0;
        wait_us(1);
        DRV_XY_EN = 1;
    }
    else actual_data.faultPoll_status &= 0xFB;
 
 
 
 
    /*if (ir1==1)
    {
        actual_data.faultIr_status &=0xFE;
    }*/
   
    if (ir2==1)
    {
        actual_data.faultIr_status &=0xFD;
    }
    if (ir3==1)
    {
        actual_data.faultIr_status &=0xFB;
    }
    if (ir4==1)
    {
        actual_data.faultIr_status &=0xF7;
    }
    if (ir5==1)
    {
        actual_data.faultIr_status &=0xEF;
    }
    if (ir6==1)
    {
        actual_data.faultIr_status &=0xDF;
    }if (ir7==1)
    {
        actual_data.faultIr_status &=0xBF;
    }
    
 
}
  

//------------------------------------------------------------------------------------------------------------------------------------------------
//SCHEDULER
//------------------------------------------------------------------------------------------------------------------------------------------------
uint8_t schedcount=1;
void T_SC(void const *args)
{    
    //printf("\n\r in scheduler");
   
    if(schedcount == 7)                         //to reset the counter
    {
        schedcount = 1;
    }
    if(schedcount%1==0)
    { //pc.printf("\nSTATE IS !!!!!! = %x !!\n",ACS_STATE);
      //pc.printf("\niterp1 !!!!!! = %x !!\n",iterP1);
      //pc.printf("\niteri2 IS !!!!!! = %x !!\n",iterI2);
      //F_ACS();
    }
    
    if(schedcount%2==0)
    {
        //F_EPS();
    }
    if(schedcount%1==0)
    { 
       //F_BCN();
    }
    schedcount++;
    //printf("\n\r BAE is alive\n");
}

Timer t_flag;
void FLAG()
{
    
//.............acs..................//    
    if(ACS_INIT_STATUS == 1)
        BAE_STATUS = BAE_STATUS | 0x00000080;  //set ACS_INIT_STATUS flag
    else if(ACS_INIT_STATUS == 0)
        BAE_STATUS &= 0xFFFFFF7F;              //clear ACS_INIT_STATUS flag 
    
    if(ACS_DATA_ACQ_STATUS == 1)
        BAE_STATUS =BAE_STATUS | 0x00000100;     //set ACS_DATA_ACQ_STATUS flag
    else if(ACS_DATA_ACQ_STATUS == 0)
        BAE_STATUS &= 0xFFFFFEFF;      //clear ACS_DATA_ACQ_STATUS flag    
    
    if(ACS_ATS_ENABLE == 1)
        BAE_ENABLE |= 0x00000004;
    else if(ACS_ATS_ENABLE == 0)
        BAE_ENABLE = BAE_ENABLE &0xFFFFFFFB | 0x00000004;
    
    if(ACS_DATA_ACQ_STATUS == 'f')
        BAE_STATUS |= 0x00000200;
    
    if(ACS_MAIN_STATUS == 1)
        BAE_STATUS = (BAE_STATUS | 0x00001000);     //set ACS_MAIN_STATUS flag
   else if(ACS_MAIN_STATUS == 0)
        BAE_STATUS &= 0xFFFFEFFF;     //clear ACS_MAIN_STATUS flag 
    
    if(ACS_STATUS == '0')
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF);                // set ACS_STATUS = ACS_CONTROL_OFF
    else if(ACS_STATUS == '1')
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x00002000;                    // set ACS_STATUS = ACS_LOW_POWER
    else if(ACS_STATUS == '2')
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF)| 0x00004000;                    // set ACS_STATUS = ACS_ZAXIS_MOMENT_ONLY  
    else if(ACS_STATUS == '3') 
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00006000;                    // set ACS_STATUS = ACS_DATA_ACQ_FAILURE
    else if(ACS_STATUS == '4')
        BAE_STATUS = (BAE_STATUS & 0xFFFF1FFF) | 0x00008000;                    // set ACS_STATUS = ACS_NOMINAL_ONLY
    else if(ACS_STATUS == '5')
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000A000;                    // set ACS_STATUS = ACS_AUTO_CONTROL
    else if(ACS_STATUS == '6')
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000C000;                    // set ACS_STATUS = ACS_DETUMBLING_ONLY  
    else 
        BAE_STATUS =(BAE_STATUS & 0xFFFF1FFF) | 0x0000E000;                    // set ACS_STATUS = INVALID STATE 
        
    if(ACS_STATE == '0')
        BAE_ENABLE = (BAE_ENABLE & 0xFFFFFF8F);                                         //ACS_STATE = ACS_CONTROL_OFF
    else if(ACS_STATE == '2')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000020);                              //   ACS_STATE = ACS_ZAXIS_MOMENT_ONLY  
    else if(ACS_STATE == '3')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000030);                              //  set ACS_STATUS = ACS_DATA_ACQ_FAILURE
    else if(ACS_STATE == '4')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000040);                              //  ACS_STATE = ACS_NOMINAL_ONLY
    else if(ACS_STATE == '5')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000050);                              //    ACS_STATE = ACS_AUTO_CONTROL
    else if(ACS_STATE == '6')
        BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000060);                             //ACS_STATE = ACS_DETUMBLING_CONTROL
        
//...............eps......................//

    
if (EPS_INIT_STATUS==1)                                  // Set EPS_INIT_STATUS
    BAE_STATUS |= 0x00010000;                     
else if(EPS_INIT_STATUS==0)                              // Clear
    BAE_STATUS &= 0xFFFEFFFF;


if (EPS_MAIN_STATUS==1)                              // Set EPS_MAIIN_STATUS
    BAE_STATUS |= 0x00040000;
else if(EPS_MAIN_STATUS==0)                          // Clear
    BAE_STATUS &= 0xFFFBFFFF;


if (EPS_BATTERY_GAUGE_STATUS==1)              // Set EPS_BATTERY_GAUGE_STATUS
    BAE_STATUS |= 0x00020000;
else if(EPS_BATTERY_GAUGE_STATUS==0)          // Clear
    BAE_STATUS &= 0xFFFDFFFF;


if (EPS_BATTERY_TEMP_STATUS==1)             // Set EPS_BATTERY_TEMP_STATUS
    BAE_STATUS |= 0x00080000;
else if(EPS_BATTERY_TEMP_STATUS==0)       // Clear
    BAE_STATUS &= 0xFFF7FFFF;

if (EPS_STATUS==0)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF);                             // Set EPS_ERR_BATTERY_TEMP
else if (EPS_STATUS==1)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00010000;           // Set EPS_BATTERY_HEATER_DISABLED
else if (EPS_STATUS==2)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00020000;           // Set EPS_ERR_HEATER_SWITCH_OFF
else if (EPS_STATUS==3)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00030000;          // Set EPS_ERR_HEATER_SWITCH_ON
else if (EPS_STATUS==4)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00040000;          // Set EPS_BATTERY_HEATER_OFF
else if (EPS_STATUS==5)
    BAE_STATUS = (BAE_STATUS & 0xFF8FFFFF)|0x00050000;          // Set EPS_BATTERY_HEATER_ON   
    

    if(EPS_BATTERY_HEAT_ENABLE == 1)
        BAE_ENABLE |= 0x00000080;
    else if(EPS_BATTERY_HEAT_ENABLE == 0)
        BAE_ENABLE = BAE_ENABLE &0xFFFFFF7;    
    

   
    pc.printf("\n\r BAE status %x BAE ENABLE %x ",BAE_STATUS,BAE_ENABLE);
}

void FCTN_BAE_INIT()
{
    printf("\n\r Initialising BAE ");
    //..........intial status....//
    ACS_STATE = 4;
    ACS_ATS_ENABLE = 1;
    ACS_DATA_ACQ_ENABLE = 1;
    
    
    EPS_BATTERY_HEAT_ENABLE = 1;
    actual_data.power_mode=3;
    //............intializing pins................//
    
    ATS2_SW_ENABLE = 1;
    ATS1_SW_ENABLE = 1;
    wait_ms(5);
    ATS1_SW_ENABLE = 0;
    
    ACS_ATS_STATUS = 0x60;  //Set Sensor 1 working , Sensor2 working and powered off by default

    DRV_XY_EN = 1;
    DRV_Z_EN = 1;
    TRZ_SW = 1;
    TRXY_SW = 1;
    
    //............................//
    //FCTN_ACS_INIT();
    //FCTN_EPS_INIT();
    //FCTN_BCN_INIT();

    
    FLAG();
}

int main()
{
    pc.baud(9600);
    pc.printf("\n\r BAE Activated. Testing Version 1.1 \n");
    //CDMS_RESET = 1;  
    /*if (BCN_FEN == 0)                       //dummy implementation
    {
        pc.printf("\n\r RF silence ");
        FCTN_BCN_FEN();
        t_rfsilence.start();//Start the timer for RF_Silence
        while(t_rfsilence.read() < RF_SILENCE_TIME); 
    }               
    */
    
    //ACS_INIT_STATUS = 0;
    //ACS_DATA_ACQ_STATUS = 0;
        

    
    //FLAG();
    FCTN_BAE_INIT();
    //pc.printf("\n\rPORTE->PCR[0] = 0x%08X",PORTE->PCR[0]);
    //pc.printf("\n\rPORTE->PCR[1] = 0x%08X",PORTE->PCR[1]);
    
    
    //...i2c..
    //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);
        
    irpt_4m_mstr.enable_irq();
    irpt_4m_mstr.rise(&FCTN_I2C_ISR);
   // ir1.fall(&ir1clear);   //Battery Gauge - Alert Bar Signal
 /*   ir2.fall(&ir2clear);   //TRXY Driver TR switch Fault
    ir3.fall(&ir3clear);   //TRZ Driver Fault Bar
    ir4.fall(&ir4clear);   //TRZ Driver TR switch Fault
    ir5.fall(&ir5clear);   //CDMS - Switch Fault
    ir6.fall(&ir6clear);   //Beacon- Switch OC bar
    ir7.fall(&ir7clear);   //Charger IC - Fault Bar
    
    */
    RtosTimer t_sc_timer(T_SC,osTimerPeriodic);               // Initiating the scheduler thread
    t_sc_timer.start(10000);
    t_start.start();
    pc.printf("\n\rStarted scheduler %f\n\r",t_start.read()); 
    printf("\n\rPTE->DIR = 0x%08X",PTE->PDIR);
    //printf("\n\rInitial values\n");
    //debug();
    //debug2();
    //FCTN_BAE_INIT();
    while(1);                                                   //required to prevent main from terminating
}