after pin config implementation

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDM by sakthi priya amirtharaj

Files at this revision

API Documentation at this revision

Comitter:
raizel_varun
Date:
Mon Dec 15 05:58:04 2014 +0000
Parent:
8:69475d16d23d
Commit message:
I2C INTEGTRATED VR1.1

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
slave.cpp Show annotated file Show diff for this revision Revisions of this file
slave.h Show annotated file Show diff for this revision Revisions of this file
diff -r 69475d16d23d -r 221d218f4690 main.cpp
--- a/main.cpp	Thu Dec 11 07:34:17 2014 +0000
+++ b/main.cpp	Mon Dec 15 05:58:04 2014 +0000
@@ -5,9 +5,12 @@
 #include "beacon.h"
 #include "ACS.h"
 #include "fault.h"
+#include "slave.h"
 
 Serial pc(USBTX, USBRX);
 
+InterruptIn interrupt(D9);
+InterruptIn master_reset(D11);
 
 
 Timer t;                                                        //To know the time of execution each thread
@@ -20,7 +23,7 @@
 Thread *ptr_t_bea;
 Thread *ptr_t_bea_telecommand;
 Thread *ptr_t_fault;
-
+Thread *ptr_t_i2c;
 
 //--------------------------------------------------------------------------------------------------------------------------------------------------
 //TASK 2 : HK
@@ -40,7 +43,7 @@
         
         FUNC_HK_MAIN();                                                             //Collecting HK data
         //thread_2.signal_set(0x4);
-        FUNC_I2C_SLAVE_MAIN(24);                                                    //Sending to CDMS via I2C
+        //FUNC_I2C_SLAVE_MAIN(24);                                                    //Sending to CDMS via I2C
         
         t.stop();
         printf("The time to execute hk_acq is %f seconds\n",t.read());
@@ -210,6 +213,69 @@
     }    
 }
 
+//---------------------------------------------------------------------------------------------------------------------------------------------------
+//TASK 5 : i2c data
+//---------------------------------------------------------------------------------------------------------------------------------------------------
+
+ 
+void C_FUNC_INT()
+{
+   FUNC_INT();
+   ptr_t_i2c->signal_set(0x4);
+     
+}
+
+void C_FUNC_RESET()
+{
+    FUNC_RESET();
+}
+
+
+void C_T_I2C_BAE(void const * args)
+{
+    //char data_send,data_receive;
+    while(1)
+    {
+        Thread::signal_wait(0x4);
+        T_I2C_BAE();
+        //i2c_status = temp;
+        //wait(0.5);
+        /*printf("\n entered thread\n\r");
+        if(i2c_status == 0 && reset !=1)
+        {
+            
+            FUNC_I2C_WRITE2CDMS(&data_receive);
+            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);
+            i2c_data_receive.free(i2c_data_r);                              // This has to be done from a differen thread
+            
+        }
+        else if(i2c_status ==1 && reset !=1)
+        {
+            osEvent evt = i2c_data_send.get();
+            if (evt.status == osEventMail) 
+            {
+                i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
+                data_send = i2c_data_s -> data;
+                FUNC_I2C_WRITE2CDMS(&data_send);
+                printf("\nData sent to CDMS is %c\n\r",data_send);
+                i2c_data_send.free(i2c_data_s);
+                i2c_status = 0;
+                //temp = i2c_status;
+            }
+        }  
+          */ 
+    }
+}
+
+
+
+
+
+
 //------------------------------------------------------------------------------------------------------------------------------------------------
 //SCHEDULER
 //------------------------------------------------------------------------------------------------------------------------------------------------
@@ -257,12 +323,14 @@
     ptr_t_bea = new Thread(t_bea);
     ptr_t_bea_telecommand = new Thread(t_bea_telecommand);
     ptr_t_fault = new Thread(T_FAULT);
+    ptr_t_i2c = new Thread(C_T_I2C_BAE);
     //ptr_t_sc = new Thread(t_sc);
   
     interrupt_fault();
     
     ptr_t_fault -> set_priority(osPriorityRealtime);
     ptr_t_acs->set_priority(osPriorityHigh);
