i2c slave working version 1.1

Dependencies:   mbed-rtos mbed

Fork of slave_i2c3 by sakthi priya amirtharaj

Revision:
8:04b912dc2443
Parent:
7:debeb7070a54
--- a/main.cpp	Fri Dec 12 12:16:48 2014 +0000
+++ b/main.cpp	Sat Dec 13 11:22:34 2014 +0000
@@ -6,9 +6,12 @@
 I2CSlave slave(D14,D15);                       //configuring pins p27, p28 as I2Cslave
 Serial pc (USBTX,USBRX);
 InterruptIn interrupt(D9);
+InterruptIn master_reset(D11);
 DigitalOut data_ready(D10);
 int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master
-char data='a';
+int reset=0;
+int temp;
+
 
 typedef struct
 {
@@ -19,7 +22,7 @@
 Mail<i2c_data,16> i2c_data_receive;
 Mail<i2c_data,16> i2c_data_send;
  
- int alpha;
+
 void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
 {
        int slave_status = 1;
@@ -33,16 +36,13 @@
                
                 
             }
-            else
-                if(slave.receive()==3 || slave.receive()==2)
-                {
-                     
-                     slave_status=slave.read(data,length);
-                     
-                }
-           
+            else if(slave.receive()==3 || slave.receive()==2)
+            {
+                slave_status=slave.read(data,length);
+            }
+            
     }
-            printf("done");
+            printf("\ndone\n\r");
 
 }
 
@@ -52,7 +52,10 @@
     while(1)
     {
         Thread::signal_wait(0x1);
-        if(i2c_status == 0)
+        //i2c_status = temp;
+        //wait(0.5);
+        printf("\n entered thread\n\r");
+        if(i2c_status == 0 && reset !=1)
         {
             
             FUNC_I2C_WRITE2CDMS(&data_receive);
@@ -60,11 +63,11 @@
             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);
+            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 )
+        else if(i2c_status ==1 && reset !=1)
         {
             osEvent evt = i2c_data_send.get();
             if (evt.status == osEventMail) 
@@ -72,15 +75,11 @@
                 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",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;
             }
-             
-            
-           
         }  
            
     }
@@ -88,21 +87,30 @@
         
 void FUNC_INT()
 {
-  
+   reset = 0;
    ptr_t_i2c->signal_set(0x1);
      
 }
 
+void FUNC_RESET()
+{
+    reset = 1;
+}
+
 void ir2master()
 {
+        
+        char data='a';
         data_ready=0;
         data = pc.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;
 }
       
    
@@ -110,9 +118,10 @@
 
 int main()
 {   
-    
+    printf("\n slave started\n\r");
     
     ptr_t_i2c = new Thread(T_I2C_BAE);
+    master_reset.fall(&FUNC_RESET);
     interrupt.rise(&FUNC_INT);
     while(1)
     {