i2c slave working version 1.1

Dependencies:   mbed-rtos mbed

Fork of slave_i2c3 by sakthi priya amirtharaj

Revision:
5:ce0bb4174ca4
Parent:
4:133b49d51b18
Child:
6:8aea752c6eda
--- a/main.cpp	Wed Dec 10 10:33:56 2014 +0000
+++ b/main.cpp	Wed Dec 10 16:38:29 2014 +0000
@@ -6,6 +6,8 @@
 I2CSlave slave(D14,D15);                       //configuring pins p27, p28 as I2Cslave
 Serial pc (USBTX,USBRX);
 InterruptIn interrupt(D9);
+DigitalOut data_ready(D10);
+int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master
 
 typedef struct
 {
@@ -16,7 +18,7 @@
 Mail<i2c_data,16> i2c_data_receive;
 Mail<i2c_data,16> i2c_data_send;
 
-void FUNC_I2C_WRITE2CDMS(char *data_receive,char *data_send, int length=1)
+void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
 {
        
     int slave_status = 1;
@@ -25,7 +27,7 @@
             slave.address(0x20);
             if(slave.receive()==1)
             {
-                slave_status=slave.write(data_send,length);
+                slave_status=slave.write(data,length);
                // printf("\n In the while loop\n");
                 
                 /*if(!slave_status)
@@ -39,7 +41,7 @@
                 if(slave.receive()==3 || slave.receive()==2)
                 {
                      //i2c_data *i2c_data_r = i2c_data_receive.alloc();
-                     slave_status=slave.read(data_receive,length);
+                     slave_status=slave.read(data,length);
                      /*if(!slave_status)
                      {
                          printf("1 read %c from master\n\r",*data);
@@ -54,16 +56,35 @@
 void T_I2C_BAE(void const * args)
 {
     char data_send,data_receive;
-    data_send = 'a';
+    //data_send = 'a';
     while(1)
     {
         Thread::signal_wait(0x1);
-        FUNC_I2C_WRITE2CDMS(&data_receive,&data_send);
-        //printf("\n Data received from CDMS is %c\n",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);
+        if(i2c_status == 0)
+        {
+            
+            FUNC_I2C_WRITE2CDMS(&data_receive);
+            //printf("\n Data received from CDMS is %c\n",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);
+        }
+        else if(i2c_status ==1 )
+        {
+            osEvent evt = i2c_data_send.get();
+            if (evt.status == osEventMail) 
+            {
+                i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
+                //printf("\nData read from CDMS is %c\n",i2c_data_r->data);
+                data_send = i2c_data_s -> data;
+                FUNC_I2C_WRITE2CDMS(&data_send);
+                printf("\nData sent to CDMS is %c\n",data_send);
+                i2c_data_send.free(i2c_data_s);
+                i2c_status = 0;
+                //delete i2c_data_r;
+            }
+        }   
     }
 }
         
@@ -91,7 +112,8 @@
         //printf("\n The data send or received by slave is %c\n",data);
         //(data)++;
         //wait(1);
-         osEvent evt = i2c_data_receive.get();
+        //Remove the comments for receiving data
+        /* osEvent evt = i2c_data_receive.get();
          if (evt.status == osEventMail) 
          {
             i2c_data *i2c_data_r = (i2c_data*)evt.value.p;
@@ -99,7 +121,16 @@
             
             i2c_data_receive.free(i2c_data_r);
             //delete i2c_data_r;
-        }
-        
+        }*/
+        //put in comments for receiving data
+        data_ready=0;
+        data = pc.getc();
+        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;
+        i2c_status=1;
+                
     }
 }
\ No newline at end of file