Add libusb support and lots more examples
[fw/altos] / cccp.c
diff --git a/cccp.c b/cccp.c
index aff75e85e8c4b543a995e75709984275c0759528..99a0d81f1a11a895d470d558d7fdfe6caa8ac585 100644 (file)
--- a/cccp.c
+++ b/cccp.c
 
 #include "ccdbg.h"
 
-void
-cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
+static void
+say(char *name, uint8_t bits)
+{
+       printf("%s: ", name);
+       if (bits & CC_RESET_N)
+               printf ("R ");
+       else
+               printf (". ");
+       if (bits & CC_CLOCK)
+               printf ("C ");
+       else
+               printf (". ");
+       if (bits & CC_DATA)
+               printf ("D\n");
+       else
+               printf (".\n");
+}
+
+static void
+_cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
 {
        uint16_t        set;
        int             ret;
@@ -31,23 +49,38 @@ cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
                perror("CP2101_IOCTL_GPIOSET");
 }
 
+void
+cccp_write(struct ccdbg *dbg, uint8_t mask, uint8_t value)
+{
+       _cccp_write(dbg, mask, value);
+//     say("w", dbg->debug_data);
+}
+
+uint8_t
+cccp_read_all(struct ccdbg *dbg)
+{
+       int ret;
+       uint8_t get;
+       ret = ioctl(dbg->fd, CP2101_IOCTL_GPIOGET, &get);
+       if (ret < 0) {
+               perror("CP2101_IOCTL_GPIOGET");
+               get = 0;
+       }
+       return get;
+}
+
 uint8_t
 cccp_read(struct ccdbg *dbg, uint8_t mask)
 {
        uint8_t         pull_up;
-       int             ret;
        uint8_t         get;
 
        /* tri-state the bits of interest */
        pull_up = (~dbg->debug_data) & mask;
-       if (pull_up) {
-               cccp_write(dbg, pull_up, pull_up);
-       }
-       ret = ioctl(dbg->fd, CP2101_IOCTL_GPIOGET, &get);
-       if (ret < 0) {
-               perror("CP2101_IOCTL_GPIOGET");
-               get = 0;
-       }
+       if (pull_up)
+               _cccp_write(dbg, pull_up, pull_up);
+       get = cccp_read_all(dbg);
+       say("\t\tr", get);
        return get & mask;
 }
 
@@ -56,7 +89,6 @@ cccp_init(struct ccdbg *dbg)
 {
        /* set all of the GPIOs to a known state */
        cccp_write(dbg, 0xf, 0xf);
-       dbg->clock = 1;
 }
 
 void