Add nuttx to the system framework, which is 10.1.0

This commit is contained in:
TangYiwen123
2021-06-09 14:33:15 +08:00
parent 06c351e27c
commit 804bd57aa0
5000 changed files with 1488544 additions and 0 deletions
@@ -0,0 +1,73 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config EXAMPLES_USBSERIAL
tristate "USB serial test example"
default n
depends on LIB_BOARDCTL
select BOARDCTL_USBDEVCTRL
---help---
Enable the USB serial test example
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
depends on USBDEV_TRACE || DEBUG_USB
---help---
If USBDEV_TRACE is enabled (or CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_USB),
then the example code will also manage the USB trace output. The
amount of trace output can be controlled this configuration value:
This setting will show USB initialization events
config EXAMPLES_USBSERIAL_TRACECLASS
bool "USB Trace Class"
default n
depends on USBDEV_TRACE || DEBUG_USB
---help---
If USBDEV_TRACE is enabled (or CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_USB),
then the example code will also manage the USB trace output. The
amount of trace output can be controlled this configuration value:
This setting will show USB class driver events
config EXAMPLES_USBSERIAL_TRACETRANSFERS
bool "USB Trace Transfers"
default n
depends on USBDEV_TRACE || DEBUG_USB
---help---
If USBDEV_TRACE is enabled (or CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_USB),
then the example code will also manage the USB trace output. The
amount of trace output can be controlled this configuration value:
This setting will show USB data transfer events
config EXAMPLES_USBSERIAL_TRACECONTROLLER
bool "USB Trace Device Controller Events"
default n
depends on USBDEV_TRACE || DEBUG_USB
---help---
If USBDEV_TRACE is enabled (or CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_USB),
then the example code will also manage the USB trace output. The
amount of trace output can be controlled this configuration value:
This setting will show USB device controller events
config EXAMPLES_USBSERIAL_TRACEINTERRUPTS
bool "USB Trace Device Controller Interrupt Events"
default n
depends on USBDEV_TRACE || DEBUG_USB
---help---
If USBDEV_TRACE is enabled (or CONFIG_DEBUG_FEATURES and CONFIG_DEBUG_USB),
then the example code will also manage the USB trace output. The
amount of trace output can be controlled this configuration value:
This setting will show USB device controller interrupt-related events.
endif
@@ -0,0 +1,39 @@
############################################################################
# apps/examples/usbserial/Make.defs
# Adds selected applications to apps/ build
#
# Copyright (C) 2015 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
ifneq ($(CONFIG_EXAMPLES_USBSERIAL),)
CONFIGURED_APPS += $(APPDIR)/examples/usbserial
endif
@@ -0,0 +1,48 @@
############################################################################
# apps/examples/usbserial/Makefile
#
# Copyright (C) 2008, 2010-2012, 2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
include $(APPDIR)/Make.defs
# USB serial device example
# Built-in application info
PROGNAME = usbserial
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = $(CONFIG_DEFAULT_TASK_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_USBSERIAL)
MAINSRC = usbserial_main.c
include $(APPDIR)/Application.mk
@@ -0,0 +1,67 @@
############################################################################
# apps/examples/usbserial/Makefile.host
#
# Copyright (C) 2008, 2011, 2015-2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
# TOPDIR must be defined on the make command line
include $(APPDIR)/Make.defs
SRC = host.c
BIN = host$(HOSTEXEEXT)
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
ifeq ($(CONFIG_CDCACM),y)
DEFINES += -DCONFIG_CDCACM=1
endif
all: $(BIN)
.PHONY: clean
$(BIN): $(SRC)
@$(HOSTCC) $(HOSTCFLAGS) $(DEFINES) $^ -o $@
clean:
$(call DELFILE, $(BIN))
$(call CLEAN)
@@ -0,0 +1,305 @@
/****************************************************************************
* examples/usbserial/host.c
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
/****************************************************************************
* Pre-processor 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
#ifdef CONFIG_CDCACM
# define DEFAULT_TTYDEV "/dev/ttyACM0"
#else
# define DEFAULT_TTYDEV "/dev/ttyUSB0"
#endif
#define BUFFER_SIZE 1024
/****************************************************************************
* Private Data
****************************************************************************/
static const char *g_ttydev = DEFAULT_TTYDEV;
#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. "
"And I am proud to visit the Federal Republic with your distinguished Chancellor "
"who for so many years has committed Germany to democracy and freedom and "
"progress, and to come here in the company of my fellow American, General Clay, "
"who has been in this city during its great moments of crisis and will come "
"again if ever needed.\n"
"Two thousand years ago the proudest boast was \"civis Romanus sum.\" Today, "
"in the world of freedom, the proudest boast is \"Ich bin ein Berliner.\"\r\""
"I appreciate my interpreter translating my German!\n"
"There are many people in the world who really don't understand, or say they "
"don't, what is the great issue between the free world and the Communist world. "
"Let them come to Berlin. There are some who say that communism is the wave of "
"the future. Let them come to Berlin. And there are some who say in Europe and "
"elsewhere we can work with the Communists. Let them come to Berlin. And there "
"are even a few who say that it is true that communism is an evil system, but it "
"permits us to make economic progress. Lass' sie nach Berlin kommen. Let them "
"come to Berlin.\n"
"Freedom has many difficulties and democracy is not perfect, but we have never "
"had to put a wall up to keep our people in, to prevent them from leaving us. I "
"want to say, on behalf of my countrymen, who live many miles away on the other "
"side of the Atlantic, who are far distant from you, that they take the greatest "
"pride that they have been able to share with you, even from a distance, the "
"story of the last 18 years. I know of no town, no city, that has been besieged "
"for 18 years that still lives with the vitality and the force, and the hope and "
"the determination of the city of West Berlin. While the wall is the most obvious "
"and vivid demonstration of the failures of the Communist system, for all the "
"world to see, we take no satisfaction in it, for it is, as your Mayor has said, "
"an offense not only against history but an offense against humanity, separating "
"families, dividing husbands and wives and brothers and sisters, and dividing a "
"people who wish to be joined together.\n"
"What is true of this city is true of Germany--real, lasting peace in Europe can "
"never be assured as long as one German out of four is denied the elementary "
"right of free men, and that is to make a free choice. In 18 years of peace and "
"good faith, this generation of Germans has earned the right to be free, "
"including the right to unite their families and their nation in lasting peace, "
"with good will to all people. You live in a defended island of freedom, but "
"your life is part of the main. So let me ask you as I close, to lift your eyes "
"beyond the dangers of today, to the hopes of tomorrow, beyond the freedom merely "
"of this city of Berlin, or your country of Germany, to the advance of freedom "
"everywhere, beyond the wall to the day of peace with justice, beyond yourselves "
"and ourselves to all mankind.\n"
"Freedom is indivisible, and when one man is enslaved, all are not free. When all "
"are free, then we can look forward to that day when this city will be joined as "
"one and this country and this great Continent of Europe in a peaceful and hopeful "
"globe. When that day finally comes, as it will, the people of West Berlin can take "
"sober satisfaction in the fact that they were in the front lines for almost two "
"decades.\n"
"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
****************************************************************************/
static void show_usage(const char *progname, int exitcode)
{
fprintf(stderr, "USAGE: %s [<ttydev>]\n", progname);
exit(exitcode);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* main
****************************************************************************/
int main(int argc, char **argv, char **envp)
{
struct termios tty;
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
ssize_t nbytes;
#endif
#ifdef COUNTER_NEEDED
int count = 0;
#endif
int fd;
int ret;
/* Handle input parameters */
if (argc > 1)
{
if (argc > 2)
{
fprintf(stderr, "Too many arguments on command line\n");
show_usage(argv[0], 1);
}
g_ttydev = argv[1];
}
/* Open the USB serial device for blocking read/write */
do
{
printf("main: Opening USB serial driver\n");
fd = open(g_ttydev, O_RDWR);
if (fd < 0)
{
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) */
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 */
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_iobuffer, BUFFER_SIZE-1);
if (nbytes < 0)
{
printf("main: ERROR: Failed to read from %s: %s\n",
g_ttydev, strerror(errno));
close(fd);
return 2;
}
else if (nbytes == 0)
{
printf("main: End-of-file encountered\n");
break;
}
g_iobuffer[nbytes] = '\0';
printf("main: Received %ld bytes:\n", (long)nbytes);
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_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)
{
printf("main: ERROR: Failed to write to %s: %s\n", g_ttydev, strerror(errno));
close(fd);
return 2;
}
printf("main: %ld bytes sent\n", (long)nbytes);
#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
}
close(fd);
return 0;
}
@@ -0,0 +1,485 @@
/****************************************************************************
* examples/usbserial/usbserial_main.c
*
* Copyright (C) 2008, 2010-2012, 2015-2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/boardctl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/usb/usbdev.h>
#include <nuttx/usb/usbdev_trace.h>
#ifdef CONFIG_CDCACM
# include <nuttx/usb/cdcacm.h>
#endif
/****************************************************************************
* Pre-processor 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
#ifndef CONFIG_USBDEV_TRACE_INITIALIDSET
# define CONFIG_USBDEV_TRACE_INITIALIDSET 0
#endif
#ifdef CONFIG_EXAMPLES_USBSERIAL_TRACEINIT
# define TRACE_INIT_BITS (TRACE_INIT_BIT)
#else
# define TRACE_INIT_BITS (0)
#endif
#define TRACE_ERROR_BITS (TRACE_DEVERROR_BIT|TRACE_CLSERROR_BIT)
#ifdef CONFIG_EXAMPLES_USBSERIAL_TRACECLASS
# define TRACE_CLASS_BITS (TRACE_CLASS_BIT|TRACE_CLASSAPI_BIT|TRACE_CLASSSTATE_BIT)
#else
# define TRACE_CLASS_BITS (0)
#endif
#ifdef CONFIG_EXAMPLES_USBSERIAL_TRACETRANSFERS
# define TRACE_TRANSFER_BITS (TRACE_OUTREQQUEUED_BIT|TRACE_INREQQUEUED_BIT|TRACE_READ_BIT|\
TRACE_WRITE_BIT|TRACE_COMPLETE_BIT)
#else
# define TRACE_TRANSFER_BITS (0)
#endif
#ifdef CONFIG_EXAMPLES_USBSERIAL_TRACECONTROLLER
# define TRACE_CONTROLLER_BITS (TRACE_EP_BIT|TRACE_DEV_BIT)
#else
# define TRACE_CONTROLLER_BITS (0)
#endif
#ifdef CONFIG_EXAMPLES_USBSERIAL_TRACEINTERRUPTS
# define TRACE_INTERRUPT_BITS (TRACE_INTENTRY_BIT|TRACE_INTDECODE_BIT|TRACE_INTEXIT_BIT)
#else
# define TRACE_INTERRUPT_BITS (0)
#endif
#define TRACE_BITSET (TRACE_INIT_BITS|TRACE_ERROR_BITS|TRACE_CLASS_BITS|\
TRACE_TRANSFER_BITS|TRACE_CONTROLLER_BITS|TRACE_INTERRUPT_BITS)
#ifdef CONFIG_CDCACM
# define USBSER_DEVNAME "/dev/ttyACM0"
#else
# define USBSER_DEVNAME "/dev/ttyUSB0"
#endif
#ifndef CONFIG_EXAMPLES_USBSERIAL_BUFSIZE
# define CONFIG_EXAMPLES_USBSERIAL_BUFSIZE 256
#endif
/****************************************************************************
* 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"
"My loving people, we have been persuaded by some, that are careful of our "
"safety, to take heed how we commit ourselves to armed multitudes, for fear "
"of treachery; but I assure you, I do not desire to live to distrust my "
"faithful and loving people.\n"
"Let tyrants fear; I have always so behaved myself that, under God, I have "
"placed my chiefest strength and safeguard in the loyal hearts and good will "
"of my subjects. And therefore I am come amongst you at this time, not as for "
"my recreation or sport, but being resolved, in the midst and heat of the "
"battle, to live or die amongst you all; to lay down, for my God, and for "
"my kingdom, and for my people, my honour and my blood, even the dust.\n"
"I know I have but the body of a weak and feeble woman; but I have the heart "
"of a king, and of a king of England, too; and think foul scorn that Parma "
"or Spain, or any prince of Europe, should dare to invade the borders of my "
"realms: to which, rather than any dishonour should grow by me, I myself will "
"take up arms; I myself will be your general, judge, and rewarder of every "
"one of your virtues in the field.\n"
"I know already, by your forwardness, that you have deserved rewards and "
"crowns; and we do assure you, on the word of a prince, they shall be duly "
"paid you. In the mean my lieutenant general shall be in my stead, than whom "
"never prince commanded a more noble and worthy subject; not doubting by "
"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[CONFIG_EXAMPLES_USBSERIAL_BUFSIZE];
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
#ifdef CONFIG_USBDEV_TRACE
static int trace_callback(struct usbtrace_s *trace, void *arg)
{
usbtrace_trprintf((trprintf_t)printf, trace->event, trace->value);
return 0;
}
static void dumptrace(void)
{
usbtrace_enumerate(trace_callback, NULL);
}
#else
# define dumptrace()
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* usbserial_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct boardioc_usbdev_ctrl_s ctrl;
FAR void *handle;
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
int infd;
#endif
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
int outfd;
#endif
#ifdef COUNTER_NEEDED
int count = 0;
#endif
ssize_t nbytes;
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
int i, j, k;
#endif
int ret;
/* Initialize the USB serial driver */
printf("usbserial_main: Registering USB serial driver\n");
#ifdef CONFIG_CDCACM
ctrl.usbdev = BOARDIOC_USBDEV_CDCACM;
ctrl.action = BOARDIOC_USBDEV_CONNECT;
ctrl.instance = 0;
ctrl.handle = &handle;
#else
ctrl.usbdev = BOARDIOC_USBDEV_PL2303;
ctrl.action = BOARDIOC_USBDEV_CONNECT;
ctrl.instance = 0;
ctrl.handle = &handle;
#endif
ret = boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl);
if (ret < 0)
{
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 defined(CONFIG_USBDEV_TRACE) && CONFIG_USBDEV_TRACE_INITIALIDSET != 0
/* If USB tracing is enabled and tracing of initial USB events is specified,
* then dump all collected trace data to stdout
*/
sleep(5);
dumptrace();
#endif
/* Then, in any event, configure trace data collection as configured */
usbtrace_enable(TRACE_BITSET);
/* Open the USB serial device for writing (blocking) */
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
do
{
printf("usbserial_main: Opening USB serial driver\n");
outfd = open(USBSER_DEVNAME, O_WRONLY);
if (outfd < 0)
{
int errcode = errno;
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME
" for writing: %d\n", errcode);
/* ENOTCONN means that the USB device is not yet connected */
if (errcode == ENOTCONN)
{
printf("usbserial_main: Not connected. Wait and try again.\n");
sleep(5);
}
else
{
/* Give up on other errors */
printf("usbserial_main: Aborting\n");
return 2;
}
}
/* If USB tracing is enabled, then dump all collected trace data to stdout */
dumptrace();
}
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(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK);
if (infd < 0)
{
printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME
" for reading: %d\n", errno);
close(outfd);
return 3;
}
#else
do
{
infd = open(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK);
if (infd < 0)
{
int errcode = 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 */
if (errcode == ENOTCONN)
{
printf("usbserial_main: Not connected. Wait and try again.\n");
sleep(5);
}
else
{
/* Give up on other errors */
printf("usbserial_main: Aborting\n");
return 3;
}
}
/* If USB tracing is enabled, then dump all collected trace data to stdout */
dumptrace();
}
while (infd < 0);
#endif
#endif
printf("usbserial_main: Successfully opened the serial driver\n");
/* Send messages and get responses -- forever */
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)
{
printf("usbserial_main: Saying hello\n");
nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg));
count++;
}
else
{
printf("usbserial_main: Reciting QEI's speech of 1588\n");
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));
#endif
/* Test if the write was successful */
if (nbytes < 0)
{
printf("usbserial_main: ERROR: write failed: %d\n", errno);
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
close(infd);
#endif
close(outfd);
return 4;
}
printf("usbserial_main: %ld bytes sent\n", (long)nbytes);
#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */
/* Test OUT (host-to-device) messages */
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
/* Poll for incoming messages */
printf("usbserial_main: Polling for OUT messages\n");
for (i = 0; i < 5; i++)
{
memset(g_iobuffer, 'X', CONFIG_EXAMPLES_USBSERIAL_BUFSIZE);
nbytes = read(infd, g_iobuffer, CONFIG_EXAMPLES_USBSERIAL_BUFSIZE);
if (nbytes < 0)
{
int errorcode = errno;
if (errorcode != EAGAIN)
{
printf("usbserial_main: ERROR: read failed: %d\n", errno);
close(infd);
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
close(outfd);
#endif
return 6;
}
}
else
{
printf("usbserial_main: Received %ld bytes:\n", (long)nbytes);
if (nbytes > 0)
{
for (j = 0; j < nbytes; j += 16)
{
printf("usbserial_main: %03x: ", j);
for (k = 0; k < 16; k++)
{
if (k == 8)
{
printf(" ");
}
if (j+k < nbytes)
{
printf("%02x", g_iobuffer[j+k]);
}
else
{
printf(" ");
}
}
printf(" ");
for (k = 0; k < 16; k++)
{
if (k == 8)
{
printf(" ");
}
if (j+k < nbytes)
{
if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f)
{
printf("%c", g_iobuffer[j+k]);
}
else
{
printf(".");
}
}
else
{
printf(" ");
}
}
printf("\n");
}
}
}
sleep(1);
}
#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
printf("usbserial_main: Waiting\n");
sleep(5);
#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
/* If USB tracing is enabled, then dump all collected trace data to stdout */
dumptrace();
}
/* 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;
}