+    ptr_t_i2c->set_priority(osPriorityAboveNormal);
     ptr_t_hk_acq->set_priority(osPriorityNormal);
     ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal);
     ptr_t_bea->set_priority(osPriorityAboveNormal);
@@ -279,11 +347,16 @@
     RtosTimer t_sc_timer(t_sc,osTimerPeriodic);
     t_sc_timer.start(10000);
     printf("\n%f\n",t1.read()); 
-        
+     
+     
+      
+    master_reset.fall(&C_FUNC_RESET);
+    interrupt.rise(&C_FUNC_INT);
+       
     while(1)
     {   
-        Thread::wait(10000);
-        ;
+        //Thread::wait(10000);
+         ir2master(); 
     }
     
 }
\ No newline at end of file
diff -r 69475d16d23d -r 221d218f4690 slave.cpp
--- a/slave.cpp	Thu Dec 11 07:34:17 2014 +0000
+++ b/slave.cpp	Mon Dec 15 05:58:04 2014 +0000
@@ -1,101 +1,123 @@
-/* Added fault to hk structure and being sent through I2C. Need to verify the format */
+#include "mbed.h"
+#include "rtos.h"
 #include "slave.h"
-#include "HK.h"
+
+void write_to_master(char);                    //function to write data to master
+
+I2CSlave slave(D14,D15);                       //configuring pins p27, p28 as I2Cslave
+
+Serial pcslave(USBTX, USBRX);
+DigitalOut data_ready(D10);
+int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master
+int reset=0;
+int temp;
+
+typedef struct
+{
+    char data;          // To avoid dynamic memory allocation
+    int length;
+}i2c_data;
+
+
+
+Mail<i2c_data,16> i2c_data_receive;
+Mail<i2c_data,16> i2c_data_send;
  
