X-Git-Url: https://oss.titaniummirror.com/gitweb?p=cp210x.git;a=blobdiff_plain;f=src%2Fexamples%2Fcpio.c;h=b08ee23f7b4b27f16d231cb4cc7aeaa02b21f892;hp=a4f2f7ad24868f1dd1dd759f5aeb89427dba0cb9;hb=31344b807962b968af815c4708f0edadfc657d54;hpb=f7c370bb62213bde2d52ec4816401fb3778773a5 diff --git a/src/examples/cpio.c b/src/examples/cpio.c index a4f2f7a..b08ee23 100644 --- a/src/examples/cpio.c +++ b/src/examples/cpio.c @@ -1,8 +1,7 @@ /** * 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 @@ -12,31 +11,34 @@ #include #include -#define IOCTL_DEVICERESET 0x8004 /* Get port configuration */ +#define IOCTL_DEVICERESET 0x8004 /* Reset the cp210x */ #define IOCTL_PORTCONFGET 0x8005 /* Get port configuration */ #define IOCTL_PORTCONFSET 0x8006 /* Set port configuration */ +/* Port config definitions */ typedef struct { - uint16_t mode; + uint16_t mode; /* Push-pull = 1, Open-drain = 0 */ uint16_t lowPower; - uint16_t latch; -} cp2103_port_state_t; + uint16_t latch; /* Logic high = 1, Logic low = 0 */ +} cp2101_port_state_t; typedef struct { - cp2103_port_state_t reset; - cp2103_port_state_t suspend; + cp2101_port_state_t reset; + cp2101_port_state_t suspend; uint8_t enhancedFxn; -} cp2103_port_config_t; +} cp2101_port_config_t; + +void exit(int); 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 +48,78 @@ void cpDisconnect(int fd) close(fd); } -int main(int argc, char* argv[]) +void cpSetPortConf(int fd, int pullups) { - int fd; - cp2103_port_config_t config; int ret; - - if (argc != 2) { - fprintf(stderr, "usage: %s \n", argv[0]); - return 1; - } - - /* open */ - if ((fd = cpConnect(argv[1])) < 0) - return 1; + cp2101_port_config_t config; /* Read the current port configuration */ if ((ret = ioctl(fd, 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. 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.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); - return 1; + exit(1); } +} + +void cpReset(int fd) +{ + int ret; - /* Reset the part so the changes take effect. */ + /* Reset the part */ if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) { fprintf(stderr, "device reset ioctl %d\n", ret); - return 1; + exit(1); + } +} + +void usage(char* program) +{ + fprintf(stderr, "usage: %s [--pullups] \n", + program); +} + +int main(int argc, char* argv[]) +{ + int fd; + int pullups = 0; + + if (argc < 2 || argc > 3) { + usage(argv[0]); + exit(1); + } + + if (argc == 3) { + if (strncmp(argv[1], "--p", 3) == 0) + pullups = 1; + else { + usage(argv[0]); + exit(1); + } } + fd = cpConnect(argv[pullups + 1]); + cpSetPortConf(fd, pullups); + cpReset(fd); cpDisconnect(fd); + printf("done\n"); return 0; }