[U-Boot] [PATCH 3/3] digsy MTC: Add 'mtc' command.

Grzegorz Bernacki gjb at semihalf.com
Wed May 27 11:22:59 CEST 2009


New command allows to:
 o check FW version
 o set LED status
 o set digital output status
 o get digital input status

Signed-off-by: Grzegorz Bernacki <gjb at semihalf.com>
---
 board/digsy_mtc/Makefile  |    2 +-
 board/digsy_mtc/cmd_mtc.c |  287 +++++++++++++++++++++++++++++++++++++++++++++
 board/digsy_mtc/cmd_mtc.h |   60 ++++++++++
 3 files changed, 348 insertions(+), 1 deletions(-)
 create mode 100644 board/digsy_mtc/cmd_mtc.c
 create mode 100644 board/digsy_mtc/cmd_mtc.h

diff --git a/board/digsy_mtc/Makefile b/board/digsy_mtc/Makefile
index 7d659e5..0bededc 100644
--- a/board/digsy_mtc/Makefile
+++ b/board/digsy_mtc/Makefile
@@ -7,7 +7,7 @@ include $(TOPDIR)/config.mk
 
 LIB	= $(obj)lib$(BOARD).a
 
-COBJS	:= $(BOARD).o
+COBJS	:= $(BOARD).o cmd_mtc.o
 
 SRCS	:= $(SOBJS:.o=.S) $(COBJS:.o=.c)
 OBJS	:= $(addprefix $(obj),$(COBJS))
