i2c slave working version 1.0

Dependencies:   mbed-rtos mbed

Fork of freeslave_write2master_working by Seeker of Truth ,

Revision:
7:debeb7070a54
Parent:
6:8aea752c6eda
--- a/main.cpp	Fri Dec 12 08:51:42 2014 +0000
+++ b/main.cpp	Fri Dec 12 12:16:48 2014 +0000
@@ -8,6 +8,7 @@
 InterruptIn interrupt(D9);
 DigitalOut data_ready(D10);
 int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master
+char data='a';
 
 typedef struct
 {
@@ -29,52 +30,38 @@
             if(slave.receive()==1)
             {
                 slave_status=slave.write(data,length);
-               // printf("\n In the while loop\n");
-                
-                /*if(!slave_status)
-                {
-                        printf("slave wrote %c to master\n\r",*data);
-              
-                }*/
+               
                 
             }
             else
                 if(slave.receive()==3 || slave.receive()==2)
                 {
-                     //i2c_data *i2c_data_r = i2c_data_receive.alloc();
+                     
                      slave_status=slave.read(data,length);
-                     /*if(!slave_status)
-                     {
-                         printf("1 read %c from master\n\r",*data);
-                     }*/
+                     
                 }
-            //slave.stop();
+           
     }
-            printf("2 done");
+            printf("done");
 
 }
 
 void T_I2C_BAE(void const * args)
 {
     char data_send,data_receive;
-    //data_send = 'a';
     while(1)
     {
         Thread::signal_wait(0x1);
-        printf("\nbdh\n");
-        //i2c_status=alpha;
-        printf(" i2c status is %d " , i2c_status);
         if(i2c_status == 0)
         {
             
             FUNC_I2C_WRITE2CDMS(&data_receive);
-            printf("\n i2c=0\n\r");
-            
             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",data_receive);
+            i2c_data_receive.free(i2c_data_r);
             
         }
         else if(i2c_status ==1 )
@@ -83,14 +70,13 @@
             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;
+               
             }
              
             
@@ -102,13 +88,23 @@
         
 void FUNC_INT()
 {
-  // char * data = new char;
-   //*data = 'a';
-   //printf("\blah\n");
+  
    ptr_t_i2c->signal_set(0x1);
-   //delete data;
-   
+     
 }
+
+void ir2master()
+{
+        data_ready=0;
+        data = pc.getc();
+        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;
+}
+      
    
 
 
@@ -117,40 +113,10 @@
     
     
     ptr_t_i2c = new Thread(T_I2C_BAE);
-    char data='a';
     interrupt.rise(&FUNC_INT);
     while(1)
     {
-        //*(data_send)=pc.getc();
-        
-        //FUNC_I2C_WRITE2CDMS(&data,1);
-        //printf("\n The data send or received by slave is %c\n",data);
-        //(data)++;
-        //wait(1);
-        //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;
-            printf("\nData read from CDMS is %c\n",i2c_data_r->data);
-            
-            i2c_data_receive.free(i2c_data_r);
-            //delete i2c_data_r;
-        }*/
-        //put in comments for receiving data
-        data_ready=0;
-        data = pc.getc();
-        printf(" %c ", data);
-        i2c_status=1;
-        printf(" %d ", i2c_status );
-        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);
-       // wait(0.5);
-        alpha=i2c_status;
-        data_ready=1;
-      
+      ir2master(); 
                 
     }