Basis for the C2 protocol from Silicon Labs.

Dependencies:   mbed

Revision:
14:c24f608a8adb
Parent:
13:45aaa8847c2b
Child:
15:0ccb0277d98e
--- a/main.cpp	Sun May 25 17:54:37 2014 +0000
+++ b/main.cpp	Mon May 26 17:36:09 2014 +0000
@@ -27,10 +27,14 @@
  
 #include "mbed.h"
 #include <stdarg.h>
+#include <stdio.h>
+#include <stdint.h>
 
 DigitalOut c2ck(p5);
 DigitalInOut c2d(p6);
 
+static uint8_t buffer[256];
+
 static struct devices {
     char *name;
     int devid;
@@ -95,7 +99,7 @@
 static void fatal(char *f, ...) {
     va_list ap;
     va_start(ap,f);
-    printf("\n\rFATAL: ");
+    printf("\r\nFATAL: ");
     vprintf(f, ap);
     va_end(ap);
     exit(1);
@@ -206,11 +210,44 @@
 static void c2_enable_pi(void) {
     c2ck_reset();
     c2_write_ar(C2_REG_FPCTL);
-    if ((c2_write_dr(0x02)+c2_write_dr(0x04)+c2_write_dr(0x02))<0)
+    if ((c2_write_dr(0x02)+c2_write_dr(0x04)+c2_write_dr(0x01))<0)
         fatal("cannot enable PI");
     wait_us(21);
 }
 
+static void c2_read_mem(int type, uint16_t address, uint8_t size, uint8_t *buf) {
+    int ret, i, ram_or_sfr = (type == C2_PI_CMD_DIRECT_READ) || (type == C2_PI_CMD_INDIRECT_READ);
+
+    c2_write_ar(C2_REG_FPDAT);
+    c2_write_dr(type);
+
+    if (poll_status(C2_MASK_INBUSY) < 0 || poll_status(C2_MASK_OUTREADY) < 0)
+        fatal("read_mem: cannot set FPDAT");
+
+    ret = c2_read_dr();
+    if (ret != C2_PI_STATUS_OK)
+        fatal("read_mem: status not OK after setting FPDAT (status %02X)", ret);
+
+    if (ram_or_sfr) {
+        c2_write_dr((address >> 8)&0xff);
+        if (poll_status(C2_MASK_INBUSY) < 0)
+            fatal("read_mem: cannot write msb of address");
+    }
+
+    c2_write_dr(address & 0xff);
+    if (poll_status(C2_MASK_INBUSY) < 0)
+        fatal("read_mem: cannot write lsb of address");
+
+    c2_write_dr(size);
+    if (poll_status(C2_MASK_INBUSY) < 0)
+        fatal("read_mem: cannot set block length");
+    if (poll_status(C2_MASK_OUTREADY) < 0)
+        fatal("read_mem: no data ready");
+
+    for (i=0; i<=size; i++)
+        buf[i] = c2_read_dr();
+}
+
 int main() {
     int i, c, devid, revid;
     
@@ -218,35 +255,39 @@
     c2ck = 1;
 
 //    printf("\033[H\033[J");
-    printf("\n\rSiLabs C2 Protocol\n\r\n\r");
-    printf("Connect C2GND to GND, C2CK (clock) to p5 and C2D (data) to p6\n\r\n\r");
-    printf("Press any key to continue\n\r");
+    printf("\r\nSiLabs C2 Protocol\r\n\r\n");
+    printf("Connect C2GND to GND, C2CK (clock) to p5 and C2D (data) to p6\r\n\r\n");
+    printf("Press any key to continue\r\n");
 
     getc(stdin);
 
     c2_write_ar(C2_REG_DEVID);
     devid = c2_read_dr();
-    if (devid <= 0) fatal("unable to read devid\n\r");
+    if (devid <= 0) fatal("unable to read devid\r\n");
     
     c2_write_ar(C2_REG_REVID);
     revid = c2_read_dr();
-    if (revid < 0) fatal("unable to read revid\n\r");
+    if (revid < 0) fatal("unable to read revid\r\n");
 
     for (i=0; devices[i].name; i++) {
         if (devices[i].devid == devid) break;
     }
     
-    if (!i) fatal("unknown device: %02X:%02X\n\r", devid, revid);
+    if (!i) fatal("unknown device: %02X:%02X\r\n", devid, revid);
 
     switch(devid) {
         case 0x0f: case 0x28: case 0x18: case 0x19: C2_REG_FPDAT = 0xad;
         default: break;
     }
 
-    printf("\n\rDevice found: %s (%02X:%02X)", devices[i].name, devid, revid);
-    printf("\n\rFPDAT Address: 0x%02X", C2_REG_FPDAT);
-    printf("\n\rPage Size: %i\n\r", page_size);
+    printf("\r\nDevice found: %s (%02X:%02X)", devices[i].name, devid, revid);
+    printf("\r\nFPDAT Address: 0x%02X", C2_REG_FPDAT);
+    printf("\r\nPage Size: %i\n\r", page_size);
     
-    printf("\n\rEnabling Programming Interface\n\r");
+    printf("\r\nEnabling Programming Interface\r\n");
     c2_enable_pi();
+    
+    printf("Reading page 0\r\n");
+    c2_read_mem(C2_PI_CMD_DIRECT_READ, 0x0000, 0xff, buffer);
+    printf("Looks like all went well!!!");
 }
\ No newline at end of file