From 6000a45218d62d5e28a5b6c5150bdd2b84b84a01 Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Mon, 17 May 2010 15:05:09 -0600 Subject: [PATCH] All example programs updated to fit new cp210x.h. --- src/examples/Makefile | 3 +- src/examples/cpfactory.c | 93 ++++------------------------------------ src/examples/cpio.c | 34 ++++----------- src/examples/cpmfg.c | 4 +- src/examples/cpread.c | 69 +++++------------------------ src/examples/cptwiddle.c | 59 ++++++------------------- 6 files changed, 45 insertions(+), 217 deletions(-) diff --git a/src/examples/Makefile b/src/examples/Makefile index 0689cd7..e0d3687 100644 --- a/src/examples/Makefile +++ b/src/examples/Makefile @@ -1,6 +1,7 @@ .PHONY: all PROGS = cpmfg cpread cpio cptwiddle cpfactory +CFLAGS += -I../ # LDFLAGS += -lusb all: $(PROGS) @@ -8,4 +9,4 @@ all: $(PROGS) $(PROGS): %: %.o clean: - @rm -rf $(PROGS) $(PROGS:%=%.o) + rm -rf $(PROGS) $(PROGS:%=%.o) diff --git a/src/examples/cpfactory.c b/src/examples/cpfactory.c index a3fc0b4..ea16ac6 100644 --- a/src/examples/cpfactory.c +++ b/src/examples/cpfactory.c @@ -4,48 +4,19 @@ * Reset the cp2103 IO registers back to factory defaults. */ +#include +#include #include #include #include #include #include #include -#include - -/* CP2103 ioctls */ -#define IOCTL_GPIOGET 0x8000 /* Get gpio bits */ -#define IOCTL_GPIOSET 0x8001 /* Set gpio bits */ -#define IOCTL_GPIOBIC 0x8002 /* Clear specific gpio bit(s) */ -#define IOCTL_GPIOBIS 0x8003 /* Set specific gpio bit(s) */ - -/* CP210x ioctls principally used during initial device configuration */ -#define IOCTL_DEVICERESET 0x8004 /* Reset the cp210x */ -#define IOCTL_PORTCONFGET 0x8005 /* Get port configuration */ -#define IOCTL_PORTCONFSET 0x8006 /* Set port configuration */ -#define IOCTL_SETVID 0x8007 /* Set vendor id */ -#define IOCTL_SETPID 0x8008 /* Set product id */ -#define IOCTL_SETMFG 0x8009 /* Set manufacturer string */ -#define IOCTL_SETPRODUCT 0x800a /* Set product string */ -#define IOCTL_SETSERIAL 0x800b /* Set serial number string */ -#define IOCTL_SETDEVVER 0x800c /* set device version id */ -/* FIXME: where is IOCTL_SETMFG? */ - - -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 +#include int cpConnect(char* device) { - int ret, ioval; int fd = open(device, O_RDWR); if (fd < 0) { fprintf(stderr, "cannot open tty\n"); @@ -64,11 +35,8 @@ void cpDisconnect(int fd) int main(int argc, char* argv[]) { int fd; - cp2103_port_config_t config; - cp2103_port_config_t saveConfig; - uint8_t saveGpio; + struct cp210x_port_config config; int ret; - int i; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); @@ -80,71 +48,26 @@ int main(int argc, char* argv[]) return 1; /* 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; } printf("port config received\n"); - /* Mode and latch bit to pin assignments. See AN223 from SiLabs: - * https://www.silabs.com/Support Documents/TechnicalDocs/an223.pdf - * - * 15 - /SUSPEND Can't configure latch - * 14 - SUSPEND Can't configure latch - * 13 - unused - * 12 - unused - * 11 - GPIO_3 - * 10 - GPIO_2 - * 9 - GPIO_1 - * 8 - GPIO_0 - * 7 - /CTS - * 6 - /RTS - * 5 - RXD Note: set latch value to '1' for proper operation. - * 4 - TXD - * 3 - /DSR - * 2 - /DTR - * 1 - /DCD - * 0 - /RI - * - * Mode bit values: - * 1 - A value of one in a bit places the corresponding IO pin in push- - * pull output mode. - * 0 - A value of zero in a bit places the corresponding IO pin in open- - * drain output mode. - * - * Latch bit values: - * 1 - A value of one in a bit sets the corresponding IO pin's value to - * output high. If the mode is push-pull, the pin sources current - * from VIO. If the mode is open-drain, the pin driver is in high - * impedance. - * 0 - A value of zero in a bit sets the corresponding IO pin's value to - * output low. In either mode, push-pull or open-drain, the pin - * driver sinks current to ground. - * - * Enhanced function bits: - * 7 - GPIO dynamic suspend, for 4 GPIO signals - * 6 - Serial dynamic suspend, for 8 UART/Modem signals - * 5 - unused, leave zero - * 4 - Enable weak pull-ups - * 3 - unused, leave zero - * 2 - /RS485_TX on GPIO_2 - * 1 - /RXLED on GPIO_1 - * 0 - /TXLED on GPIO_0 - */ config.reset.mode = 0xf054; config.suspend.mode = 0xf054; config.reset.latch = 0x0fff; /* RXD must be 1 for some reason */ config.suspend.latch = 0x0fff; /* RXD must be 1 for some reason */ config.enhancedFxn = 0x00; - if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &config))) { + if ((ret = ioctl(fd, CP210x_IOCTL_PORTCONFSET, &config))) { fprintf(stderr, "portconfset ioctl failed %d\n", ret); return 1; } printf("port config altered\n"); /* Reset the part */ - if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) { + if ((ret = ioctl(fd, CP210x_IOCTL_DEVICERESET, 0))) { fprintf(stderr, "device reset ioctl failed %d\n", ret); return 1; } diff --git a/src/examples/cpio.c b/src/examples/cpio.c index 8fec895..b62afb0 100644 --- a/src/examples/cpio.c +++ b/src/examples/cpio.c @@ -4,35 +4,19 @@ * Configure the and GPIO configuration for a cp2103-equipped device. */ +#include #include #include #include +#include +#include #include #include -#include - -#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; /* Push-pull = 1, Open-drain = 0 */ - uint16_t lowPower; - uint16_t latch; /* Logic high = 1, Logic low = 0 */ -} cp2101_port_state_t; - -typedef struct { - cp2101_port_state_t reset; - cp2101_port_state_t suspend; - uint8_t enhancedFxn; -} cp2101_port_config_t; - -void exit(int); +#include +#include int cpConnect(char* device) { - int ret, ioval; int fd = open(device, O_RDWR); if (fd < 0) { fprintf(stderr, "cannot open %s\n", device); @@ -51,10 +35,10 @@ void cpDisconnect(int fd) void cpSetPortConf(int fd, int nopullups) { int ret; - cp2101_port_config_t config; + 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); exit(1); } @@ -75,7 +59,7 @@ void cpSetPortConf(int fd, int nopullups) if (nopullups) config.enhancedFxn &= ~0x10; - if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &config))) { + if ((ret = ioctl(fd, CP210x_IOCTL_PORTCONFSET, &config))) { fprintf(stderr, "portconfset ioctl failed %d\n", ret); exit(1); } @@ -86,7 +70,7 @@ void cpReset(int fd) int ret; /* Reset the part */ - if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) { + if ((ret = ioctl(fd, CP210x_IOCTL_DEVICERESET, 0))) { fprintf(stderr, "device reset ioctl %d\n", ret); exit(1); } diff --git a/src/examples/cpmfg.c b/src/examples/cpmfg.c index f4c5401..22976f9 100644 --- a/src/examples/cpmfg.c +++ b/src/examples/cpmfg.c @@ -1,5 +1,5 @@ /** - * cpcfg.c + * cpmfg.c * * Configure the USB descriptor and GPIO configurations for a cp2103-equipped * device. @@ -14,7 +14,7 @@ #include #include #include -#include "../cp210x.h" +#include #define VID 0x10c4 #define PID 0xea60 diff --git a/src/examples/cpread.c b/src/examples/cpread.c index f5a5d8a..cac0b2f 100644 --- a/src/examples/cpread.c +++ b/src/examples/cpread.c @@ -4,68 +4,19 @@ * Read the configuration of a cp2103-equipped device. */ +#include #include #include #include +#include #include #include #include -#include - -#define VID 0x10c4 -#define PID 0xea60 - -/* CP2103 ioctls */ -#define IOCTL_GPIOGET 0x8000 /* Get gpio bits */ -#define IOCTL_GPIOSET 0x8001 /* Set gpio bits */ -#define IOCTL_GPIOBIC 0x8002 /* Clear specific gpio bit(s) */ -#define IOCTL_GPIOBIS 0x8003 /* Set specific gpio bit(s) */ - -/* CP210x ioctls principally used during initial device configuration */ -#define IOCTL_DEVICERESET 0x8004 /* Reset the cp210x */ -#define IOCTL_PORTCONFGET 0x8005 /* Get port configuration */ -#define IOCTL_PORTCONFSET 0x8006 /* Set port configuration */ -#define IOCTL_SETVID 0x8007 /* Set vendor id */ -#define IOCTL_SETPID 0x8008 /* Set product id */ -#define IOCTL_SETMFG 0x8009 /* Set manufacturer string */ -#define IOCTL_SETPRODUCT 0x800a /* Set product string */ -#define IOCTL_SETSERIAL 0x800b /* Set serial number string */ -#define IOCTL_SETDEVVER 0x800c /* set device version id */ -/* FIXME: where is IOCTL_SETMFG? */ - -/* CP2103 GPIO */ -#define GPIO_0 0x01 -#define GPIO_1 0x02 -#define GPIO_2 0x04 -#define GPIO_3 0x08 -#define GPIO_MASK (GPIO_0|GPIO_1|GPIO_2|GPIO_3) - -/* Port config definitions */ -typedef struct { - uint16_t mode; /* Push-pull = 1, Open-drain = 0 */ - uint16_t lowPower; - uint16_t latch; /* Logic high = 1, Logic low = 0 */ -} cp2101_port_state_t; - -typedef struct { - cp2101_port_state_t reset; - cp2101_port_state_t suspend; - uint8_t enhancedFxn; -} cp2101_port_config_t; - -#define PORT_CONFIG_LEN 13 /* Because sizeof() will pad to full words */ - -/* Used to pass variable sized buffers between user and kernel space (ioctls) */ -typedef struct { - char* buf; - size_t len; -} cp210x_buffer_t; - -void exit(int); +#include +#include int cpConnect(char* device) { - int ret, ioval; int fd = open(device, O_RDWR); if (fd < 0) { fprintf(stderr, "cannot open %s\n", device); @@ -81,13 +32,13 @@ void cpDisconnect(int fd) close(fd); } -void printState(cp2101_port_state_t* state) +void printState(struct cp210x_port_state* state) { printf("m:0x%02x lp:0x%02x l:0x%02x", state->mode, state->lowPower, state->latch); } -void printConfig(cp2101_port_config_t* config) +void printConfig(struct cp210x_port_config* config) { printf("config: reset: "); printState(&config->reset); printf("\nconfig: suspend: "); printState(&config->reset); @@ -99,7 +50,7 @@ void cpReset(int fd) int ret; /* Reset the part */ - if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) { + if ((ret = ioctl(fd, CP210x_IOCTL_DEVICERESET, 0))) { fprintf(stderr, "device reset ioctl %d\n", ret); exit(1); } @@ -107,7 +58,7 @@ void cpReset(int fd) int main(int argc, char* argv[]) { - cp2101_port_config_t config; + struct cp210x_port_config config; uint8_t gpio = 0xff; int ret; int fd; @@ -120,13 +71,13 @@ int main(int argc, char* argv[]) fd = cpConnect(argv[1]); /* 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; } /* Read the current gpio latches */ - if ((ret = ioctl(fd, IOCTL_GPIOGET, &gpio))) { + if ((ret = ioctl(fd, CP210x_IOCTL_GPIOGET, &gpio))) { fprintf(stderr, "gpioget ioctl failed %d\n", ret); return 1; } diff --git a/src/examples/cptwiddle.c b/src/examples/cptwiddle.c index 5417175..5e144eb 100644 --- a/src/examples/cptwiddle.c +++ b/src/examples/cptwiddle.c @@ -10,52 +10,21 @@ * - Twiddles the LEDs * - Restores the original configuration * - Resets the device again so that the original config is immediately active - * - * Watch out; the tty device is hard-coded. */ +#include +#include #include #include #include #include #include #include -#include - -/* CP2103 ioctls */ -#define IOCTL_GPIOGET 0x8000 /* Get gpio bits */ -#define IOCTL_GPIOSET 0x8001 /* Set gpio bits */ -#define IOCTL_GPIOBIC 0x8002 /* Clear specific gpio bit(s) */ -#define IOCTL_GPIOBIS 0x8003 /* Set specific gpio bit(s) */ - -/* CP210x ioctls principally used during initial device configuration */ -#define IOCTL_DEVICERESET 0x8004 /* Reset the cp210x */ -#define IOCTL_PORTCONFGET 0x8005 /* Get port configuration */ -#define IOCTL_PORTCONFSET 0x8006 /* Set port configuration */ -#define IOCTL_SETVID 0x8007 /* Set vendor id */ -#define IOCTL_SETPID 0x8008 /* Set product id */ -#define IOCTL_SETMFG 0x8009 /* Set manufacturer string */ -#define IOCTL_SETPRODUCT 0x800a /* Set product string */ -#define IOCTL_SETSERIAL 0x800b /* Set serial number string */ -#define IOCTL_SETDEVVER 0x800c /* set device version id */ -/* FIXME: where is IOCTL_SETMFG? */ - - -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 +#include int cpConnect(char* device) { - int ret, ioval; int fd = open(device, O_RDWR); if (fd < 0) { fprintf(stderr, "cannot open tty\n"); @@ -74,8 +43,8 @@ void cpDisconnect(int fd) int main(int argc, char* argv[]) { int fd; - cp2103_port_config_t config; - cp2103_port_config_t saveConfig; + struct cp210x_port_config config; + struct cp210x_port_config saveConfig; uint8_t saveGpio; int ret; int i; @@ -90,7 +59,7 @@ int main(int argc, char* argv[]) return 1; /* 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; } @@ -98,7 +67,7 @@ int main(int argc, char* argv[]) printf("port config received\n"); /* Read the current gpio latches */ - if ((ret = ioctl(fd, IOCTL_GPIOGET, &saveGpio))) { + if ((ret = ioctl(fd, CP210x_IOCTL_GPIOGET, &saveGpio))) { fprintf(stderr, "gpioget ioctl failed %d\n", ret); return 1; } @@ -109,14 +78,14 @@ int main(int argc, char* argv[]) config.suspend.mode &= ~0x0300; config.reset.latch |= 0x0300; config.enhancedFxn &= ~0x03; - if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &config))) { + if ((ret = ioctl(fd, CP210x_IOCTL_PORTCONFSET, &config))) { fprintf(stderr, "portconfset ioctl failed %d\n", ret); return 1; } printf("port config altered\n"); /* Reset the part */ - if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) { + if ((ret = ioctl(fd, CP210x_IOCTL_DEVICERESET, 0))) { fprintf(stderr, "device reset ioctl failed %d\n", ret); return 1; } @@ -139,7 +108,7 @@ int main(int argc, char* argv[]) /* Now, twiddle some bits */ for (i = 0; i < 8; i++) { uint8_t leds = i & 0x03; - if ((ret = ioctl(fd, IOCTL_GPIOSET, leds))) { + if ((ret = ioctl(fd, CP210x_IOCTL_GPIOSET, leds))) { fprintf(stderr, "gpio set ioctl failed %d\n", ret); return 1; } @@ -148,21 +117,21 @@ int main(int argc, char* argv[]) } /* Restore the original latches */ - if ((ret = ioctl(fd, IOCTL_GPIOSET, saveGpio))) { + if ((ret = ioctl(fd, CP210x_IOCTL_GPIOSET, saveGpio))) { fprintf(stderr, "gpio set ioctl failed %d\n", ret); return 1; } printf("gpio latches restored\n"); /* Return the original configuration */ - if ((ret = ioctl(fd, IOCTL_PORTCONFSET, &saveConfig))) { + if ((ret = ioctl(fd, CP210x_IOCTL_PORTCONFSET, &saveConfig))) { fprintf(stderr, "portconfset ioctl failed %d\n", ret); return 1; } printf("port config restored\n"); /* Reset the part again */ - if ((ret = ioctl(fd, IOCTL_DEVICERESET, 0))) { + if ((ret = ioctl(fd, CP210x_IOCTL_DEVICERESET, 0))) { fprintf(stderr, "device reset ioctl failed %d\n", ret); return 1; } -- 2.39.2