]> oss.titaniummirror.com Git - cp210x.git/blobdiff - src/examples/cpio.c
More generic unicode support, no redefinitions of USB headers, no USBSTRLEN.
[cp210x.git] / src / examples / cpio.c
index a4f2f7ad24868f1dd1dd759f5aeb89427dba0cb9..b62afb0b2b4145e3d5832ccc36d870580e325da3 100644 (file)
@@ -1,42 +1,28 @@
 /**
  * cpio.c
  *
- * Set cp2103 GPIO_0 and GPIO_1 for LED connection to get RX/TX activity.
- * Watch out, the tty is hardcoded.
+ * Configure the and GPIO configuration for a cp2103-equipped device.
  */
 
+#include <fcntl.h>
 #include <netinet/in.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.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;
+#include <unistd.h>
+#include <cp210x.h>
 
 int cpConnect(char* device)
 {
-    int ret, ioval;
     int fd = open(device, O_RDWR);
     if (fd < 0) {
-       fprintf(stderr, "cannot open tty\n");
-       return -1;
+       fprintf(stderr, "cannot open %s\n", device);
+       exit(1);
     }
-    printf("tty opened\n");
+    printf("%s opened\n", device);
     return fd;
 }
 
@@ -46,45 +32,79 @@ void cpDisconnect(int fd)
        close(fd);
 }
 
-int main(int argc, char* argv[])
+void cpSetPortConf(int fd, int nopullups)
 {
-    int fd;
-    cp2103_port_config_t config;
     int ret;
-
-    if (argc != 2) {
-       fprintf(stderr, "usage: %s <tty>\n", argv[0]);
-       return 1;
-    }
-
-    /* open */
-    if ((fd = cpConnect(argv[1])) < 0)
-       return 1;
+    struct cp210x_port_config config;
 
     /* Read the current port configuration */
-    if ((ret = ioctl(fd, IOCTL_PORTCONFGET, &config))) {
+    if ((ret = ioctl(fd, CP210x_IOCTL_PORTCONFGET, &config))) {
        fprintf(stderr, "portconfget ioctl failed %d\n", ret);
-       return 1;
+       exit(1);
     }
 
-    /* Set the current port configuration; turn on GPIO_0 and GPIO_1 to get
-     * activity LEDs.
+    /* This section assumes the device is currently at factory defaults.  If
+     * not, first run 'cpfactory'.  The only delta from factory defaults is to
+     * activate the activity pin functions for GPIOs 0 and 1.  Most boards
+     * require the use of weak pull-ups, including TMI's RWS.
+     *
+     * GPIO mapping:
+     *   GPIO_0 -> TX activity, active low
+     *   GPIO_1 -> RX activity, active low
+     *   GPIO_2 -> MSP430 UC_TCK
+     *   GPIO_3 -> MSP430 UC_RST
+     *
      */
-    config.reset.mode &= ~0x0300;
-    config.suspend.mode &= ~0x0300;
-    config.reset.latch |= 0x0300;
-    config.enhancedFxn &= ~0x03;
-    if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &config))) {
+    config.enhancedFxn |= 0x13;
+    if (nopullups)
+       config.enhancedFxn &= ~0x10;
+
+    if ((ret = ioctl(fd, CP210x_IOCTL_PORTCONFSET, &config))) {
        fprintf(stderr, "portconfset ioctl failed %d\n", ret);
-       return 1;
+       exit(1);
     }
+}
 
-    /* Reset the part so the changes take effect. */
-    if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) {
+void cpReset(int fd)
+{
+    int ret;
+
+    /* Reset the part */
+    if ((ret = ioctl(fd, CP210x_IOCTL_DEVICERESET, 0))) {
        fprintf(stderr, "device reset ioctl %d\n", ret);
-       return 1;
+       exit(1);
+    }
+}
+
+void usage(char* program)
+{
+    fprintf(stderr, "usage: %s [--nopullups] <tty> <mfg> <part#> <sn#>\n",
+       program);
+}
+
+int main(int argc, char* argv[])
+{
+    int fd;
+    int nopullups = 0;
+
+    if (argc < 2 || argc > 3) {
+       usage(argv[0]);
+       exit(1);
+    }
+
+    if (argc == 3) {
+       if (strncmp(argv[1], "--n", 3) == 0)
+           nopullups = 1;
+       else {
+           usage(argv[0]);
+           exit(1);
+       }
     }
 
+    fd = cpConnect(argv[nopullups + 1]);
+    cpSetPortConf(fd, nopullups);
+    cpReset(fd);
     cpDisconnect(fd);
+    printf("done\n");
     return 0;
 }