2008-10-07 01:09:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/usbserial/main.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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>
|
2008-10-09 17:02:26 +02:00
|
|
|
|
2008-10-09 22:25:33 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2008-10-07 01:09:23 +02:00
|
|
|
#include <stdio.h>
|
2008-10-09 17:02:26 +02:00
|
|
|
#include <unistd.h>
|
2008-10-10 18:45:32 +02:00
|
|
|
#include <string.h>
|
2008-10-09 22:25:33 +02:00
|
|
|
#include <fcntl.h>
|
2008-10-07 01:09:23 +02:00
|
|
|
#include <errno.h>
|
2008-10-09 17:02:26 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2008-10-07 01:09:23 +02:00
|
|
|
#include <nuttx/usbdev.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
#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
|
|
|
|
|
2008-10-08 01:04:52 +02:00
|
|
|
#ifdef CONFIG_CPP_HAVE_VARARGS
|
|
|
|
# ifdef CONFIG_DEBUG
|
|
|
|
# define message(...) lib_lowprintf(__VA_ARGS__)
|
|
|
|
# else
|
|
|
|
# define message(...) printf(__VA_ARGS__)
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifdef CONFIG_DEBUG
|
|
|
|
# define message lib_lowprintf
|
|
|
|
# else
|
|
|
|
# define message printf
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2008-10-09 22:25:33 +02:00
|
|
|
#define IOBUFFER_SIZE 256
|
|
|
|
|
2008-10-07 01:09:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYBIG
|
2008-10-09 22:25:33 +02:00
|
|
|
static const char g_shortmsg[] = "Hello, World!!\n";
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL
|
2008-10-09 22:25:33 +02:00
|
|
|
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";
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
static char g_iobuffer[IOBUFFER_SIZE];
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
2008-10-09 22:25:33 +02:00
|
|
|
|
2008-10-07 01:09:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* user_initialize
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_HAVE_WEAKFUNCTIONS
|
|
|
|
void user_initialize(void)
|
|
|
|
{
|
|
|
|
/* Stub that must be provided only if the toolchain does not support weak
|
|
|
|
* functions.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* user_start
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int user_start(int argc, char *argv[])
|
|
|
|
{
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
int infd;
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
int outfd;
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
|
|
|
#ifdef COUNTER_NEEDED
|
|
|
|
int count = 0;
|
|
|
|
#endif
|
2008-10-09 22:25:33 +02:00
|
|
|
ssize_t nbytes;
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
|
|
|
int i, j, k;
|
|
|
|
#endif
|
2008-10-07 01:09:23 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Initialize the USB serial driver */
|
|
|
|
|
2008-10-08 01:04:52 +02:00
|
|
|
message("user_start: Registering USB serial driver\n");
|
2008-10-07 01:09:23 +02:00
|
|
|
ret = usbdev_serialinitialize(0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-10-08 01:04:52 +02:00
|
|
|
message("user_start: ERROR: Failed to create the USB serial device: %d\n", -ret);
|
2008-10-07 01:09:23 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2008-10-08 01:04:52 +02:00
|
|
|
message("user_start: Successfully registered the serial driver\n");
|
2008-10-07 01:09:23 +02:00
|
|
|
|
2008-10-09 22:25:33 +02:00
|
|
|
/* Open the USB serial device for writing (blocking) */
|
2008-10-07 01:09:23 +02:00
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
|
2008-10-08 01:04:52 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
message("user_start: Opening USB serial driver\n");
|
2008-10-09 22:25:33 +02:00
|
|
|
outfd = open("/dev/ttyUSB0", O_WRONLY);
|
|
|
|
if (outfd < 0)
|
2008-10-08 01:04:52 +02:00
|
|
|
{
|
|
|
|
int errcode = errno;
|
2008-10-09 22:25:33 +02:00
|
|
|
message("user_start: ERROR: Failed to open /dev/ttyUSB0 for writing: %d\n", errcode);
|
2008-10-08 01:04:52 +02:00
|
|
|
|
|
|
|
/* ENOTCONN means that the USB device is not yet connected */
|
|
|
|
|
2008-10-09 17:02:26 +02:00
|
|
|
if (errcode == ENOTCONN)
|
2008-10-08 01:04:52 +02:00
|
|
|
{
|
|
|
|
message("user_start: Not connected. Wait and try again.\n");
|
|
|
|
sleep(5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Give up on other errors */
|
|
|
|
|
|
|
|
message("user_start: Aborting\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-09 22:25:33 +02:00
|
|
|
while (outfd < 0);
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
2008-10-09 22:25:33 +02:00
|
|
|
|
|
|
|
/* Open the USB serial device for reading (non-blocking) */
|
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
infd = open("/dev/ttyUSB0", O_RDONLY|O_NONBLOCK);
|
|
|
|
if (infd < 0)
|
|
|
|
{
|
|
|
|
message("user_start: ERROR: Failed to open /dev/ttyUSB0 for reading: %d\n", errno);
|
|
|
|
close(outfd);
|
|
|
|
return 3;
|
|
|
|
}
|
2008-10-10 18:45:32 +02:00
|
|
|
#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
|
2008-10-09 22:25:33 +02:00
|
|
|
|
2008-10-08 01:04:52 +02:00
|
|
|
message("user_start: Successfully opened the serial driver\n");
|
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
/* Send messages and get responses -- forever */
|
2008-10-08 01:04:52 +02:00
|
|
|
|
|
|
|
for (;;)
|
2008-10-07 01:09:23 +02:00
|
|
|
{
|
2008-10-10 18:45:32 +02:00
|
|
|
|
|
|
|
/* Test IN (device-to-host) messages */
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
|
|
|
|
#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL)
|
2008-10-09 22:25:33 +02:00
|
|
|
if (count < 8)
|
2008-10-08 01:04:52 +02:00
|
|
|
{
|
2008-10-09 22:25:33 +02:00
|
|
|
message("user_start: Saying hello\n");
|
|
|
|
nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg));
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
message("user_start: Reciting QEI's speech of 1588\n");
|
|
|
|
nbytes = write(outfd, g_longmsg, sizeof(g_longmsg));
|
|
|
|
count = 0;
|
2008-10-08 01:04:52 +02:00
|
|
|
}
|
2008-10-10 18:45:32 +02:00
|
|
|
#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 */
|
2008-10-08 01:04:52 +02:00
|
|
|
|
2008-10-09 22:25:33 +02:00
|
|
|
if (nbytes < 0)
|
2008-10-08 01:04:52 +02:00
|
|
|
{
|
2008-10-09 22:25:33 +02:00
|
|
|
message("user_start: ERROR: write failed: %d\n", errno);
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
close(infd);
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
2008-10-09 22:25:33 +02:00
|
|
|
close(outfd);
|
2008-10-08 01:04:52 +02:00
|
|
|
return 4;
|
|
|
|
}
|
2008-10-10 18:45:32 +02:00
|
|
|
message("user_start:%d bytes sent\n", nbytes);
|
|
|
|
#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */
|
2008-10-08 01:04:52 +02:00
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
/* Test OUT (host-to-device) messages */
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
/* Poll for incoming messages */
|
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
message("user_start: Polling for OUT messages\n");
|
2008-10-09 22:25:33 +02:00
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
{
|
2008-10-10 18:45:32 +02:00
|
|
|
memset(g_iobuffer, 'X', IOBUFFER_SIZE);
|
2008-10-09 22:25:33 +02:00
|
|
|
nbytes = read(infd, g_iobuffer, IOBUFFER_SIZE);
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
int errorcode = errno;
|
|
|
|
if (errorcode != EAGAIN)
|
|
|
|
{
|
|
|
|
message("user_start: ERROR: read failed: %d\n", errno);
|
|
|
|
close(infd);
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
close(outfd);
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
2008-10-09 22:25:33 +02:00
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
message("user_start: Received %d bytes:\n", nbytes);
|
|
|
|
if (nbytes > 0)
|
|
|
|
{
|
2008-10-10 18:45:32 +02:00
|
|
|
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");
|
|
|
|
}
|
2008-10-09 22:25:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
}
|
2008-10-10 18:45:32 +02:00
|
|
|
#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
|
|
|
|
message("user_start: Waiting\n");
|
|
|
|
sleep(5);
|
|
|
|
#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
|
2008-10-07 01:09:23 +02:00
|
|
|
}
|
|
|
|
|
2008-10-08 01:04:52 +02:00
|
|
|
/* Won't get here, but if we did this what we would have to do */
|
2008-10-07 01:09:23 +02:00
|
|
|
|
2008-10-10 18:45:32 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
close(infd);
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
|
2008-10-09 22:25:33 +02:00
|
|
|
close(outfd);
|
2008-10-10 18:45:32 +02:00
|
|
|
#endif
|
2008-10-07 01:09:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|