i2c while removed and works - to be characterised

Dependencies:   mbed-rtos mbed

Fork of BAE_working_verified by sakthi priya amirtharaj

Files at this revision

API Documentation at this revision

Comitter:
sakthipriya
Date:
Sat Jan 31 14:11:53 2015 +0000
Parent:
2:edd107ea4740
Commit message:
i2c while removed and works - to be characterised

Changed in this revision

ACS.cpp Show annotated file Show diff for this revision Revisions of this file
HK.cpp Show annotated file Show diff for this revision Revisions of this file
beacon.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r edd107ea4740 -r 20647ff68b3c ACS.cpp
--- a/ACS.cpp	Fri Dec 26 06:12:09 2014 +0000
+++ b/ACS.cpp	Sat Jan 31 14:11:53 2015 +0000
@@ -25,7 +25,7 @@
  {
      
 
-     printf("\nEnterd PWMGEN function\n");
+     printf("\n\rEnterd PWMGEN function\n\r\r");
      float DCx = 0;         //Duty cycle of Moment in x, y, z directions
      float ix = 0;          //Current sent in x, y, z TR's
      float timep = 0.02 ;  
@@ -57,7 +57,7 @@
          }
         else if(ix >= 0.0624&& ix < 0.555)
          {
-            printf("\nACS entered if\n");
+            printf("\n\rACS entered if\n\r");
             DCx =  331.15*pow(ix,4) - 368.09*pow(ix,3) + 140.43*pow(ix,2) + 158.59*ix + 0.0338;
             PWM1.period(timep);
             PWM1 = DCx/100 ;            
@@ -73,7 +73,7 @@
             // printf("!!!!!!!!!!Error!!!!!!!!!");
          } 
          
-    printf("\n moment :%f\n",DCx);
+    printf("\n\r moment :%f\n\r",DCx);
     float DCy = 0;         //Duty cycle of Moment in x, y, z directions
      float iy = 0;          //Current sent in x, y, z TR's
        
@@ -104,7 +104,7 @@
          }
         else if(iy >= 0.0624&& iy < 0.555)
          {
-            printf("\nACS entered if\n");
+            printf("\n\rACS entered if\n\r");
             DCy =  331.15*pow(iy,4) - 368.09*pow(iy,3) + 140.43*pow(iy,2) + 158.59*iy + 0.0338;
             PWM2.period(timep);
             PWM2 = DCy/100 ;            
@@ -149,7 +149,7 @@
          }
         else if(iz >= 0.0624&& iz < 0.555)
          {
-            printf("\nACS entered if\n");
+            printf("\n\rACS entered if\n\r");
             DCz =  331.15*pow(iz,4) - 368.09*pow(iz,3) + 140.43*pow(iz,2) + 158.59*iz + 0.0338;
             PWM3.period(timep);
             PWM3 = DCz/100 ;            
@@ -165,7 +165,7 @@
             // printf("!!!!!!!!!!Error!!!!!!!!!");
          }    
     
-printf("\nExited PWMGEN function\n");
+printf("\n\rExited PWMGEN function\n\r");
 }
 /*-------------------------------------------------------------------------------------------------------------------------------------------------------
 -------------------------------------------MAGNETOMETER-------------------------------------------------------------------------------------------------*/
@@ -181,7 +181,7 @@
   DRDY = 0;
   int a ;
   a=DRDY;
-  printf("\n DRDY is %d\n",a);
+  printf("\n\r DRDY is %d\n\r",a);
   SSN_MAG=1;                                    //pin is disabled
   spi_acs.format(8,0);                         //   8bits,Mode 0
   spi_acs.frequency(100000);                   //clock frequency
