diff options
Diffstat (limited to 'nuttx/examples/usbserial')
-rw-r--r-- | nuttx/examples/usbserial/Makefile.host | 20 | ||||
-rw-r--r-- | nuttx/examples/usbserial/host.c | 92 | ||||
-rw-r--r-- | nuttx/examples/usbserial/main.c | 148 |
3 files changed, 241 insertions, 19 deletions
diff --git a/nuttx/examples/usbserial/Makefile.host b/nuttx/examples/usbserial/Makefile.host index ea394090b..0997ce81c 100644 --- a/nuttx/examples/usbserial/Makefile.host +++ b/nuttx/examples/usbserial/Makefile.host @@ -33,16 +33,30 @@ # ############################################################################ --include $(TOPDIR)/.config --include $(TOPDIR)/Make.defs +include $(TOPDIR)/.config +include $(TOPDIR)/Make.defs SRC = host.c BIN = host +DEFINES = +ifeq ($(CONFIG_EXAMPLES_USBSERIAL_INONLY),y) +DEFINES += -DCONFIG_EXAMPLES_USBSERIAL_INONLY=1 +endif +ifeq ($(CONFIG_EXAMPLES_USBSERIAL_OUTONLY),y) +DEFINES += -DCONFIG_EXAMPLES_USBSERIAL_OUTONLY=1 +endif +ifeq ($(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL),y) +DEFINES += -DCONFIG_EXAMPLES_USBSERIAL_ONLYSMALL=1 +endif +ifeq ($(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG),y) +DEFINES += -DCONFIG_EXAMPLES_USBSERIAL_ONLYBIG=1 +endif + all: $(BIN)$(EXEEXT) $(BIN)$(EXEEXT): $(SRC) - $(HOSTCC) $(HOSTCFLAGS) $^ -o $@ + $(HOSTCC) $(HOSTCFLAGS) $(DEFINES) $^ -o $@ clean: @rm -f $(BIN) *~ .*.swp *.o diff --git a/nuttx/examples/usbserial/host.c b/nuttx/examples/usbserial/host.c index 944582783..90cb85ed7 100644 --- a/nuttx/examples/usbserial/host.c +++ b/nuttx/examples/usbserial/host.c @@ -45,6 +45,7 @@ #include <stdlib.h> #include <unistd.h> #include <string.h> +#include <termios.h> #include <fcntl.h> #include <errno.h> @@ -52,6 +53,19 @@ * Definitions ****************************************************************************/ +#if defined(CONFIG_EXAMPLES_USBSERIAL_INONLY) && defined(CONFIG_EXAMPLES_USBSERIAL_OUTONLY) +# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_INONLY and _OUTONLY" +#endif +#if defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) && defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) +# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL and _ONLYBIG" +#endif + +#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) +# ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY +# define COUNTER_NEEDED 1 +# endif +#endif + #define DEFAULT_TTYDEV "/dev/ttyUSB0" #define BUFFER_SIZE 1024 @@ -60,8 +74,12 @@ ****************************************************************************/ static const char *g_ttydev = DEFAULT_TTYDEV; -static char g_buffer[BUFFER_SIZE]; + +#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYBIG static const char g_shortmsg[] = "Sure... You betcha!!\n"; +#endif + +#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL static const char g_longmsg[] = "I am proud to come to this city as the guest of your distinguished Mayor, " "who has symbolized throughout the world the fighting spirit of West Berlin. " @@ -114,6 +132,11 @@ static const char g_longmsg[] = "All free men, wherever they may live, are citizens of Berlin, and, therefore, " "as a free man, I take pride in the words \"Ich bin ein Berliner.\"\n" "President John F. Kennedy - June 26, 1963\n"; +#endif + +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY +static char g_iobuffer[BUFFER_SIZE]; +#endif /**************************************************************************** * show_usage @@ -135,9 +158,15 @@ static void show_usage(const char *progname, int exitcode) int main(int argc, char **argv, char **envp) { + struct termios tty; +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY ssize_t nbytes; - int count; +#endif +#ifdef COUNTER_NEEDED + int count = 0; +#endif int fd; + int ret; /* Handle input parameters */ @@ -168,14 +197,40 @@ int main(int argc, char **argv, char **envp) while (fd < 0); printf("main: Successfully opened the serial driver\n"); - /* Wait for hello... */ + /* Configure the serial port in raw mode (at least turn off echo) */ + + ret = tcgetattr(fd, &tty); + if (ret < 0) + { + printf("main: ERROR: Failed to get termios for %s: %s\n", g_ttydev, strerror(errno)); + close(fd); + return 1; + } + + tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + tty.c_oflag &= ~OPOST; + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + tty.c_cflag &= ~(CSIZE|PARENB); + tty.c_cflag |= CS8; + + ret = tcsetattr(fd, TCSANOW, &tty); + if (ret < 0) + { + printf("main: ERROR: Failed to set termios for %s: %s\n", g_ttydev, strerror(errno)); + close(fd); + return 1; + } + + /* Wait for and/or send messages -- forever */ - count = 0; for (;;) { + /* Test IN messages (device-to-host) */ + +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY printf("main: Reading from the serial driver\n"); printf("main: ... (Control-C to terminate) ...\n"); - nbytes = read(fd, g_buffer, BUFFER_SIZE-1); + nbytes = read(fd, g_iobuffer, BUFFER_SIZE-1); if (nbytes < 0) { printf("main: ERROR: Failed to read from %s: %s\n", g_ttydev, strerror(errno)); @@ -183,21 +238,39 @@ int main(int argc, char **argv, char **envp) return 2; } - g_buffer[nbytes] = '\0'; + g_iobuffer[nbytes] = '\0'; printf("main: Received %d bytes:\n", nbytes); - printf(" \"%s\"\n", g_buffer); - count++; + printf(" \"%s\"\n", g_iobuffer); +#else + printf("main: Waiting...\n"); + sleep(5); +#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */ + + /* Test OUT messages (host-to-device) */ +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY +#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) + count++; if (count < 5) { printf("main: Sending %d bytes..\n", sizeof(g_shortmsg)); - nbytes = write(fd, g_longmsg, sizeof(g_shortmsg)); + nbytes = write(fd, g_shortmsg, sizeof(g_shortmsg)); } else { printf("main: Sending %d bytes..\n", sizeof(g_longmsg)); nbytes = write(fd, g_longmsg, sizeof(g_longmsg)); + count = 0; } +#elif !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) + printf("main: Sending %d bytes..\n", sizeof(g_longmsg)); + nbytes = write(fd, g_longmsg, sizeof(g_longmsg)); +#else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */ + printf("main: Sending %d bytes..\n", sizeof(g_shortmsg)); + nbytes = write(fd, g_shortmsg, sizeof(g_shortmsg)); +#endif + + /* Test if write was successful */ if (nbytes < 0) { @@ -206,6 +279,7 @@ int main(int argc, char **argv, char **envp) return 2; } printf("main: %d bytes sent\n", nbytes); +#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ } /* Won't get here, but if we did this what we would have to do */ diff --git a/nuttx/examples/usbserial/main.c b/nuttx/examples/usbserial/main.c index 9a890a2ad..a29cd5f11 100644 --- a/nuttx/examples/usbserial/main.c +++ b/nuttx/examples/usbserial/main.c @@ -43,6 +43,7 @@ #include <sys/stat.h> #include <stdio.h> #include <unistd.h> +#include <string.h> #include <fcntl.h> #include <errno.h> #include <debug.h> @@ -53,6 +54,19 @@ * Definitions ****************************************************************************/ +#if defined(CONFIG_EXAMPLES_USBSERIAL_INONLY) && defined(CONFIG_EXAMPLES_USBSERIAL_OUTONLY) +# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_INONLY and _OUTONLY" +#endif +#if defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) && defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) +# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL and _ONLYBIG" +#endif + +#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) +# ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY +# define COUNTER_NEEDED 1 +# endif +#endif + #ifdef CONFIG_CPP_HAVE_VARARGS # ifdef CONFIG_DEBUG # define message(...) lib_lowprintf(__VA_ARGS__) @@ -73,7 +87,11 @@ * Private Data ****************************************************************************/ +#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYBIG static const char g_shortmsg[] = "Hello, World!!\n"; +#endif + +#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL static const char g_longmsg[] = "The Spanish Armada a Speech by Queen Elizabeth I of England\n" "Addressed to the English army at Tilbury Fort - 1588\n" @@ -100,7 +118,11 @@ static const char g_longmsg[] = "your obedience to my general, by your concord in the camp, and by your " "valour in the field, we shall shortly have a famous victory over the enemies " "of my God, of my kingdom, and of my people.\n"; +#endif + +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY static char g_iobuffer[IOBUFFER_SIZE]; +#endif /**************************************************************************** * Public Functions @@ -125,12 +147,20 @@ void user_initialize(void) int user_start(int argc, char *argv[]) { +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY int infd; +#endif +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY int outfd; - int count; +#endif +#ifdef COUNTER_NEEDED + int count = 0; +#endif ssize_t nbytes; +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY + int i, j, k; +#endif int ret; - int i; /* Initialize the USB serial driver */ @@ -145,6 +175,7 @@ int user_start(int argc, char *argv[]) /* Open the USB serial device for writing (blocking) */ +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY do { message("user_start: Opening USB serial driver\n"); @@ -171,9 +202,12 @@ int user_start(int argc, char *argv[]) } } while (outfd < 0); +#endif /* Open the USB serial device for reading (non-blocking) */ +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY infd = open("/dev/ttyUSB0", O_RDONLY|O_NONBLOCK); if (infd < 0) { @@ -181,14 +215,46 @@ int user_start(int argc, char *argv[]) close(outfd); return 3; } +#else + do + { + infd = open("/dev/ttyUSB0", O_RDONLY|O_NONBLOCK); + if (infd < 0) + { + int errcode = errno; + message("user_start: ERROR: Failed to open /dev/ttyUSB0 for reading: %d\n", errno); + + /* ENOTCONN means that the USB device is not yet connected */ + + if (errcode == ENOTCONN) + { + message("user_start: Not connected. Wait and try again.\n"); + sleep(5); + } + else + { + /* Give up on other errors */ + + message("user_start: Aborting\n"); + return 3; + } + } + } + while (infd < 0); +#endif +#endif message("user_start: Successfully opened the serial driver\n"); - /* Then say hello -- forever */ + /* Send messages and get responses -- forever */ - count = 0; for (;;) { + + /* Test IN (device-to-host) messages */ + +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY +#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) if (count < 8) { message("user_start: Saying hello\n"); @@ -201,20 +267,37 @@ int user_start(int argc, char *argv[]) nbytes = write(outfd, g_longmsg, sizeof(g_longmsg)); count = 0; } +#elif !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) + message("user_start: Reciting QEI's speech of 1588\n"); + nbytes = write(outfd, g_longmsg, sizeof(g_longmsg)); +#else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */ + message("user_start: Saying hello\n"); + nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); +#endif + + /* Test if the write was successful */ if (nbytes < 0) { message("user_start: ERROR: write failed: %d\n", errno); +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY close(infd); +#endif close(outfd); return 4; } + message("user_start:%d bytes sent\n", nbytes); +#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */ + /* Test OUT (host-to-device) messages */ + +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY /* Poll for incoming messages */ - message("user_start: Waiting\n"); + message("user_start: Polling for OUT messages\n"); for (i = 0; i < 5; i++) { + memset(g_iobuffer, 'X', IOBUFFER_SIZE); nbytes = read(infd, g_iobuffer, IOBUFFER_SIZE); if (nbytes < 0) { @@ -223,7 +306,9 @@ int user_start(int argc, char *argv[]) { message("user_start: ERROR: read failed: %d\n", errno); close(infd); +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY close(outfd); +#endif return 6; } } @@ -232,18 +317,67 @@ int user_start(int argc, char *argv[]) message("user_start: Received %d bytes:\n", nbytes); if (nbytes > 0) { - message("user_start: "); - (void)fwrite(g_iobuffer, 1, nbytes, stdout); + for (j = 0; j < nbytes; j += 16) + { + message("user_start: %03x: ", j); + for (k = 0; k < 16; k++) + { + if (k == 8) + { + message(" "); + } + if (j+k < nbytes) + { + message("%02x", g_iobuffer[j+k]); + } + else + { + message(" "); + } + } + message(" "); + for (k = 0; k < 16; k++) + { + if (k == 8) + { + message(" "); + } + if (j+k < nbytes) + { + if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f) + { + message("%c", g_iobuffer[j+k]); + } + else + { + message("."); + } + } + else + { + message(" "); + } + } + message("\n"); + } } } sleep(1); } +#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ + message("user_start: Waiting\n"); + sleep(5); +#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ } /* Won't get here, but if we did this what we would have to do */ +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY close(infd); +#endif +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY close(outfd); +#endif return 0; } |