apps/examples/usbserial: Can now be run as an NSH builtin-function. Now uses a configurable IO buffer size

This commit is contained in:
Gregory Nutt 2015-08-20 10:40:31 -06:00
parent bc2cf8affd
commit d836478728
6 changed files with 66 additions and 18 deletions

View File

@ -1398,4 +1398,6 @@
* apps/examples/can: Extend the CAN loopback test by adding more
command line options (2015-08-17).
* apps/examples/usbserial: Can now be run as an NSH builtin-function.
Now uses a configurable IO buffer size (2015-08-20).

View File

@ -11,6 +11,13 @@ config EXAMPLES_USBSERIAL
if EXAMPLES_USBSERIAL
config EXAMPLES_USBSERIAL_BUFSIZE
int "Target I/O Buffer Size"
default 512
---help---
The size of the array that is used as an I/O buffer for USB serial
data transfers.
config EXAMPLES_USBSERIAL_TRACEINIT
bool "USB Trace Initialization"
default n

View File

@ -37,6 +37,11 @@
include $(APPDIR)/Make.defs
# USB serial device example
# Built-in application info
APPNAME = usbserial
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
ASRCS =
CSRCS =
@ -104,7 +109,14 @@ install:
endif
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
else
context:
endif
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep

View File

@ -1,7 +1,7 @@
############################################################################
# apps/examples/usbserial/Makefile.host
#
# Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
# Copyright (C) 2008, 2011, 2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
@ -53,6 +53,9 @@ endif
ifeq ($(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG),y)
DEFINES += -DCONFIG_EXAMPLES_USBSERIAL_ONLYBIG=1
endif
ifeq ($(CONFIG_CDCACM),y)
DEFINES += -DCONFIG_CDCACM=1
endif
all: $(BIN)

View File