@@ -205,12 +205,12 @@
 
 float* FUNC_ACS_MAG_EXEC()
 {
-   //printf("\nEntered magnetometer function\n");
+   //printf("\n\rEntered magnetometer function\n\r");
    //DRDY.output();
    DRDY.write(0);
    int a;
    a = DRDY;
-   printf("\n DRDY is %d\n",a);
+   printf("\n\r DRDY is %d\n\r",a);
    SSN_MAG=0;                                //enabling slave to measure the values
    wait_ms(10);
    spi_acs.write(0x82);                     //initiates measurement
@@ -229,7 +229,7 @@
     wait_ms(5);
     if(DRDY==1)
     {
-        printf("\nwth\n");
+        printf("\n\rwth\n");
         SSN_MAG=0;
         spi_acs.write(0xc9);                  //command  byte for retrieving data
  
@@ -256,7 +256,7 @@
             Bnewvalue[axis]=(float)Bvalue[axis]*22.0*pow(10.0,-3.0);  //1 LSB=(22nT)...final value of field obtained in micro tesla
   
             wait_ms(10);
-            printf("\t%lf\n",Bnewvalue[axis]);
+            printf("\t%lf\n\r",Bnewvalue[axis]);
 
         }
         SSN_MAG=1;
@@ -285,7 +285,7 @@
     float Mu[2], z[2], dv[2], v[2], u[2], tauc[3] = {0, 0, 0}; //outputs
     float invJm[3][3];
     float kmu2 = 0.07, gamma2 = 1.9e4, kz2 = 0.4e-2, kmu = 0.003, gamma = 5.6e4, kz = 0.1e-4;
-    printf("Entered cntrl algo\n");
+    printf("Entered cntrl algo\n\r");
     //structure parameters
 
     void inverse (float mat[3][3], float inv[3][3]); 
@@ -320,7 +320,7 @@
         
         for(i=0;i<3;i++)
         {
-            printf("\nreached here\n");
+            printf("\n\rreached here\n\r");
             if(den!=0)
                 //b[i]=b[i]/den;                                      //there is a problem here. The code gets stuck here.  Maf value is required 
                 ;
@@ -384,7 +384,7 @@
 }
 /////////// Output to Matlab //////////////////
 /* for(i=0;i<3;i++) {
- printf("%f\n",tauc[i]*10000000);
+ printf("%f\n\r",tauc[i]*10000000);
  wait_ms(10);
  }
  }
@@ -423,7 +423,7 @@
  }
  sscanf (tempchar, "%8x", &n);
  memcpy(&flval, &n, sizeof(long));
- printf("%f\n", flval);
+ printf("%f\n\r", flval);
  x[j] = flval;
  }
 }*/
@@ -532,7 +532,7 @@
 
 float * FUNC_ACS_EXEC_GYR()
 {
-    printf("\nEntered gyro\n");
+    printf("\n\rEntered gyro\n\r");
     uint8_t response;
     uint8_t MSB,LSB;
     int16_t bit_data;
@@ -574,7 +574,7 @@
             {
                 printf("%f\t",data[i]); //printing the angular velocity values
             }
-            printf("\n");
+            printf("\n\r");
             break;
         }
             drFlag=0;
@@ -590,7 +590,7 @@
     response=spi_acs.write(response|BIT_SLEEP); //setting the gyroscope in sleep mode
     ssn_gyr=1;
     wait(0.1);
-    printf("\nExited gyro\n");
+    printf("\n\rExited gyro\n\r");
     return data;
 }
 
diff -r edd107ea4740 -r 20647ff68b3c HK.cpp
--- a/HK.cpp	Fri Dec 26 06:12:09 2014 +0000
+++ b/HK.cpp	Sat Jan 31 14:11:53 2015 +0000
@@ -137,7 +137,7 @@
         resistance_thermistor=24000*voltage_thermistor/(3.3-voltage_thermistor);//resistance of thermistor
         if (LoopIterator==0)
         {
-            printf(" Temp =%f",-90.7*3.3*TemperatureInput.read()+190.1543);
+            printf(" \n\rTemp =%f",-90.7*3.3*TemperatureInput.read()+190.1543);
         }
         
         if(LoopIterator%2==0)
