I2C BAE standalone hardware testing

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_Flowchart_BAE_1 by Team Fox

Revision:
5:bb592f3185cc
Parent:
4:39a4ae8c7ecd
Child:
6:036d08b62785
--- a/main.cpp	Wed Dec 30 04:57:46 2015 +0000
+++ b/main.cpp	Thu Dec 31 17:12:52 2015 +0000
@@ -8,12 +8,14 @@
 
 #define tm_len 134
 #define tc_len 135
-#define bae_data_len 150
 #define batt_heat_low 20
 //***************************************************** 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';
@@ -37,7 +39,7 @@
 char EPS_BATTERY_HEAT_ENABLE = 'q';
 
 //.......................global variables..................................................................// new hk structure- everything has to changed based on this
-char BAE_data[bae_data_len];     
+uint8_t BAE_data[73];     
     
 
 //*************************************Global declarations************************************************//
@@ -56,11 +58,15 @@
 extern BAE_HK_min_max bae_HK_minmax;
 extern BAE_HK_arch arch_data;
 
-bool write_ack = 1;
-bool read_ack = 1;
+int write_ack = 1;
+int read_ack = 1;
 char telecommand[tc_len];
 char* telemetry;
-char data_send_flag = 'h'; 
+
+bool pf1check = 0;
+bool pf2check = 0;
+bool if1check = 0;
+bool if2check = 1;
 
 //*****************************************************Assigning pins******************************************************//
 DigitalOut gpo1(PTC0); // enable of att sens2 switch
@@ -75,6 +81,30 @@
 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(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);
+
+
+
 
 /*****************************************************************Threads USed***********************************************************************************/
 
@@ -89,9 +119,67 @@
 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)
+        {
+            gpo1 = 1;  // turn off ats1 permanently
+            //FCTN_SWITCH_ATS(0);  // switch on ATS2    
+        }
+        else    
+        {
+        gpo1 = 0;
+        iterP1++;
+        }
+    }
+    if(pf2check == 1)
+    {
+        if(iterP1 >= 3)
+        {
+            gpo2 = 1;  // turn off ats2 permanently
+            ACS_DATA_ACQ_ENABLE == 'd';
+            ACS_STATE == '2' ; // check ACS_STATE = ACS_ZAXIS_MOMENT_ONLY    
+        }
+        else    
+        {
+        gpo2 = 0;
+        iterP2++;
+        }
+    }
+     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};
@@ -120,13 +208,12 @@
     for(int i=3; i<6; i++) 
     {
         pc.printf("%f\n\r",data[i]);
-        
+    }
         for(int i=0;i<3;i++)
     {
     omega1[i]= data[i];
     b1[i] = data[i+3];
     }
-    }
     }//if ACS_DATA_ACQ_ENABLE = 1
      else
     {
@@ -245,7 +332,7 @@
           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
+              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
               }
@@ -283,8 +370,8 @@
 //          EPS_STATUS = EPS_ERR_BATTERY_TEMP;
 //        }
         FCTN_HK_MAIN();
-        //FCTN_APPEND_HKDATA();
-        //minMaxHkData();
+        FCTN_APPEND_HKDATA();
+        minMaxHkData();
           
         EPS_MAIN_STATUS = 'c'; // clear EPS main status 
 
@@ -313,18 +400,24 @@
             slave.stop();     
         else if( slave.receive() == 1)                                     // slave writes to master
         {
-            write_ack=slave.write((char*)telemetry,tm_len);
-             irpt_2_mstr = 0;
+             if(data_send_flag == 'h')
+                write_ack=slave.write((char*)BAE_data,73); 
+            else if(data_send_flag == 't')
+            {
+                write_ack=slave.write(telemetry,tm_len);
+                data_send_flag = 'h';
+                irpt_2_mstr = 0;
+            }       
         }
         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"); 
+           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);
            uint8_t* temp = FCTN_BAE_TM_TC((uint8_t*) telecommand);
            telemetry = (char*)temp;
-            FCTN_TM();
+           FCTN_TM();
             // for(int i = 0; i<134; i++)
             //pc.printf("%c", telemetry[i]);
         } 
@@ -335,6 +428,7 @@
 void FCTN_TM()
 {
     //irpt_2_mstr = 0; 
+    data_send_flag = 't';
     pc.printf("\n\r Telemetry Generation \n");
     irpt_2_mstr = 1;   
 }
@@ -348,6 +442,125 @@
 }
 
 
+//***********************************************************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_SLP = 0;
+    wait_us(1);
+    DRV_Z_SLP = 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 ;
+        gpo1 = 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 ;
+        gpo2 = 1;  // turn off ats2  // turn on in ACS
+    }
+    else actual_data.faultPoll_status &= 0xFD;
+ 
+    if (pf3==0)
+    {   actual_data.faultPoll_status |=0x04 ;
+        DRV_XY_SLP = 0;
+        wait_us(1);
+        DRV_XY_SLP = 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
 //------------------------------------------------------------------------------------------------------------------------------------------------
@@ -436,15 +649,49 @@
         BAE_ENABLE = ((BAE_ENABLE & 0xFFFFFF8F)| 0x00000060);                             //ACS_STATE = ACS_DETUMBLING_CONTROL
         
 //...............eps......................//
-    if(EPS_INIT_STATUS == 's')
-        BAE_STATUS |= 0x00010000;               //set EPS_INIT_STATUS flag
-    else if(EPS_INIT_STATUS == 'c')
-        BAE_STATUS &= 0xFFFEFFFF;               //clear EPS_INIT_STATUS flag
-    if(EPS_BATTERY_GAUGE_STATUS == 'c')
-        BAE_STATUS &= 0xFFFDFFFF;               //clear EPS_BATTERY_GAUGE_STATUS
-    else if(EPS_BATTERY_GAUGE_STATUS == 's')
-        BAE_STATUS |= 0x00020000;               //set EPS_BATTERY_GAUGE_STATUS
-        
+
+    
+if (EPS_INIT_STATUS=='s')                                  // Set EPS_INIT_STATUS
+    BAE_STATUS |= 0x00010000;                     
+else if(EPS_INIT_STATUS=='c')                              // Clear
+    BAE_STATUS &= 0xFFFEFFFF;
+
+
+if (EPS_MAIN_STATUS=='s')                              // Set EPS_MAIIN_STATUS
+    BAE_STATUS |= 0x00040000;
+else if(EPS_MAIN_STATUS=='c')                          // Clear
+    BAE_STATUS &= 0xFFFBFFFF;
+
+
+if (EPS_BATTERY_GAUGE_STATUS=='s')              // Set EPS_BATTERY_GAUGE_STATUS
+    BAE_STATUS |= 0x00020000;
+else if(EPS_BATTERY_GAUGE_STATUS=='c')          // Clear
+    BAE_STATUS &= 0xFFFDFFFF;
+
+
+if (EPS_BATTERY_TEMP_STATUS=='s')             // Set EPS_BATTERY_TEMP_STATUS
+    BAE_STATUS |= 0x00080000;
+else if(EPS_BATTERY_TEMP_STATUS=='c')       // 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 == 'e')
+        BAE_ENABLE |= 0x00000080;
+    else if(EPS_BATTERY_HEAT_ENABLE == 'd')
+        BAE_ENABLE = BAE_ENABLE &0xFFFFFF7;    
     
 
    
@@ -495,9 +742,16 @@
     
     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();