i2cslave class without interrupt process

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Sun Feb 26 07:04:56 2017 +0000
Commit message:
i2cslave class without interrupt process 2017/2/26

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 000000000000 -r 2976149f5c28 i2cslave.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2cslave.cpp	Sun Feb 26 07:04:56 2017 +0000
@@ -0,0 +1,39 @@
+#include "i2cslave.h"
+
+i2cslave::i2cslave(PinName sda, PinName scl, char *Registar) : I2CSlave(sda, scl)
+{
+    _Registar = Registar;
+    frequency(400000);
+}
+
+void i2cslave::communication()
+{
+    switch(receive())
+    {
+        case I2CSlave::NoData:            //データなし
+            break;
+        case I2CSlave::ReadAddressed:     //マスターからのデータ送信要求
+            break;
+        case I2CSlave::WriteAddressed:    //マスターからデータを受信
+            geti2c();
+            break;
+        case I2CSlave::WriteGeneral:      //マスターからデータを受信(全スレーブ宛)
+            break;
+    }
+}
+void i2cslave::geti2c()
+{
+    char i2cData[3] = {};
+    read(i2cData, 3);
+    wait_us(30);
+    switch(i2cData[2])
+    {
+        case SendData:
+            read(_Registar + i2cData[1], i2cData[0]);
+            break;
+        case RequestData:
+            write(_Registar + i2cData[1], i2cData[0]);
+            break;
+    }
+}
+
diff -r 000000000000 -r 2976149f5c28 i2cslave.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/i2cslave.h	Sun Feb 26 07:04:56 2017 +0000
@@ -0,0 +1,51 @@
+#ifndef MBED_I2CSLAVE_H
+#define MBED_I2CSLAVE_H
+
+#include "mbed.h"
+
+#define SendData        0
+#define RequestData     1
+
+class i2cslave : public I2CSlave
+{
+    public:
+    char *_Registar;
+    i2cslave(PinName sda, PinName scl, char *Registar);
+    /*{
+        _Registar = Registar;
+        frequency(400000);
+    }*/
+    
+    void communication();
+    /*{
+        switch(receive())
+        {
+            case I2CSlave::NoData:            //データなし
+                break;
+            case I2CSlave::ReadAddressed:     //マスターからのデータ送信要求
+                break;
+            case I2CSlave::WriteAddressed:    //マスターからデータを受信
+                geti2c();
+                break;
+            case I2CSlave::WriteGeneral:      //マスターからデータを受信(全スレーブ宛)
+                break;
+        }
+    }*/
+    void geti2c();
+    /*{
+        char i2cData[3] = {};
+        read(i2cData, 3);
+        wait_us(30);
+        switch(i2cData[2])
+        {
+            case SendData:
+                read(_Registar + i2cData[1], i2cData[0]);
+                break;
+            case RequestData:
+                write(_Registar + i2cData[1], i2cData[0]);
+                break;
+        }
+    }*/
+};
+ 
+#endif