]> oss.titaniummirror.com Git - cp210x.git/blobdiff - examples/cpio.c
This is the version of the driver modified by TMI to support setting of the
[cp210x.git] / examples / cpio.c
diff --git a/examples/cpio.c b/examples/cpio.c
new file mode 100644 (file)
index 0000000..f0f6f38
--- /dev/null
@@ -0,0 +1,85 @@
+/**
+ * cpio.c
+ *
+ * Set cp2103 GPIO_0 and GPIO_1 for LED connection to get RX/TX activity.
+ * Watch out, the tty is hardcoded.
+ */
+
+#include <netinet/in.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define IOCTL_DEVICERESET      0x8004  /* Get port configuration */
+#define IOCTL_PORTCONFGET      0x8005  /* Get port configuration */
+#define IOCTL_PORTCONFSET      0x8006  /* Set port configuration */
+
+typedef struct {
+    uint16_t mode;
+    uint16_t lowPower;
+    uint16_t latch;
+} cp2103_port_state_t;
+
+typedef struct {
+    cp2103_port_state_t reset;
+    cp2103_port_state_t suspend;
+    uint8_t enhancedFxn;
+} cp2103_port_config_t;
+
+int cpConnect()
+{
+    int ret, ioval;
+    int fd = open("/dev/usb/tts/0", O_RDWR);
+    if (fd < 0) {
+       fprintf(stderr, "cannot open tty\n");
+       return -1;
+    }
+    printf("tty opened\n");
+    return fd;
+}
+
+void cpDisconnect(int fd)
+{
+    if (fd >= 0)
+       close(fd);
+}
+
+int main()
+{
+    int fd;
+    cp2103_port_config_t config;
+    int ret;
+
+    /* open */
+    if ((fd = cpConnect()) < 0)
+       return 1;
+
+    /* Read the current port configuration */
+    if ((ret = ioctl(fd, IOCTL_PORTCONFGET, &config))) {
+       fprintf(stderr, "portconfget ioctl failed %d\n", ret);
+       return 1;
+    }
+
+    /* Set the current port configuration; turn on GPIO_0 and GPIO_1 to get
+     * activity LEDs.
+     */
+    config.reset.mode &= ~0x0300;
+    config.suspend.mode &= ~0x0300;
+    config.reset.latch |= 0x0300;
+    config.enhancedFxn &= ~0x03;
+    if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &config))) {
+       fprintf(stderr, "portconfset ioctl failed %d\n", ret);
+       return 1;
+    }
+
+    /* Reset the part so the changes take effect. */
+    if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) {
+       fprintf(stderr, "device reset ioctl %d\n", ret);
+       return 1;
+    }
+
+    cpDisconnect(fd);
+    return 0;
+}