diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-03-14 01:19:27 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-03-14 01:19:27 +0000 |
commit | df6ed18f19686c2d27717803320576040b05842b (patch) | |
tree | 66d8c405ea691ef7b061d7f6d830eba7a6382045 /apps/examples/can | |
parent | 3f0eacb1397adfef5c6f8445fa1f28ccd18e616b (diff) | |
download | px4-nuttx-df6ed18f19686c2d27717803320576040b05842b.tar.gz px4-nuttx-df6ed18f19686c2d27717803320576040b05842b.tar.bz2 px4-nuttx-df6ed18f19686c2d27717803320576040b05842b.zip |
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
Diffstat (limited to 'apps/examples/can')
-rw-r--r-- | apps/examples/can/can.h | 2 | ||||
-rw-r--r-- | apps/examples/can/can_main.c | 48 |
2 files changed, 45 insertions, 5 deletions
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: |