i2c slave class to RaspberryPi with interrupt

Fork of i2cslave by hidaka sato

Revision:
0:69b088a9899b
Child:
1:cd92200b8a50
diff -r 000000000000 -r 69b088a9899b i2cslave.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2cslave.cpp	Sat Apr 16 04:25:19 2016 +0000
@@ -0,0 +1,85 @@
+#include "i2cslave.h"
+
+#if DEVICE_I2CSLAVE
+
+namespace mbed {
+
+i2cslave* instance;
+
+i2cslave::i2cslave(PinName sda, PinName scl, char *_Registar) : I2CSlave(sda, scl)
+{
+    frequency(400000);
+    i2cslave::attach(this, &i2cslave::getdata);
+    Registar = _Registar;
+}
+
+void i2cslave::getdata( void )
+{
+    if(I2CSlave::receive()==I2CSlave::WriteAddressed)
+    {
+        char DATA[2] = {}; 
+        I2CSlave::read(DATA,2);
+        char reg=DATA[0];
+        char num =DATA[1]; 
+        char X[num];
+        char f=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;
+                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::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++;
+                    }
+                break;
+            }
+        }
+    }
+}
+void i2cslave::attach(void (*fptr)(void)) {
+    fp.attach(fptr);
+    enable_attach();
+}
+
+void i2cslave::func_call(uint32_t id, uint8_t addr, uint8_t state) {
+    instance->fp.call();
+}
+
+void i2cslave::enable_attach() {
+    instance = this;
+    NVIC_SetVector(I2C_IRQn, (uint32_t)(&i2cslave::func_call));
+    NVIC_EnableIRQ(I2C_IRQn);    
+}
+
+void i2cslave::disable_attach() {
+    NVIC_DisableIRQ(I2C_IRQn);    
+}
+
+}
+
+#endif