Basis for the C2 protocol from Silicon Labs.

Dependencies:   mbed

Revision:
2:9092d0d1558b
Parent:
1:7a82f806fe92
Child:
3:b30605f1c435
--- a/main.cpp	Sat May 24 11:54:33 2014 +0000
+++ b/main.cpp	Sat May 24 12:34:13 2014 +0000
@@ -50,7 +50,97 @@
 // 01b  Data Write
 // 10b  Address Read
 // 11b  Address Write
+//
+// Remember, ALL fields are sent LSB first(!)
 
+#define START       do{ c2ck_strobe(); }while(0)
+#define STOP        START
+#define INS(a,b)    do{ c2d = a; c2ck_strobe(); c2d = b; c2ck_strobe(); }while(0)
+#define LENGTH(a,b) INS(a,b)
+#define WAIT        do{\
+        while(t-- && !c2d) {    \
+            c2ck_strobe();      \
+            wait_us(1);         \
+        } if (!t) return -1;    \
+    } while(0)
+
+static int c2_read_dr(void) {
+    int t = 20, i, d = 0;
+    
+    START;
+    c2d.output();
+    INS(0,0);
+    LENGTH(0,0);    
+    c2d.input();
+    WAIT;    
+
+    for (i=0; i<8; i++) {   // DATA
+        d >>= 1;
+        c2ck_strobe();
+        if (c2d) d |= 0x80;
+    }
+    
+    STOP;
+    
+    return d;
+}
+
+static int c2_write_dr(int d) {
+    int t = 20, i;
+    
+    START;
+    c2d.output();
+    INS(1,0);
+    LENGTH(0,0);
+
+    for(i=0; i<8; i++) {
+        c2d = d & 1;
+        c2ck_strobe();
+        d >>= 1;
+    }
+
+    c2d.input();
+    WAIT;
+    STOP;
+    
+    return 0;
+}
+
+static int c2_read_ar(void) {
+    int i, a = 0;
+    
+    START;
+    c2d.output();
+    INS(0,1);
+    c2d.input();
+
+    for(i=0; i<8; i++) {        // ADDRESS
+        a >>= 1;
+        c2ck_strobe();
+        if (c2d) a |= 0x80;
+    }
+    
+    STOP;
+    
+    return a;
+}
+
+static void c2_write_ar(int a) {
+    int i;
+
+    START;
+    c2d.output();
+    INS(1,1);
+
+    for(i=0; i<8; i++) {        // ADDRESS
+        c2d = a & 1;
+        c2ck_strobe();
+        a >>= 1;
+    }
+    
+    c2d.input();
+    STOP;
+}
 
 int main() {
     int c;