I2C BAE standalone hardware testing

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_Flowchart_BAE_1 by Team Fox

Revision:
17:8a8024c45dc0
Parent:
16:cc77770d787f
Child:
18:95f0cc565ee3
--- a/main.cpp	Fri Jun 03 13:53:55 2016 +0000
+++ b/main.cpp	Fri Jun 24 08:08:52 2016 +0000
@@ -9,6 +9,7 @@
 #define tm_len 135
 #define tc_len 11
 #define batt_heat_low 20
+#define PRINT 1
 
 //***************************************************** flags *************************************************************//
 uint32_t BAE_STATUS = 0x00000000;
@@ -71,6 +72,7 @@
 Timer t_start;
 Timer t_tc;
 Timer t_tm;
+Timer synch;
 Serial pc(USBTX, USBRX);
 int power_flag_dummy=2;
 float data[6];
@@ -84,7 +86,7 @@
 
 int write_ack = 1;
 int read_ack = 1;
-char telecommand[tc_len];
+uint8_t telecommand[11];
 extern uint8_t telemetry[135];
 
 bool pf1check = 0;
@@ -133,7 +135,7 @@
 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); // CDMS RESET
+DigitalOut CDMS_RESET(PIN7,1); // CDMS RESET
 DigitalOut BCN_SW(PIN14);      //Beacon switch
 DigitalOut DRV_XY_EN(PIN82);
 
@@ -424,61 +426,317 @@
 }
 
 //**************************************************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(200);  
-        //printf("\n\rreached\n");                                             // can be between 38 to 15700
+        wait_us(300);                                                // can be between 38 to 15700
         if( slave.receive() == 0)        
-            {slave.stop();
-            //printf("\n\rnot send\n");
-            }     
-        else if( slave.receive() == 1)                                     // slave writes to master
+        {
+            //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); 
+                write_ack=slave.write(BAE_chardata,74);                
+                irpt_2_mstr = 0;
                 if(write_ack==0)                
-                    {irpt_2_mstr = 0;
-                    printf("\n\rgot interrupt\n");
-                    }
+                {
+                    irpt_2_mstr = 0;
+                    pc.printf("\n\rgot interrupt\n");
+                }
             }
             else if(data_send_flag == 't')
             {
-                write_ack=slave.write((char*)telemetry,tm_len);
-                data_send_flag = 'h';
-                if(write_ack==0)                
-                    irpt_2_mstr = 0;
-            }       
+                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 
         {
-           read_ack=slave.read(telecommand,tc_len);
-           t_tc.start();
-           //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_BAE_TM_TC((uint8_t*) telecommand);
-           //telemetry = (char*)temp;
-          
-           FCTN_TM();
-            t_tc.stop();
-           printf("\n\r time taken %d",t_tc.read_us());
-            t_tc.reset();
+            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; 
@@ -492,6 +750,7 @@
 
 void FCTN_I2C_ISR()
 {
+     //synch.start();
      ptr_t_i2c->signal_set(0x4);
 }
 
@@ -623,29 +882,29 @@
 uint8_t schedcount=1;
 void T_SC(void const *args)
 {    
-    printf("\n\r in scheduler");
+    //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();
+    { //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();
+        //F_EPS();
     }
     if(schedcount%1==0)
     { 
-       F_BCN();
+       //F_BCN();
     }
     schedcount++;
-    printf("\n\r exited scheduler");
+    //printf("\n\r BAE is alive\n");
 }
 
 Timer t_flag;
@@ -782,8 +1041,8 @@
     TRXY_SW = 1;
     
     //............................//
-    FCTN_ACS_INIT();
-   // FCTN_EPS_INIT();
+    //FCTN_ACS_INIT();
+    //FCTN_EPS_INIT();
     //FCTN_BCN_INIT();
 
     
@@ -792,8 +1051,9 @@
 
 int main()
 {
+    pc.baud(9600);
     pc.printf("\n\r BAE Activated. Testing Version 1.1 \n");
-    CDMS_RESET = 1;  
+    //CDMS_RESET = 1;  
     /*if (BCN_FEN == 0)                       //dummy implementation
     {
         pc.printf("\n\r RF silence ");
@@ -810,6 +1070,8 @@
     
     //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..
@@ -835,8 +1097,10 @@
     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
 }