diff -r edd107ea4740 -r 20647ff68b3c beacon.cpp
--- a/beacon.cpp	Fri Dec 26 06:12:09 2014 +0000
+++ b/beacon.cpp	Sat Jan 31 14:11:53 2015 +0000
@@ -52,7 +52,7 @@
 void FUNC_BEA() {
     
     //button.rise(&interrupt_func);         //interrupt enabled ( rising edge of pin 9)
-    printf("\nBeacon function entered\n");
+    printf("\n\rBeacon function entered\n\r");
     wait(0.02);                             //takes 10 ms for POR event + 10ms for safety                 
     
     uint8_t byte_counter = 0;
@@ -74,7 +74,7 @@
         
     for(int i = 0; i < 15 ; i++)
     {
-        chavan.printf("0x%X\n",(short_beacon[i]));    
+        chavan.printf("0x%X\n\r",(short_beacon[i]));    
     }
     
     spi.format(8,0);                    
@@ -134,9 +134,9 @@
     cs_bar = 1; //cs_bar
                 
     //Check for fifoThresh
-    printf("\nfor loop executed\n");
+    printf("\n\rfor loop executed\n\r");
     while((readreg(0x28) & 0x20) != 0x20);                         
-    printf("\nwhile loop executed\n");
+    printf("\n\rwhile loop executed\n\r");
     //Highpower settings
     writereg(0x11,0x7F);    //RegPalevel (20db)                //~
     writereg(0x13,0x0F);    //RegOCP
@@ -146,10 +146,10 @@
     //Set to Tx mode
     writereg(0x01,0x0C);
     
-        printf("\npre 2nd while loop\n");
+        printf("\n\rpre 2nd while loop\n\r");
         //Check for fifoThresh
         while((readreg(0x28) & 0x20) != 0x00);  
-        printf("\n2nd while loop executed\n");        
+        printf("\n\r2nd while loop executed\n\r");        
         while(byte_counter!=15){
             
         //writing again
@@ -175,11 +175,11 @@
         //Check for fifoThresh
         while((readreg(0x28) & 0x20) != 0x00);
     }
-    printf("\n3rd big while loop executed\n");
+    printf("\n\r3rd big while loop executed\n\r");
     //wait for packet sent bit to fire
     while((readreg(0x28) & 0x08) != 0x08);
-    printf("\n4th while loop executed\n");
-    //chavan.printf("packet sent!!! \n");    
+    printf("\n\r4th while loop executed\n\r");
+    //chavan.printf("packet sent!!! \n\r");    
     
     //Switch back to Standby Mode
     writereg(0x01,0x04);
@@ -196,5 +196,5 @@
     //t.stop();
     //chavan.printf(" time taken to init + transmit = %f \n", t.read()) ;
     //}
-printf("\nBeacon function exiting\n");
+printf("\n\rBeacon function exiting\n\r");
 }
diff -r edd107ea4740 -r 20647ff68b3c main.cpp
--- a/main.cpp	Fri Dec 26 06:12:09 2014 +0000
+++ b/main.cpp	Sat Jan 31 14:11:53 2015 +0000
@@ -17,7 +17,9 @@
 Timer t;                                                        //To know the time of execution each thread
 Timer t1;
                                                        //To know the time of entering  of each thread
-
+Timer t2;
+Timer t3;
+Timer t4;
 
 
 
@@ -70,7 +72,7 @@
     {
         Thread::signal_wait(0x2);
         SensorQuantised.power_mode='3';
-        printf("\nTHIS IS HK    %f\n",t1.read());
+        printf("\n\rTHIS IS HK    %f\n\r",t1.read());
         t.start();
         FUNC_HK_FAULTS();
         FUNC_HK_POWER(SensorQuantised.power_mode);                                                   //The power mode algorithm is yet to be obtained
@@ -79,7 +81,7 @@
         //FUNC_I2C_SLAVE_MAIN(25);    
         FUNC_I2C_IR2CDMS();
         t.stop();
-        printf("The time to execute hk_acq is %f seconds\n",t.read());
+        printf("The time to execute hk_acq is %f seconds\n\r",t.read());
         t.reset();
     }
 }
@@ -97,11 +99,11 @@
     while(1)
     {
         Thread::signal_wait(0x1);
-        printf("\nTHIS IS ACS   %f\n",t1.read());
+        printf("\n\rTHIS IS ACS   %f\n\r",t1.read());
         t.start();
         mag_field= FUNC_ACS_MAG_EXEC();                              //actual execution
         omega = FUNC_ACS_EXEC_GYR();
-        printf("\n gyr 1 value %f",omega[0]);
+        printf("\n\r gyr 1 value %f",omega[0]);
         
         
         if(acs_pflag == 1)
@@ -118,7 +120,7 @@
 {
     while(1)
     {
-        //printf("Writing in the flash\n");
+        //printf("Writing in the flash\n\r");
         osEvent evt = q_acs.get();
         if(evt.status == osEventMail)
         {
@@ -126,7 +128,7 @@
             FUNC_ACS_WRITE2FLASH(ptr);
             q_acs.free(ptr);
         }
-        printf("Writing acs data in the flash\n");
+        printf("Writing acs data in the flash\n\r");
     }
 }
 
@@ -141,7 +143,7 @@
     char c = pc.getc();
     if(c=='a')
     {
-        printf("Telecommand detected\n");
+        printf("Telecommand detected\n\r");
         beac_flag=1;
     }
 }
