[U-Boot] [RFC 2/5] CAN device test command

Wolfgang Grandegger wg at grandegger.com
Sun Nov 1 12:33:34 CET 2009


From: Wolfgang Grandegger <wg at denx.de>

Signed-off-by: Wolfgang Grandegger <wg at denx.de>
---
 common/Makefile  |    1 +
 common/cmd_can.c |  119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 0 deletions(-)
 create mode 100644 common/cmd_can.c

diff --git a/common/Makefile b/common/Makefile
index 3781738..b7f4c22 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -72,6 +72,7 @@ COBJS-$(CONFIG_CMD_BEDBUG) += bedbug.o cmd_bedbug.o
 COBJS-$(CONFIG_CMD_BMP) += cmd_bmp.o
 COBJS-$(CONFIG_CMD_BOOTLDR) += cmd_bootldr.o
 COBJS-$(CONFIG_CMD_CACHE) += cmd_cache.o
+COBJS-$(CONFIG_CMD_CAN) += cmd_can.o
 COBJS-$(CONFIG_CMD_CONSOLE) += cmd_console.o
 COBJS-$(CONFIG_CMD_CPLBINFO) += cmd_cplbinfo.o
 COBJS-$(CONFIG_DATAFLASH_MMC_SELECT) += cmd_dataflash_mmc_mux.o
diff --git a/common/cmd_can.c b/common/cmd_can.c
new file mode 100644
index 0000000..af7bf34
--- /dev/null
+++ b/common/cmd_can.c
@@ -0,0 +1,119 @@
+/*
+ * (C) Copyright 2007-2009, Wolfgang Grandegger <wg at denx.de>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+/*
+ * CAN device test command
+ */
+#include <common.h>
+#include <command.h>
+#include <can.h>
+
+static struct can_dev *can_dev;
+
+int do_can (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	struct can_msg msg;
+	char op;
+	int i;
+
+	op = argv[1][0];
+
+	if (argc < 2) {
+		can_list ();
+		return 0;
+	}
+
+	if (!can_dev && op != 'i') {
+		can_dev = can_init (0, 0);
+		if (!can_dev)
+			return 1;
+	}
+
+	if (op == 's') {
+		unsigned int level = 0;
+		if (argc > 2)
+			level = simple_strtoul (argv[2], NULL, 10);
+		can_status (can_dev, level);
+	}
+
+	else if (op == 'i') {
+		unsigned int dev_num = 0, ibaud = 0;
+		struct can_dev *dev;
+
+		if (argc > 2)
+			dev_num = simple_strtoul (argv[2], NULL, 10);
+		if (argc > 3) {
+			ibaud = simple_strtoul (argv[3], NULL, 10);
+			if (ibaud > 2)
+				ibaud = 2;
+		}
+		dev = can_init (dev_num, ibaud);
+		if (!dev)
+			return 1;
+		can_dev = dev;
+	}
+
+	else if (op == 'r') {
+		while (!can_recv (can_dev, &msg)) {
+			printf ("<0x%03x>", msg.id & CAN_SFF_MASK);
+
+			printf (" [%d]", msg.dlc);
+			if (msg.id & CAN_RTR_FLAG)
+				puts (" rtr");
+			else {
+				for (i = 0; i < msg.dlc; i++)
+					printf (" %02x", msg.data[i]);
+			}
+			puts ("\n");
+		}
+	} else if (op == 'x') {
+		memset (&msg, 0, sizeof (msg));
+		msg.id = 0x123;
+		if (argc > 2)
+		    msg.id = simple_strtoul (argv[2], NULL, 16);
+		for (i = 0; argc > (3 + i); i++, msg.dlc++) {
+			msg.data[i] = simple_strtoul (argv[3 + i], NULL, 16);
+		}
+		if (argc == 2)
+			printf ("Transmitting id %#x dlc %d\n",
+				msg.id, msg.dlc);
+
+		if (can_xmit (can_dev, &msg))
+			puts("FAILED\n");
+		else
+			puts("OK\n");
+	} else {
+		printf ("Usage:\n%s\n", cmdtp->usage);
+		return 1;
+	}
+
+	return 0;
+}
+
+U_BOOT_CMD(
+	can, 3,	1, do_can,
+	"can - CAN bus commands\n",
+	"can status [level]\n"
+	"can init [dev] [baud-index]\n"
+	"can xmit [id] [d0] [d1] ... [d7]\n"
+	"can recv, abort with CTRL-C\n"
+);
-- 
1.6.2.5



More information about the U-Boot mailing list