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 uchar user_out;
36 static const char *led_names[] = {
49 static int msp430_xfer(const void *dout, void *din)
53 err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
54 SPI_XFER_BEGIN | SPI_XFER_END);
56 /* The MSP chip needs time to ready itself for the next command */
62 static void mtc_calculate_checksum(tx_msp_cmd *packet)
67 buff = (uchar *) packet;
69 for (i = 0; i < 6; i++)
70 packet->cks += buff[i];
73 static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
81 return cmd_usage(cmdtp);
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 pcmd.user_out = user_out;
119 mtc_calculate_checksum(&pcmd);
120 err = msp430_xfer(&pcmd, &prx);
125 static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
131 memset(&pcmd, 0, sizeof(pcmd));
132 memset(&prx, 0, sizeof(prx));
134 pcmd.cmd = CMD_GET_VIM;
135 pcmd.user_out = user_out;
137 mtc_calculate_checksum(&pcmd);
138 err = msp430_xfer(&pcmd, &prx);
141 /* function returns '0' if key is pressed */
142 err = (prx.input & 0x80) ? 0 : 1;
148 static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
153 uchar channel_mask = 0;
156 return cmd_usage(cmdtp);
158 if (strncmp(argv[1], "on", 2) == 0)
160 if (strncmp(argv[2], "on", 2) == 0)
163 memset(&pcmd, 0, sizeof(pcmd));
164 memset(&prx, 0, sizeof(prx));
166 pcmd.cmd = CMD_GET_VIM;
167 pcmd.user_out = channel_mask;
168 user_out = channel_mask;
170 mtc_calculate_checksum(&pcmd);
171 err = msp430_xfer(&pcmd, &prx);
176 static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
181 uchar channel_num = 0;
184 return cmd_usage(cmdtp);
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;
196 pcmd.user_out = user_out;
198 mtc_calculate_checksum(&pcmd);
199 err = msp430_xfer(&pcmd, &prx);
202 /* function returns '0' when digin is on */
203 err = (prx.input & channel_num) ? 0 : 1;
209 static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
218 memset(&pcmd, 0, sizeof(pcmd));
219 memset(&prx, 0, sizeof(prx));
221 pcmd.cmd = CMD_WD_PARA;
222 pcmd.cmd_val0 = 5; /* max. Count */
223 pcmd.cmd_val1 = 5; /* max. Time */
224 pcmd.cmd_val2 = 0; /* =0 means read appreg */
225 pcmd.user_out = user_out;
227 mtc_calculate_checksum(&pcmd);
228 err = msp430_xfer(&pcmd, &prx);
230 /* on success decide between read or write */
233 appreg = simple_strtol(argv[1], NULL, 10);
235 printf("mtc appreg: invalid parameter - "
236 "must be between 1 and 255\n");
239 memset(&pcmd, 0, sizeof(pcmd));
240 pcmd.cmd = CMD_WD_PARA;
241 pcmd.cmd_val0 = prx.ack3; /* max. Count */
242 pcmd.cmd_val1 = prx.ack0; /* max. Time */
243 pcmd.cmd_val2 = appreg; /* !=0 means write appreg */
244 pcmd.user_out = user_out;
245 memset(&prx, 0, sizeof(prx));
247 mtc_calculate_checksum(&pcmd);
248 err = msp430_xfer(&pcmd, &prx);
250 sprintf(buf, "%d", prx.ack2);
251 setenv("appreg", buf);
258 static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
264 memset(&pcmd, 0, sizeof(pcmd));
265 memset(&prx, 0, sizeof(prx));
267 pcmd.cmd = CMD_FW_VERSION;
268 pcmd.user_out = user_out;
270 mtc_calculate_checksum(&pcmd);
271 err = msp430_xfer(&pcmd, &prx);
274 printf("FW V%d.%d.%d / HW %d\n",
275 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
281 static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
287 memset(&pcmd, 0, sizeof(pcmd));
288 memset(&prx, 0, sizeof(prx));
290 pcmd.cmd = CMD_WD_WDSTATE;
292 pcmd.user_out = user_out;
294 mtc_calculate_checksum(&pcmd);
295 err = msp430_xfer(&pcmd, &prx);
298 printf("State %02Xh\n", prx.state);
299 printf("Input %02Xh\n", prx.input);
300 printf("UserWD %02Xh\n", prx.ack2);
301 printf("Sys WD %02Xh\n", prx.ack3);
302 printf("WD Timout %02Xh\n", prx.ack0);
303 printf("eSysState %02Xh\n", prx.ack1);
309 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
311 cmd_tbl_t cmd_mtc_sub[] = {
312 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
314 "[ledname] [state] [blink]\n"
315 " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
316 " - state: off red green orange\n"
317 " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
318 U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
319 "returns state of user key", ""),
320 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
321 "returns firmware version of supervisor uC", ""),
322 U_BOOT_CMD_MKENT(appreg, 1, 1, do_mtc_appreg,
323 "reads or writes appreg value and stores in environment "
325 "[value] - value (1 - 255) to write to appreg"),
326 U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
327 "returns state of digital input",
328 "<channel_num> - get state of digital input (1 or 2)\n"),
329 U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
330 "sets digital outputs",
331 "<on|off> <on|off>- set state of digital output 1 and 2\n"),
332 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
333 "displays state", ""),
334 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
335 "[command] - get help for command\n"),
338 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
340 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
341 cmd_tbl_t *cmdtp, int flag,
342 int argc, char * const argv[]);
343 #ifdef CONFIG_SYS_LONGHELP
346 return _do_help(&cmd_mtc_sub[0],
347 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
350 int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
355 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
359 return c->cmd(c, flag, argc, argv);
361 /* Unrecognized command */
362 return cmd_usage(cmdtp);
368 U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
369 "special commands for digsyMTC",
370 "[subcommand] [args...]\n"
371 "Subcommands list:\n"
372 "led [ledname] [state] [blink] - set state of leds\n"
373 " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
374 " [state]: off red green orange\n"
375 " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
376 "key - returns state of user key\n"
377 "version - returns firmware version of supervisor uC\n"
378 "appreg [value] - reads (in environment variable 'appreg') or writes"
380 " [value]: value (1 - 255) to write to appreg\n"
381 "digin [channel] - returns state of digital input (1 or 2)\n"
382 "digout <on|off> <on|off> - sets state of two digital outputs\n"
383 "state - displays state\n"
384 "help [subcommand] - get help for subcommand\n"