-extern struct SensorData Sensor;
-I2CSlave slave(PTC9,PTC8);                       //configuring pins p27, p28 as I2Cslave
-Serial screen (USBTX,USBRX);
-void write_to_master(char send)                           //function to write data to master
-        {
-           int acknowledge; 
-           int loopvariable4=1;
-           while(loopvariable4)
-           {
-           acknowledge = slave.write(send);    //sending the byte to master
-           if(acknowledge==1)
+
+void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
+{
+       int slave_status = 1;
+   
+    while(slave_status)
+    {                 
+            slave.address(0x20);
+            if(slave.receive()==1)
             {
-              screen.printf(" acknowledge %d sent %u \n",acknowledge,send);
-              loopvariable4=0;
+                slave_status=slave.write(data,length);
+               
+                
+            }
+            else if(slave.receive()==3 || slave.receive()==2)
+            {
+                slave_status=slave.read(data,length);
             }
-           } 
-        }
-        
- 
- 
- 
-void FUNC_I2C_SLAVE_MAIN(int iterations)
+            
+    }
+            printf("\ndone\n\r");
+
+}
+char data_send,data_receive;
+void T_I2C_BAE()
 {
-    
-    screen.printf("\nSlave entered\n");
-    slave.address(slave_address);                           //assigning slave address
-    slave.stop();
-    char Switch_Variable;
-    
-    
-    int loopvariable1=1;
-    int loopvariable2=0;
-    int loopvariable3=1;
-//initialising dummy sensor data                         
-    
-    while(loopvariable1)
-    { 
-//to read data from master       
-    if(slave.receive()==WriteGeneral)                   //checking if slave is addressed to write
-    {
-      slave.stop();
-      Switch_Variable=slave.read();                   //receiving data
-      screen.printf("switch variable=%d\n",Switch_Variable);
-      slave.stop();                          //reset slave to default receiving state
-      loopvariable1=0;
-//to interpret and write data to master       
-      switch(Switch_Variable)
-      {
+    //char data_send,data_receive;
+    //while(1)
+    //{
+       // Thread::signal_wait(0x1);
+        //i2c_status = temp;
+        //wait(0.5);
+        printf("\n entered thread\n\r");
+        if(i2c_status == 0 && reset !=1)
+        {
+            
+            FUNC_I2C_WRITE2CDMS(&data_receive);
+            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);
+            i2c_data_receive.free(i2c_data_r);                              // This has to be done from a differen thread
+            
+        }
+        else if(i2c_status ==1 && reset !=1)
+        {
+            osEvent evt = i2c_data_send.get();
+            if (evt.status == osEventMail) 
+            {
+                i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
+                data_send = i2c_data_s -> data;
+                FUNC_I2C_WRITE2CDMS(&data_send);
+                printf("\nData sent to CDMS is %c\n\r",data_send);
+                i2c_data_send.free(i2c_data_s);
+                i2c_status = 0;
+                //temp = i2c_status;
+            }
+        }  
+           
+    //}
+}
 
-       
-       case 1:   printf("\nEntered switch\n"); 
-                   while(loopvariable3)
-                  {
-                     if(slave.receive()==ReadAddressed)             //check if slave is addressed to read 
-                   {
-                      while(loopvariable2<iterations)
-                     {
-                      if(loopvariable2%3==0)
-                          {
-                          screen.printf("\nvoltage%d\n",loopvariable2/3);
-                          write_to_master(Sensor.Voltage[loopvariable2/3]);
-                          }
-                      else if(loopvariable2%3==1)
-                          {   
-                          screen.printf("\ncurrent%d\n",loopvariable2/3);
-                          write_to_master(Sensor.Current[loopvariable2/3]);
-                          }
-                      else if(loopvariable2%3==2)
-                          {   
-                          screen.printf("\ntemp%d\n",loopvariable2/3);
-                          write_to_master(Sensor.Temperature[loopvariable2/3]);
-                          }         
-                      loopvariable2++;
-                       }
-                       screen.printf("\nfault %c\n",Sensor.faultpoll);
-                       write_to_master(Sensor.faultpoll);
-                       screen.printf("\nfault %c\n",Sensor.faultir);
-                       write_to_master(Sensor.faultir);
-                       screen.printf("\npower mode %c\n",Sensor.power_mode);
-                       write_to_master(Sensor.power_mode);
-                       printf("\nExited i2c while loop\n");
-                      slave.stop();    
-                      loopvariable3=0;  
-                    }//if(read addressed)
-                    
-                    }//while(loopvariable3)
-                  
-                  break;
-         case 2 : screen.printf(" telecommand 2\n");
-                  break;         
-                 
-        }//switch case ends
-   }   
+        
+
+void FUNC_INT()
+{
+   reset = 0;
+   
+     
+}
+
+void FUNC_RESET()
+{
+    reset = 1;
 }
-   screen.printf("\nexited slave function\n");
+
+void ir2master()
+{
+        
+        char data='a';
+        data_ready=0;
+        data = pcslave.getc();
+        reset =0;
+        i2c_status=1;
+        i2c_data * i2c_data_s = i2c_data_send.alloc();
+        i2c_data_s->data = data;
+        i2c_data_s->length = 1;
+        i2c_data_send.put(i2c_data_s); 
+        data_ready=1;
+        //temp = i2c_status;
 }
- 
\ No newline at end of file
+      
+   
+   
+
+
+
diff -r 69475d16d23d -r 221d218f4690 slave.h
--- a/slave.h	Thu Dec 11 07:34:17 2014 +0000
+++ b/slave.h	Mon Dec 15 05:58:04 2014 +0000
@@ -2,5 +2,11 @@
 #define WriteGeneral 3
 #define ReadAddressed 1
 #define slave_address 0x20
-void write_to_master(char);                    //function to write data to master
-void FUNC_I2C_SLAVE_MAIN(int iterations);
\ No newline at end of file
+
+
+void FUNC_I2C_WRITE2CDMS();
+void T_I2C_BAE();
+void ir2master();
+void FUNC_INT();
+void FUNC_RESET();
+