Plese use this class with my "i2cmaster" class.

Dependents:   2016_slave_rorikon_2 2016_slave_cylinder 2016_slave_MD 2016_slave_MD_rorikon ... more

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Thu Jul 12 10:17:19 2018 +0000
Parent:
1:cd92200b8a50
Commit message:
perfect

Changed in this revision

i2cslave.cpp Show annotated file Show diff for this revision Revisions of this file
i2cslave.h Show annotated file Show diff for this revision Revisions of this file
--- a/i2cslave.cpp	Thu Jul 12 08:09:43 2018 +0000
+++ b/i2cslave.cpp	Thu Jul 12 10:17:19 2018 +0000
@@ -1,12 +1,36 @@
 #include "i2cslave.h"
 
+/*example*************************************************
+
+#include "mbed.h"
+#include "i2cslave.h"
+
+int32_t Registar[0x20];
+i2cslave i2c(p28, p27, Registar);
+    
+int main()
+{
+    i2c.address(0xa0);
+    Registar[10] = 0;
+    while(1)
+    {
+        printf("%d\r\n", Registar[0]);
+        Registar[1] = 1234;
+        wait_ms(500);
+        Registar[1] = 5678;
+        wait_ms(500);
+    }
+}
+
+**********************************************************/
+
 #if DEVICE_I2CSLAVE
 
 namespace mbed {
 
 i2cslave* instance;
 
-i2cslave::i2cslave(PinName sda, PinName scl, char *_Registar) : I2CSlave(sda, scl)
+i2cslave::i2cslave(PinName sda, PinName scl, int32_t *_Registar) : I2CSlave(sda, scl)
 {
     frequency(400000);
     i2cslave::attach(this, &i2cslave::getdata);
@@ -22,40 +46,31 @@
         char reg=DATA[0];
         char num =DATA[1]; 
         char X[num];
-        char f=0;
+        for(int i = 0; i < num; i++)X[i] = 0;
+        int32_t tmp = 0;
         wait_us(50);
         switch(I2CSlave::receive())
         {
             case 0 :break;
             case I2CSlave::ReadAddressed:
             {
-                char *po = Registar+reg;
-                for(int i=0;i<num;i++)
-                    X[i]=*po+i;
+                int32_t tmp = *(Registar+reg);
+                for(int i = 0; i < num; i++)
+                {
+                    X[i] = (tmp >> i*8) & 0xFF;
+                }
                 I2CSlave::write(X,num);
-                f=1;
-                do
-                {
-
-                    //f = I2CSlave::write(Registar[reg]);
-                    f = I2CSlave::write(*(Registar+reg));
-                    reg++;
-                }while(f==1);
                 break;
             }
-            case I2CSlave::WriteGeneral:{break;}
+            case I2CSlave::WriteGeneral:break;
             case I2CSlave::WriteAddressed:
             {
-                char num = DATA[1];
-                for(int i=1; i<num; i++,reg++)
-                    char X[num];
-                    I2CSlave::read(X,num);
-                    for (int i=0;i<num;i++)
-                    {
-                        //Registar[reg]=X[i];
-                        *(Registar+reg) = X[i];
-                        reg++;
-                    }
+                I2CSlave::read(X, num);
+                for (int i=0; i < num; i++)
+                {
+                    tmp |= X[i] << (i*8);
+                }
+                *(Registar+reg) = tmp;
                 break;
             }
         }
--- a/i2cslave.h	Thu Jul 12 08:09:43 2018 +0000
+++ b/i2cslave.h	Thu Jul 12 10:17:19 2018 +0000
@@ -5,29 +5,11 @@
 
 namespace mbed {
     
-    /*example****************************
-    
-    *   #include "mbed.h"
-    *   #include "i2cslave.h"
-
-    *   char Registar[0x80];
-    *   i2cslave i2c(dp5, dp27, Registar);
-    
-    *   int main()
-    *   {
-    *       i2c.address(0xa0);
-    *       while(1)
-    *       {
-    *           printf("%d\r\n", Registar[1]);
-    *       }
-    *  }
-    *************************************/
-    
 class i2cslave : public I2CSlave
 {
 public:
-    char *Registar;
-    i2cslave(PinName sda, PinName scl, char *Registar);
+    int32_t *Registar;
+    i2cslave(PinName sda, PinName scl, int32_t *Registar);
      
      void getdata();