]> oss.titaniummirror.com Git - cp210x.git/blobdiff - src/examples/cpmfg.c
New features for cpmfg and cpio.
[cp210x.git] / src / examples / cpmfg.c
index 832e75b356e6f9f86805f9ca9aa1ca480777b15c..e8b2efbf04a433e411b06f1cf171d3b2fc441ce2 100644 (file)
@@ -95,7 +95,7 @@ void cpSetStr(int fd, unsigned int ioctlno, char* string, size_t len)
     }
 }
 
-void cpSetLeds(int fd)
+void cpSetPortConf(int fd, int pullups)
 {
     int ret;
     cp2101_port_config_t config;
@@ -106,15 +106,21 @@ void cpSetLeds(int fd)
        exit(1);
     }
 
-    /* Set the current port configuration; turn on GPIO_0 and GPIO_1 to get
-     * activity LEDs.  GPIO_2 and GPIO_3 are set for 'regular' gpio.
+    /* 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.  Some boards may
+     * require weak pull-ups, but not 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;
-    //config.enhancedFxn &= ~0x10; /* turn off weak pullups */
-    config.enhancedFxn |= 0x10; /* turn on weak pullups */
+    config.reset.mode |= 0x0800; /* GPIO_3 gets push-pull driver */
+    config.enhancedFxn |= (pullups) ? 0x13 : 0x03;
+
     if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &config))) {
        fprintf(stderr, "portconfset ioctl failed %d\n", ret);
        exit(1);
@@ -132,30 +138,47 @@ void cpReset(int fd)
     }
 }
 
+void usage(char* program)
+{
+    fprintf(stderr, "usage: %s [--pullups] <tty> <mfg> <part#> <sn#>\n",
+       program);
+}
+
 int main(int argc, char* argv[])
 {
     /* char newmfg[255]; */
     char newprod[255];
     char newsn[255];
     int fd;
+    int pullups = 0;
+
+    if (argc < 5 || argc > 6) {
+       usage(argv[0]);
+       exit(1);
+    }
 
-    if (argc != 5) {
-      fprintf(stderr, "usage: %s <tty> <mfg> <part#> <sn#>\n", argv[0]);
-      exit(1);
+    if (argc == 6) {
+       if (strncmp(argv[1], "--p", 3) == 0)
+           pullups = 1;
+       else {
+           usage(argv[0]);
+           exit(1);
+       }
     }
 
-    /* strcpy(newmfg, argv[2]);
-     * strcpy(newprod, argv[3]);
+    /* strcpy(newmfg, argv[pullups + 2]);
+     * strcpy(newprod, argv[pullups + 3]);
      */
-    snprintf(newprod, sizeof(newprod), "%s %s", argv[2], argv[3]);
-    strcpy(newsn, argv[4]);
-    fd = cpConnect(argv[1]);
+    snprintf(newprod, sizeof(newprod), "%s %s", argv[pullups + 2],
+           argv[pullups + 3]);
+    strcpy(newsn, argv[pullups + 4]);
+    fd = cpConnect(argv[pullups + 1]);
     /* SiLabs doesn't allow set of mfg string on cp210x.
      * cpSetStr(fd, IOCTL_SETMFG, newmfg, strlen(newmfg));
      */
     cpSetStr(fd, IOCTL_SETPRODUCT, newprod, strlen(newprod));
     cpSetStr(fd, IOCTL_SETSERIAL, newsn, strlen(newsn));
-    cpSetLeds(fd);
+    cpSetPortConf(fd, pullups);
     cpReset(fd);
     cpDisconnect(fd);
     printf("done\n");