i2c slave class to RaspberryPi with interrupt

Fork of i2cslave by hidaka sato

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Sun Jul 15 14:20:26 2018 +0000
Parent:
2:0dff052266ca
Commit message:
i2c slave class to RaspberryPi with interrupt

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
diff -r 0dff052266ca -r 5d19d3a231b3 i2cslave.cpp
--- a/i2cslave.cpp	Thu Jul 12 10:17:19 2018 +0000
+++ b/i2cslave.cpp	Sun Jul 15 14:20:26 2018 +0000
@@ -1,27 +1,28 @@
 #include "i2cslave.h"
 
 /*example*************************************************
-
 #include "mbed.h"
 #include "i2cslave.h"
 
-int32_t Registar[0x20];
-i2cslave i2c(p28, p27, Registar);
-    
+#define ADDR 0x1D
+#define _ADDR ADDR<<1   //on raspi
+
+union U{
+    int8_t _Registar[0x40];
+    int32_t Registar[0x10];
+};
+union U u = { 0 };
+i2cslave slave(p28, p27, u._Registar);
+Serial PC(USBTX, USBRX);
+
 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);
+    slave.address(_ADDR);
+    while(1){
+        PC.printf("%d, %d\r\n", u.Registar[0], u.Registar[1]);
+        wait_ms(100);
     }
 }
-
 **********************************************************/
 
 #if DEVICE_I2CSLAVE
@@ -30,7 +31,7 @@
 
 i2cslave* instance;
 
-i2cslave::i2cslave(PinName sda, PinName scl, int32_t *_Registar) : I2CSlave(sda, scl)
+i2cslave::i2cslave(PinName sda, PinName scl, int8_t *_Registar) : I2CSlave(sda, scl)
 {
     frequency(400000);
     i2cslave::attach(this, &i2cslave::getdata);
@@ -41,38 +42,34 @@
 {
     if(I2CSlave::receive()==I2CSlave::WriteAddressed)
     {
-        char DATA[2] = {}; 
-        I2CSlave::read(DATA,2);
+        char DATA[3] = {}; 
+        I2CSlave::read(DATA, 3);
         char reg=DATA[0];
-        char num =DATA[1]; 
-        char X[num];
-        for(int i = 0; i < num; i++)X[i] = 0;
-        int32_t tmp = 0;
-        wait_us(50);
-        switch(I2CSlave::receive())
+        char size =DATA[1];
+        char cmd = DATA[2];
+        char X[size];
+        for(int i = 0; i < size; i++)X[i] = 0;
+        int8_t tmp = 0;
+        //wait_us(50);  //to mbed devices
+        wait_us(500);   //to RaspberryPi
+        switch(cmd)
         {
-            case 0 :break;
-            case I2CSlave::ReadAddressed:
-            {
-                int32_t tmp = *(Registar+reg);
-                for(int i = 0; i < num; i++)
+            case ReadRequired:
+                I2CSlave::read(X, size);
+                for(int i = 0; i < size; i++)
                 {
-                    X[i] = (tmp >> i*8) & 0xFF;
+                    *(Registar+reg*4+i) = X[i];
                 }
-                I2CSlave::write(X,num);
                 break;
-            }
-            case I2CSlave::WriteGeneral:break;
-            case I2CSlave::WriteAddressed:
-            {
-                I2CSlave::read(X, num);
-                for (int i=0; i < num; i++)
-                {
-                    tmp |= X[i] << (i*8);
-                }
-                *(Registar+reg) = tmp;
+
+            case WriteRequired:
+                I2CSlave::read(DATA, 1);    //捨て受信
+                
+                int8_t tmp = *(Registar+reg);
+                I2CSlave::write(tmp);
+                I2CSlave::write(tmp);
+                I2CSlave::write(tmp);
                 break;
-            }
         }
     }
 }
diff -r 0dff052266ca -r 5d19d3a231b3 i2cslave.h
--- a/i2cslave.h	Thu Jul 12 10:17:19 2018 +0000
+++ b/i2cslave.h	Sun Jul 15 14:20:26 2018 +0000
@@ -3,13 +3,17 @@
 
 #include "mbed.h"
 
+#define ReadRequired 1
+#define WriteRequired 2
+
 namespace mbed {
     
 class i2cslave : public I2CSlave
 {
 public:
-    int32_t *Registar;
-    i2cslave(PinName sda, PinName scl, int32_t *Registar);
+    int8_t *Registar;
+    int log[10];
+    i2cslave(PinName sda, PinName scl, int8_t *Registar);
      
      void getdata();