diff --git a/board/digsy_mtc/cmd_mtc.c b/board/digsy_mtc/cmd_mtc.c
new file mode 100644
index 0000000..c05d5da
--- /dev/null
+++ b/board/digsy_mtc/cmd_mtc.c
@@ -0,0 +1,287 @@
+/*
+ * (C) Copyright 2009
+ * Werner Pfister <Pfister_Werner at intercontrol.de>
+ *
+ * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
+ *
+ * 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
+ */
+
+#include <common.h>
+#include <command.h>
+#include <mpc5xxx.h>
+#include "spi.h"
+#include "cmd_mtc.h"
+
+static const char *led_names[] = {
+	"diag",
+	"can1",
+	"can2",
+	"can3",
+	"can4",
+	"usbpwr",
+	"usbbusy",
+	"user1",
+	"user2",
+	""
+};
+
+static void mtc_calculate_checksum(tx_msp_cmd *packet)
+{
+	int i;
+	uchar *buff;
+
+	buff = (uchar *) packet;
+
+	for (i = 0; i < 6; i++)
+		packet->cks += buff[i];
+}
+
+static int subcmd_led(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	tx_msp_cmd pcmd;
+	rx_msp_cmd prx;
+	int err = 0;
+	int i;
+
+	if (argc <= 2) {
+		printf("Usage:\n%s\n", cmdtp->help);
+		return -1;
+	}
+
+	memset(&pcmd, 0, sizeof(pcmd));
+	memset(&prx, 0, sizeof(prx));
+
+	pcmd.cmd = CMD_SET_LED;
+
+	pcmd.cmd_val0 = 0xff;
+	for (i = 0; strlen(led_names[i]) != 0; i++) {
+		if (strncmp(argv[2], led_names[i], strlen(led_names[i])) == 0) {
+			pcmd.cmd_val0 = i;
+			break;
+		}
+	}
+
+	if (pcmd.cmd_val0 == 0xff) {
+		printf("Usage:\n%s\n", cmdtp->help);
+		return -1;
+	}
+
+	if (argc >= 4) {
+		if (strncmp(argv[3], "red", 3) == 0)
+			pcmd.cmd_val1 = 1;
+		else if (strncmp(argv[3], "green", 5) == 0)
+			pcmd.cmd_val1 = 2;
+		else if (strncmp(argv[3], "orange", 6) == 0)
+			pcmd.cmd_val1 = 3;
+		else
+			pcmd.cmd_val1 = 0;
+	}
+
+	if (argc >= 5)
+		pcmd.cmd_val2 = simple_strtol(argv[4], NULL, 10);
+	else
+		pcmd.cmd_val2 = 0;
+
+	mtc_calculate_checksum(&pcmd);
+	err = spi_xfer(NULL, MTC_TRANSFER_SIZE, &pcmd, &prx,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+
+	return err;
+}
+
+static int subcmd_key(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	tx_msp_cmd pcmd;
+	rx_msp_cmd prx;
+	int err = 0;
+
+	memset(&pcmd, 0, sizeof(pcmd));
+	memset(&prx, 0, sizeof(prx));
+
+	pcmd.cmd = CMD_GET_VIM;
+
+	mtc_calculate_checksum(&pcmd);
+	err = spi_xfer(NULL, MTC_TRANSFER_SIZE, &pcmd, &prx,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+
+	if (!err) {
+		/* function returns '0' if key is pressed */
+		err = (prx.input & 0x80) ? 0 : 1;
+	}
+
+	return err;
+}
+
+static int subcmd_digout(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	tx_msp_cmd pcmd;
+	rx_msp_cmd prx;
+	int err = 0;
+	uchar channel_mask = 0;
+
+	if (argc < 4) {
+		printf("Usage:\n%s\n", cmdtp->help);
+		return -1;
+	}
+
+	if (strncmp(argv[2], "on", 2) == 0)
+		channel_mask |= 1;
+	if (strncmp(argv[3], "on", 2) == 0)
+		channel_mask |= 2;
+
+	memset(&pcmd, 0, sizeof(pcmd));
+	memset(&prx, 0, sizeof(prx));
+
+	pcmd.cmd = CMD_GET_VIM;
+	pcmd.user_out = channel_mask;
+
+	mtc_calculate_checksum(&pcmd);
+	err = spi_xfer(NULL, MTC_TRANSFER_SIZE, &pcmd, &prx,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+
+	return err;
+}
+
+static int subcmd_digin(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	tx_msp_cmd pcmd;
+	rx_msp_cmd prx;
+	int err = 0;
+	uchar channel_num = 0;
+
+	if (argc < 3) {
+		printf("Usage:\n%s\n", cmdtp->help);
+		return -1;
+	}
+
+	channel_num = simple_strtol(argv[2], NULL, 10);
+	if ((channel_num != 1) && (channel_num != 2)) {
+		printf("invalid parameter\n");
+		return -1;
+	}
+
+	memset(&pcmd, 0, sizeof(pcmd));
+	memset(&prx, 0, sizeof(prx));
+
+	pcmd.cmd = CMD_GET_VIM;
+
+	mtc_calculate_checksum(&pcmd);
+	err = spi_xfer(NULL, MTC_TRANSFER_SIZE, &pcmd, &prx,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+
+	if (!err) {
+		/* function returns '0' when digin is on */
+		err = (prx.input & channel_num) ? 0 : 1;
+	}
+
+	return err;
+}
+
+static int subcmd_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	tx_msp_cmd pcmd;
+	rx_msp_cmd prx;
+	int err;
+	char buf[5];
+
+	/* read appreg */
+	memset(&pcmd, 0, sizeof(pcmd));
+	memset(&prx, 0, sizeof(prx));
+
+	pcmd.cmd = CMD_WD_PARA;
+	pcmd.cmd_val0 = 5;	/* max. Count */
+	pcmd.cmd_val1 = 5;	/* max. Time */
+	pcmd.cmd_val2 = 0;	/* =0 means read appreg */
+
+	mtc_calculate_checksum(&pcmd);
+	err = spi_xfer(NULL, MTC_TRANSFER_SIZE, &pcmd, &prx,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+	if (!err) {
+		sprintf(buf, "%d", prx.ack2);
+		setenv("appreg", buf);
+	}
+
+	return err;
+}
+
+static int subcmd_version(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	tx_msp_cmd pcmd;
+	rx_msp_cmd prx;
+	int err = 0;
+
+	memset(&pcmd, 0, sizeof(pcmd));
+	memset(&prx, 0, sizeof(prx));
+
+	pcmd.cmd = CMD_FW_VERSION;
+
+	mtc_calculate_checksum(&pcmd);
+	err = spi_xfer(NULL, MTC_TRANSFER_SIZE, &pcmd, &prx,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+
+	if (!err) {
+		printf("FW V%d.%d.%d / HW %d\n",
+		       prx.ack0, prx.ack1, prx.ack3, prx.ack2);
+	}
+
+	return err;
+}
+
+int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+	int err = 0;
+
+	if (argc > 1) {
+		if (strncmp(argv[1], "led", 3) == 0)
+			err = subcmd_led(cmdtp, flag, argc, argv);
+		else if (strncmp(argv[1], "key", 3) == 0)
+			err = subcmd_key(cmdtp, flag, argc, argv);
+		else if (strncmp(argv[1], "version", 7) == 0)
+			err = subcmd_version(cmdtp, flag, argc, argv);
+		else if (strncmp(argv[1], "appreg", 6) == 0)
+			err = subcmd_appreg(cmdtp, flag, argc, argv);
+		else if (strncmp(argv[1], "digin", 5) == 0)
+			err = subcmd_digin(cmdtp, flag, argc, argv);
+		else if (strncmp(argv[1], "digout", 6) == 0)
+			err = subcmd_digout(cmdtp, flag, argc, argv);
+		else {
+			printf("Usage:\n%s\n", cmdtp->help);
+			err = 1;
+		}
+	} else {
+		printf("Usage:\n%s\n", cmdtp->help);
+		err = 1;
+	}
+
+	return err;
+}
+
+U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
+	   "mtc     - special commands for digsyMTC\n",
+	   "mtc led [ledname] [state] [blink] - set state of leds\n"
+	   "    - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
+	   "    - state: off red green orange\n"
+	   "    - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"
+	   "mtc key - returns state of user key\n"
+	   "mtc version - returns firmware version of supervisor uC\n"
+	   "mtc appreg - reads appreg value and stores in environment variable"
+	   "'appreg'\n"
+	   "mtc digin channel - returns state of digital input (1 or 2)\n"
+	   "mtc digout ch1 ch2 - sets digital outputs ('on' or 'off')\n");
diff --git a/board/digsy_mtc/cmd_mtc.h b/board/digsy_mtc/cmd_mtc.h
new file mode 100644
index 0000000..db3aeed
--- /dev/null
+++ b/board/digsy_mtc/cmd_mtc.h
@@ -0,0 +1,60 @@
+/*
+ * (C) Copyright 2009
+ * Werner Pfister <Pfister_Werner at intercontrol.de>
+ *
+ * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
+ *
+ * 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
+ */
+
+#ifndef CMD_MTC_H
+#define CMD_MTC_H
+
+#define	CMD_WD_PARA		0x02
+#define	CMD_FW_VERSION		0x10
+#define	CMD_GET_VIM		0x30
+#define	CMD_SET_LED		0x40
+
+typedef struct {
+	u8 cmd;
+	u8 sys_in;
+	u8 cmd_val0;
+	u8 cmd_val1;
+	u8 cmd_val2;
+	u8 user_out;
+	u8 cks;
+	u8 dummy1;
+	u8 dummy2;
+} tx_msp_cmd;
+
+typedef struct {
+	u8 input;
+	u8 state;
+	u8 ack2;
+	u8 ack3;
+	u8 ack0;
+	u8 ack1;
+	u8 ack;
+	u8 dummy;
+	u8 cks;
+} rx_msp_cmd;
+
+#define MTC_TRANSFER_SIZE (sizeof(tx_msp_cmd) * 8)
+
+#endif
-- 
1.6.0.6



More information about the U-Boot mailing list