summaryrefslogtreecommitdiff
path: root/board/digsy_mtc/cmd_mtc.c
blob: 2ecb4f8c394c1a6d6287bec1be63cba1b3d9dd88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * (C) Copyright 2009
 * Werner Pfister <Pfister_Werner@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"

DECLARE_GLOBAL_DATA_PTR;

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 do_mtc_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) {
		cmd_usage(cmdtp);
		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[1], 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 >= 3) {
		if (strncmp(argv[2], "red", 3) == 0)
			pcmd.cmd_val1 = 1;
		else if (strncmp(argv[2], "green", 5) == 0)
			pcmd.cmd_val1 = 2;
		else if (strncmp(argv[2], "orange", 6) == 0)
			pcmd.cmd_val1 = 3;
		else
			pcmd.cmd_val1 = 0;
	}

	if (argc >= 4)
		pcmd.cmd_val2 = simple_strtol(argv[3], 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 do_mtc_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 do_mtc_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 < 3) {
		cmd_usage(cmdtp);
		return -1;
	}

	if (strncmp(argv[1], "on", 2) == 0)
		channel_mask |= 1;
	if (strncmp(argv[2], "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 do_mtc_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 < 2) {
		cmd_usage(cmdtp);
		return -1;
	}

	channel_num = simple_strtol(argv[1], NULL, 10);
	if ((channel_num != 1) && (channel_num != 2)) {
		printf("mtc digin: invalid parameter - must be '1' or '2'\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 do_mtc_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 do_mtc_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;
}

static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);

cmd_tbl_t cmd_mtc_sub[] = {
	U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
	"set state of leds",
	"[ledname] [state] [blink]\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"),
	U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
	"returns state of user key\n", ""),
	U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
	"returns firmware version of supervisor uC\n", ""),
	U_BOOT_CMD_MKENT(appreg, 0, 1, do_mtc_appreg,
	"reads appreg value and stores in environment variable 'appreg'\n", ""),
	U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
	"returns state of digital input",
	"<channel_num> - get state of digital input (1 or 2)\n"),
	U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
	"sets digital outputs",
	"<on|off> <on|off>- set state of digital output 1 and 2\n"),
	U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
	"[command] - get help for command\n"),
};

static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
			    cmd_tbl_t *cmdtp, int flag,
			    int argc, char *argv[]);
#ifdef CONFIG_SYS_LONGHELP
	puts("mtc ");
#endif
	return _do_help(&cmd_mtc_sub[0],
			ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
}

/* Relocate the command table function pointers when running in RAM */
int mtc_cmd_init_r(void)
{
	cmd_tbl_t *cmdtp;

	for (cmdtp = &cmd_mtc_sub[0]; cmdtp !=
	     &cmd_mtc_sub[ARRAY_SIZE(cmd_mtc_sub)]; cmdtp++) {
		ulong addr;

		addr = (ulong)(cmdtp->cmd) + gd->reloc_off;
		cmdtp->cmd =
		    (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;

		addr = (ulong)(cmdtp->name) + gd->reloc_off;
		cmdtp->name = (char *)addr;

		if (cmdtp->usage) {
			addr = (ulong)(cmdtp->usage) + gd->reloc_off;
			cmdtp->usage = (char *)addr;
		}
#ifdef CONFIG_SYS_LONGHELP
		if (cmdtp->help) {
			addr = (ulong)(cmdtp->help) + gd->reloc_off;
			cmdtp->help = (char *)addr;
		}
#endif
	}
	return 0;
}

int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
	cmd_tbl_t *c;
	int err = 0;

	c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
	if (c) {
		argc--;
		argv++;
		return c->cmd(c, flag, argc, argv);
	} else {
		/* Unrecognized command */
		cmd_usage(cmdtp);
		return 1;
	}

	return err;
}

U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
	"mtc     - special commands for digsyMTC\n",
	"[subcommand] [args...]\n"
	"Subcommands list:\n"
	"led [ledname] [state] [blink] - set state of leds\n"
	"  [ledname]: 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"
	"key - returns state of user key\n"
	"version - returns firmware version of supervisor uC\n"
	"appreg - reads appreg value and stores in environment variable"
	" 'appreg'\n"
	"digin [channel] - returns state of digital input (1 or 2)\n"
	"digout <on|off> <on|off> - sets state of two digital outputs\n"
	"help [subcommand] - get help for subcommand\n"
);