I2C BAE standalone hardware testing

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of ACS_Flowchart_BAE_1 by Team Fox

Revision:
18:95f0cc565ee3
Parent:
17:8a8024c45dc0
Child:
19:95ba0ed3370c
--- a/main.cpp	Fri Jun 24 08:08:52 2016 +0000
+++ b/main.cpp	Mon Jul 04 17:46:48 2016 +0000
@@ -6,10 +6,22 @@
 #include "BCN.h"
 #include "TCTM.h"
 
+//#include "crc.h"
+//namespace CRC{
+//    };
+#define TOPBIT16 (1 << 15)
+#define TOPBIT8 (1 << 7)
+#define POLYNOMIAL16 0x1021
+#define POLYNOMIAL8 0xEA
+
 #define tm_len 135
 #define tc_len 11
 #define batt_heat_low 20
+
+//temporary definitions
 #define PRINT 1
+#define PRINT2 1
+//DigitalOut temp_irpt(PIN88);
 
 //***************************************************** flags *************************************************************//
 uint32_t BAE_STATUS = 0x00000000;
@@ -62,8 +74,7 @@
 
 //.......................global variables..................................................................// new hk structure- everything has to changed based on this
 uint8_t BAE_data[74];  
-char BAE_chardata[74];     
-    
+uint8_t BAE_chardata[74];     
 
 //*************************************Global declarations************************************************//
 const int addr = 0x20;                                            //slave address 
@@ -86,7 +97,7 @@
 
 int write_ack = 1;
 int read_ack = 1;
-uint8_t telecommand[11];
+uint8_t telecommand[930];
 extern uint8_t telemetry[135];
 
 bool pf1check = 0;
@@ -151,6 +162,42 @@
 void F_EPS();
 void F_BCN();
 
+//delete
+uint16_t crc16_gen2(const unsigned char message[], unsigned int nBytes){
+        uint16_t remainder = 0xffff;
+        int byte;
+        char bit;
+        
+        for( byte = 0 ; byte < nBytes ; byte++ ){
+            /*
+            Bring the data byte by byte
+            each time only one byte is brought
+            0 xor x = x
+            */
+            remainder = remainder ^ ( message[byte] << 8 );
+            
+            for( bit = 8 ; bit > 0 ; bit--){
+                /*
+                for each bit, xor the remainder with polynomial
+                if the MSB is 1
+                */
+                if(remainder & TOPBIT16){
+                    remainder = (remainder << 1) ^ POLYNOMIAL16;
+                    /*
+                    each time the remainder is xor-ed with polynomial, the MSB is made zero
+                    hence the first digit of the remainder is ignored in the loop
+                    */
+                }
+                else{
+                    remainder = (remainder << 1);
+                }
+            }
+        }
+        
+        return remainder;
+    }
+    
+
 //*******************************************ACS THREAD**************************************************//
 uint8_t iterP1;
 uint8_t iterP2;
@@ -411,7 +458,6 @@
         minMaxHkData();
         //printf("\n\r here");  
         EPS_MAIN_STATUS = 0; // clear EPS main status 
-
 }
 
 //**************************************************BCN THREAD*******************************************************************//
@@ -426,7 +472,7 @@
 }
 
 //**************************************************TCTM THREAD*******************************************************************//
