From df6ed18f19686c2d27717803320576040b05842b Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 14 Mar 2012 01:19:27 +0000 Subject: Extend examples/can so that it can be used in other contexts git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4489 42af7a65-404d-4744-a932-0658087f49c3 --- apps/ChangeLog.txt | 4 ++++ apps/examples/README.txt | 7 +++++++ apps/examples/can/can.h | 2 ++ apps/examples/can/can_main.c | 48 +++++++++++++++++++++++++++++++++++++++----- 4 files changed, 56 insertions(+), 5 deletions(-) (limited to 'apps') diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index eb8cab861..ae4e5ec54 100755 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -204,3 +204,7 @@ allows NSH to be used on boards that have USB but no serial connectors. 6.17 2012-xx-xx Gregory Nutt + + * apps/examples/can: Add conditional compilation so that the test can be + configured to only send messages or to only receive messages. This will + let the test work in other modes than simple loopback testing. diff --git a/apps/examples/README.txt b/apps/examples/README.txt index 86edc6e33..54cbec978 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -110,6 +110,13 @@ examples/can built-in, the default is 32. Otherwise messages are sent and received indefinitely. + The default behavior assumes loopback mode. Messages are sent, then read + and verified. The behavior can be altered for other kinds of testing where + the test only sends or received (but does not verify) can messages. + + CONFIG_EXAMPLES_CAN_READONLY - Only receive messages + CONFIG_EXAMPLES_CAN_WRITEONLY - Only send messages + examples/cdcacm ^^^^^^^^^^^^^^^ diff --git a/apps/examples/can/can.h b/apps/examples/can/can.h index 0b781c669..53a6b63ea 100644 --- a/apps/examples/can/can.h +++ b/apps/examples/can/can.h @@ -65,6 +65,8 @@ * collected and the program terminates. Default: If built as an NSH * built-in, the default is 32. Otherwise messages are sent and received * indefinitely. + * CONFIG_EXAMPLES_CAN_READONLY - Only receive messages + * CONFIG_EXAMPLES_CAN_WRITEONLY - Only send messages */ #ifndef CONFIG_CAN diff --git a/apps/examples/can/can_main.c b/apps/examples/can/can_main.c index 27e19b156..c7b0859ba 100644 --- a/apps/examples/can/can_main.c +++ b/apps/examples/can/can_main.c @@ -57,6 +57,19 @@ * Pre-processor Definitions ****************************************************************************/ +#if defined(CONFIG_EXAMPLES_CAN_READONLY) +# undef CONFIG_EXAMPLES_CAN_WRITEONLY +# undef CONFIG_EXAMPLES_CAN_READWRITE +# define CAN_OFLAGS O_RDONLY +#elif defined(CONFIG_EXAMPLES_CAN_WRITEONLY) +# undef CONFIG_EXAMPLES_CAN_READWRITE +# define CAN_OFLAGS O_WRONLY +#else +# undef CONFIG_EXAMPLES_CAN_READWRITE +# define CONFIG_EXAMPLES_CAN_READWRITE 1 +# define CAN_OFLAGS O_RDWR +#endif + #ifdef CONFIG_CAN_EXTID # define MAX_ID (1 << 29) #else @@ -101,20 +114,27 @@ int MAIN_NAME(int argc, char *argv[]) { +#ifndef CONFIG_EXAMPLES_CAN_READONLY struct can_msg_s txmsg; - struct can_msg_s rxmsg; - size_t msgsize; - ssize_t nbytes; #ifdef CONFIG_CAN_EXTID uint32_t msgid; #else uint16_t msgid; #endif + int msgdlc; uint8_t msgdata; +#endif + +#ifndef CONFIG_EXAMPLES_CAN_WRITEONLY + struct can_msg_s rxmsg; +#endif + + size_t msgsize; + ssize_t nbytes; #if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_CAN_NMSGS) long nmsgs; #endif - int msgdlc; + int fd; int errval = 0; int ret; @@ -151,7 +171,7 @@ int MAIN_NAME(int argc, char *argv[]) /* Open the CAN device for reading */ message(MAIN_STRING "Hardware initialized. Opening the CAN device\n"); - fd = open(CONFIG_EXAMPLES_CAN_DEVPATH, O_RDWR); + fd = open(CONFIG_EXAMPLES_CAN_DEVPATH, CAN_OFLAGS); if (fd < 0) { message(MAIN_STRING "open %s failed: %d\n", @@ -164,9 +184,11 @@ int MAIN_NAME(int argc, char *argv[]) * on each pass. */ +#ifndef CONFIG_EXAMPLES_CAN_READONLY msgdlc = 1; msgid = 1; msgdata = 0; +#endif #if defined(CONFIG_NSH_BUILTIN_APPS) for (; nmsgs > 0; nmsgs--) @@ -184,6 +206,7 @@ int MAIN_NAME(int argc, char *argv[]) /* Construct the next TX message */ +#ifndef CONFIG_EXAMPLES_CAN_READONLY txmsg.cm_hdr.ch_id = msgid; txmsg.cm_hdr.ch_rtr = false; txmsg.cm_hdr.ch_dlc = msgdlc; @@ -207,8 +230,14 @@ int MAIN_NAME(int argc, char *argv[]) goto errout_with_dev; } +#ifndef CONFIG_EXAMPLES_CAN_READWRITE + message(" ID: %4d DLC: %d\n", msgid, msgdlc); +#endif +#endif + /* Read the RX message */ +#ifndef CONFIG_EXAMPLES_CAN_WRITEONLY msgsize = sizeof(struct can_msg_s); nbytes = read(fd, &rxmsg, msgsize); if (nbytes < CAN_MSGLEN(0) || nbytes > msgsize) @@ -218,8 +247,14 @@ int MAIN_NAME(int argc, char *argv[]) goto errout_with_dev; } +#ifndef CONFIG_EXAMPLES_CAN_READWRITE + message(" ID: %4d DLC: %d\n", rxmsg.cm_hdr.id, rxmsg.cm_hdr.dlc); +#endif +#endif + /* Verify that the received messages are the same */ +#ifdef CONFIG_EXAMPLES_CAN_READWRITE if (memcmp(&txmsg.cm_hdr, &rxmsg.cm_hdr, sizeof(struct can_hdr_s)) != 0) { message("ERROR: Sent header does not match received header:\n"); @@ -245,9 +280,11 @@ int MAIN_NAME(int argc, char *argv[]) /* Report success */ message(" ID: %4d DLC: %d -- OK\n", msgid, msgdlc); +#endif /* Set up for the next pass */ +#ifndef CONFIG_EXAMPLES_CAN_READONLY msgdata += msgdlc; if (++msgid >= MAX_ID) @@ -259,6 +296,7 @@ int MAIN_NAME(int argc, char *argv[]) { msgdlc = 1; } +#endif } errout_with_dev: -- cgit v1.2.3