@ -50,7 +50,7 @@
#include <errno.h>
/****************************************************************************
* Definitions
* Pre-processor Definitions
****************************************************************************/
#if defined(CONFIG_EXAMPLES_USBSERIAL_INONLY) && defined(CONFIG_EXAMPLES_USBSERIAL_OUTONLY)
@ -181,6 +181,7 @@ int main(int argc, char **argv, char **envp)
fprintf(stderr, "Too many arguments on command line\n");
show_usage(argv[0], 1);
}
g_ttydev = argv[1];
}
@ -192,13 +193,15 @@ int main(int argc, char **argv, char **envp)
fd = open(g_ttydev, O_RDWR);
if (fd < 0)
{
printf("main: ERROR: Failed to open %s: %s\n", g_ttydev, strerror(errno));
printf("main: ERROR: Failed to open %s: %s\n",
g_ttydev, strerror(errno));
printf("main: Assume not connected. Wait and try again.\n");
printf("main: (Control-C to terminate).\n");
sleep(5);
}
}
while (fd < 0);
printf("main: Successfully opened the serial driver\n");
/* Configure the serial port in raw mode (at least turn off echo) */
@ -206,7 +209,8 @@ int main(int argc, char **argv, char **envp)
ret = tcgetattr(fd, &tty);
if (ret < 0)
{
printf("main: ERROR: Failed to get termios for %s: %s\n", g_ttydev, strerror(errno));
printf("main: ERROR: Failed to get termios for %s: %s\n",
g_ttydev, strerror(errno));
close(fd);
return 1;
}
@ -220,7 +224,8 @@ int main(int argc, char **argv, char **envp)
ret = tcsetattr(fd, TCSANOW, &tty);
if (ret < 0)
{
printf("main: ERROR: Failed to set termios for %s: %s\n", g_ttydev, strerror(errno));
printf("main: ERROR: Failed to set termios for %s: %s\n",
g_ttydev, strerror(errno));
close(fd);
return 1;
}
@ -237,7 +242,8 @@ int main(int argc, char **argv, char **envp)
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));
printf("main: ERROR: Failed to read from %s: %s\n",
g_ttydev, strerror(errno));
close(fd);
return 2;
}
@ -271,9 +277,11 @@ int main(int argc, char **argv, char **envp)
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));
@ -287,6 +295,7 @@ int main(int argc, char **argv, char **envp)
close(fd);
return 2;
}
printf("main: %ld bytes sent\n", (long)nbytes);
#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
}

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/usbserial/usbserial_main.c
*
* Copyright (C) 2008, 2010-2012 Gregory Nutt. All rights reserved.
* Copyright (C) 2008, 2010-2012, 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -56,7 +56,7 @@
#endif
/****************************************************************************
* Definitions
* Pre-processor Definitions
****************************************************************************/
#if defined(CONFIG_EXAMPLES_USBSERIAL_INONLY) && defined(CONFIG_EXAMPLES_USBSERIAL_OUTONLY)
@ -113,7 +113,9 @@
# define USBSER_DEVNAME "/dev/ttyUSB0"
#endif
#define IOBUFFER_SIZE 256
#ifndef CONFIG_EXAMPLES_USBSERIAL_BUFSIZE
# define CONFIG_EXAMPLES_USBSERIAL_BUFSIZE 256
#endif
/****************************************************************************
* Private Data
@ -153,7 +155,7 @@ static const char g_longmsg[] =
#endif
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
static char g_iobuffer[IOBUFFER_SIZE];
static char g_iobuffer[CONFIG_EXAMPLES_USBSERIAL_BUFSIZE];
#endif
/****************************************************************************
@ -214,9 +216,11 @@ int usbserial_main(int argc, char *argv[])
#endif
if (ret < 0)
{
printf("usbserial_main: ERROR: Failed to create the USB serial device: %d\n", -ret);
printf("usbserial_main: ERROR: Failed to create the USB serial device: %d\n",
-ret);
return 1;
}
printf("usbserial_main: Successfully registered the serial driver\n");
#if CONFIG_USBDEV_TRACE && CONFIG_USBDEV_TRACE_INITIALIDSET != 0
@ -242,7 +246,8 @@ int usbserial_main(int argc, char *argv[])
if (outfd < 0)
{
int errcode = errno;
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for writing: %d\n", errcode);
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME
" for writing: %d\n", errcode);
/* ENOTCONN means that the USB device is not yet connected */
@ -274,7 +279,8 @@ int usbserial_main(int argc, char *argv[])
infd = open(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK);
if (infd < 0)
{
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for reading: %d\n", errno);
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME
" for reading: %d\n", errno);
close(outfd);
return 3;
}
@ -285,7 +291,8 @@ int usbserial_main(int argc, char *argv[])
if (infd < 0)
{
int errcode = errno;
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for reading: %d\n", errno);
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME
" for reading: %d\n", errno);
/* ENOTCONN means that the USB device is not yet connected */
@ -333,9 +340,11 @@ int usbserial_main(int argc, char *argv[])
nbytes = write(outfd, g_longmsg, sizeof(g_longmsg));
count = 0;
}
#elif !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL)
printf("usbserial_main: Reciting QEI's speech of 1588\n");
nbytes = write(outfd, g_longmsg, sizeof(g_longmsg));
#else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */
printf("usbserial_main: Saying hello\n");
nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg));
@ -352,6 +361,7 @@ int usbserial_main(int argc, char *argv[])
close(outfd);
return 4;
}
printf("usbserial_main: %ld bytes sent\n", (long)nbytes);
#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */
@ -363,8 +373,8 @@ int usbserial_main(int argc, char *argv[])
printf("usbserial_main: Polling for OUT messages\n");
for (i = 0; i < 5; i++)
{
memset(g_iobuffer, 'X', IOBUFFER_SIZE);
nbytes = read(infd, g_iobuffer, IOBUFFER_SIZE);
memset(g_iobuffer, 'X', CONFIG_EXAMPLES_USBSERIAL_BUFSIZE);
nbytes = read(infd, g_iobuffer, CONFIG_EXAMPLES_USBSERIAL_BUFSIZE);
if (nbytes < 0)
{
int errorcode = errno;
@ -380,7 +390,7 @@ int usbserial_main(int argc, char *argv[])
}
else
{
printf("usbserial_main: Received l%d bytes:\n", (long)nbytes);
printf("usbserial_main: Received %ld bytes:\n", (long)nbytes);
if (nbytes > 0)
{
for (j = 0; j < nbytes; j += 16)
@ -392,6 +402,7 @@ int usbserial_main(int argc, char *argv[])
{
printf(" ");
}
if (j+k < nbytes)
{
printf("%02x", g_iobuffer[j+k]);
@ -401,6 +412,7 @@ int usbserial_main(int argc, char *argv[])
printf(" ");
}
}
printf(" ");
for (k = 0; k < 16; k++)
{
@ -408,6 +420,7 @@ int usbserial_main(int argc, char *argv[])
{
printf(" ");
}
if (j+k < nbytes)
{
if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f)
@ -424,12 +437,15 @@ int usbserial_main(int argc, char *argv[])
printf(" ");
}
}
printf("\n");
}
}
}
sleep(1);
}
#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
printf("usbserial_main: Waiting\n");
sleep(5);
@ -450,4 +466,3 @@ int usbserial_main(int argc, char *argv[])
#endif
return 0;
}