@@ -153,7 +155,7 @@
     while(1)
     {
         Thread::signal_wait(0x3);
-        printf("\nTHIS IS BEACON    %f\n",t1.read());
+        printf("\n\rTHIS IS BEACON    %f\n\r",t1.read());
         t.start();
         
         
@@ -167,7 +169,7 @@
             beac_flag = 0;
         }
         
-        printf("The time to execute beacon thread is %f seconds\n",t.read());
+        printf("The time to execute beacon thread is %f seconds\n\r",t.read());
         t.reset();
     }
 }
@@ -177,38 +179,6 @@
 //---------------------------------------------------------------------------------------------------------------------------------------------------
 //Dummy fault rectifier functions
 
-/*Mail<int,16> faults;
-
-void FUNC_FAULT_FUNCT1()
-{
-    printf("\nFault 1 detected... \n");
-}
-
-void FUNC_FAULT_FUNCT2()
-{
-    printf("\nFault 2 detected...\n");
-}
-
-void T_FAULT(void const *args)
-{
-    while(1)
-    {
-        osEvent evt = faults.get();
-        if(evt.status==osEventMail)
-        {
-            int *fault_id= (int *)evt.value.p;
-            switch(*fault_id)
-            {
-                case 1: FUNC_FAULT_FUNCT1();
-                        break;
-                case 2: FUNC_FAULT_FUNCT2();
-                        break;
-            }
-            faults.free(fault_id);
-        }
-    }
-}*/
-
 extern SensorDataQuantised SensorQuantised;
 
 
@@ -233,7 +203,7 @@
     while(true)
     {
         Thread::signal_wait(0x5);                               //signal set from scheduler or sthing. r RTOS timer nce the timing is finalized
-        printf("\nEntered WD\n");
+        printf("\n\rEntered WD\n\r");
         trigger = !trigger;
     }
 }
