cmd_mtc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * (C) Copyright 2009
  3. * Werner Pfister <Pfister_Werner@intercontrol.de>
  4. *
  5. * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  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.
  14. *
  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.
  19. *
  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,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <command.h>
  27. #include <mpc5xxx.h>
  28. #include "spi.h"
  29. #include "cmd_mtc.h"
  30. DECLARE_GLOBAL_DATA_PTR;
  31. static uchar user_out;
  32. static const char *led_names[] = {
  33. "diag",
  34. "can1",
  35. "can2",
  36. "can3",
  37. "can4",
  38. "usbpwr",
  39. "usbbusy",
  40. "user1",
  41. "user2",
  42. ""
  43. };
  44. static int msp430_xfer(const void *dout, void *din)
  45. {
  46. int err;
  47. err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
  48. SPI_XFER_BEGIN | SPI_XFER_END);
  49. /* The MSP chip needs time to ready itself for the next command */
  50. udelay(1000);
  51. return err;
  52. }
  53. static void mtc_calculate_checksum(tx_msp_cmd *packet)
  54. {
  55. int i;
  56. uchar *buff;
  57. buff = (uchar *) packet;
  58. for (i = 0; i < 6; i++)
  59. packet->cks += buff[i];
  60. }
  61. static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  62. {
  63. tx_msp_cmd pcmd;
  64. rx_msp_cmd prx;
  65. int err;
  66. int i;
  67. if (argc < 2)
  68. return cmd_usage(cmdtp);
  69. memset(&pcmd, 0, sizeof(pcmd));
  70. memset(&prx, 0, sizeof(prx));
  71. pcmd.cmd = CMD_SET_LED;
  72. pcmd.cmd_val0 = 0xff;
  73. for (i = 0; strlen(led_names[i]) != 0; i++) {
  74. if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
  75. pcmd.cmd_val0 = i;
  76. break;
  77. }
  78. }
  79. if (pcmd.cmd_val0 == 0xff) {
  80. printf("Usage:\n%s\n", cmdtp->help);
  81. return -1;
  82. }
  83. if (argc >= 3) {
  84. if (strncmp(argv[2], "red", 3) == 0)
  85. pcmd.cmd_val1 = 1;
  86. else if (strncmp(argv[2], "green", 5) == 0)
  87. pcmd.cmd_val1 = 2;
  88. else if (strncmp(argv[2], "orange", 6) == 0)
  89. pcmd.cmd_val1 = 3;
  90. else
  91. pcmd.cmd_val1 = 0;
  92. }
  93. if (argc >= 4)
  94. pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
  95. else
  96. pcmd.cmd_val2 = 0;
  97. pcmd.user_out = user_out;
  98. mtc_calculate_checksum(&pcmd);
  99. err = msp430_xfer(&pcmd, &prx);
  100. return err;
  101. }
  102. static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  103. {
  104. tx_msp_cmd pcmd;
  105. rx_msp_cmd prx;
  106. int err;
  107. memset(&pcmd, 0, sizeof(pcmd));
  108. memset(&prx, 0, sizeof(prx));
  109. pcmd.cmd = CMD_GET_VIM;
  110. pcmd.user_out = user_out;
  111. mtc_calculate_checksum(&pcmd);
  112. err = msp430_xfer(&pcmd, &prx);
  113. if (!err) {
  114. /* function returns '0' if key is pressed */
  115. err = (prx.input & 0x80) ? 0 : 1;
  116. }
  117. return err;
  118. }
  119. static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  120. {
  121. tx_msp_cmd pcmd;
  122. rx_msp_cmd prx;
  123. int err;
  124. uchar channel_mask = 0;
  125. if (argc < 3)
  126. return cmd_usage(cmdtp);
  127. if (strncmp(argv[1], "on", 2) == 0)
  128. channel_mask |= 1;
  129. if (strncmp(argv[2], "on", 2) == 0)
  130. channel_mask |= 2;
  131. memset(&pcmd, 0, sizeof(pcmd));
  132. memset(&prx, 0, sizeof(prx));
  133. pcmd.cmd = CMD_GET_VIM;
  134. pcmd.user_out = channel_mask;
  135. user_out = channel_mask;
  136. mtc_calculate_checksum(&pcmd);
  137. err = msp430_xfer(&pcmd, &prx);
  138. return err;
  139. }
  140. static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  141. {
  142. tx_msp_cmd pcmd;
  143. rx_msp_cmd prx;
  144. int err;
  145. uchar channel_num = 0;
  146. if (argc < 2)
  147. return cmd_usage(cmdtp);
  148. channel_num = simple_strtol(argv[1], NULL, 10);
  149. if ((channel_num != 1) && (channel_num != 2)) {
  150. printf("mtc digin: invalid parameter - must be '1' or '2'\n");
  151. return -1;
  152. }
  153. memset(&pcmd, 0, sizeof(pcmd));
  154. memset(&prx, 0, sizeof(prx));
  155. pcmd.cmd = CMD_GET_VIM;
  156. pcmd.user_out = user_out;
  157. mtc_calculate_checksum(&pcmd);
  158. err = msp430_xfer(&pcmd, &prx);
  159. if (!err) {
  160. /* function returns '0' when digin is on */
  161. err = (prx.input & channel_num) ? 0 : 1;
  162. }
  163. return err;
  164. }
  165. static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  166. {
  167. tx_msp_cmd pcmd;
  168. rx_msp_cmd prx;
  169. int err;
  170. char buf[5];
  171. uchar appreg;
  172. /* read appreg */
  173. memset(&pcmd, 0, sizeof(pcmd));
  174. memset(&prx, 0, sizeof(prx));
  175. pcmd.cmd = CMD_WD_PARA;
  176. pcmd.cmd_val0 = 5; /* max. Count */
  177. pcmd.cmd_val1 = 5; /* max. Time */
  178. pcmd.cmd_val2 = 0; /* =0 means read appreg */
  179. pcmd.user_out = user_out;
  180. mtc_calculate_checksum(&pcmd);
  181. err = msp430_xfer(&pcmd, &prx);
  182. /* on success decide between read or write */
  183. if (!err) {
  184. if (argc == 2) {
  185. appreg = simple_strtol(argv[1], NULL, 10);
  186. if (appreg == 0) {
  187. printf("mtc appreg: invalid parameter - "
  188. "must be between 1 and 255\n");
  189. return -1;
  190. }
  191. memset(&pcmd, 0, sizeof(pcmd));
  192. pcmd.cmd = CMD_WD_PARA;
  193. pcmd.cmd_val0 = prx.ack3; /* max. Count */
  194. pcmd.cmd_val1 = prx.ack0; /* max. Time */
  195. pcmd.cmd_val2 = appreg; /* !=0 means write appreg */
  196. pcmd.user_out = user_out;
  197. memset(&prx, 0, sizeof(prx));
  198. mtc_calculate_checksum(&pcmd);
  199. err = msp430_xfer(&pcmd, &prx);
  200. } else {
  201. sprintf(buf, "%d", prx.ack2);
  202. setenv("appreg", buf);
  203. }
  204. }
  205. return err;
  206. }
  207. static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  208. {
  209. tx_msp_cmd pcmd;
  210. rx_msp_cmd prx;
  211. int err;
  212. memset(&pcmd, 0, sizeof(pcmd));
  213. memset(&prx, 0, sizeof(prx));
  214. pcmd.cmd = CMD_FW_VERSION;
  215. pcmd.user_out = user_out;
  216. mtc_calculate_checksum(&pcmd);
  217. err = msp430_xfer(&pcmd, &prx);
  218. if (!err) {
  219. printf("FW V%d.%d.%d / HW %d\n",
  220. prx.ack0, prx.ack1, prx.ack3, prx.ack2);
  221. }
  222. return err;
  223. }
  224. static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  225. {
  226. tx_msp_cmd pcmd;
  227. rx_msp_cmd prx;
  228. int err;
  229. memset(&pcmd, 0, sizeof(pcmd));
  230. memset(&prx, 0, sizeof(prx));
  231. pcmd.cmd = CMD_WD_WDSTATE;
  232. pcmd.cmd_val2 = 1;
  233. pcmd.user_out = user_out;
  234. mtc_calculate_checksum(&pcmd);
  235. err = msp430_xfer(&pcmd, &prx);
  236. if (!err) {
  237. printf("State %02Xh\n", prx.state);
  238. printf("Input %02Xh\n", prx.input);
  239. printf("UserWD %02Xh\n", prx.ack2);
  240. printf("Sys WD %02Xh\n", prx.ack3);
  241. printf("WD Timout %02Xh\n", prx.ack0);
  242. printf("eSysState %02Xh\n", prx.ack1);
  243. }
  244. return err;
  245. }
  246. static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  247. cmd_tbl_t cmd_mtc_sub[] = {
  248. U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
  249. "set state of leds",
  250. "[ledname] [state] [blink]\n"
  251. " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
  252. " - state: off red green orange\n"
  253. " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
  254. U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
  255. "returns state of user key", ""),
  256. U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
  257. "returns firmware version of supervisor uC", ""),
  258. U_BOOT_CMD_MKENT(appreg, 1, 1, do_mtc_appreg,
  259. "reads or writes appreg value and stores in environment "
  260. "variable 'appreg'",
  261. "[value] - value (1 - 255) to write to appreg"),
  262. U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
  263. "returns state of digital input",
  264. "<channel_num> - get state of digital input (1 or 2)\n"),
  265. U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
  266. "sets digital outputs",
  267. "<on|off> <on|off>- set state of digital output 1 and 2\n"),
  268. U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
  269. "displays state", ""),
  270. U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
  271. "[command] - get help for command\n"),
  272. };
  273. static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  274. {
  275. extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
  276. cmd_tbl_t *cmdtp, int flag,
  277. int argc, char * const argv[]);
  278. #ifdef CONFIG_SYS_LONGHELP
  279. puts("mtc ");
  280. #endif
  281. return _do_help(&cmd_mtc_sub[0],
  282. ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
  283. }
  284. int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  285. {
  286. cmd_tbl_t *c;
  287. int err = 0;
  288. c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
  289. if (c) {
  290. argc--;
  291. argv++;
  292. return c->cmd(c, flag, argc, argv);
  293. } else {
  294. /* Unrecognized command */
  295. return cmd_usage(cmdtp);
  296. }
  297. return err;
  298. }
  299. U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
  300. "special commands for digsyMTC",
  301. "[subcommand] [args...]\n"
  302. "Subcommands list:\n"
  303. "led [ledname] [state] [blink] - set state of leds\n"
  304. " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
  305. " [state]: off red green orange\n"
  306. " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
  307. "key - returns state of user key\n"
  308. "version - returns firmware version of supervisor uC\n"
  309. "appreg [value] - reads (in environment variable 'appreg') or writes"
  310. " appreg value\n"
  311. " [value]: value (1 - 255) to write to appreg\n"
  312. "digin [channel] - returns state of digital input (1 or 2)\n"
  313. "digout <on|off> <on|off> - sets state of two digital outputs\n"
  314. "state - displays state\n"
  315. "help [subcommand] - get help for subcommand\n"
  316. );