2011-12-22 16:59:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/can/can_main.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2012-09-13 16:14:18 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-12-22 16:59:50 +01:00
|
|
|
*
|
|
|
|
* 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>
|
2015-08-18 19:21:47 +02:00
|
|
|
#include <sys/ioctl.h>
|
2011-12-22 16:59:50 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-05-12 16:57:41 +02:00
|
|
|
#include <nuttx/can/can.h>
|
2011-12-22 16:59:50 +01:00
|
|
|
|
|
|
|
#include "can.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#if defined(CONFIG_EXAMPLES_CAN_READ)
|
2012-03-14 02:19:27 +01:00
|
|
|
# define CAN_OFLAGS O_RDONLY
|
2015-11-10 21:13:13 +01:00
|
|
|
#elif defined(CONFIG_EXAMPLES_CAN_WRITE)
|
2012-03-14 02:19:27 +01:00
|
|
|
# define CAN_OFLAGS O_WRONLY
|
2015-11-10 21:13:13 +01:00
|
|
|
#elif defined(CONFIG_EXAMPLES_CAN_READWRITE)
|
2012-03-14 02:19:27 +01:00
|
|
|
# define CAN_OFLAGS O_RDWR
|
2015-11-10 21:13:13 +01:00
|
|
|
# define CONFIG_EXAMPLES_CAN_READ 1
|
|
|
|
# define CONFIG_EXAMPLES_CAN_WRITE 1
|
2012-03-14 02:19:27 +01:00
|
|
|
#endif
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
# ifdef CONFIG_CAN_EXTID
|
|
|
|
# define MAX_ID CAN_MAX_EXTMSGID
|
|
|
|
# else
|
|
|
|
# define MAX_ID CAN_MAX_STDMSGID
|
|
|
|
# endif
|
2015-08-17 19:07:39 +02:00
|
|
|
#endif
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
# ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
# ifdef CONFIG_CAN_EXTID
|
|
|
|
# define OPT_STR ":n:a:b:hs"
|
|
|
|
# else
|
|
|
|
# define OPT_STR ":n:a:b:h"
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# define OPT_STR ":n:h"
|
|
|
|
# endif
|
2012-01-20 22:52:35 +01:00
|
|
|
#else
|
2015-11-10 21:13:13 +01:00
|
|
|
# ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
# ifdef CONFIG_CAN_EXTID
|
|
|
|
# define OPT_STR ":a:b:hs"
|
|
|
|
# else
|
|
|
|
# define OPT_STR ":a:b:h"
|
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
# define OPT_STR ":h"
|
|
|
|
# endif
|
2012-01-20 22:52:35 +01:00
|
|
|
#endif
|
|
|
|
|
2011-12-22 16:59:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-17 19:07:39 +02:00
|
|
|
static void show_usage(FAR const char *progname)
|
|
|
|
{
|
2015-11-10 21:13:13 +01:00
|
|
|
fprintf(stderr, "USAGE: %s"
|
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
" [-n <nmsgs]"
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
2015-08-17 19:07:39 +02:00
|
|
|
#ifdef CONFIG_CAN_EXTID
|
2015-11-10 21:13:13 +01:00
|
|
|
" [-s]"
|
|
|
|
#endif
|
|
|
|
" [-a <min-id>] [b <max-id>]"
|
2015-08-17 19:07:39 +02:00
|
|
|
#endif
|
2015-11-10 21:13:13 +01:00
|
|
|
"\n",
|
|
|
|
progname);
|
2015-08-17 19:07:39 +02:00
|
|
|
fprintf(stderr, "USAGE: %s -h\n",
|
|
|
|
progname);
|
|
|
|
fprintf(stderr, "\nWhere:\n");
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
fprintf(stderr, "-n <nmsgs>: The number of messages to send. Default: 32\n");
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
2015-08-17 19:07:39 +02:00
|
|
|
#ifdef CONFIG_CAN_EXTID
|
|
|
|
fprintf(stderr, "-s: Use standard IDs. Default: Extended ID\n");
|
|
|
|
#endif
|
|
|
|
fprintf(stderr, "-a <min-id>: The start message id. Default 1\n");
|
2015-11-10 21:13:13 +01:00
|
|
|
fprintf(stderr, "-b <max-id>: The start message id. Default %d\n", MAX_ID);
|
|
|
|
#endif
|
2015-08-17 19:07:39 +02:00
|
|
|
fprintf(stderr, "-h: Show this message and exit\n");
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:59:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: can_main
|
2011-12-22 16:59:50 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:19:15 +02:00
|
|
|
#ifdef CONFIG_BUILD_KERNEL
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2014-09-06 17:19:15 +02:00
|
|
|
#else
|
2015-11-10 21:13:13 +01:00
|
|
|
int can_main(int argc, FAR char *argv[])
|
2014-09-06 17:19:15 +02:00
|
|
|
#endif
|
2011-12-22 16:59:50 +01:00
|
|
|
{
|
2015-08-18 19:21:47 +02:00
|
|
|
struct canioc_bittiming_s bt;
|
2015-11-10 21:13:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
2011-12-22 16:59:50 +01:00
|
|
|
struct can_msg_s txmsg;
|
2012-01-20 22:52:35 +01:00
|
|
|
#ifdef CONFIG_CAN_EXTID
|
2015-11-10 21:13:13 +01:00
|
|
|
bool extended = true;
|
2012-01-20 22:52:35 +01:00
|
|
|
uint32_t msgid;
|
|
|
|
#else
|
|
|
|
uint16_t msgid;
|
|
|
|
#endif
|
2015-11-10 21:13:13 +01:00
|
|
|
long minid = 1;
|
|
|
|
long maxid = MAX_ID;
|
2011-12-22 16:59:50 +01:00
|
|
|
uint8_t msgdata;
|
2012-03-14 02:19:27 +01:00
|
|
|
#endif
|
2016-06-23 16:04:37 +02:00
|
|
|
int msgdlc;
|
|
|
|
int i;
|
2012-03-14 02:19:27 +01:00
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_READ
|
2012-03-14 02:19:27 +01:00
|
|
|
struct can_msg_s rxmsg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size_t msgsize;
|
|
|
|
ssize_t nbytes;
|
2015-11-10 21:13:13 +01:00
|
|
|
bool badarg = false;
|
|
|
|
bool help = false;
|
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
long nmsgs = CONFIG_EXAMPLES_CAN_NMSGS;
|
|
|
|
long msgno;
|
|
|
|
#endif
|
2015-08-17 19:07:39 +02:00
|
|
|
int option;
|
2011-12-22 16:59:50 +01:00
|
|
|
int fd;
|
2015-11-10 21:13:13 +01:00
|
|
|
int errval = 0;
|
2011-12-22 16:59:50 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-08-17 19:07:39 +02:00
|
|
|
/* Parse command line parameters */
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
while ((option = getopt(argc, argv, OPT_STR)) != ERROR)
|
2011-12-22 16:59:50 +01:00
|
|
|
{
|
2015-08-17 19:07:39 +02:00
|
|
|
switch (option)
|
|
|
|
{
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
#ifdef CONFIG_CAN_EXTID
|
|
|
|
case 's':
|
|
|
|
extended = false;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2015-08-17 19:07:39 +02:00
|
|
|
case 'a':
|
|
|
|
minid = strtol(optarg, NULL, 10);
|
|
|
|
if (minid < 1 || minid > maxid)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "<min-id> out of range\n");
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
maxid = strtol(optarg, NULL, 10);
|
2015-11-10 21:13:13 +01:00
|
|
|
if (maxid < minid || maxid > MAX_ID)
|
2015-08-17 19:07:39 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: <max-id> out of range\n");
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
2015-11-10 21:13:13 +01:00
|
|
|
#endif
|
2015-08-17 19:07:39 +02:00
|
|
|
|
|
|
|
case 'h':
|
|
|
|
help = true;
|
|
|
|
break;
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
2015-08-17 19:07:39 +02:00
|
|
|
case 'n':
|
|
|
|
nmsgs = strtol(optarg, NULL, 10);
|
|
|
|
if (nmsgs < 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: <nmsgs> out of range\n");
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
2015-11-10 21:13:13 +01:00
|
|
|
#endif
|
2015-08-17 19:07:39 +02:00
|
|
|
|
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "ERROR: Bad option argument\n");
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "ERROR: Unrecognized option\n");
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (help)
|
|
|
|
{
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_SUCCESS;
|
2011-12-22 16:59:50 +01:00
|
|
|
}
|
2014-11-25 15:09:57 +01:00
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#if defined(CONFIG_EXAMPLES_CAN_WRITE) && defined(CONFIG_CAN_EXTID)
|
|
|
|
if (!extended && maxid > CAN_MAX_STDMSGID)
|
2015-08-17 19:07:39 +02:00
|
|
|
{
|
2015-11-10 21:13:13 +01:00
|
|
|
maxid = CAN_MAX_STDMSGID;
|
2015-08-17 19:07:39 +02:00
|
|
|
if (minid > maxid)
|
|
|
|
{
|
|
|
|
minid = maxid;
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 16:59:50 +01:00
|
|
|
#endif
|
|
|
|
|
2015-08-17 19:07:39 +02:00
|
|
|
if (optind != argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Garbage on command line\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
|
|
|
printf("nmsgs: %d\n", nmsgs);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
printf("min ID: %ld max ID: %ld\n", minid, maxid);
|
|
|
|
#endif
|
2015-08-17 19:07:39 +02:00
|
|
|
|
2016-12-06 15:54:38 +01:00
|
|
|
/* Initialization of the CAN hardware is performed by board-specific,
|
|
|
|
* logic external prior to running this test.
|
2011-12-22 16:59:50 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Open the CAN device for reading */
|
|
|
|
|
2012-03-14 02:19:27 +01:00
|
|
|
fd = open(CONFIG_EXAMPLES_CAN_DEVPATH, CAN_OFLAGS);
|
2011-12-22 16:59:50 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2015-08-18 20:41:10 +02:00
|
|
|
printf("ERROR: open %s failed: %d\n",
|
2015-11-10 21:13:13 +01:00
|
|
|
CONFIG_EXAMPLES_CAN_DEVPATH, errno);
|
2011-12-22 16:59:50 +01:00
|
|
|
errval = 2;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
/* Show bit timing information if provided by the driver. Not all CAN
|
2015-08-18 19:21:47 +02:00
|
|
|
* drivers will support this IOCTL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ioctl(fd, CANIOC_GET_BITTIMING, (unsigned long)((uintptr_t)&bt));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("Bit timing not available: %d\n", errno);
|
|
|
|
}
|
|
|
|
else
|
2015-11-10 21:13:13 +01:00
|
|
|
{
|
2015-08-18 19:21:47 +02:00
|
|
|
printf("Bit timing:\n");
|
|
|
|
printf(" Baud: %lu\n", (unsigned long)bt.bt_baud);
|
|
|
|
printf(" TSEG1: %u\n", bt.bt_tseg1);
|
|
|
|
printf(" TSEG2: %u\n", bt.bt_tseg2);
|
|
|
|
printf(" SJW: %u\n", bt.bt_sjw);
|
2015-11-10 21:13:13 +01:00
|
|
|
}
|
2015-08-18 19:21:47 +02:00
|
|
|
|
2011-12-22 16:59:50 +01:00
|
|
|
/* Now loop the appropriate number of times, performing one loopback test
|
|
|
|
* on each pass.
|
|
|
|
*/
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
2011-12-22 16:59:50 +01:00
|
|
|
msgdlc = 1;
|
2015-08-17 19:07:39 +02:00
|
|
|
msgid = minid;
|
2011-12-22 16:59:50 +01:00
|
|
|
msgdata = 0;
|
2012-03-14 02:19:27 +01:00
|
|
|
#endif
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_NSH_BUILTIN_APPS
|
2015-08-17 19:57:25 +02:00
|
|
|
for (msgno = 0; msgno < nmsgs; msgno++)
|
2015-11-10 21:13:13 +01:00
|
|
|
#else
|
|
|
|
for (; ; )
|
|
|
|
#endif
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
|
|
|
/* Flush any output before the loop entered or from the previous pass
|
|
|
|
* through the loop.
|
|
|
|
*/
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
fflush(stdout);
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
/* Construct the next TX message */
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
txmsg.cm_hdr.ch_id = msgid;
|
|
|
|
txmsg.cm_hdr.ch_rtr = false;
|
|
|
|
txmsg.cm_hdr.ch_dlc = msgdlc;
|
2016-01-18 19:34:19 +01:00
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
2015-08-18 16:50:14 +02:00
|
|
|
txmsg.cm_hdr.ch_error = 0;
|
2016-01-18 19:34:19 +01:00
|
|
|
#endif
|
2012-01-20 22:52:35 +01:00
|
|
|
#ifdef CONFIG_CAN_EXTID
|
2015-08-18 16:50:14 +02:00
|
|
|
txmsg.cm_hdr.ch_extid = extended;
|
2012-01-20 22:52:35 +01:00
|
|
|
#endif
|
2015-08-18 16:50:14 +02:00
|
|
|
txmsg.cm_hdr.ch_unused = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < msgdlc; i++)
|
|
|
|
{
|
|
|
|
txmsg.cm_data[i] = msgdata + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the TX message */
|
2012-01-20 22:52:35 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
msgsize = CAN_MSGLEN(msgdlc);
|
|
|
|
nbytes = write(fd, &txmsg, msgsize);
|
|
|
|
if (nbytes != msgsize)
|
|
|
|
{
|
|
|
|
printf("ERROR: write(%ld) returned %ld\n",
|
|
|
|
(long)msgsize, (long)nbytes);
|
|
|
|
errval = 3;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
printf(" ID: %4u DLC: %d\n", msgid, msgdlc);
|
2015-11-10 21:13:13 +01:00
|
|
|
|
2012-03-14 02:19:27 +01:00
|
|
|
#endif
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_READ
|
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
/* Read the RX message */
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
msgsize = sizeof(struct can_msg_s);
|
|
|
|
nbytes = read(fd, &rxmsg, msgsize);
|
|
|
|
if (nbytes < CAN_MSGLEN(0) || nbytes > msgsize)
|
|
|
|
{
|
|
|
|
printf("ERROR: read(%ld) returned %ld\n",
|
|
|
|
(long)msgsize, (long)nbytes);
|
|
|
|
errval = 4;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
printf(" ID: %4u DLC: %u\n",
|
|
|
|
rxmsg.cm_hdr.ch_id, rxmsg.cm_hdr.ch_dlc);
|
2012-03-14 02:19:27 +01:00
|
|
|
|
2017-05-18 00:07:34 +02:00
|
|
|
msgdlc = rxmsg.cm_hdr.ch_dlc;
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
#ifdef CONFIG_CAN_ERRORS
|
2015-08-18 16:50:14 +02:00
|
|
|
/* Check for error reports */
|
|
|
|
|
|
|
|
if (rxmsg.cm_hdr.ch_error != 0)
|
|
|
|
{
|
2015-11-10 21:13:13 +01:00
|
|
|
printf("ERROR: CAN error report: [0x%04x]\n", rxmsg.cm_hdr.ch_id);
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_TXTIMEOUT) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" TX timeout\n");
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_LOSTARB) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Lost arbitration: %02x\n", rxmsg.cm_data[0]);
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_CONTROLLER) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Controller error: %02x\n", rxmsg.cm_data[1]);
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_PROTOCOL) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Protocol error: %02x %02x\n", rxmsg.cm_data[2], rxmsg.cm_data[3]);
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_TRANSCEIVER) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Transceiver error: %02x\n", rxmsg.cm_data[4]);
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_NOACK) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" No ACK received on transmission\n");
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_BUSOFF) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Bus off\n");
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_BUSERROR) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Bus error\n");
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:34:19 +01:00
|
|
|
if ((rxmsg.cm_hdr.ch_id & CAN_ERROR_RESTARTED) != 0)
|
2015-08-18 16:50:14 +02:00
|
|
|
{
|
2016-01-18 19:34:19 +01:00
|
|
|
printf(" Controller restarted\n");
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-01-18 19:34:19 +01:00
|
|
|
#endif
|
2015-11-10 21:13:13 +01:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_EXAMPLES_CAN_WRITE) && defined(CONFIG_CAN_LOOPBACK)
|
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
/* Verify that the received messages are the same */
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
if (memcmp(&txmsg.cm_hdr, &rxmsg.cm_hdr, sizeof(struct can_hdr_s)) != 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: Sent header does not match received header:\n");
|
2015-11-10 21:13:13 +01:00
|
|
|
lib_dumpbuffer("Sent header",
|
|
|
|
(FAR const uint8_t *)&txmsg.cm_hdr,
|
2015-08-18 16:50:14 +02:00
|
|
|
sizeof(struct can_hdr_s));
|
2015-11-10 21:13:13 +01:00
|
|
|
lib_dumpbuffer("Received header",
|
|
|
|
(FAR const uint8_t *)&rxmsg.cm_hdr,
|
2015-08-18 16:50:14 +02:00
|
|
|
sizeof(struct can_hdr_s));
|
|
|
|
errval = 4;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(txmsg.cm_data, rxmsg.cm_data, msgdlc) != 0)
|
|
|
|
{
|
|
|
|
printf("ERROR: Data does not match. DLC=%d\n", msgdlc);
|
|
|
|
for (i = 0; i < msgdlc; i++)
|
|
|
|
{
|
2015-11-10 21:13:13 +01:00
|
|
|
printf(" %d: TX 0x%02x RX 0x%02x\n",
|
2015-08-18 16:50:14 +02:00
|
|
|
i, txmsg.cm_data[i], rxmsg.cm_data[i]);
|
|
|
|
errval = 5;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
/* Report success */
|
|
|
|
|
|
|
|
printf(" ID: %4u DLC: %d -- OK\n", msgid, msgdlc);
|
2015-08-18 16:50:14 +02:00
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* Print the data received */
|
|
|
|
|
|
|
|
printf("Data received:\n");
|
|
|
|
for (i = 0; i < msgdlc; i++)
|
|
|
|
{
|
|
|
|
printf(" %d: 0x%02x\n", i, rxmsg.cm_data[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2012-03-14 02:19:27 +01:00
|
|
|
#endif
|
2012-01-03 14:47:40 +01:00
|
|
|
|
2015-11-10 21:13:13 +01:00
|
|
|
#ifdef CONFIG_EXAMPLES_CAN_WRITE
|
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
/* Set up for the next pass */
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
msgdata += msgdlc;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
if (++msgid > maxid)
|
|
|
|
{
|
|
|
|
msgid = minid;
|
|
|
|
}
|
2011-12-22 16:59:50 +01:00
|
|
|
|
2015-08-18 16:50:14 +02:00
|
|
|
if (++msgdlc > CAN_MAXDATALEN)
|
|
|
|
{
|
|
|
|
msgdlc = 1;
|
|
|
|
}
|
2012-03-14 02:19:27 +01:00
|
|
|
#endif
|
2015-08-18 16:50:14 +02:00
|
|
|
}
|
2011-12-22 16:59:50 +01:00
|
|
|
|
|
|
|
errout_with_dev:
|
|
|
|
close(fd);
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Terminating!\n");
|
|
|
|
fflush(stdout);
|
2011-12-22 16:59:50 +01:00
|
|
|
return errval;
|
|
|
|
}
|