@@ -250,26 +220,41 @@
 void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
 {
        int slave_status = 1;
-   
-    while(slave_status && interrupt == 1)
-    {                 
-            slave.address(0x20);
-            if(slave.receive()==1)
+//t2.stop();
+       if(interrupt ==1)
+       {
+
+           if(slave.receive() == 0)
+           t2.stop();
+//t4.stop();     
+//  printf("\n\r %d ",slave.receive());  
+           if( slave.receive()==1)
+           {
+               t2.stop();
+               //t3.start();
+               slave_status=slave.write(data,length);
+               //t3.stop();
+            
+            }
+            else if( slave.receive()==3 ||  slave.receive()==2)
             {
-                slave_status=slave.write(data,length);
-               
+                t2.stop();
+                //t3.start();
+                slave_status=slave.read(data,length);
+                //t3.stop();
                 
             }
-            else if(slave.receive()==3 || slave.receive()==2)
-            {
-                slave_status=slave.read(data,length);
-            }
+         
             
     }
-            
-            printf("\ndone\n\r");
-
+           
+            printf("\n\r%d\r",t2.read_us());
+            t2.reset();
+             //printf("\n\r%d\r",t3.read_us());
+            //t3.reset();
+     
 }
+   
 char data_send[25],data_receive;
 void T_I2C_BAE(void const * args)
 {
@@ -277,20 +262,17 @@
     while(1)
     {
         Thread::signal_wait(0x4);
-        //T_I2C_BAE();
-        //i2c_status = temp;
-        //wait(0.5);
-        printf("\n entered thread   %d\n\r",i2c_status);
+       // printf("\n\r entered thread");
         if(i2c_status == 0 )
         {
-            
+            wait_ms(30);
             FUNC_I2C_WRITE2CDMS(&data_receive,1);
-            printf("\n received\n");
+               
             /*i2c_data * i2c_data_r = i2c_data_receive.alloc();
             i2c_data_r->data = data_receive;
             i2c_data_r->length = 1;
             i2c_data_receive.put(i2c_data_r);*/
-            printf("\n Data received from CDMS is %c \n\r",data_receive);
+            printf("\n\r Data received from CDMS is %c \n\r",data_receive);
             FUNC_I2C_TC_EXECUTE(data_receive);                             // This has to be done from a differen thread
             
         }
@@ -301,11 +283,12 @@
             {
                 i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
                 strcpy(data_send,i2c_data_s -> data);
+                wait_ms(29);
                 FUNC_I2C_WRITE2CDMS(data_send,25);
-                printf("\nData sent to CDMS is %s\n\r",data_send);
+                printf("\n\rData sent to CDMS is %s\n\r",data_send);
                 i2c_data_send.free(i2c_data_s);
                 i2c_status = 0;
-                //temp = i2c_status;
+                
                 
             }
         }  
@@ -317,19 +300,16 @@
 
 void FUNC_I2C_INT()
 {
-   reset = 0;
+   
+   t2.start();
    ptr_t_i2c->signal_set(0x4);
    
      
 }
 
-
-
-
-
 void FUNC_I2C_IR2CDMS()
 {
-        
+        data_ready=0; 
         //char data[25];
         //strcpy(data,"sakthi ");
         //strcat(data,"priya");
@@ -345,8 +325,8 @@
         strcat(hk_data,sfaultir);
         strcat(hk_data,spower_mode);*/
         strcat(hk_data,fdata);
-        printf("\nhk data : %s\n",hk_data);
-        data_ready=0;
+        printf("\n\rhk data : %s\n\r",hk_data);
+       
         //data = pcslave.getc();
         reset =0;
         i2c_status=1;
@@ -383,7 +363,7 @@
 void T_SC(void const *args)
 {
     //DRDY=0;
-    printf("The value of i in scheduler is %d\n",schedcount);
+    printf("The value of i in scheduler is %d\n\r",schedcount);
     if(schedcount == 65532)                         //to reset the counter
     {
         schedcount = 0;
@@ -391,12 +371,12 @@
     
     if(schedcount%1==0)
     {
-        ptr_t_acs -> signal_set(0x1);
-        ptr_t_wdt -> signal_set(0x5);
+     ptr_t_acs -> signal_set(0x1);
+       ptr_t_wdt -> signal_set(0x5);
     }
     if(schedcount%2==0)
     {
-        //ptr_t_fault -> signal_set(0x2);
+       // ptr_t_fault -> signal_set(0x2);
         ptr_t_hk_acq -> signal_set(0x2);
         
     }
@@ -405,7 +385,7 @@
         if(beac_flag==0)
         {
             
-            //ptr_t_bea -> signal_set(0x3);
+            ptr_t_bea -> signal_set(0x3);
         }
     }
     schedcount++;
@@ -416,6 +396,7 @@
 int main()
 {
     t1.start();
+     slave.address(0x20);
     //DRDY=0;
      FUNC_ACS_MAG_INIT();
      FUNC_ACS_INIT_GYR();
@@ -444,14 +425,14 @@
     
   
    // ----------------------------------------------------------------------------------------------
-    //printf("\n T_FAULT priority is %d",ptr_t_fault->get_priority()); 
-    printf("\n T_ACS priority is %d",ptr_t_acs->get_priority());
-    printf("\n T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority());
-    //printf("\n T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority());
-    printf("\n T_BEA priority is %d",ptr_t_bea->get_priority());  
+    //printf("\n\r T_FAULT priority is %d",ptr_t_fault->get_priority()); 
+    printf("\n\r T_ACS priority is %d",ptr_t_acs->get_priority());
+    printf("\n\r T_HK_ACQ priority is %d",ptr_t_hk_acq->get_priority());
+    //printf("\n\r T_ACS_WRITE2FLASH priority is %d",ptr_t_acs_write2flash->get_priority());
+    printf("\n\r T_BEA priority is %d",ptr_t_bea->get_priority());  
     RtosTimer t_sc_timer(T_SC,osTimerPeriodic);
     t_sc_timer.start(10000);
-    printf("\n%f\n",t1.read()); 
+    printf("\n\r%f\n\r",t1.read()); 
      
      
       
@@ -463,7 +444,7 @@
         //Thread::wait(10000);
          //ir2master(); 
          //DRDY = 0;
-         Thread::wait(5000);
+        Thread::wait(5000);
     }
     
 }