-/*void I2C_busreset(void)
+/*void I2C_busreset_test(void)
 {
     uint8_t count=0;
     if((PORTE->PCR[1] & PORT_PCR_MUX(6)) && (PORTE->PCR[0] & PORT_PCR_MUX(6)))
@@ -490,18 +536,18 @@
 }
 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);    
+    pc.printf("\n\rI2C1->A1    = 0x%02X",I2C1->A1);
+    pc.printf("\n\rI2C1->F     = 0x%02X",I2C1->F);
+    pc.printf("\n\rI2C1->C1    = 0x%02X",I2C1->C1);
+    pc.printf("\n\rI2C1->S     = 0x%02X",I2C1->S);
+    pc.printf("\n\rI2C1->D     = 0x%02X",I2C1->D);
+    pc.printf("\n\rI2C1->C2    = 0x%02X",I2C1->C2);
+    pc.printf("\n\rI2C1->FLT   = 0x%02X",I2C1->FLT);
+    pc.printf("\n\rI2C1->RA    = 0x%02X",I2C1->RA);
+    pc.printf("\n\rI2C1->SMB   = 0x%02X",I2C1->SMB);
+    pc.printf("\n\rI2C1->A2    = 0x%02X",I2C1->A2);
+    pc.printf("\n\rI2C1->SLTH  = 0x%02X",I2C1->SLTH);
+    pc.printf("\n\rI2C1->SLTL  = 0x%02X\n",I2C1->SLTL);    
 }
 
 void debug3()
@@ -530,152 +576,142 @@
 }
 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;
+uint32_t pdir_tc1,pdir_tc2,pdir_tm1,pdir_tm2,pdir_ss1,pdir_ss2;//variables used to verify i2c working
+uint16_t crc16_check;   
 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;
+        Thread::signal_wait(0x4);   //signalled by ISR when CDMS sends an interrupt
+        wait_us(300);               // can be between 38 to 15700 but currently fixed as 300us
+        if( slave.receive() == 0)   //Slave not addressed
+        {           
             irpt_2_mstr = 0;
             data_send_flag = 'h';            
             #if PRINT
-            pc.printf("\n\rSlave stop");           
-            pc.printf("\n\rPTE->DIR = 0x%08X",pdirss1);
+            pc.printf("\n\rSlave not addressed");
+            pc.printf("\n\rPTE->DIR = 0x%08X",pdir_ss1);
             #endif
             slave.stop();            
-            I2C_busreset();
-            pdirss2=PTE->PDIR;            
-            #if PRINT
-            pc.printf("\n\rPTE->DIR = 0x%08X",pdirss2);
+            pdir_ss1=PTE->PDIR;
+            if(((pdir_ss1 & 0x00000003)!=3))
+            {                  
+                I2C_busreset();
+            }
+            pdir_ss2=PTE->PDIR;            
+            #if PRINT2
+            pc.printf("\n\rPTE->DIR = 0x%08X",pdir_ss2);
             #endif
-            //pc.printf("\n\rPTE->DIR = 0x%08X",pdir2);            
         }     
         else if( slave.receive() == 1)                              // slave writes to master
         {
-             if(data_send_flag == 'h')
+             if(data_send_flag == 'h')      //Send Housekeeping data
              {
                 //FCTN_APPEND_HKDATA();
-                // pc.printf("\n\r here");
-                write_ack=slave.write(BAE_chardata,74);                
+                crc16_check = crc16_gen2(BAE_chardata,72);
+                BAE_chardata[72] = (uint8_t)((crc16_check&0xFF00)>>8);
+                BAE_chardata[73] = (uint8_t)(crc16_check&0x00FF);                
+                write_ack=slave.write((char*)BAE_chardata,74);
                 irpt_2_mstr = 0;
                 if(write_ack==0)                
                 {
                     irpt_2_mstr = 0;
-                    pc.printf("\n\rgot interrupt\n");
+                    pc.printf("\n\rHK data sent\n");
                 }
             }
-            else if(data_send_flag == 't')
-            {
+            else if(data_send_flag == 't')   //Send Telemetry corresponding to Telecommand
+            {                
                 uint8_t i2c_count =0;
-                write_ack=slave.write((char*)telemetry,135);                
-                Thread::wait(1);
-                pdirtm1=PTE->PDIR;
+                write_ack=slave.write((char*)telemetry,134);
+                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(((pdirtm1 & 0x00000003)!=3)&& i2c_count<10)
+                    while(((pdir_tm1 & 0x00000003)!=3)&& i2c_count<10)
                     {
                         Thread::wait(1);
-                        pdirtm1=PTE->PDIR;
+                        pdir_tm1=PTE->PDIR;
                         i2c_count++;
                     }                    
-                    if(((pdirtm1 & 0x00000003)==3))
+                    if(((pdir_tm1 & 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);
+                        pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tm1);
                         #endif
-                        I2C_busreset();
-                        pdirtm2=PTE->PDIR;                        
-                        #if PRINT
-                        pc.printf("\n\rPTE->DIR = 0x%08X",pdirtm2);
+                        wait_ms(20); //should be atleast 7ms for correct operation but fix this value as 20ms
+                        I2C_busreset();                       
+                        #if PRINT2
+                        pdir_tm2=PTE->PDIR;                        
+                        pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tm2);
                         #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);
+                    pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tm1);
                     #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_busreset();                   
+                    #if PRINT2                          
+                    pdir_tm2=PTE->PDIR;
+                    pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tm2);
+                    #endif
                 }
-                i2c_count=0;
+                //i2c_count=0;
+                irpt_2_mstr = 0;                
             }
             else if(data_send_flag == 'i')
             {
-                //to be filled
-                data_send_flag = 'h';                
+                data_send_flag = 'h';
+                //to be filled by Lakshya during code integration                
+                ///////////////////////////////////////////////  
+                ///////////////////////////////////////////////              
             }
         }
-        else if( slave.receive()==3 ||  slave.receive()==2)             // slave read 
+        else if( slave.receive()==3 ||  slave.receive()==2)             // slave reads from master 
         {
             uint8_t i2c_count = 0;            
-            synch.stop();            
-            read_ack=slave.read((char *)telecommand,11);            
+            data_send_flag = 't';
+            read_ack=slave.read((char *)telecommand,11);    //read() function returns acknowledgement
             Thread::wait(1);
-            pdirtc1=PTE->PDIR;
-            irpt_2_mstr = 1;  
-            data_send_flag = 't'; 
-            if(read_ack==0)
+            pdir_tc1=PTE->PDIR;            
+            if(read_ack==0)     //read() says it was successful
             {
-                while(((pdirtc1 & 0x00000003)!=3)&& i2c_count<10)
+                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);
-                    pdirtc1=PTE->PDIR;
+                    pdir_tc1=PTE->PDIR;
                     i2c_count++;
                 }                    
-                if(((pdirtc1 & 0x00000003)==3))
+                if(((pdir_tc1 & 0x00000003)==3))
                 {
-                    pc.printf("\n\n\rRead TC success");
-                    
-                    // FCTN_TC_DECODE((uint8_t*) telecommand);                   
+                    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();
+                    //data_send_flag = 't';
+                    //pc.printf("\n\r Telemetry Generation \n");
+                    //irpt_2_mstr = 1;
                     
                     //uint8_t read_ack2 = (uint8_t)(PTE->PDIR & 0x00000003);
                     //printf("\n\rread_ack2 = %d",read_ack2);
@@ -688,64 +724,48 @@
                     //tempt.stop();               
                     //data_send_flag = 't';                                 //uncomment later
                }
-               else
+               else     //either or both of SDA and SCL lines low
                {                   
                     #if PRINT
                     pc.printf("\n\n\rRead TC error");
-                    pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc1);
+                    pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tc1);
                     #endif
                     I2C_busreset();
-                    pdirtc2=PTE->PDIR;                    
-                    #if PRINT
-                    pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc2);
+                    //data_send_flag = 'i';                    
+                    #if PRINT2
+                    pdir_tc2=PTE->PDIR;
+                    pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tc2);
                     #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';
                 }
+                //irpt_2_mstr = 1;
                 //pc.printf("\n\rTC count:%d",i2c_count);
             }
-            else
+            else //read() says it was not successful
             {
                 #if PRINT
                 pc.printf("\n\n\rTC ack failed");
-                pc.printf("\n\rPTE->DIR = 0x%08X",pdirtc1);
+                pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tc1);
                 #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);
+                pdir_tc2 = PTE->PDIR;
+                //data_send_flag = 'i';   //'i' = invalid                 
+                #if PRINT2
+                pc.printf("\n\rPTE->DIR = 0x%08X",pdir_tc2);
+                #endif                
             }           
             i2c_count = 0;
+            irpt_2_mstr = 1;
             //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()
@@ -754,7 +774,6 @@
      ptr_t_i2c->signal_set(0x4);
 }
 
-
 //***********************************************************FAULTS***************************************************************//
 /*void ir1clear()
 {
@@ -813,9 +832,6 @@
 
 void pollfault()
 {   
-    
-    
- 
     if (pf1==0)                // OC_ATS1
     { 
         pf1check=1;
@@ -1002,15 +1018,12 @@
 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   
-    
+    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;    
-    
-
+        BAE_ENABLE = BAE_ENABLE &0xFFFFFF7;
    
     pc.printf("\n\r BAE status %x BAE ENABLE %x ",BAE_STATUS,BAE_ENABLE);
 }
@@ -1053,6 +1066,7 @@
 {
     pc.baud(9600);
     pc.printf("\n\r BAE Activated. Testing Version 1.1 \n");
+    //slave.frequency(100000);
     //CDMS_RESET = 1;  
     /*if (BCN_FEN == 0)                       //dummy implementation
     {
@@ -1077,6 +1091,7 @@
     //...i2c..
     //strcpy(telemetry,"This is telemetry THis is sample telemetry. ffffffffffffffffffffffffffffff  end");
     slave.address(addr);
+    slave.frequency(50000);
     irpt_2_mstr = 0;
     
     ptr_t_i2c = new Thread(T_TC);