3 * Werner Pfister <Pfister_Werner@intercontrol.de>
5 * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 DECLARE_GLOBAL_DATA_PTR;
34 static const char *led_names[] = {
47 static int msp430_xfer(const void *dout, void *din)
51 err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
52 SPI_XFER_BEGIN | SPI_XFER_END);
54 /* The MSP chip needs time to ready itself for the next command */
60 static void mtc_calculate_checksum(tx_msp_cmd *packet)
65 buff = (uchar *) packet;
67 for (i = 0; i < 6; i++)
68 packet->cks += buff[i];
71 static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
83 memset(&pcmd, 0, sizeof(pcmd));
84 memset(&prx, 0, sizeof(prx));
86 pcmd.cmd = CMD_SET_LED;
89 for (i = 0; strlen(led_names[i]) != 0; i++) {
90 if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
96 if (pcmd.cmd_val0 == 0xff) {
97 printf("Usage:\n%s\n", cmdtp->help);
102 if (strncmp(argv[2], "red", 3) == 0)
104 else if (strncmp(argv[2], "green", 5) == 0)
106 else if (strncmp(argv[2], "orange", 6) == 0)
113 pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
117 mtc_calculate_checksum(&pcmd);
118 err = msp430_xfer(&pcmd, &prx);
123 static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
129 memset(&pcmd, 0, sizeof(pcmd));
130 memset(&prx, 0, sizeof(prx));
132 pcmd.cmd = CMD_GET_VIM;
134 mtc_calculate_checksum(&pcmd);
135 err = msp430_xfer(&pcmd, &prx);
138 /* function returns '0' if key is pressed */
139 err = (prx.input & 0x80) ? 0 : 1;
145 static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
150 uchar channel_mask = 0;
157 if (strncmp(argv[1], "on", 2) == 0)
159 if (strncmp(argv[2], "on", 2) == 0)
162 memset(&pcmd, 0, sizeof(pcmd));
163 memset(&prx, 0, sizeof(prx));
165 pcmd.cmd = CMD_GET_VIM;
166 pcmd.user_out = channel_mask;
168 mtc_calculate_checksum(&pcmd);
169 err = msp430_xfer(&pcmd, &prx);
174 static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
179 uchar channel_num = 0;
186 channel_num = simple_strtol(argv[1], NULL, 10);
187 if ((channel_num != 1) && (channel_num != 2)) {
188 printf("mtc digin: invalid parameter - must be '1' or '2'\n");
192 memset(&pcmd, 0, sizeof(pcmd));
193 memset(&prx, 0, sizeof(prx));
195 pcmd.cmd = CMD_GET_VIM;
197 mtc_calculate_checksum(&pcmd);
198 err = msp430_xfer(&pcmd, &prx);
201 /* function returns '0' when digin is on */
202 err = (prx.input & channel_num) ? 0 : 1;
208 static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
216 memset(&pcmd, 0, sizeof(pcmd));
217 memset(&prx, 0, sizeof(prx));
219 pcmd.cmd = CMD_WD_PARA;
220 pcmd.cmd_val0 = 5; /* max. Count */
221 pcmd.cmd_val1 = 5; /* max. Time */
222 pcmd.cmd_val2 = 0; /* =0 means read appreg */
224 mtc_calculate_checksum(&pcmd);
225 err = msp430_xfer(&pcmd, &prx);
228 sprintf(buf, "%d", prx.ack2);
229 setenv("appreg", buf);
235 static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
241 memset(&pcmd, 0, sizeof(pcmd));
242 memset(&prx, 0, sizeof(prx));
244 pcmd.cmd = CMD_FW_VERSION;
246 mtc_calculate_checksum(&pcmd);
247 err = msp430_xfer(&pcmd, &prx);
250 printf("FW V%d.%d.%d / HW %d\n",
251 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
257 static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
263 memset(&pcmd, 0, sizeof(pcmd));
264 memset(&prx, 0, sizeof(prx));
266 pcmd.cmd = CMD_WD_WDSTATE;
269 mtc_calculate_checksum(&pcmd);
270 err = msp430_xfer(&pcmd, &prx);
273 printf("State %02Xh\n", prx.state);
274 printf("Input %02Xh\n", prx.input);
275 printf("UserWD %02Xh\n", prx.ack2);
276 printf("Sys WD %02Xh\n", prx.ack3);
277 printf("WD Timout %02Xh\n", prx.ack0);
278 printf("eSysState %02Xh\n", prx.ack1);
284 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
286 cmd_tbl_t cmd_mtc_sub[] = {
287 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
289 "[ledname] [state] [blink]\n"
290 " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
291 " - state: off red green orange\n"
292 " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
293 U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
294 "returns state of user key", ""),
295 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
296 "returns firmware version of supervisor uC", ""),
297 U_BOOT_CMD_MKENT(appreg, 0, 1, do_mtc_appreg,
298 "reads appreg value and stores in environment variable 'appreg'", ""),
299 U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
300 "returns state of digital input",
301 "<channel_num> - get state of digital input (1 or 2)\n"),
302 U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
303 "sets digital outputs",
304 "<on|off> <on|off>- set state of digital output 1 and 2\n"),
305 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
306 "displays state", ""),
307 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
308 "[command] - get help for command\n"),
311 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
313 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
314 cmd_tbl_t *cmdtp, int flag,
315 int argc, char *argv[]);
316 #ifdef CONFIG_SYS_LONGHELP
319 return _do_help(&cmd_mtc_sub[0],
320 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
323 /* Relocate the command table function pointers when running in RAM */
324 int mtc_cmd_init_r(void)
328 for (cmdtp = &cmd_mtc_sub[0]; cmdtp !=
329 &cmd_mtc_sub[ARRAY_SIZE(cmd_mtc_sub)]; cmdtp++) {
332 addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
334 (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
336 addr = (ulong)(cmdtp->name) + gd->reloc_off;
337 cmdtp->name = (char *)addr;
340 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
341 cmdtp->usage = (char *)addr;
343 #ifdef CONFIG_SYS_LONGHELP
345 addr = (ulong)(cmdtp->help) + gd->reloc_off;
346 cmdtp->help = (char *)addr;
353 int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
358 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
362 return c->cmd(c, flag, argc, argv);
364 /* Unrecognized command */
372 U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
373 "special commands for digsyMTC",
374 "[subcommand] [args...]\n"
375 "Subcommands list:\n"
376 "led [ledname] [state] [blink] - set state of leds\n"
377 " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
378 " [state]: off red green orange\n"
379 " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
380 "key - returns state of user key\n"
381 "version - returns firmware version of supervisor uC\n"
382 "appreg - reads appreg value and stores in environment variable"
384 "digin [channel] - returns state of digital input (1 or 2)\n"
385 "digout <on|off> <on|off> - sets state of two digital outputs\n"
386 "state - displays state\n"
387 "help [subcommand] - get help for subcommand\n"